66
77from mpos .apps import Activity , Intent
88import mpos .ui
9+ import mpos .config
910
10- # Global variables because they're shared between activities :
11+ # Global variables because they're used by multiple Activities :
1112access_points = {}
1213last_tried_ssid = ""
1314last_tried_result = ""
@@ -61,7 +62,9 @@ def onStart(self, screen):
6162 self .start_scan_networks ()
6263
6364 def onResume (self , screen ):
64- load_config ()
65+ global access_points
66+ access_points = mpos .config .SharedPreferences ("com.micropythonos.wificonf" ).get_dict ("access_points" )
67+
6568
6669 def show_error (self , message ):
6770 print (f"show_error: Displaying error: { message } " )
@@ -103,7 +106,7 @@ def start_scan_networks(self):
103106 self .scan_button_label .set_text (self .scan_button_scanning_text )
104107 _thread .stack_size (mpos .apps .good_stack_size ())
105108 _thread .start_new_thread (self .scan_networks_thread , ())
106-
109+
107110 def refresh_list (self ):
108111 print ("refresh_list: Clearing current list" )
109112 self .aplist .clean () # this causes an issue with lost taps if an ssid is clicked that has been removed
@@ -203,6 +206,7 @@ class PasswordPage(Activity):
203206 connect_button = None
204207 cancel_button = None
205208
209+
206210 def onCreate (self ):
207211 self .selected_ssid = self .getIntent ().extras .get ("selected_ssid" )
208212 print ("PasswordPage: Creating new password page" )
@@ -218,11 +222,9 @@ def onCreate(self):
218222 self .password_ta .set_one_line (True )
219223 self .password_ta .align_to (label , lv .ALIGN .OUT_BOTTOM_MID , 5 , 0 )
220224 self .password_ta .add_event_cb (self .password_ta_cb ,lv .EVENT .CLICKED ,None )
221- # try to find saved password:
222- for apssid ,password in access_points .items ():
223- if self .selected_ssid == apssid :
224- self .password_ta .set_text (password )
225- break
225+ pwd = self .findSavedPassword (self .selected_ssid )
226+ if pwd :
227+ self .password_ta .set_text (pwd )
226228 self .password_ta .set_placeholder_text ("Password" )
227229 print ("PasswordPage: Creating keyboard (hidden by default)" )
228230 self .keyboard = lv .keyboard (password_page )
@@ -291,9 +293,11 @@ def connect_cb(self, event):
291293 print ("connect_cb: Connect button clicked" )
292294 password = self .password_ta .get_text ()
293295 print (f"connect_cb: Got password: { password } " )
294- access_points [ self .selected_ssid ] = password
296+ self .setPassword ( self . selected_ssid , password )
295297 print (f"connect_cb: Updated access_points: { access_points } " )
296- save_config ()
298+ editor = mpos .config .SharedPreferences ("com.micropythonos.wificonf" ).edit ()
299+ editor .put_dict ("access_points" , access_points )
300+ editor .commit ()
297301 self .setResult (True , {"ssid" : self .selected_ssid , "password" : password })
298302 print ("connect_cb: Restoring main_screen" )
299303 self .finish ()
@@ -302,47 +306,21 @@ def cancel_cb(self, event):
302306 print ("cancel_cb: Cancel button clicked" )
303307 self .finish ()
304308
305-
306-
307-
308- # Non-class functions:
309-
310-
311- def load_config (): # Maybe this should call a framework function
312- print ("load_config: Checking for /data directory" )
313- try :
314- os .stat ('data' )
315- print ("load_config: /data exists" )
316- except OSError :
317- print ("load_config: Creating /data directory" )
318- os .mkdir ('data' )
319- print ("load_config: Checking for /data/com.example.wificonf directory" )
320- try :
321- os .stat ('data/com.example.wificonf' )
322- print ("load_config: /data/com.example.wificonf exists" )
323- except OSError :
324- print ("load_config: Creating /data/com.example.wificonf directory" )
325- os .mkdir ('data/com.example.wificonf' )
326- print ("load_config: Loading config from conf.json" )
327- try :
328- with open ('data/com.example.wificonf/conf.json' ,'r' ) as f :
329- global access_points
330- access_points = ujson .load (f )
331- print (f"load_config: Loaded access_points: { access_points } " )
332- except OSError :
333- access_points = {}
334- print ("load_config: No config file found, using empty access_points" )
335-
336-
337- def save_config (): # Maybe this should call a framework function
338- print ("save_config: Saving access_points to conf.json" )
339- try :
340- with open ('data/com.example.wificonf/conf.json' ,'w' ) as f :
341- ujson .dump (access_points ,f )
342- print (f"save_config: Saved access_points: { access_points } " )
343- except OSError :
344- self .show_error ("Failed to save config" )
345- print ("save_config: Failed to save config" )
346-
347-
348-
309+ @staticmethod
310+ def setPassword (ssid , password ):
311+ global access_points
312+ ap = access_points .get (ssid )
313+ if ap :
314+ ap ["password" ] = password
315+ return
316+ # if not found, then add it:
317+ access_points [ssid ] = { "password" : password }
318+
319+ @staticmethod
320+ def findSavedPassword (ssid ):
321+ if not access_points :
322+ return None
323+ ap = access_points .get (ssid )
324+ if ap :
325+ return ap .get ("password" )
326+ return None
0 commit comments