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