WifiSettings.java revision 9e3a4700b6f6035c6ce79b0194ca0cff5f0f9574
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.app.AlertDialog;
27import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.net.ConnectivityManager;
33import android.net.NetworkInfo;
34import android.net.NetworkInfo.DetailedState;
35import android.net.wifi.ScanResult;
36import android.net.wifi.SupplicantState;
37import android.net.wifi.WifiConfiguration;
38import android.net.wifi.WifiInfo;
39import android.net.wifi.WifiManager;
40import android.net.wifi.WifiConfiguration.KeyMgmt;
41import android.os.Bundle;
42import android.os.Handler;
43import android.os.Message;
44import android.preference.CheckBoxPreference;
45import android.preference.Preference;
46import android.preference.PreferenceActivity;
47import android.preference.PreferenceScreen;
48import android.provider.Settings.Secure;
49import android.security.Credentials;
50import android.security.KeyStore;
51import android.view.ContextMenu;
52import android.view.LayoutInflater;
53import android.view.Menu;
54import android.view.MenuInflater;
55import android.view.MenuItem;
56import android.view.View;
57import android.view.ViewGroup;
58import android.view.ContextMenu.ContextMenuInfo;
59import android.widget.Toast;
60import android.widget.AdapterView.AdapterContextMenuInfo;
61
62import java.util.ArrayList;
63import java.util.Collection;
64import java.util.List;
65import java.util.concurrent.atomic.AtomicBoolean;
66
67/**
68 * This currently provides three types of UI.
69 *
70 * Two are for phones with relatively small screens: "for SetupWizard" and "for usual Settings".
71 * Users just need to launch WifiSettings Activity as usual. The request will be appropriately
72 * handled by ActivityManager, and they will have appropriate look-and-feel with this fragment.
73 *
74 * Third type is for Setup Wizard with X-Large, landscape UI. Users need to launch
75 * {@link WifiSettingsForSetupWizardXL} Activity, which contains this fragment but also has
76 * other decorations specific to that screen.
77 */
78public class WifiSettings extends SettingsPreferenceFragment
79        implements DialogInterface.OnClickListener {
80    private static final int MENU_ID_SCAN = Menu.FIRST;
81    private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
82    private static final int MENU_ID_CONNECT = Menu.FIRST + 2;
83    private static final int MENU_ID_FORGET = Menu.FIRST + 3;
84    private static final int MENU_ID_MODIFY = Menu.FIRST + 4;
85
86    private final IntentFilter mFilter;
87    private final BroadcastReceiver mReceiver;
88    private final Scanner mScanner;
89
90    private WifiManager mWifiManager;
91    private WifiEnabler mWifiEnabler;
92    private CheckBoxPreference mNotifyOpenNetworks;
93    private ProgressCategoryBase mAccessPoints;
94    private Preference mAddNetwork;
95    // An access point being editted is stored here.
96    private AccessPoint mSelectedAccessPoint;
97    private boolean mEdit;
98
99    private DetailedState mLastState;
100    private WifiInfo mLastInfo;
101
102    private AtomicBoolean mConnected = new AtomicBoolean(false);
103
104    private int mKeyStoreNetworkId = INVALID_NETWORK_ID;
105
106    private WifiDialog mDialog;
107
108    /* Used in Wifi Setup context */
109
110    // this boolean extra specifies whether to disable the Next button when not connected
111    private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect";
112
113    // should Next button only be enabled when we have a connection?
114    private boolean mEnableNextOnConnection;
115    private boolean mInXlSetupWizard;
116
117    /* End of "used in Wifi Setup context" */
118
119    public WifiSettings() {
120        mFilter = new IntentFilter();
121        mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
122        mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
123        mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
124        mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
125        mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
126        mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
127        mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
128        mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
129
130        mReceiver = new BroadcastReceiver() {
131            @Override
132            public void onReceive(Context context, Intent intent) {
133                handleEvent(intent);
134            }
135        };
136
137        mScanner = new Scanner();
138    }
139
140    @Override
141    public void onAttach(Activity activity) {
142        super.onAttach(activity);
143
144        mInXlSetupWizard = (activity instanceof WifiSettingsForSetupWizardXL);
145    }
146
147    @Override
148    public View onCreateView(LayoutInflater inflater, ViewGroup container,
149            Bundle savedInstanceState) {
150        if (mInXlSetupWizard) {
151            return inflater.inflate(R.layout.custom_preference_list_fragment, container, false);
152        } else {
153            return super.onCreateView(inflater, container, savedInstanceState);
154        }
155    }
156
157    @Override
158    public void onActivityCreated(Bundle savedInstanceState) {
159        // We don't call super.onActivityCreated() here, since it assumes we already set up
160        // Preference (probably in onCreate()), while WifiSettings exceptionally set it up in
161        // this method.
162
163        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
164
165        final Activity activity = getActivity();
166        final Intent intent = activity.getIntent();
167
168        // if we're supposed to enable/disable the Next button based on our current connection
169        // state, start it off in the right state
170        mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
171
172        // Avoid re-adding on returning from an overlapping activity/fragment.
173        if (getPreferenceScreen() == null || getPreferenceScreen().getPreferenceCount() < 2) {
174            if (mEnableNextOnConnection) {
175                if (hasNextButton()) {
176                    final ConnectivityManager connectivity = (ConnectivityManager)
177                            getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
178                    if (connectivity != null) {
179                        NetworkInfo info = connectivity.getNetworkInfo(
180                                ConnectivityManager.TYPE_WIFI);
181                        changeNextButtonState(info.isConnected());
182                    }
183                }
184            }
185
186            if (mInXlSetupWizard) {
187                addPreferencesFromResource(R.xml.wifi_access_points_for_wifi_setup_xl);
188            } else if (intent.getBooleanExtra("only_access_points", false)) {
189                addPreferencesFromResource(R.xml.wifi_access_points);
190            } else {
191                addPreferencesFromResource(R.xml.wifi_settings);
192                mWifiEnabler = new WifiEnabler(activity,
193                        (CheckBoxPreference) findPreference("enable_wifi"));
194                mNotifyOpenNetworks =
195                        (CheckBoxPreference) findPreference("notify_open_networks");
196                mNotifyOpenNetworks.setChecked(Secure.getInt(getContentResolver(),
197                        Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
198            }
199            // This may be either ProgressCategory or AccessPointCategoryForXL.
200            final ProgressCategoryBase preference =
201                    (ProgressCategoryBase) findPreference("access_points");
202            mAccessPoints = preference;
203            mAccessPoints.setOrderingAsAdded(false);
204            mAddNetwork = findPreference("add_network");
205
206            registerForContextMenu(getListView());
207            setHasOptionsMenu(true);
208        }
209
210        // After confirming PreferenceScreen is available, we call super.
211        super.onActivityCreated(savedInstanceState);
212
213    }
214
215    @Override
216    public void onResume() {
217        super.onResume();
218        if (mWifiEnabler != null) {
219            mWifiEnabler.resume();
220        }
221        getActivity().registerReceiver(mReceiver, mFilter);
222        if (mKeyStoreNetworkId != INVALID_NETWORK_ID &&
223                KeyStore.getInstance().test() == KeyStore.NO_ERROR) {
224            mWifiManager.connectNetwork(mKeyStoreNetworkId);
225        }
226        mKeyStoreNetworkId = INVALID_NETWORK_ID;
227        updateAccessPoints();
228    }
229
230    @Override
231    public void onPause() {
232        super.onPause();
233        if (mWifiEnabler != null) {
234            mWifiEnabler.pause();
235        }
236        getActivity().unregisterReceiver(mReceiver);
237        mScanner.pause();
238        if (mDialog != null) {
239            mDialog.dismiss();
240            mDialog = null;
241        }
242    }
243
244    @Override
245    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
246        // We don't want menus in Setup Wizard XL.
247        if (!mInXlSetupWizard) {
248            menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.wifi_menu_scan)
249                    .setIcon(R.drawable.ic_menu_scan_network);
250            menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
251                    .setIcon(android.R.drawable.ic_menu_manage);
252        }
253        super.onCreateOptionsMenu(menu, inflater);
254    }
255
256    @Override
257    public boolean onOptionsItemSelected(MenuItem item) {
258        switch (item.getItemId()) {
259            case MENU_ID_SCAN:
260                if (mWifiManager.isWifiEnabled()) {
261                    mScanner.forceScan();
262                }
263                return true;
264            case MENU_ID_ADVANCED:
265                startFragment(this, AdvancedSettings.class.getCanonicalName(), -1, null);
266                return true;
267        }
268        return super.onOptionsItemSelected(item);
269    }
270
271    @Override
272    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
273        if (info instanceof AdapterContextMenuInfo) {
274            Preference preference = (Preference) getListView().getItemAtPosition(
275                    ((AdapterContextMenuInfo) info).position);
276
277            if (preference instanceof AccessPoint) {
278                mSelectedAccessPoint = (AccessPoint) preference;
279                menu.setHeaderTitle(mSelectedAccessPoint.ssid);
280                if (mSelectedAccessPoint.getLevel() != -1
281                        && mSelectedAccessPoint.getState() == null) {
282                    menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect);
283                }
284                if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
285                    menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget);
286                    menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify);
287                }
288            }
289        }
290    }
291
292    @Override
293    public boolean onContextItemSelected(MenuItem item) {
294        if (mSelectedAccessPoint == null) {
295            return super.onContextItemSelected(item);
296        }
297        switch (item.getItemId()) {
298            case MENU_ID_CONNECT: {
299                if (mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
300                    if (!requireKeyStore(mSelectedAccessPoint.getConfig())) {
301                        mWifiManager.connectNetwork(mSelectedAccessPoint.networkId);
302                    }
303                } else if (mSelectedAccessPoint.security == AccessPoint.SECURITY_NONE) {
304                    // Shortcut for open networks.
305                    WifiConfiguration config = new WifiConfiguration();
306                    config.SSID = AccessPoint.convertToQuotedString(mSelectedAccessPoint.ssid);
307                    config.allowedKeyManagement.set(KeyMgmt.NONE);
308                    mWifiManager.connectNetwork(config);
309                } else {
310                    showConfigUi(mSelectedAccessPoint, true);
311                }
312                return true;
313            }
314            case MENU_ID_FORGET: {
315                mWifiManager.forgetNetwork(mSelectedAccessPoint.networkId);
316                return true;
317            }
318            case MENU_ID_MODIFY: {
319                showConfigUi(mSelectedAccessPoint, true);
320                return true;
321            }
322        }
323        return super.onContextItemSelected(item);
324    }
325
326    @Override
327    public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
328        if (preference instanceof AccessPoint) {
329            mSelectedAccessPoint = (AccessPoint) preference;
330            showConfigUi(mSelectedAccessPoint, false);
331        } else if (preference == mAddNetwork) {
332            onAddNetworkPressed();
333        } else if (preference == mNotifyOpenNetworks) {
334            Secure.putInt(getContentResolver(),
335                    Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
336                    mNotifyOpenNetworks.isChecked() ? 1 : 0);
337        } else {
338            return super.onPreferenceTreeClick(screen, preference);
339        }
340        return true;
341    }
342
343    /**
344     * Shows an appropriate Wifi configuration component.
345     * Called when a user clicks "Add network" preference or one of available networks is selected.
346     */
347    private void showConfigUi(AccessPoint accessPoint, boolean edit) {
348        mEdit = edit;
349        if (mInXlSetupWizard) {
350            ((WifiSettingsForSetupWizardXL)getActivity()).showConfigUi(accessPoint, edit);
351        } else {
352            showDialog(accessPoint, edit);
353        }
354    }
355
356    private void showDialog(AccessPoint accessPoint, boolean edit) {
357        if (mDialog != null) {
358            mDialog.dismiss();
359        }
360        mDialog = new WifiDialog(getActivity(), this, accessPoint, edit);
361        mDialog.show();
362    }
363
364    private boolean requireKeyStore(WifiConfiguration config) {
365        if (WifiConfigController.requireKeyStore(config) &&
366                KeyStore.getInstance().test() != KeyStore.NO_ERROR) {
367            mKeyStoreNetworkId = config.networkId;
368            Credentials.getInstance().unlock(getActivity());
369            return true;
370        }
371        return false;
372    }
373
374    /**
375     * Shows the latest access points available with supplimental information like
376     * the strength of network and the security for it.
377     */
378    private void updateAccessPoints() {
379        mAccessPoints.removeAll();
380
381        // AccessPoints are automatically sorted with TreeSet.
382        final Collection<AccessPoint> accessPoints = constructAccessPoints();
383        if (mInXlSetupWizard) {
384            ((WifiSettingsForSetupWizardXL)getActivity()).onAccessPointsUpdated(
385                    mAccessPoints, accessPoints);
386        } else {
387            for (AccessPoint accessPoint : accessPoints) {
388                mAccessPoints.addPreference(accessPoint);
389            }
390        }
391    }
392
393    private Collection<AccessPoint> constructAccessPoints() {
394        Collection<AccessPoint> accessPoints = new ArrayList<AccessPoint>();
395
396        final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
397        if (configs != null) {
398            for (WifiConfiguration config : configs) {
399                AccessPoint accessPoint = new AccessPoint(getActivity(), config);
400                accessPoint.update(mLastInfo, mLastState);
401                accessPoints.add(accessPoint);
402            }
403        }
404
405        final List<ScanResult> results = mWifiManager.getScanResults();
406        if (results != null) {
407            for (ScanResult result : results) {
408                // Ignore hidden and ad-hoc networks.
409                if (result.SSID == null || result.SSID.length() == 0 ||
410                        result.capabilities.contains("[IBSS]")) {
411                    continue;
412                }
413
414                boolean found = false;
415                for (AccessPoint accessPoint : accessPoints) {
416                    if (accessPoint.update(result)) {
417                        found = true;
418                    }
419                }
420                if (!found) {
421                    accessPoints.add(new AccessPoint(getActivity(), result));
422                }
423            }
424        }
425
426        return accessPoints;
427    }
428
429    private void handleEvent(Intent intent) {
430        String action = intent.getAction();
431        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
432            updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
433                    WifiManager.WIFI_STATE_UNKNOWN));
434        } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
435                WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
436                WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
437                updateAccessPoints();
438        } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
439            //Ignore supplicant state changes when network is connected
440            //TODO: we should deprecate SUPPLICANT_STATE_CHANGED_ACTION and
441            //introduce a broadcast that combines the supplicant and network
442            //network state change events so the apps dont have to worry about
443            //ignoring supplicant state change when network is connected
444            //to get more fine grained information.
445            if (!mConnected.get()) {
446                updateConnectionState(WifiInfo.getDetailedStateOf((SupplicantState)
447                        intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
448            }
449        } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
450            NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
451                    WifiManager.EXTRA_NETWORK_INFO);
452            mConnected.set(info.isConnected());
453            changeNextButtonState(info.isConnected());
454            updateConnectionState(info.getDetailedState());
455        } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
456            updateConnectionState(null);
457        }
458    }
459
460    private void updateConnectionState(DetailedState state) {
461        /* sticky broadcasts can call this when wifi is disabled */
462        if (!mWifiManager.isWifiEnabled()) {
463            mScanner.pause();
464            return;
465        }
466
467        if (state == DetailedState.OBTAINING_IPADDR) {
468            mScanner.pause();
469        } else {
470            mScanner.resume();
471        }
472
473        mLastInfo = mWifiManager.getConnectionInfo();
474        if (state != null) {
475            mLastState = state;
476        }
477
478        for (int i = mAccessPoints.getPreferenceCount() - 1; i >= 0; --i) {
479            // Maybe there's a WifiConfigPreference
480            Preference preference = mAccessPoints.getPreference(i);
481            if (preference instanceof AccessPoint) {
482                final AccessPoint accessPoint = (AccessPoint) preference;
483                accessPoint.update(mLastInfo, mLastState);
484            }
485        }
486
487        if (mInXlSetupWizard) {
488            ((WifiSettingsForSetupWizardXL)getActivity()).updateConnectionState(mLastState);
489        }
490    }
491
492    private void updateWifiState(int state) {
493        if (state == WifiManager.WIFI_STATE_ENABLED) {
494            mScanner.resume();
495        } else {
496            mScanner.pause();
497            mAccessPoints.removeAll();
498        }
499    }
500
501    private class Scanner extends Handler {
502        private int mRetry = 0;
503
504        void resume() {
505            if (!hasMessages(0)) {
506                sendEmptyMessage(0);
507            }
508        }
509
510        void forceScan() {
511            sendEmptyMessage(0);
512        }
513
514        void pause() {
515            mRetry = 0;
516            mAccessPoints.setProgress(false);
517            removeMessages(0);
518        }
519
520        @Override
521        public void handleMessage(Message message) {
522            if (mWifiManager.startScanActive()) {
523                mRetry = 0;
524            } else if (++mRetry >= 3) {
525                mRetry = 0;
526                Toast.makeText(getActivity(), R.string.wifi_fail_to_scan,
527                        Toast.LENGTH_LONG).show();
528                return;
529            }
530            mAccessPoints.setProgress(mRetry != 0);
531            // Combo scans can take 5-6s to complete. Increase interval to 10s.
532            sendEmptyMessageDelayed(0, 10000);
533        }
534    }
535
536    /**
537     * Renames/replaces "Next" button when appropriate. "Next" button usually exists in
538     * Wifi setup screens, not in usual wifi settings screen.
539     *
540     * @param connected true when the device is connected to a wifi network.
541     */
542    private void changeNextButtonState(boolean connected) {
543        if (mInXlSetupWizard) {
544            ((WifiSettingsForSetupWizardXL)getActivity()).changeNextButtonState(connected);
545        } else if (mEnableNextOnConnection && hasNextButton()) {
546            getNextButton().setEnabled(connected);
547        }
548    }
549
550    public void onClick(DialogInterface dialogInterface, int button) {
551        if (mInXlSetupWizard) {
552            if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
553                ((WifiSettingsForSetupWizardXL)getActivity()).onForgetButtonPressed();
554            } else if (button == WifiDialog.BUTTON_SUBMIT) {
555                ((WifiSettingsForSetupWizardXL)getActivity()).onConnectButtonPressed();
556            }
557        } else {
558            if (button == WifiDialog.BUTTON_FORGET && mSelectedAccessPoint != null) {
559                forget();
560            } else if (button == WifiDialog.BUTTON_SUBMIT) {
561                submit(mDialog.getController());
562            }
563        }
564
565    }
566
567    /* package */ void submit(WifiConfigController configController) {
568        switch(configController.chosenNetworkSetupMethod()) {
569            case WifiConfigController.WPS_PBC:
570            case WifiConfigController.WPS_PIN_FROM_ACCESS_POINT:
571                mWifiManager.startWps(configController.getWpsConfig());
572                break;
573            case WifiConfigController.WPS_PIN_FROM_DEVICE:
574                String pin = mWifiManager.startWps(configController.getWpsConfig());
575                new AlertDialog.Builder(getActivity())
576                        .setTitle(R.string.wifi_wps_pin_method_configuration)
577                        .setMessage(getResources().getString(R.string.wifi_wps_pin_output, pin))
578                        .setPositiveButton(android.R.string.ok, null)
579                        .show();
580                break;
581            case WifiConfigController.MANUAL:
582                final WifiConfiguration config = configController.getConfig();
583
584                if (config == null) {
585                    if (mSelectedAccessPoint != null
586                            && !requireKeyStore(mSelectedAccessPoint.getConfig())
587                            && mSelectedAccessPoint.networkId != INVALID_NETWORK_ID) {
588                        mWifiManager.connectNetwork(mSelectedAccessPoint.networkId);
589                    }
590                } else if (config.networkId != INVALID_NETWORK_ID) {
591                    if (mSelectedAccessPoint != null) {
592                        mWifiManager.saveNetwork(config);
593                    }
594                } else {
595                    if (configController.isEdit() || requireKeyStore(config)) {
596                        mWifiManager.saveNetwork(config);
597                    } else {
598                        mWifiManager.connectNetwork(config);
599                    }
600                }
601                break;
602        }
603
604        if (mWifiManager.isWifiEnabled()) {
605            mScanner.resume();
606        }
607        updateAccessPoints();
608    }
609
610    /* package */ void forget() {
611        mWifiManager.forgetNetwork(mSelectedAccessPoint.networkId);
612
613        if (mWifiManager.isWifiEnabled()) {
614            mScanner.resume();
615        }
616        updateAccessPoints();
617
618        // We need to rename/replace "Next" button in wifi setup context.
619        changeNextButtonState(false);
620    }
621
622    /**
623     * Refreshes acccess points and ask Wifi module to scan networks again.
624     */
625    /* package */ void refreshAccessPoints() {
626        if (mWifiManager.isWifiEnabled()) {
627            mScanner.resume();
628        }
629
630        mAccessPoints.removeAll();
631    }
632
633    /**
634     * Called when "add network" button is pressed.
635     */
636    /* package */ void onAddNetworkPressed() {
637        // No exact access point is selected.
638        mSelectedAccessPoint = null;
639        showConfigUi(null, true);
640    }
641
642    /* package */ int getAccessPointsCount() {
643        if (mAccessPoints != null) {
644            return mAccessPoints.getPreferenceCount();
645        } else {
646            return 0;
647        }
648    }
649
650    /**
651     * Requests wifi module to pause wifi scan. May be ignored when the module is disabled.
652     */
653    /* package */ void pauseWifiScan() {
654        if (mWifiManager.isWifiEnabled()) {
655            mScanner.pause();
656        }
657    }
658
659    /**
660     * Requests wifi module to resume wifi scan. May be ignored when the module is disabled.
661     */
662    /* package */ void resumeWifiScan() {
663        if (mWifiManager.isWifiEnabled()) {
664            mScanner.resume();
665        }
666    }
667}
668