Skip to content

Commit a062a79

Browse files
Wifi app: add "hidden network" handling
1 parent 1edbd64 commit a062a79

File tree

1 file changed

+48
-38
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.wifi/assets

1 file changed

+48
-38
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ def add_network_callback(self, event):
154154
intent.putExtra("selected_ssid", None)
155155
self.startActivityForResult(intent, self.password_page_result_cb)
156156

157-
158157
def scan_cb(self, event):
159158
print("scan_cb: Scan button clicked, refreshing list")
160159
self.start_scan_networks()
@@ -163,19 +162,29 @@ def select_ssid_cb(self,ssid):
163162
print(f"select_ssid_cb: SSID selected: {ssid}")
164163
intent = Intent(activity_class=PasswordPage)
165164
intent.putExtra("selected_ssid", ssid)
165+
intent.putExtra("known_password", self.findSavedPassword(ssid))
166166
self.startActivityForResult(intent, self.password_page_result_cb)
167167

168168
def password_page_result_cb(self, result):
169169
print(f"PasswordPage finished, result: {result}")
170170
if result.get("result_code") is True:
171171
data = result.get("data")
172172
if data:
173+
ssid = data.get("ssid")
174+
password = data.get("password")
175+
hidden = data.get("hidden")
176+
self.setPassword(ssid, password, hidden)
177+
global access_points
178+
print(f"connect_cb: Updated access_points: {access_points}")
179+
editor = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").edit()
180+
editor.put_dict("access_points", access_points)
181+
editor.commit()
173182
self.start_attempt_connecting(data.get("ssid"), data.get("password"))
174183

175184
def start_attempt_connecting(self, ssid, password):
176185
print(f"start_attempt_connecting: Attempting to connect to SSID '{ssid}' with password '{password}'")
177186
self.scan_button.add_state(lv.STATE.DISABLED)
178-
self.scan_button_label.set_text(f"Connecting to '{ssid}'")
187+
self.scan_button_label.set_text("Connecting...")
179188
if self.busy_connecting:
180189
print("Not attempting connect because busy_connecting.")
181190
else:
@@ -218,6 +227,27 @@ def attempt_connecting_thread(self, ssid, password):
218227
self.update_ui_threadsafe_if_foreground(self.scan_button.remove_state, lv.STATE.DISABLED)
219228
self.update_ui_threadsafe_if_foreground(self.refresh_list)
220229

230+
@staticmethod
231+
def findSavedPassword(ssid):
232+
if not access_points:
233+
return None
234+
ap = access_points.get(ssid)
235+
if ap:
236+
return ap.get("password")
237+
return None
238+
239+
@staticmethod
240+
def setPassword(ssid, password, hidden=False):
241+
global access_points
242+
ap = access_points.get(ssid)
243+
if ap:
244+
ap["password"] = password
245+
if hidden is True:
246+
ap["hidden"] = True
247+
return
248+
# if not found, then add it:
249+
access_points[ssid] = { "password": password, "hidden": hidden }
250+
221251

222252
class PasswordPage(Activity):
223253
# Would be good to add some validation here so the password is not too short etc...
@@ -227,6 +257,7 @@ class PasswordPage(Activity):
227257
# Widgets:
228258
ssid_ta = None
229259
password_ta=None
260+
hidden_cb = None
230261
keyboard=None
231262
connect_button=None
232263
cancel_button=None
@@ -236,6 +267,8 @@ def onCreate(self):
236267
password_page.set_style_pad_all(0, lv.PART.MAIN)
237268
password_page.set_flex_flow(lv.FLEX_FLOW.COLUMN)
238269
self.selected_ssid = self.getIntent().extras.get("selected_ssid")
270+
known_password = self.getIntent().extras.get("known_password")
271+
239272
# SSID:
240273
if self.selected_ssid is None:
241274
print("No ssid selected, the user should fill it out.")
@@ -260,17 +293,16 @@ def onCreate(self):
260293
self.password_ta.set_width(lv.pct(90))
261294
self.password_ta.set_style_margin_left(5, lv.PART.MAIN)
262295
self.password_ta.set_one_line(True)
263-
pwd = self.findSavedPassword(self.selected_ssid)
264-
if pwd:
265-
self.password_ta.set_text(pwd)
296+
if known_password:
297+
self.password_ta.set_text(known_password)
266298
self.password_ta.set_placeholder_text("Password")
267299
self.keyboard=MposKeyboard(password_page)
268300
self.keyboard.set_textarea(self.password_ta)
269301
self.keyboard.add_flag(lv.obj.FLAG.HIDDEN)
270302
# Hidden network:
271-
cb = lv.checkbox(password_page)
272-
cb.set_text("Hidden network (always try connecting)")
273-
cb.set_style_margin_left(5, lv.PART.MAIN)
303+
self.hidden_cb = lv.checkbox(password_page)
304+
self.hidden_cb.set_text("Hidden network (always try connecting)")
305+
self.hidden_cb.set_style_margin_left(5, lv.PART.MAIN)
274306
# Buttons
275307
buttons = lv.obj(password_page)
276308
# Connect button
@@ -296,8 +328,9 @@ def onCreate(self):
296328
self.setContentView(password_page)
297329

298330
def connect_cb(self, event):
299-
global access_points
300331
print("connect_cb: Connect button clicked")
332+
333+
# Validate the form
301334
if self.selected_ssid is None:
302335
new_ssid = self.ssid_ta.get_text()
303336
if not new_ssid:
@@ -306,36 +339,13 @@ def connect_cb(self, event):
306339
return
307340
else:
308341
self.selected_ssid = new_ssid
309-
password=self.password_ta.get_text()
310-
print(f"connect_cb: Got password: {password}")
311-
self.setPassword(self.selected_ssid, password)
312-
print(f"connect_cb: Updated access_points: {access_points}")
313-
editor = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").edit()
314-
editor.put_dict("access_points", access_points)
315-
editor.commit()
316-
self.setResult(True, {"ssid": self.selected_ssid, "password": password})
317-
print("connect_cb: Restoring main_screen")
342+
343+
# Return the result
344+
hidden_checked = True if self.hidden_cb.get_state() & lv.STATE.CHECKED else False
345+
self.setResult(True, {"ssid": self.selected_ssid, "password": self.password_ta.get_text(), "hidden": hidden_checked})
346+
print("connect_cb: finishing")
318347
self.finish()
319-
348+
320349
def cancel_cb(self, event):
321350
print("cancel_cb: Cancel button clicked")
322351
self.finish()
323-
324-
@staticmethod
325-
def setPassword(ssid, password):
326-
global access_points
327-
ap = access_points.get(ssid)
328-
if ap:
329-
ap["password"] = password
330-
return
331-
# if not found, then add it:
332-
access_points[ssid] = { "password": password }
333-
334-
@staticmethod
335-
def findSavedPassword(ssid):
336-
if not access_points:
337-
return None
338-
ap = access_points.get(ssid)
339-
if ap:
340-
return ap.get("password")
341-
return None

0 commit comments

Comments
 (0)