OSDN Git Service

UI refresh
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / WifiSettings.java
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
17 package com.android.settings.wifi;
18
19 import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
20
21 import com.android.settings.ProgressCategoryBase;
22 import com.android.settings.R;
23 import com.android.settings.SettingsPreferenceFragment;
24
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.IntentFilter;
31 import android.net.ConnectivityManager;
32 import android.net.NetworkInfo;
33 import android.net.NetworkInfo.DetailedState;
34 import android.net.wifi.ScanResult;
35 import android.net.wifi.SupplicantState;
36 import android.net.wifi.WifiConfiguration;
37 import android.net.wifi.WifiConfiguration.KeyMgmt;
38 import android.net.wifi.WifiInfo;
39 import android.net.wifi.WifiManager;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.os.Message;
43 import android.preference.CheckBoxPreference;
44 import android.preference.Preference;
45 import android.preference.PreferenceScreen;
46 import android.provider.Settings.Secure;
47 import android.security.Credentials;
48 import android.security.KeyStore;
49 import android.util.Log;
50 import android.view.ContextMenu;
51 import android.view.ContextMenu.ContextMenuInfo;
52 import android.view.Menu;
53 import android.view.MenuInflater;
54 import android.view.MenuItem;
55 import android.view.View;
56 import android.widget.AdapterView.AdapterContextMenuInfo;
57 import android.widget.Button;
58 import android.widget.Toast;
59
60 import java.util.Collection;
61 import java.util.List;
62 import 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  */
75 public 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 = 6;
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             if (mInXlSetupWizard) {
455                 //limit access points on set up wizard
456                 int count = MAX_MENU_COUNT_IN_XL;
457                 for (AccessPoint accessPoint : accessPoints) {
458                     mAccessPoints.addPreference(accessPoint);
459                     count--;
460                     if (count <= 0) {
461                         break;
462                     }
463                 }
464             } else {
465                 for (AccessPoint accessPoint : accessPoints) {
466                     mAccessPoints.addPreference(accessPoint);
467                 }
468             }
469         }
470     }
471
472     private Collection<AccessPoint> constructAccessPoints() {
473         Collection<AccessPoint> accessPoints =
474                 new TreeSet<AccessPoint>(new AccessPoint.Comparater());
475
476         final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
477         if (configs != null) {
478             for (WifiConfiguration config : configs) {
479                 AccessPoint accessPoint = new AccessPoint(getActivity(), config);
480                 accessPoint.update(mLastInfo, mLastState);
481                 accessPoints.add(accessPoint);
482             }
483         }
484
485         final List<ScanResult> results = mWifiManager.getScanResults();
486         if (results != null) {
487             for (ScanResult result : results) {
488                 // Ignore hidden and ad-hoc networks.
489                 if (result.SSID == null || result.SSID.length() == 0 ||
490                         result.capabilities.contains("[IBSS]")) {
491                     continue;
492                 }
493
494                 boolean found = false;
495                 for (AccessPoint accessPoint : accessPoints) {
496                     if (accessPoint.update(result)) {
497                         found = true;
498                     }
499                 }
500                 if (!found) {
501                     accessPoints.add(new AccessPoint(getActivity(), result));
502                 }
503             }
504         }
505
506         return accessPoints;
507     }
508
509     private void handleEvent(Intent intent) {
510         String action = intent.getAction();
511         if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
512             updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
513                     WifiManager.WIFI_STATE_UNKNOWN));
514         } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
515                 WifiManager.SUPPLICANT_CONFIG_CHANGED_ACTION.equals(action)) {
516                 updateAccessPoints();
517         } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
518             updateConnectionState(WifiInfo.getDetailedStateOf((SupplicantState)
519                     intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
520         } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
521             NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
522                     WifiManager.EXTRA_NETWORK_INFO);
523             changeNextButtonState(info.isConnected());
524             updateConnectionState(info.getDetailedState());
525         } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
526             updateConnectionState(null);
527         }
528     }
529
530     private void updateConnectionState(DetailedState state) {
531         /* sticky broadcasts can call this when wifi is disabled */
532         if (!mWifiManager.isWifiEnabled()) {
533             mScanner.pause();
534             return;
535         }
536
537         if (state == DetailedState.OBTAINING_IPADDR) {
538             mScanner.pause();
539         } else {
540             mScanner.resume();
541         }
542
543         mLastInfo = mWifiManager.getConnectionInfo();
544         if (state != null) {
545             mLastState = state;
546         }
547
548         for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) {
549             // Maybe there's a WifiConfigPreference
550             Preference preference = mAccessPoints.getPreference(i);
551             if (preference instanceof AccessPoint) {
552                 final AccessPoint accessPoint = (AccessPoint) preference;
553                 accessPoint.update(mLastInfo, mLastState);
554             }
555         }
556
557         final Activity activity = getActivity();
558         if (activity instanceof WifiSettingsForSetupWizardXL) {
559             if (mLastState == DetailedState.FAILED) {
560                 // We clean up the status and let users select another network if they want.
561                 refreshAccessPoints();
562             }
563             ((WifiSettingsForSetupWizardXL)activity).updateConnectionState(mLastState);
564         }
565     }
566
567     private void updateWifiState(int state) {
568         if (state == WifiManager.WIFI_STATE_ENABLED) {
569             mScanner.resume();
570         } else {
571             mScanner.pause();
572             mAccessPoints.removeAll();
573         }
574     }
575
576     private class Scanner extends Handler {
577         private int mRetry = 0;
578
579         void resume() {
580             synchronized (WifiSettings.this) {
581                 mRefrainListUpdate = false;
582             }
583             if (!hasMessages(0)) {
584                 sendEmptyMessage(0);
585             }
586         }
587
588         void pause() {
589             mRetry = 0;
590             mAccessPoints.setProgress(false);
591             synchronized (WifiSettings.this) {
592                 mRefrainListUpdate = true;
593             }
594             removeMessages(0);
595         }
596
597         @Override
598         public void handleMessage(Message message) {
599             if (mWifiManager.startScanActive()) {
600                 mRetry = 0;
601             } else if (++mRetry >= 3) {
602                 mRetry = 0;
603                 Toast.makeText(getActivity(), R.string.wifi_fail_to_scan,
604                         Toast.LENGTH_LONG).show();
605                 return;
606             }
607             mAccessPoints.setProgress(mRetry != 0);
608             // Combo scans can take 5-6s to complete. Increase interval to 10s.
609             sendEmptyMessageDelayed(0, 10000);
610         }
611     }
612
613     private void changeNextButtonState(boolean wifiAvailable) {
614         if (mInXlSetupWizard) {
615             final Button button =
616                     (Button)getActivity().findViewById(R.id.wifi_setup_skip_or_next);
617             if (wifiAvailable) {
618                 button.setText(R.string.wifi_setup_next);
619             } else {
620                 button.setText(R.string.wifi_setup_skip);
621             }
622         } else if (mEnableNextOnConnection && hasNextButton()) {
623             // Assumes layout for phones has next button inside it.
624             getNextButton().setEnabled(wifiAvailable);
625         }
626     }
627
628     public void onClick(DialogInterface dialogInterface, int button) {
629         if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
630             forget();
631         } else if (button == WifiDialog.BUTTON_SUBMIT) {
632             submit();
633         }
634     }
635
636     /* package */ void submit() {
637         final WifiConfigUiBase uiBase = (mDialog != null ? mDialog : mConfigPreference);
638         final WifiConfigController configController = uiBase.getController();
639
640         boolean successful = true;
641         switch(configController.chosenNetworkSetupMethod()) {
642             case WifiConfigController.WPS_PBC:
643                 mWifiManager.startWpsPbc(mSelectedAccessPoint.bssid);
644                 break;
645             case WifiConfigController.WPS_PIN:
646                 int apPin = configController.getWpsPin();
647                 mWifiManager.startWpsPin(mSelectedAccessPoint.bssid, apPin);
648                 break;
649             case WifiConfigController.MANUAL:
650                 final WifiConfiguration config = configController.getConfig();
651
652                 if (config == null) {
653                     if (mSelectedAccessPoint != null
654                             && !requireKeyStore(mSelectedAccessPoint.getConfig())
655                             && mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
656                         mWifiManager.connectNetwork(mSelectedAccessPoint.networkId);
657                     } else {
658                         successful = false;
659                     }
660                 } else if (config.networkId != INVALID_NETWORK_ID) {
661                     if (mSelectedAccessPoint != null) {
662                         mWifiManager.saveNetwork(config);
663                     }
664                 } else {
665                     if (uiBase.isEdit() || requireKeyStore(config)) {
666                         mWifiManager.saveNetwork(config);
667                     } else {
668                         mWifiManager.connectNetwork(config);
669                     }
670                 }
671                 break;
672         }
673
674         if (mInXlSetupWizard && successful && mConfigPreference != null) {
675             // Now connecting to the AccessPoint.
676             mConnectingAccessPoint = mSelectedAccessPoint;
677             mConnectingAccessPoint.setSelectable(false);
678         }
679
680         detachConfigPreference();
681     }
682
683     /* package */ void forget() {
684         mWifiManager.forgetNetwork(mSelectedAccessPoint.networkId);
685
686         detachConfigPreference();
687
688         changeNextButtonState(false);
689
690         final Activity activity = getActivity();
691         if (activity instanceof WifiSettingsForSetupWizardXL) {
692             ((WifiSettingsForSetupWizardXL)activity).onForget();
693         }
694     }
695
696     /* package */ void refreshAccessPoints() {
697         mWifiManager.disconnect();
698         if (mWifiManager.isWifiEnabled()) {
699             mScanner.resume();
700         }
701
702         mConfigPreference = null;
703         mConnectingAccessPoint = null;
704         mAccessPoints.removeAll();
705
706         if (mInXlSetupWizard) {
707             ((WifiSettingsForSetupWizardXL)getActivity()).onRefreshAccessPoints();
708         }
709     }
710
711     /* package */ void detachConfigPreference() {
712         if (mConfigPreference != null) {
713             if (mWifiManager.isWifiEnabled()) {
714                 mScanner.resume();
715             }
716             mAccessPoints.removePreference(mConfigPreference);
717             mConfigPreference = null;
718             updateAccessPoints();
719             toggleButtonsVisibility(true);
720         }
721     }
722
723     /* package */ void onAddNetworkPressed() {
724         mSelectedAccessPoint = null;
725         showConfigUi(null, true);
726     }
727
728     /* package */ int getAccessPointsCount() {
729         if (mAccessPoints != null) {
730             return mAccessPoints.getPreferenceCount();
731         } else {
732             return 0;
733         }
734     }
735 }