WifiSettings.java revision 3b6aa41cce9dafa48dfbb3e93ea856610de1f1e7
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.settings.wifi; 18 19import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID; 20 21import com.android.settings.ProgressCategoryBase; 22import com.android.settings.R; 23import com.android.settings.SettingsPreferenceFragment; 24 25import android.app.Activity; 26import android.content.BroadcastReceiver; 27import android.content.Context; 28import android.content.DialogInterface; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.net.ConnectivityManager; 32import android.net.NetworkInfo; 33import android.net.NetworkInfo.DetailedState; 34import android.net.wifi.ScanResult; 35import android.net.wifi.SupplicantState; 36import android.net.wifi.WifiConfiguration; 37import android.net.wifi.WifiConfiguration.KeyMgmt; 38import android.net.wifi.WifiInfo; 39import android.net.wifi.WifiManager; 40import android.os.Bundle; 41import android.os.Handler; 42import android.os.Message; 43import android.preference.CheckBoxPreference; 44import android.preference.Preference; 45import android.preference.PreferenceScreen; 46import android.provider.Settings.Secure; 47import android.security.Credentials; 48import android.security.KeyStore; 49import android.util.Log; 50import android.view.ContextMenu; 51import android.view.ContextMenu.ContextMenuInfo; 52import android.view.Menu; 53import android.view.MenuInflater; 54import android.view.MenuItem; 55import android.view.View; 56import android.widget.AdapterView.AdapterContextMenuInfo; 57import android.widget.Button; 58import android.widget.Toast; 59 60import java.util.Collection; 61import java.util.List; 62import java.util.TreeSet; 63 64/** 65 * This currently provides three types of UI. 66 * 67 * Two are for phones with relatively small screens: "for SetupWizard" and "for usual Settings". 68 * Users just need to launch WifiSettings Activity as usual. The request will be appropriately 69 * handled by ActivityManager, and they will have appropriate look-and-feel with this fragment. 70 * 71 * Third type is for Setup Wizard with X-Large, landscape UI. Users need to launch 72 * {@link WifiSettingsForSetupWizardXL} Activity, which contains this fragment but also has 73 * other decorations specific to that screen. 74 */ 75public class WifiSettings extends SettingsPreferenceFragment 76 implements DialogInterface.OnClickListener { 77 private static final int MENU_ID_SCAN = Menu.FIRST; 78 private static final int MENU_ID_ADVANCED = Menu.FIRST + 1; 79 private static final int MENU_ID_CONNECT = Menu.FIRST + 2; 80 private static final int MENU_ID_FORGET = Menu.FIRST + 3; 81 private static final int MENU_ID_MODIFY = Menu.FIRST + 4; 82 83 // Indicates that this fragment is used as a part of Setup Wizard with XL screen settings. 84 // This fragment should show information which has been shown as Dialog in combined UI 85 // inside this fragment. 86 /* package */ static final String IN_XL_SETUP_WIZARD = "in_setup_wizard"; 87 88 // this boolean extra specifies whether to disable the Next button when not connected 89 // Note: this is only effective in Setup Wizard with XL screen size. 90 private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect"; 91 92 // In SetupWizard XL, We limit the number of showable access points so that the 93 // ListView won't become larger than the screen. 94 // 95 // This constant doesn't affect other contexts other than SetupWizard XL. 96 private static int MAX_MENU_COUNT_IN_XL = 7; 97 98 private final IntentFilter mFilter; 99 private final BroadcastReceiver mReceiver; 100 private final Scanner mScanner; 101 102 private WifiManager mWifiManager; 103 private WifiEnabler mWifiEnabler; 104 private CheckBoxPreference mNotifyOpenNetworks; 105 private ProgressCategoryBase mAccessPoints; 106 private Preference mAddNetwork; 107 // An access point being editted is stored here. 108 private AccessPoint mSelectedAccessPoint; 109 110 private DetailedState mLastState; 111 private WifiInfo mLastInfo; 112 113 private int mKeyStoreNetworkId = INVALID_NETWORK_ID; 114 115 // should Next button only be enabled when we have a connection? 116 private boolean mEnableNextOnConnection; 117 private boolean mInXlSetupWizard; 118 119 120 // TODO: merge into one 121 private WifiConfigPreference mConfigPreference; 122 private WifiDialog mDialog; 123 124 // Used only in SetupWizard XL, which remembers the network a user selected and 125 // refrain other available networks when trying to connect it. 126 private AccessPoint mConnectingAccessPoint; 127 128 private boolean mRefrainListUpdate; 129 130 public WifiSettings() { 131 mFilter = new IntentFilter(); 132 mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 133 mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 134 mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); 135 mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 136 mFilter.addAction(WifiManager.SUPPLICANT_CONFIG_CHANGED_ACTION); 137 mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 138 mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); 139 140 mReceiver = new BroadcastReceiver() { 141 @Override 142 public void onReceive(Context context, Intent intent) { 143 handleEvent(intent); 144 } 145 }; 146 147 mScanner = new Scanner(); 148 } 149 150 @Override 151 public void onActivityCreated(Bundle savedInstanceState) { 152 // We don't call super.onActivityCreated() here, since it assumes we already set up 153 // Preference (probably in onCreate()), while WifiSettings exceptionally set it up in 154 // this method. 155 156 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 157 158 final Activity activity = getActivity(); 159 final Intent intent = activity.getIntent(); 160 161 mInXlSetupWizard = intent.getBooleanExtra(IN_XL_SETUP_WIZARD, false); 162 163 // if we're supposed to enable/disable the Next button based on our current connection 164 // state, start it off in the right state 165 mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false); 166 167 if (mEnableNextOnConnection) { 168 if (mEnableNextOnConnection && hasNextButton()) { 169 final ConnectivityManager connectivity = (ConnectivityManager) 170 getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 171 if (connectivity != null) { 172 NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 173 changeNextButtonState(info.isConnected()); 174 } 175 } 176 } 177 178 if (mInXlSetupWizard) { 179 addPreferencesFromResource(R.xml.wifi_access_points_for_wifi_setup_xl); 180 } else if (intent.getBooleanExtra("only_access_points", false)) { 181 addPreferencesFromResource(R.xml.wifi_access_points); 182 } else { 183 addPreferencesFromResource(R.xml.wifi_settings); 184 mWifiEnabler = new WifiEnabler(activity, 185 (CheckBoxPreference) findPreference("enable_wifi")); 186 mNotifyOpenNetworks = 187 (CheckBoxPreference) findPreference("notify_open_networks"); 188 mNotifyOpenNetworks.setChecked(Secure.getInt(getContentResolver(), 189 Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1); 190 } 191 192 // After confirming PreferenceScreen is available, we call super. 193 super.onActivityCreated(savedInstanceState); 194 195 // This may be either ProgressCategory or AccessPointCategoryForXL. 196 final ProgressCategoryBase preference = 197 (ProgressCategoryBase) findPreference("access_points"); 198 mAccessPoints = preference; 199 mAccessPoints.setOrderingAsAdded(true); 200 mAddNetwork = findPreference("add_network"); 201 202 registerForContextMenu(getListView()); 203 setHasOptionsMenu(true); 204 } 205 206 @Override 207 public void onResume() { 208 super.onResume(); 209 if (mWifiEnabler != null) { 210 mWifiEnabler.resume(); 211 } 212 getActivity().registerReceiver(mReceiver, mFilter); 213 if (mKeyStoreNetworkId != INVALID_NETWORK_ID && 214 KeyStore.getInstance().test() == KeyStore.NO_ERROR) { 215 mWifiManager.connectNetwork(mKeyStoreNetworkId); 216 } 217 mKeyStoreNetworkId = INVALID_NETWORK_ID; 218 if (mInXlSetupWizard) { 219 // We show "Now scanning" 220 final int wifiState = mWifiManager.getWifiState(); 221 switch (wifiState) { 222 case WifiManager.WIFI_STATE_ENABLED: { 223 updateAccessPoints(); 224 break; 225 } 226 case WifiManager.WIFI_STATE_DISABLED: 227 case WifiManager.WIFI_STATE_DISABLING: 228 case WifiManager.WIFI_STATE_UNKNOWN: { 229 mWifiManager.setWifiEnabled(true); 230 } // $FALL-THROUGH$ 231 default: { 232 mAccessPoints.removeAll(); 233 Preference preference = new Preference(getActivity()); 234 preference.setLayoutResource(R.layout.preference_widget_shortcut); 235 preference.setSelectable(false); 236 preference.setTitle("Connecting"); 237 preference.setSummary("COONNECTING"); 238 mAccessPoints.addPreference(preference); 239 break; 240 } 241 } 242 } else { 243 updateAccessPoints(); 244 } 245 } 246 247 @Override 248 public void onPause() { 249 super.onPause(); 250 if (mWifiEnabler != null) { 251 mWifiEnabler.pause(); 252 } 253 getActivity().unregisterReceiver(mReceiver); 254 mScanner.pause(); 255 if (mDialog != null) { 256 mDialog.dismiss(); 257 mDialog = null; 258 } 259 } 260 261 @Override 262 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 263 // We don't want menus in Setup Wizard XL. 264 if (!mInXlSetupWizard) { 265 menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.wifi_menu_scan) 266 .setIcon(R.drawable.ic_menu_scan_network); 267 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced) 268 .setIcon(android.R.drawable.ic_menu_manage); 269 } 270 super.onCreateOptionsMenu(menu, inflater); 271 } 272 273 @Override 274 public boolean onOptionsItemSelected(MenuItem item) { 275 switch (item.getItemId()) { 276 case MENU_ID_SCAN: 277 if (mWifiManager.isWifiEnabled()) { 278 mScanner.resume(); 279 } 280 return true; 281 case MENU_ID_ADVANCED: 282 startFragment(this, AdvancedSettings.class.getCanonicalName(), -1, null); 283 return true; 284 } 285 return super.onOptionsItemSelected(item); 286 } 287 288 @Override 289 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) { 290 if (info instanceof AdapterContextMenuInfo) { 291 Preference preference = (Preference) getListView().getItemAtPosition( 292 ((AdapterContextMenuInfo) info).position); 293 294 if (preference instanceof AccessPoint) { 295 mSelectedAccessPoint = (AccessPoint) preference; 296 menu.setHeaderTitle(mSelectedAccessPoint.ssid); 297 if (mSelectedAccessPoint.getLevel() != -1 298 && mSelectedAccessPoint.getState() == null) { 299 menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect); 300 } 301 if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) { 302 menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget); 303 menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify); 304 } 305 } 306 } 307 } 308 309 @Override 310 public boolean onContextItemSelected(MenuItem item) { 311 if (mSelectedAccessPoint == null) { 312 return super.onContextItemSelected(item); 313 } 314 switch (item.getItemId()) { 315 case MENU_ID_CONNECT: { 316 if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) { 317 if (!requireKeyStore(mSelectedAccessPoint.getConfig())) { 318 mWifiManager.connectNetwork(mSelectedAccessPoint.networkId); 319 } 320 } else if (mSelectedAccessPoint.security == AccessPoint.SECURITY_NONE) { 321 // Shortcut for open networks. 322 WifiConfiguration config = new WifiConfiguration(); 323 config.SSID = AccessPoint.convertToQuotedString(mSelectedAccessPoint.ssid); 324 config.allowedKeyManagement.set(KeyMgmt.NONE); 325 mWifiManager.connectNetwork(config); 326 } else { 327 showConfigUi(mSelectedAccessPoint, true); 328 } 329 return true; 330 } 331 case MENU_ID_FORGET: { 332 mWifiManager.forgetNetwork(mSelectedAccessPoint.networkId); 333 return true; 334 } 335 case MENU_ID_MODIFY: { 336 showConfigUi(mSelectedAccessPoint, true); 337 return true; 338 } 339 } 340 return super.onContextItemSelected(item); 341 } 342 343 @Override 344 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { 345 if (preference instanceof AccessPoint) { 346 mSelectedAccessPoint = (AccessPoint) preference; 347 showConfigUi(mSelectedAccessPoint, false); 348 } else if (preference == mAddNetwork) { 349 onAddNetworkPressed(); 350 } else if (preference == mNotifyOpenNetworks) { 351 Secure.putInt(getContentResolver(), 352 Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 353 mNotifyOpenNetworks.isChecked() ? 1 : 0); 354 } else { 355 return super.onPreferenceTreeClick(screen, preference); 356 } 357 return true; 358 } 359 360 /** 361 * Called when a user clicks "Add network" preference or relevant button. 362 */ 363 private void showConfigUi(AccessPoint accessPoint, boolean edit) { 364 synchronized (this) { 365 mRefrainListUpdate = false; 366 } 367 if (mInXlSetupWizard) { 368 final Activity activity = getActivity(); 369 activity.findViewById(R.id.wifi_setup_connect).setVisibility(View.VISIBLE); 370 activity.findViewById(R.id.wifi_setup_cancel).setVisibility(View.VISIBLE); 371 showConfigPreference(accessPoint, edit); 372 } else { 373 showDialog(accessPoint, edit); 374 } 375 } 376 377 private void showConfigPreference(AccessPoint accessPoint, boolean edit) { 378 // We don't want to show more than one WifiConfigPreference 379 if (mConfigPreference != null) { 380 mAccessPoints.removePreference(mConfigPreference); 381 } 382 383 mConfigPreference = new WifiConfigPreference(this, this, accessPoint, edit); 384 toggleButtonsVisibility(false); 385 final Activity activity = getActivity(); 386 if (activity instanceof WifiSettingsForSetupWizardXL) { 387 ((WifiSettingsForSetupWizardXL)activity).onWifiConfigPreferenceAttached(edit); 388 } 389 updateAccessPoints(); 390 mScanner.pause(); 391 } 392 393 private void toggleButtonsVisibility(boolean firstLayout) { 394 final Activity activity = getActivity(); 395 if (firstLayout) { 396 activity.findViewById(R.id.wifi_setup_add_network).setVisibility(View.VISIBLE); 397 activity.findViewById(R.id.wifi_setup_refresh_list).setVisibility(View.VISIBLE); 398 activity.findViewById(R.id.wifi_setup_skip_or_next).setVisibility(View.VISIBLE); 399 activity.findViewById(R.id.wifi_setup_connect).setVisibility(View.GONE); 400 activity.findViewById(R.id.wifi_setup_forget).setVisibility(View.GONE); 401 activity.findViewById(R.id.wifi_setup_cancel).setVisibility(View.GONE); 402 } else { 403 activity.findViewById(R.id.wifi_setup_add_network).setVisibility(View.GONE); 404 activity.findViewById(R.id.wifi_setup_refresh_list).setVisibility(View.GONE); 405 activity.findViewById(R.id.wifi_setup_skip_or_next).setVisibility(View.GONE); 406 407 // made visible from controller. 408 } 409 } 410 411 private void showDialog(AccessPoint accessPoint, boolean edit) { 412 if (mDialog != null) { 413 mDialog.dismiss(); 414 } 415 mDialog = new WifiDialog(getActivity(), this, accessPoint, edit); 416 mDialog.show(); 417 } 418 419 private boolean requireKeyStore(WifiConfiguration config) { 420 if (WifiConfigController.requireKeyStore(config) && 421 KeyStore.getInstance().test() != KeyStore.NO_ERROR) { 422 mKeyStoreNetworkId = config.networkId; 423 Credentials.getInstance().unlock(getActivity()); 424 return true; 425 } 426 return false; 427 } 428 429 /** 430 * Shows the latest access points available with supplimental information like 431 * the strength of network and the security for it. 432 */ 433 private void updateAccessPoints() { 434 synchronized (this) { 435 if (mRefrainListUpdate) { 436 return; 437 } 438 } 439 440 mAccessPoints.removeAll(); 441 if (mConnectingAccessPoint != null) { 442 mAccessPoints.addPreference(mConnectingAccessPoint); 443 } else if (mConfigPreference != null) { 444 final AccessPoint parent = mConfigPreference.getAccessPoint(); 445 if (parent != null) { 446 parent.setSelectable(false); 447 mAccessPoints.addPreference(parent); 448 } 449 mAccessPoints.addPreference(mConfigPreference); 450 } else { 451 // AccessPoints are automatically sorted with TreeSet. 452 final Collection<AccessPoint> accessPoints = constructAccessPoints(); 453 454 int count = MAX_MENU_COUNT_IN_XL; 455 for (AccessPoint accessPoint : accessPoints) { 456 mAccessPoints.addPreference(accessPoint); 457 count--; 458 if (count <= 0) { 459 break; 460 } 461 } 462 } 463 } 464 465 private Collection<AccessPoint> constructAccessPoints() { 466 Collection<AccessPoint> accessPoints = 467 new TreeSet<AccessPoint>(new AccessPoint.Comparater()); 468 469 final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); 470 if (configs != null) { 471 for (WifiConfiguration config : configs) { 472 AccessPoint accessPoint = new AccessPoint(getActivity(), config); 473 accessPoint.update(mLastInfo, mLastState); 474 accessPoints.add(accessPoint); 475 } 476 } 477 478 final List<ScanResult> results = mWifiManager.getScanResults(); 479 if (results != null) { 480 for (ScanResult result : results) { 481 // Ignore hidden and ad-hoc networks. 482 if (result.SSID == null || result.SSID.length() == 0 || 483 result.capabilities.contains("[IBSS]")) { 484 continue; 485 } 486 487 boolean found = false; 488 for (AccessPoint accessPoint : accessPoints) { 489 if (accessPoint.update(result)) { 490 found = true; 491 } 492 } 493 if (!found) { 494 accessPoints.add(new AccessPoint(getActivity(), result)); 495 } 496 } 497 } 498 499 return accessPoints; 500 } 501 502 private void handleEvent(Intent intent) { 503 String action = intent.getAction(); 504 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { 505 updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 506 WifiManager.WIFI_STATE_UNKNOWN)); 507 } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) || 508 WifiManager.SUPPLICANT_CONFIG_CHANGED_ACTION.equals(action)) { 509 updateAccessPoints(); 510 } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) { 511 updateConnectionState(WifiInfo.getDetailedStateOf((SupplicantState) 512 intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE))); 513 } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { 514 NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( 515 WifiManager.EXTRA_NETWORK_INFO); 516 changeNextButtonState(info.isConnected()); 517 updateConnectionState(info.getDetailedState()); 518 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { 519 updateConnectionState(null); 520 } 521 } 522 523 private void updateConnectionState(DetailedState state) { 524 /* sticky broadcasts can call this when wifi is disabled */ 525 if (!mWifiManager.isWifiEnabled()) { 526 mScanner.pause(); 527 return; 528 } 529 530 if (state == DetailedState.OBTAINING_IPADDR) { 531 mScanner.pause(); 532 } else { 533 mScanner.resume(); 534 } 535 536 mLastInfo = mWifiManager.getConnectionInfo(); 537 if (state != null) { 538 mLastState = state; 539 } 540 541 for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) { 542 // Maybe there's a WifiConfigPreference 543 Preference preference = mAccessPoints.getPreference(i); 544 if (preference instanceof AccessPoint) { 545 final AccessPoint accessPoint = (AccessPoint) preference; 546 accessPoint.update(mLastInfo, mLastState); 547 } 548 } 549 550 final Activity activity = getActivity(); 551 if (activity instanceof WifiSettingsForSetupWizardXL) { 552 if (mLastState == DetailedState.FAILED) { 553 // We clean up the status and let users select another network if they want. 554 refreshAccessPoints(); 555 } 556 ((WifiSettingsForSetupWizardXL)activity).updateConnectionState(mLastState); 557 } 558 } 559 560 private void updateWifiState(int state) { 561 if (state == WifiManager.WIFI_STATE_ENABLED) { 562 mScanner.resume(); 563 } else { 564 mScanner.pause(); 565 mAccessPoints.removeAll(); 566 } 567 } 568 569 private class Scanner extends Handler { 570 private int mRetry = 0; 571 572 void resume() { 573 synchronized (WifiSettings.this) { 574 mRefrainListUpdate = false; 575 } 576 if (!hasMessages(0)) { 577 sendEmptyMessage(0); 578 } 579 } 580 581 void pause() { 582 mRetry = 0; 583 mAccessPoints.setProgress(false); 584 synchronized (WifiSettings.this) { 585 mRefrainListUpdate = true; 586 } 587 removeMessages(0); 588 } 589 590 @Override 591 public void handleMessage(Message message) { 592 if (mWifiManager.startScanActive()) { 593 mRetry = 0; 594 } else if (++mRetry >= 3) { 595 mRetry = 0; 596 Toast.makeText(getActivity(), R.string.wifi_fail_to_scan, 597 Toast.LENGTH_LONG).show(); 598 return; 599 } 600 mAccessPoints.setProgress(mRetry != 0); 601 // Combo scans can take 5-6s to complete. Increase interval to 10s. 602 sendEmptyMessageDelayed(0, 10000); 603 } 604 } 605 606 private void changeNextButtonState(boolean wifiAvailable) { 607 if (mInXlSetupWizard) { 608 final Button button = 609 (Button)getActivity().findViewById(R.id.wifi_setup_skip_or_next); 610 button.setEnabled(true); 611 if (wifiAvailable) { 612 button.setText(R.string.wifi_setup_next); 613 } else { 614 button.setText(R.string.wifi_setup_skip); 615 } 616 } else if (mEnableNextOnConnection && hasNextButton()) { 617 // Assumes layout for phones has next button inside it. 618 getNextButton().setEnabled(wifiAvailable); 619 } 620 } 621 622 public void onClick(DialogInterface dialogInterface, int button) { 623 if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) { 624 forget(); 625 } else if (button == WifiDialog.BUTTON_SUBMIT) { 626 submit(); 627 } 628 } 629 630 /* package */ void submit() { 631 final WifiConfigUiBase uiBase = (mDialog != null ? mDialog : mConfigPreference); 632 final WifiConfigController configController = uiBase.getController(); 633 634 boolean successful = true; 635 switch(configController.chosenNetworkSetupMethod()) { 636 case WifiConfigController.WPS_PBC: 637 mWifiManager.startWpsPbc(mSelectedAccessPoint.bssid); 638 break; 639 case WifiConfigController.WPS_PIN: 640 int apPin = configController.getWpsPin(); 641 mWifiManager.startWpsPin(mSelectedAccessPoint.bssid, apPin); 642 break; 643 case WifiConfigController.MANUAL: 644 final WifiConfiguration config = configController.getConfig(); 645 646 if (config == null) { 647 if (mSelectedAccessPoint != null 648 && !requireKeyStore(mSelectedAccessPoint.getConfig()) 649 && mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) { 650 mWifiManager.connectNetwork(mSelectedAccessPoint.networkId); 651 } else { 652 successful = false; 653 } 654 } else if (config.networkId != INVALID_NETWORK_ID) { 655 if (mSelectedAccessPoint != null) { 656 mWifiManager.saveNetwork(config); 657 } 658 } else { 659 if (uiBase.isEdit() || requireKeyStore(config)) { 660 mWifiManager.saveNetwork(config); 661 } else { 662 mWifiManager.connectNetwork(config); 663 } 664 } 665 break; 666 } 667 668 if (mInXlSetupWizard && successful && mConfigPreference != null) { 669 // Now connecting to the AccessPoint. 670 mConnectingAccessPoint = mSelectedAccessPoint; 671 mConnectingAccessPoint.setSelectable(false); 672 } 673 674 detachConfigPreference(); 675 } 676 677 /* package */ void forget() { 678 mWifiManager.forgetNetwork(mSelectedAccessPoint.networkId); 679 680 detachConfigPreference(); 681 682 changeNextButtonState(false); 683 684 final Activity activity = getActivity(); 685 if (activity instanceof WifiSettingsForSetupWizardXL) { 686 ((WifiSettingsForSetupWizardXL)activity).onForget(); 687 } 688 } 689 690 /* package */ void refreshAccessPoints() { 691 mWifiManager.disconnect(); 692 if (mWifiManager.isWifiEnabled()) { 693 mScanner.resume(); 694 } 695 696 mConfigPreference = null; 697 mConnectingAccessPoint = null; 698 mAccessPoints.removeAll(); 699 700 if (mInXlSetupWizard) { 701 ((WifiSettingsForSetupWizardXL)getActivity()).onRefreshAccessPoints(); 702 } 703 } 704 705 /* package */ void detachConfigPreference() { 706 if (mConfigPreference != null) { 707 if (mWifiManager.isWifiEnabled()) { 708 mScanner.resume(); 709 } 710 mAccessPoints.removePreference(mConfigPreference); 711 mConfigPreference = null; 712 updateAccessPoints(); 713 toggleButtonsVisibility(true); 714 } 715 } 716 717 /* package */ void onAddNetworkPressed() { 718 mSelectedAccessPoint = null; 719 showConfigUi(null, true); 720 } 721 722 /* package */ int getAccessPointsCount() { 723 if (mAccessPoints != null) { 724 return mAccessPoints.getPreferenceCount(); 725 } else { 726 return 0; 727 } 728 } 729} 730