WifiSettings.java revision 79f347602186a4057b3f29cab9bed0fd229d3cb1
148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh/*
248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * Copyright (C) 2010 The Android Open Source Project
348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh *
448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * Licensed under the Apache License, Version 2.0 (the "License");
548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * you may not use this file except in compliance with the License.
648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * You may obtain a copy of the License at
748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh *
848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh *      http://www.apache.org/licenses/LICENSE-2.0
948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh *
1048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * Unless required by applicable law or agreed to in writing, software
1148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * distributed under the License is distributed on an "AS IS" BASIS,
1248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * See the License for the specific language governing permissions and
1448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh * limitations under the License.
1548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh */
1648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
1748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehpackage com.android.settings.wifi;
1848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
192df0bf9d5cc160611099167625659db9d736036bSundeep Ghumanimport static android.os.UserManager.DISALLOW_CONFIG_WIFI;
202df0bf9d5cc160611099167625659db9d736036bSundeep Ghuman
21f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghumanimport android.annotation.NonNull;
229c8bde576ab18fcfe653587f3e63604430c8791cDaisuke Miyakawaimport android.app.Activity;
233d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasaniimport android.app.Dialog;
2489403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Banimport android.app.admin.DevicePolicyManager;
254cfe39f5397d037d106abf07db31b0e01484bd92Makoto Onukiimport android.content.ComponentName;
265de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Banimport android.content.ContentResolver;
2748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.content.Context;
2848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.content.Intent;
2989403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Banimport android.content.pm.PackageManager;
3089403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Banimport android.content.pm.PackageManager.NameNotFoundException;
31fc86669031e9c11efa688cbf95e53c77dbc0a03dRussell Brennerimport android.content.res.Resources;
329f59de930ae14566673d4d57597b836556820b29Maurice Lamimport android.net.ConnectivityManager;
3348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.net.NetworkInfo;
34b46a9194fe88a26e80d415919d191703c340925eJeff Davidsonimport android.net.NetworkInfo.State;
3548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.net.wifi.WifiConfiguration;
3648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.net.wifi.WifiManager;
3790380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriffimport android.net.wifi.WpsInfo;
3864c7429ac84e2f041e64fb7094f390b7491472dcAndres Moralesimport android.nfc.NfcAdapter;
3948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.os.Bundle;
407a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monkimport android.os.HandlerThread;
418ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chenimport android.os.PowerManager;
427a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monkimport android.os.Process;
435de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Banimport android.provider.Settings;
44c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Lingimport android.support.annotation.VisibleForTesting;
4539b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
4641456785482f41e82e8fd1b2538749da73303980Stephen Chenimport android.support.v7.preference.PreferenceCategory;
47590e6c23b7ba4bf9a75ecfe5e6eda11d0dfa5c71Irfan Sheriffimport android.util.Log;
4848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.view.ContextMenu;
49e78c1879058571c00879f0d3e756951302adf513Gilles Debunneimport android.view.ContextMenu.ContextMenuInfo;
5048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.view.Menu;
5148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.view.MenuItem;
5248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yehimport android.view.View;
53e78c1879058571c00879f0d3e756951302adf513Gilles Debunneimport android.widget.Toast;
543f0e1344d39ac58b37e21c8aae548344d853c29aFan Zhang
55265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
56be794c30ca49c17521e66482ce30c94ffb2ec8eeLifu Tangimport com.android.settings.LinkifyUtils;
57873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.R;
58873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.RestrictedSettingsFragment;
59873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.SettingsActivity;
603069581512bcb9687cb8d79675f6c4950c9ac087Jason Monkimport com.android.settings.dashboard.SummaryLoader;
61be794c30ca49c17521e66482ce30c94ffb2ec8eeLifu Tangimport com.android.settings.location.ScanningSettings;
62873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.search.BaseSearchIndexProvider;
63873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.search.Indexable;
64873fa4f2ba4385a553381c0ed15f45d1e82e8947Jeff Davidsonimport com.android.settings.search.SearchIndexableRaw;
65c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Lingimport com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
66c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Lingimport com.android.settings.widget.SwitchBarController;
6707db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghumanimport com.android.settings.wifi.details.WifiNetworkDetailsFragment;
68dee1a22c45c78dd1d4a681314045b0757b63623dDoris Lingimport com.android.settings.wrapper.WifiManagerWrapper;
69e2b2c3ffe43da02317c7a1c80a5ec47cd1dc3ee8Sudheer Shankaimport com.android.settingslib.RestrictedLockUtils;
70fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monkimport com.android.settingslib.wifi.AccessPoint;
71fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monkimport com.android.settingslib.wifi.AccessPoint.AccessPointListener;
7299b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantlerimport com.android.settingslib.wifi.AccessPointPreference;
73fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monkimport com.android.settingslib.wifi.WifiTracker;
742df0bf9d5cc160611099167625659db9d736036bSundeep Ghumanimport com.android.settingslib.wifi.WifiTrackerFactory;
753f0e1344d39ac58b37e21c8aae548344d853c29aFan Zhang
76ac57f3e1eb56998550534c1b32d99cb7b5cc2b7bRussell Brennerimport java.util.ArrayList;
77ac57f3e1eb56998550534c1b32d99cb7b5cc2b7bRussell Brennerimport java.util.List;
78590e6c23b7ba4bf9a75ecfe5e6eda11d0dfa5c71Irfan Sheriff
79d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa/**
80fc5dd2cbf0d614f661d513ba6e672b8731d8ea6bRussell Brenner * Two types of UI are provided here.
81d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa *
82fc5dd2cbf0d614f661d513ba6e672b8731d8ea6bRussell Brenner * The first is for "usual Settings", appearing as any other Setup fragment.
83d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa *
84fc5dd2cbf0d614f661d513ba6e672b8731d8ea6bRussell Brenner * The second is for Setup Wizard, with a simplified interface that hides the action bar
85fc5dd2cbf0d614f661d513ba6e672b8731d8ea6bRussell Brenner * and menus.
86d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa */
87e38ea38002290da8ed16f07ea45f0ebfb41b38cdGeoffrey Borggaardpublic class WifiSettings extends RestrictedSettingsFragment
882662df843327dd938225c75722d0bbf3b01068f7Maurice Lam        implements Indexable, WifiTracker.WifiListener, AccessPointListener,
892662df843327dd938225c75722d0bbf3b01068f7Maurice Lam        WifiDialog.WifiDialogListener {
90ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales
91590e6c23b7ba4bf9a75ecfe5e6eda11d0dfa5c71Irfan Sheriff    private static final String TAG = "WifiSettings";
92060e01a93e678a62d9328bc3c48555976bda7710PauloftheWest
93d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner    /* package */ static final int MENU_ID_WPS_PBC = Menu.FIRST;
9490380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff    private static final int MENU_ID_WPS_PIN = Menu.FIRST + 1;
956ac5554d3006451c2d0bf9250a6ac9e77d572b0dIrfan Sheriff    private static final int MENU_ID_CONNECT = Menu.FIRST + 6;
966ac5554d3006451c2d0bf9250a6ac9e77d572b0dIrfan Sheriff    private static final int MENU_ID_FORGET = Menu.FIRST + 7;
976ac5554d3006451c2d0bf9250a6ac9e77d572b0dIrfan Sheriff    private static final int MENU_ID_MODIFY = Menu.FIRST + 8;
98ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales    private static final int MENU_ID_WRITE_NFC = Menu.FIRST + 9;
9948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
1007837b999862cf341b0f124be7ace1dfc64d0a5a8PauloftheWest    public static final int WIFI_DIALOG_ID = 1;
101d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner    /* package */ static final int WPS_PBC_DIALOG_ID = 2;
10290380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff    private static final int WPS_PIN_DIALOG_ID = 3;
103ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales    private static final int WRITE_NFC_DIALOG_ID = 6;
1043d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
1053d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    // Instance state keys
106771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam    private static final String SAVE_DIALOG_MODE = "dialog_mode";
1073d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    private static final String SAVE_DIALOG_ACCESS_POINT_STATE = "wifi_ap_state";
108aef794a2894e996fafa17097744a2bab75eb680dAndres Morales    private static final String SAVED_WIFI_NFC_DIALOG_STATE = "wifi_nfc_dlg_state";
1093d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
110b8e8485720757bcc1c8e5e1e3c5f3f8b2d33ae03Doris Ling    private static final String PREF_KEY_EMPTY_WIFI_LIST = "wifi_empty_list";
111f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private static final String PREF_KEY_CONNECTED_ACCESS_POINTS = "connected_access_point";
11241456785482f41e82e8fd1b2538749da73303980Stephen Chen    private static final String PREF_KEY_ACCESS_POINTS = "access_points";
11341456785482f41e82e8fd1b2538749da73303980Stephen Chen    private static final String PREF_KEY_ADDITIONAL_SETTINGS = "additional_settings";
1149d2712cd78514eb35ba1d480738841f3c203ecfdStephen Chen    private static final String PREF_KEY_CONFIGURE_WIFI_SETTINGS = "configure_settings";
11541456785482f41e82e8fd1b2538749da73303980Stephen Chen    private static final String PREF_KEY_SAVED_NETWORKS = "saved_networks";
116b8e8485720757bcc1c8e5e1e3c5f3f8b2d33ae03Doris Ling
1175e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh    private final Runnable mUpdateAccessPointsRunnable = () -> {
1185e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh        updateAccessPointPreferences();
1195e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh    };
1205e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh    private final Runnable mHideProgressBarRunnable = () -> {
1215e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh        setProgressBarVisible(false);
1225e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh    };
1235e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh
124fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    protected WifiManager mWifiManager;
1258f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff    private WifiManager.ActionListener mConnectListener;
1268f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff    private WifiManager.ActionListener mSaveListener;
1278f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff    private WifiManager.ActionListener mForgetListener;
12890380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff
129f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen    /**
130f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen     * The state of {@link #isUiRestricted()} at {@link #onCreate(Bundle)}}. This is neccesary to
131f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen     * ensure that behavior is consistent if {@link #isUiRestricted()} changes. It could be changed
132f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen     * by the Test DPC tool in AFW mode.
133f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen     */
134f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen    private boolean mIsRestricted;
135f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen
13648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    private WifiEnabler mWifiEnabler;
137d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    // An access point being editted is stored here.
138d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    private AccessPoint mSelectedAccessPoint;
13948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
14058b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa    private WifiDialog mDialog;
141ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales    private WriteWifiConfigToNfcDialog mWifiToNfcDialog;
142d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa
14362a3220efb7dfbae6d3ef6de68a07e3c07117ca1Fan Zhang    private View mProgressHeader;
14418e9992a5e5f515566aa9ea861b2e1a3c2450ebcGilles Debunne
1459f59de930ae14566673d4d57597b836556820b29Maurice Lam    // this boolean extra specifies whether to disable the Next button when not connected. Used by
1469f59de930ae14566673d4d57597b836556820b29Maurice Lam    // account creation outside of setup wizard.
1479f59de930ae14566673d4d57597b836556820b29Maurice Lam    private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect";
14836760c108b4b427da95afee23bc0ac7aeace9323Jason Monk    // This string extra specifies a network to open the connect dialog on, so the user can enter
149c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    // network credentials.  This is used by quick settings for secured networks, among other
150c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    // things.
151c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    public static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid";
1529f59de930ae14566673d4d57597b836556820b29Maurice Lam
1539f59de930ae14566673d4d57597b836556820b29Maurice Lam    // should Next button only be enabled when we have a connection?
1549f59de930ae14566673d4d57597b836556820b29Maurice Lam    private boolean mEnableNextOnConnection;
1559f59de930ae14566673d4d57597b836556820b29Maurice Lam
1563d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    // Save the dialog details
157771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam    private int mDialogMode;
1583d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    private AccessPoint mDlgAccessPoint;
1593d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    private Bundle mAccessPointSavedState;
160aef794a2894e996fafa17097744a2bab75eb680dAndres Morales    private Bundle mWifiNfcDialogSavedState;
1613d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
162fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    private WifiTracker mWifiTracker;
16302f0a4aff3349513e69ea6697151c60088f75dcdJason Monk    private String mOpenSsid;
164653cfeeb0a84d836a107653d7e0a478a56e6cc5fvandwalle
1657a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    private HandlerThread mBgThread;
1667a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk
16799b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler    private AccessPointPreference.UserBadgeCache mUserBadgeCache;
16841456785482f41e82e8fd1b2538749da73303980Stephen Chen
169f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private PreferenceCategory mConnectedAccessPointPreferenceCategory;
17041456785482f41e82e8fd1b2538749da73303980Stephen Chen    private PreferenceCategory mAccessPointsPreferenceCategory;
17141456785482f41e82e8fd1b2538749da73303980Stephen Chen    private PreferenceCategory mAdditionalSettingsPreferenceCategory;
1720b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private Preference mAddPreference;
1739d2712cd78514eb35ba1d480738841f3c203ecfdStephen Chen    private Preference mConfigureWifiSettingsPreference;
17441456785482f41e82e8fd1b2538749da73303980Stephen Chen    private Preference mSavedNetworksPreference;
17541456785482f41e82e8fd1b2538749da73303980Stephen Chen    private LinkablePreference mStatusMessagePreference;
17655dd8869db9c2a66423fcbb83cb5f5db38f8fb5fJason Monk
177b759516cbf26c9bb5f71c7f93e764fac73f48701Matthew Fritze    // For Search
178b759516cbf26c9bb5f71c7f93e764fac73f48701Matthew Fritze    private static final String DATA_KEY_REFERENCE = "main_toggle_wifi";
179b759516cbf26c9bb5f71c7f93e764fac73f48701Matthew Fritze
180055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman    /**
181055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman     * Tracks whether the user initiated a connection via clicking in order to autoscroll to the
182055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman     * network once connected.
183055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman     */
184055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman    private boolean mClickedConnect;
185055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman
18658b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa    /* End of "used in Wifi Setup context" */
18779470d1a66f479919c13e27546391439cafa0ee7Freeman Ng
188065cd4be6471084863c6560b8404dfdeaf4f2154Chia-chi Yeh    public WifiSettings() {
189e38ea38002290da8ed16f07ea45f0ebfb41b38cdGeoffrey Borggaard        super(DISALLOW_CONFIG_WIFI);
19048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
19148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
19248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
19328c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam    public void onViewCreated(View view, Bundle savedInstanceState) {
19428c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam        super.onViewCreated(view, savedInstanceState);
19528c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam        final Activity activity = getActivity();
19628c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam        if (activity != null) {
19762a3220efb7dfbae6d3ef6de68a07e3c07117ca1Fan Zhang            mProgressHeader = setPinnedHeaderView(R.layout.wifi_progress_header)
19862a3220efb7dfbae6d3ef6de68a07e3c07117ca1Fan Zhang                    .findViewById(R.id.progress_bar_animation);
199b77cbe5bb288b6814d57d18ad3a00b8414fbdab7Stephen Chen            setProgressBarVisible(false);
20028c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam        }
20128c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam    }
20228c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam
20328c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam    @Override
2047a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    public void onCreate(Bundle icicle) {
2057a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk        super.onCreate(icicle);
20607db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman
207bb46d23c8f0e264863457d2b7a342ac331558b04Sundeep Ghuman        // TODO(b/37429702): Add animations and preference comparator back after initial screen is
208bb46d23c8f0e264863457d2b7a342ac331558b04Sundeep Ghuman        // loaded (ODR).
209bb46d23c8f0e264863457d2b7a342ac331558b04Sundeep Ghuman        setAnimationAllowed(false);
210bb46d23c8f0e264863457d2b7a342ac331558b04Sundeep Ghuman
211a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        addPreferences();
212a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
213a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        mIsRestricted = isUiRestricted();
214a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
215a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
216a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        mBgThread.start();
217a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    }
218a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
219a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    private void addPreferences() {
22055dd8869db9c2a66423fcbb83cb5f5db38f8fb5fJason Monk        addPreferencesFromResource(R.xml.wifi_settings);
22141456785482f41e82e8fd1b2538749da73303980Stephen Chen
222f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        mConnectedAccessPointPreferenceCategory =
223f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman                (PreferenceCategory) findPreference(PREF_KEY_CONNECTED_ACCESS_POINTS);
22441456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAccessPointsPreferenceCategory =
22541456785482f41e82e8fd1b2538749da73303980Stephen Chen                (PreferenceCategory) findPreference(PREF_KEY_ACCESS_POINTS);
22641456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAdditionalSettingsPreferenceCategory =
22741456785482f41e82e8fd1b2538749da73303980Stephen Chen                (PreferenceCategory) findPreference(PREF_KEY_ADDITIONAL_SETTINGS);
2289d2712cd78514eb35ba1d480738841f3c203ecfdStephen Chen        mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
22941456785482f41e82e8fd1b2538749da73303980Stephen Chen        mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
23041456785482f41e82e8fd1b2538749da73303980Stephen Chen
23141456785482f41e82e8fd1b2538749da73303980Stephen Chen        Context prefContext = getPrefContext();
23241456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAddPreference = new Preference(prefContext);
233ebfbe5cfe64c0ac46a6812652c9139e674fc3056Jason Monk        mAddPreference.setIcon(R.drawable.ic_menu_add_inset);
2340b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mAddPreference.setTitle(R.string.wifi_add_network);
23541456785482f41e82e8fd1b2538749da73303980Stephen Chen        mStatusMessagePreference = new LinkablePreference(prefContext);
2360b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
23799b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler        mUserBadgeCache = new AccessPointPreference.UserBadgeCache(getPackageManager());
2387a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    }
2397a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk
2407a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    @Override
2417a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    public void onDestroy() {
2427a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk        mBgThread.quit();
2437a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk        super.onDestroy();
2447a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    }
2457a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk
2467a0b412ed25f35d7bb5660b44336b20f454cc438Jason Monk    @Override
2479c8bde576ab18fcfe653587f3e63604430c8791cDaisuke Miyakawa    public void onActivityCreated(Bundle savedInstanceState) {
248852cfa9335d8abbc164ae6a3b6b25e7da7e9a0fdRussell Brenner        super.onActivityCreated(savedInstanceState);
249852cfa9335d8abbc164ae6a3b6b25e7da7e9a0fdRussell Brenner
2502df0bf9d5cc160611099167625659db9d736036bSundeep Ghuman        mWifiTracker = WifiTrackerFactory.create(
2512df0bf9d5cc160611099167625659db9d736036bSundeep Ghuman                getActivity(), this, mBgThread.getLooper(), true, true, false);
252fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk        mWifiManager = mWifiTracker.getManager();
2538f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff
2548f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff        mConnectListener = new WifiManager.ActionListener() {
255eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                   @Override
2568f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   public void onSuccess() {
2578f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   }
258eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                   @Override
2598f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   public void onFailure(int reason) {
260001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       Activity activity = getActivity();
261001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       if (activity != null) {
262001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                           Toast.makeText(activity,
263001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                                R.string.wifi_failed_connect_message,
264001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                                Toast.LENGTH_SHORT).show();
265001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       }
2668f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   }
2678f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                               };
2688f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff
2698f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff        mSaveListener = new WifiManager.ActionListener() {
270eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                @Override
2718f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                public void onSuccess() {
2728f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                }
273eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                @Override
2748f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                public void onFailure(int reason) {
275001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                    Activity activity = getActivity();
276001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                    if (activity != null) {
277001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                        Toast.makeText(activity,
278001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                            R.string.wifi_failed_save_message,
279001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                            Toast.LENGTH_SHORT).show();
280001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                    }
2818f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                }
2828f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                            };
2838f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff
2848f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff        mForgetListener = new WifiManager.ActionListener() {
285eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                   @Override
2868f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   public void onSuccess() {
2878f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   }
288eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner                                   @Override
2898f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   public void onFailure(int reason) {
290001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       Activity activity = getActivity();
291001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       if (activity != null) {
292001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                           Toast.makeText(activity,
293001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                               R.string.wifi_failed_forget_message,
294001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                               Toast.LENGTH_SHORT).show();
295001e14a118e8d5c0fac5876439dc0356d7943eb7Wink Saville                                       }
2968f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                                   }
2978f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff                               };
2988f2f08e0eefaea2cff27beae9c43b3baac81dee6Irfan Sheriff
299fd19aff782bd382ae34900e5ce0f0b7410e3082fPauloftheWest        if (savedInstanceState != null) {
300771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam            mDialogMode = savedInstanceState.getInt(SAVE_DIALOG_MODE);
301fd19aff782bd382ae34900e5ce0f0b7410e3082fPauloftheWest            if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
3022e7162618dab134a5e3c60b5c128dc71ff635fddJeff Davidson                mAccessPointSavedState =
303fd19aff782bd382ae34900e5ce0f0b7410e3082fPauloftheWest                    savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
304fd19aff782bd382ae34900e5ce0f0b7410e3082fPauloftheWest            }
305aef794a2894e996fafa17097744a2bab75eb680dAndres Morales
306aef794a2894e996fafa17097744a2bab75eb680dAndres Morales            if (savedInstanceState.containsKey(SAVED_WIFI_NFC_DIALOG_STATE)) {
307aef794a2894e996fafa17097744a2bab75eb680dAndres Morales                mWifiNfcDialogSavedState =
308aef794a2894e996fafa17097744a2bab75eb680dAndres Morales                    savedInstanceState.getBundle(SAVED_WIFI_NFC_DIALOG_STATE);
309aef794a2894e996fafa17097744a2bab75eb680dAndres Morales            }
3103d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        }
31148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
3129f59de930ae14566673d4d57597b836556820b29Maurice Lam        // if we're supposed to enable/disable the Next button based on our current connection
3139f59de930ae14566673d4d57597b836556820b29Maurice Lam        // state, start it off in the right state
3149f59de930ae14566673d4d57597b836556820b29Maurice Lam        Intent intent = getActivity().getIntent();
3159f59de930ae14566673d4d57597b836556820b29Maurice Lam        mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
3169f59de930ae14566673d4d57597b836556820b29Maurice Lam
3179f59de930ae14566673d4d57597b836556820b29Maurice Lam        if (mEnableNextOnConnection) {
3189f59de930ae14566673d4d57597b836556820b29Maurice Lam            if (hasNextButton()) {
3199f59de930ae14566673d4d57597b836556820b29Maurice Lam                final ConnectivityManager connectivity = (ConnectivityManager)
3209f59de930ae14566673d4d57597b836556820b29Maurice Lam                        getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
3219f59de930ae14566673d4d57597b836556820b29Maurice Lam                if (connectivity != null) {
3229f59de930ae14566673d4d57597b836556820b29Maurice Lam                    NetworkInfo info = connectivity.getNetworkInfo(
3239f59de930ae14566673d4d57597b836556820b29Maurice Lam                            ConnectivityManager.TYPE_WIFI);
3249f59de930ae14566673d4d57597b836556820b29Maurice Lam                    changeNextButtonState(info.isConnected());
3259f59de930ae14566673d4d57597b836556820b29Maurice Lam                }
3269f59de930ae14566673d4d57597b836556820b29Maurice Lam            }
3279f59de930ae14566673d4d57597b836556820b29Maurice Lam        }
3289f59de930ae14566673d4d57597b836556820b29Maurice Lam
329d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner        registerForContextMenu(getListView());
330263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setHasOptionsMenu(true);
33136760c108b4b427da95afee23bc0ac7aeace9323Jason Monk
33236760c108b4b427da95afee23bc0ac7aeace9323Jason Monk        if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) {
33302f0a4aff3349513e69ea6697151c60088f75dcdJason Monk            mOpenSsid = intent.getStringExtra(EXTRA_START_CONNECT_SSID);
33436760c108b4b427da95afee23bc0ac7aeace9323Jason Monk        }
335263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    }
336263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
337263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    @Override
338138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    public void onDestroyView() {
339138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        super.onDestroyView();
340138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio
341138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        if (mWifiEnabler != null) {
342c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            mWifiEnabler.teardownSwitchController();
343138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        }
344138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    }
345138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio
346138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    @Override
347263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio    public void onStart() {
348263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        super.onStart();
349263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
350d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner        // On/off switch is hidden for Setup Wizard (returns null)
351d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner        mWifiEnabler = createWifiEnabler();
3529d12d101be7ed735b47a5214a98318d17e7a7563Zhihong Li
3539d12d101be7ed735b47a5214a98318d17e7a7563Zhihong Li        mWifiTracker.startTracking();
35460c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman
355f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        if (mIsRestricted) {
356a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks            restrictUi();
357f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen            return;
35887bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman        }
359f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen
360f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        onWifiStateChanged(mWifiManager.getWifiState());
36187bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman    }
36260c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman
363a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    private void restrictUi() {
364a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        if (!isUiRestrictedByOnlyAdmin()) {
365a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks            getEmptyTextView().setText(R.string.wifi_empty_list_user_restricted);
366a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        }
367a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        getPreferenceScreen().removeAll();
368a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    }
369a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
3704ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman    /**
3714ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     * Only update the AP list if there are not any APs currently shown.
3724ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     *
3734ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     * <p>Thus forceUpdate will only be called during cold start or when toggling between wifi on
3744ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     * and off. In other use cases, the previous APs will remain until the next update is received
3754ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     * from {@link WifiTracker}.
3764ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman     */
3774ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman    private void conditionallyForceUpdateAPs() {
3784ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman        if (mAccessPointsPreferenceCategory.getPreferenceCount() > 0
3794ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman                && mAccessPointsPreferenceCategory.getPreference(0) instanceof
3804ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman                        AccessPointPreference) {
3814ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman            // Make sure we don't update due to callbacks initiated by sticky broadcasts in
3824ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman            // WifiTracker.
3834ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman            Log.d(TAG, "Did not force update APs due to existing APs displayed");
3844ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman            getView().removeCallbacks(mUpdateAccessPointsRunnable);
3854ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman            return;
3864ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman        }
38787bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman        setProgressBarVisible(true);
38887bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman        mWifiTracker.forceUpdate();
389381987a8ce7fd979cc6438fe815d29712a9ce52aSundeep Ghuman        if (WifiTracker.sVerboseLogging) {
390381987a8ce7fd979cc6438fe815d29712a9ce52aSundeep Ghuman            Log.i(TAG, "WifiSettings force update APs: " + mWifiTracker.getAccessPoints());
39160c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman        }
39287bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman        getView().removeCallbacks(mUpdateAccessPointsRunnable);
39387bf49a43689dae7673f1aeb3a6498939b1117acSundeep Ghuman        updateAccessPointPreferences();
394d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner    }
395263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio
396d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner    /**
397d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner     * @return new WifiEnabler or null (as overridden by WifiSettingsForSetupWizard)
398d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner     */
3997e6df837ee23a62879d2e5182f923388d209ce56Fan Zhang    private WifiEnabler createWifiEnabler() {
400d3446c5708aff2580d4ddb9f559e72784bfac55eRussell Brenner        final SettingsActivity activity = (SettingsActivity) getActivity();
401c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling        return new WifiEnabler(activity, new SwitchBarController(activity.getSwitchBar()),
402c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            mMetricsFeatureProvider);
40348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
40448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
40548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
4069c8bde576ab18fcfe653587f3e63604430c8791cDaisuke Miyakawa    public void onResume() {
407d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio        final Activity activity = getActivity();
40848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        super.onResume();
409a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
410a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        // Because RestrictedSettingsFragment's onResume potentially requests authorization,
411a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        // which changes the restriction state, recalculate it.
412a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        final boolean alreadyImmutablyRestricted = mIsRestricted;
413a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        mIsRestricted = isUiRestricted();
414a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        if (!alreadyImmutablyRestricted && mIsRestricted) {
415a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks            restrictUi();
416a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        }
417a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
41848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        if (mWifiEnabler != null) {
419d25314d3305ed1a07b53991a978cd71219ef2a10Fabrice Di Meglio            mWifiEnabler.resume(activity);
42048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
42148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
42248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
42348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
4249c8bde576ab18fcfe653587f3e63604430c8791cDaisuke Miyakawa    public void onPause() {
42548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        super.onPause();
42648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        if (mWifiEnabler != null) {
42748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh            mWifiEnabler.pause();
42848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
4299d12d101be7ed735b47a5214a98318d17e7a7563Zhihong Li    }
430ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales
4319d12d101be7ed735b47a5214a98318d17e7a7563Zhihong Li    @Override
4329d12d101be7ed735b47a5214a98318d17e7a7563Zhihong Li    public void onStop() {
433fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk        mWifiTracker.stopTracking();
4345e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh        getView().removeCallbacks(mUpdateAccessPointsRunnable);
4355e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh        getView().removeCallbacks(mHideProgressBarRunnable);
4365e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh        super.onStop();
43748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
43848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
43948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
440a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    public void onActivityResult(int requestCode, int resultCode, Intent data) {
441a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        super.onActivityResult(requestCode, resultCode, data);
442a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
443a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        final boolean formerlyRestricted = mIsRestricted;
444a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        mIsRestricted = isUiRestricted();
445a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        if (formerlyRestricted && !mIsRestricted
446a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks                && getPreferenceScreen().getPreferenceCount() == 0) {
447a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks            // De-restrict the ui
448a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks            addPreferences();
449a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks        }
450a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    }
451a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks
452a0dd987d20f224a6419f0680fab232287e8c5aecChristine Franks    @Override
4536507613ebcd22e4691c2af92a5c161bd327db336Fan Zhang    public int getMetricsCategory() {
4549d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.WIFI;
4558a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
4568a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
4578a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
4583d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    public void onSaveInstanceState(Bundle outState) {
4593d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        super.onSaveInstanceState(outState);
4603d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
4613d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        // If the dialog is showing, save its state.
4623d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        if (mDialog != null && mDialog.isShowing()) {
463771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam            outState.putInt(SAVE_DIALOG_MODE, mDialogMode);
4643d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani            if (mDlgAccessPoint != null) {
4653d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani                mAccessPointSavedState = new Bundle();
4663d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani                mDlgAccessPoint.saveWifiState(mAccessPointSavedState);
4673d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani                outState.putBundle(SAVE_DIALOG_ACCESS_POINT_STATE, mAccessPointSavedState);
4683d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani            }
4693d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        }
470aef794a2894e996fafa17097744a2bab75eb680dAndres Morales
471aef794a2894e996fafa17097744a2bab75eb680dAndres Morales        if (mWifiToNfcDialog != null && mWifiToNfcDialog.isShowing()) {
472aef794a2894e996fafa17097744a2bab75eb680dAndres Morales            Bundle savedState = new Bundle();
473aef794a2894e996fafa17097744a2bab75eb680dAndres Morales            mWifiToNfcDialog.saveState(savedState);
474aef794a2894e996fafa17097744a2bab75eb680dAndres Morales            outState.putBundle(SAVED_WIFI_NFC_DIALOG_STATE, savedState);
475aef794a2894e996fafa17097744a2bab75eb680dAndres Morales        }
4763d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    }
4773d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
4783d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    @Override
47948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    public boolean onOptionsItemSelected(MenuItem item) {
4807e61c233061b4e87257a332be83f72f840d07c62Maggie Benthall        // If the user is not allowed to configure wifi, do not handle menu selections.
481f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        if (mIsRestricted) {
482f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen            return false;
483f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        }
4847e61c233061b4e87257a332be83f72f840d07c62Maggie Benthall
48548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        switch (item.getItemId()) {
48690380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            case MENU_ID_WPS_PBC:
48790380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                showDialog(WPS_PBC_DIALOG_ID);
48890380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                return true;
48990380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            case MENU_ID_WPS_PIN:
49090380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                showDialog(WPS_PIN_DIALOG_ID);
49190380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                return true;
49248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
49348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        return super.onOptionsItemSelected(item);
49448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
49548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
49648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
49748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
49839b467482d1bf256a111c757e9b7621c6f523271Jason Monk            Preference preference = (Preference) view.getTag();
49948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
50099b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler            if (preference instanceof LongPressAccessPointPreference) {
50199b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler                mSelectedAccessPoint =
50299b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler                        ((LongPressAccessPointPreference) preference).getAccessPoint();
503fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                menu.setHeaderTitle(mSelectedAccessPoint.getSsid());
504fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                if (mSelectedAccessPoint.isConnectable()) {
505fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                    menu.add(Menu.NONE, MENU_ID_CONNECT, 0, R.string.wifi_menu_connect);
5065ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                }
507b46a9194fe88a26e80d415919d191703c340925eJeff Davidson
50889403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban                WifiConfiguration config = mSelectedAccessPoint.getConfig();
5091a2aa9942f2c14bdaf46ce4911f9838dc21652baZoltan Szatmary-Ban                // Some configs are ineditable
5101a2aa9942f2c14bdaf46ce4911f9838dc21652baZoltan Szatmary-Ban                if (isEditabilityLockedDown(getActivity(), config)) {
51189403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban                    return;
51289403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban                }
51389403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban
514fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                if (mSelectedAccessPoint.isSaved() || mSelectedAccessPoint.isEphemeral()) {
51506b65d1c7d5f5c87f8d723b6ab0040f9bfc834c4Jeff Davidson                    // Allow forgetting a network if either the network is saved or ephemerally
51606b65d1c7d5f5c87f8d723b6ab0040f9bfc834c4Jeff Davidson                    // connected. (In the latter case, "forget" blacklists the network so it won't
51706b65d1c7d5f5c87f8d723b6ab0040f9bfc834c4Jeff Davidson                    // be used again, ephemerally).
5185ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                    menu.add(Menu.NONE, MENU_ID_FORGET, 0, R.string.wifi_menu_forget);
51948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                }
520fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                if (mSelectedAccessPoint.isSaved()) {
521f027ce5e0223799f98f6d297b40ab57c8a7a8b5cIrfan Sheriff                    menu.add(Menu.NONE, MENU_ID_MODIFY, 0, R.string.wifi_menu_modify);
5222e75203a6ecb81725b68bced6c5f8d5be37365f1Andres Morales                    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
52364c7429ac84e2f041e64fb7094f390b7491472dcAndres Morales                    if (nfcAdapter != null && nfcAdapter.isEnabled() &&
524fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                            mSelectedAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE) {
525ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales                        // Only allow writing of NFC tags for password-protected networks.
526ae04155bbe875279d6637ffa5dda974b9af019faAndres Morales                        menu.add(Menu.NONE, MENU_ID_WRITE_NFC, 0, R.string.wifi_menu_write_to_nfc);
527ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales                    }
52848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                }
52948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh            }
53048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
53148090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
53248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
53348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    public boolean onContextItemSelected(MenuItem item) {
534d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa        if (mSelectedAccessPoint == null) {
53548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh            return super.onContextItemSelected(item);
53648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
53748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        switch (item.getItemId()) {
538d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            case MENU_ID_CONNECT: {
539d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                boolean isSavedNetwork = mSelectedAccessPoint.isSaved();
540d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                if (isSavedNetwork) {
541d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                    connect(mSelectedAccessPoint.getConfig(), isSavedNetwork);
542b5f4a2c86ea6ca3abe91362afc3d3aaa6ad0721fSanket Padawe                } else if (mSelectedAccessPoint.getSecurity() == AccessPoint.SECURITY_NONE) {
54322d441acdba0fb7b99f5958ddd0198f4c371b2a7Isaac Levy                    /** Bypass dialog for unsecured networks */
54422d441acdba0fb7b99f5958ddd0198f4c371b2a7Isaac Levy                    mSelectedAccessPoint.generateOpenNetworkConfig();
545d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                    connect(mSelectedAccessPoint.getConfig(), isSavedNetwork);
54648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                } else {
547771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam                    showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_CONNECT);
54848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                }
54948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                return true;
550d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            }
551d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            case MENU_ID_FORGET: {
5525ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                forget();
55348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                return true;
554d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            }
555d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            case MENU_ID_MODIFY: {
556771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam                showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_MODIFY);
55748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh                return true;
558d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa            }
559ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales            case MENU_ID_WRITE_NFC:
560ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales                showDialog(WRITE_NFC_DIALOG_ID);
561ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales                return true;
562ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales
56348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
56448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        return super.onContextItemSelected(item);
56548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
56648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
56748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    @Override
56839b467482d1bf256a111c757e9b7621c6f523271Jason Monk    public boolean onPreferenceTreeClick(Preference preference) {
56907db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        // If the preference has a fragment set, open that
57007db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        if (preference.getFragment() != null) {
57107db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman            preference.setOnPreferenceClickListener(null);
57207db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman            return super.onPreferenceTreeClick(preference);
57307db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        }
57407db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman
57599b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler        if (preference instanceof LongPressAccessPointPreference) {
57699b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler            mSelectedAccessPoint = ((LongPressAccessPointPreference) preference).getAccessPoint();
57773df1a9894886da01a9bba352d9df9e7f2b91a7dJason Monk            if (mSelectedAccessPoint == null) {
57873df1a9894886da01a9bba352d9df9e7f2b91a7dJason Monk                return false;
57973df1a9894886da01a9bba352d9df9e7f2b91a7dJason Monk            }
580b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen            if (mSelectedAccessPoint.isActive()) {
581b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen                return super.onPreferenceTreeClick(preference);
582b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen            }
58336b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu            /**
58436b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu             * Bypass dialog and connect to unsecured networks, or previously connected saved
58536b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu             * networks, or Passpoint provided networks.
58636b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu             */
587b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen            WifiConfiguration config = mSelectedAccessPoint.getConfig();
588b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen            if (mSelectedAccessPoint.getSecurity() == AccessPoint.SECURITY_NONE) {
58922d441acdba0fb7b99f5958ddd0198f4c371b2a7Isaac Levy                mSelectedAccessPoint.generateOpenNetworkConfig();
590b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen                connect(mSelectedAccessPoint.getConfig(), mSelectedAccessPoint.isSaved());
591b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen            } else if (mSelectedAccessPoint.isSaved() && config != null
592b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen                    && config.getNetworkSelectionStatus() != null
593b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen                    && config.getNetworkSelectionStatus().getHasEverConnected()) {
594b19b8f37c95e26c8b321453e84f37f92c0b8106fStephen Chen                connect(config, true /* isSavedNetwork */);
59536b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu            } else if (mSelectedAccessPoint.isPasspoint()) {
59636b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu                // Access point provided by an installed Passpoint provider, connect using
59736b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu                // the associated config.
59836b68df4d4d69534254d5bb35d1b424620db854bPeter Qiu                connect(config, true /* isSavedNetwork */);
5993bf0db8d10fee87c6c2cb7f012dcf97d1ce458f3Sanket Padawe            } else {
600771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam                showDialog(mSelectedAccessPoint, WifiConfigUiBase.MODE_CONNECT);
60122d441acdba0fb7b99f5958ddd0198f4c371b2a7Isaac Levy            }
6020b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        } else if (preference == mAddPreference) {
6030b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            onAddNetworkPressed();
60448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        } else {
60539b467482d1bf256a111c757e9b7621c6f523271Jason Monk            return super.onPreferenceTreeClick(preference);
60648090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
60748090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        return true;
60848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
60948090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
610771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam    private void showDialog(AccessPoint accessPoint, int dialogMode) {
6116221467fae3f8ee073095d3ea590f2b390dee164Zoltan Szatmary-Ban        if (accessPoint != null) {
6126221467fae3f8ee073095d3ea590f2b390dee164Zoltan Szatmary-Ban            WifiConfiguration config = accessPoint.getConfig();
6131a2aa9942f2c14bdaf46ce4911f9838dc21652baZoltan Szatmary-Ban            if (isEditabilityLockedDown(getActivity(), config) && accessPoint.isActive()) {
614e2b2c3ffe43da02317c7a1c80a5ec47cd1dc3ee8Sudheer Shanka                RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getActivity(),
615e2b2c3ffe43da02317c7a1c80a5ec47cd1dc3ee8Sudheer Shanka                        RestrictedLockUtils.getDeviceOwner(getActivity()));
6166221467fae3f8ee073095d3ea590f2b390dee164Zoltan Szatmary-Ban                return;
61789403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban            }
61889403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        }
61989403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban
62048090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        if (mDialog != null) {
6213d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani            removeDialog(WIFI_DIALOG_ID);
6223d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani            mDialog = null;
6233d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        }
6243d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
6253d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        // Save the access point and edit mode
6263d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        mDlgAccessPoint = accessPoint;
627771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam        mDialogMode = dialogMode;
6283d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
6293d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani        showDialog(WIFI_DIALOG_ID);
6303d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    }
6313d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani
6323d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    @Override
6333d13110d40b972a44c80558e0179323f9b330b2fAmith Yamasani    public Dialog onCreateDialog(int dialogId) {
63490380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        switch (dialogId) {
63590380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            case WIFI_DIALOG_ID:
636bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                if (mDlgAccessPoint == null && mAccessPointSavedState == null) {
637bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                    // add new network
638bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                    mDialog = WifiDialog
639bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                            .createFullscreen(getActivity(), this, mDlgAccessPoint, mDialogMode);
640bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                } else {
641bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                    // modify network
642bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                    if (mDlgAccessPoint == null) {
643bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                        // restore AP from save state
644bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                        mDlgAccessPoint = new AccessPoint(getActivity(), mAccessPointSavedState);
6452dcf6083f77ea8abc200dc87eed22b8bbb45fd07Birdson_Chen                        // Reset the saved access point data
6462dcf6083f77ea8abc200dc87eed22b8bbb45fd07Birdson_Chen                        mAccessPointSavedState = null;
64790380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                    }
648bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                    mDialog = WifiDialog
649bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                            .createModal(getActivity(), this, mDlgAccessPoint, mDialogMode);
65090380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                }
651bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach
652bc1cf7e2608e6cd0e03372975ded5f7e61e6dac9Eric Schwarzenbach                mSelectedAccessPoint = mDlgAccessPoint;
65390380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                return mDialog;
65490380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            case WPS_PBC_DIALOG_ID:
65590380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                return new WpsDialog(getActivity(), WpsInfo.PBC);
65690380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            case WPS_PIN_DIALOG_ID:
65790380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff                return new WpsDialog(getActivity(), WpsInfo.DISPLAY);
658ef7a40a0d65a4d3e43d454273c6334448158e555Andres Morales            case WRITE_NFC_DIALOG_ID:
659ae04155bbe875279d6637ffa5dda974b9af019faAndres Morales                if (mSelectedAccessPoint != null) {
660ae04155bbe875279d6637ffa5dda974b9af019faAndres Morales                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(
661bef3c6ddfd91fb9845c134f83d0cadc35254236eAmin Shaikh                            getActivity(),
662aef794a2894e996fafa17097744a2bab75eb680dAndres Morales                            mSelectedAccessPoint.getSecurity(),
663bef3c6ddfd91fb9845c134f83d0cadc35254236eAmin Shaikh                            new WifiManagerWrapper(mWifiManager));
664aef794a2894e996fafa17097744a2bab75eb680dAndres Morales                } else if (mWifiNfcDialogSavedState != null) {
665bef3c6ddfd91fb9845c134f83d0cadc35254236eAmin Shaikh                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(getActivity(),
666bef3c6ddfd91fb9845c134f83d0cadc35254236eAmin Shaikh                            mWifiNfcDialogSavedState, new WifiManagerWrapper(mWifiManager));
667ae04155bbe875279d6637ffa5dda974b9af019faAndres Morales                }
66888c9f2889fafb53a2875418317ecd197d9aae883Russell Brenner
669aef794a2894e996fafa17097744a2bab75eb680dAndres Morales                return mWifiToNfcDialog;
67090380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        }
67190380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        return super.onCreateDialog(dialogId);
67248090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh    }
67348090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
674d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang    @Override
675d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang    public int getDialogMetricsCategory(int dialogId) {
676d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang        switch (dialogId) {
677d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang            case WIFI_DIALOG_ID:
678d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang                return MetricsEvent.DIALOG_WIFI_AP_EDIT;
679d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang            case WPS_PBC_DIALOG_ID:
680d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang                return MetricsEvent.DIALOG_WIFI_PBC;
681d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang            case WPS_PIN_DIALOG_ID:
682d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang                return MetricsEvent.DIALOG_WIFI_PIN;
683d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang            case WRITE_NFC_DIALOG_ID:
684d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang                return MetricsEvent.DIALOG_WIFI_WRITE_NFC;
685d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang            default:
686d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang                return 0;
687d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang        }
688d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang    }
689d65184faef1076280e121c5cbc1db863f3f37924Fan Zhang
690d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    /**
69119b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     * Called to indicate the list of AccessPoints has been updated and
69219b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     * getAccessPoints should be called to get the latest information.
693d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa     */
694fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    @Override
695fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    public void onAccessPointsChanged() {
6964ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman        Log.d(TAG, "onAccessPointsChanged (WifiTracker) callback initiated");
69719b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        updateAccessPointsDelayed();
69819b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    }
69919b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen
70019b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    /**
70119b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     * Updates access points from {@link WifiManager#getScanResults()}. Adds a delay to have
70219b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     * progress bar displayed before starting to modify APs.
70319b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     */
70419b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    private void updateAccessPointsDelayed() {
7059da80a90bec430c52f78d6a951687a208d311d9eAmith Yamasani        // Safeguard from some delayed event handling
706f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        if (getActivity() != null && !mIsRestricted && mWifiManager.isWifiEnabled()) {
70719b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen            setProgressBarVisible(true);
70819b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen            getView().postDelayed(mUpdateAccessPointsRunnable, 300 /* delay milliseconds */);
70919b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        }
71019b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    }
71119b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen
71219b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    /** Called when the state of Wifi has changed. */
71319b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    @Override
71419b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    public void onWifiStateChanged(int state) {
715f5d02aa7659ce0e0ea1057acbbe26468feaa137fStephen Chen        if (mIsRestricted) {
7167e61c233061b4e87257a332be83f72f840d07c62Maggie Benthall            return;
7177e61c233061b4e87257a332be83f72f840d07c62Maggie Benthall        }
718e78c1879058571c00879f0d3e756951302adf513Gilles Debunne
71919b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        final int wifiState = mWifiManager.getWifiState();
720e78c1879058571c00879f0d3e756951302adf513Gilles Debunne        switch (wifiState) {
721e78c1879058571c00879f0d3e756951302adf513Gilles Debunne            case WifiManager.WIFI_STATE_ENABLED:
7224ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman                conditionallyForceUpdateAPs();
723e78c1879058571c00879f0d3e756951302adf513Gilles Debunne                break;
72486eaa09aa42e3281ed6c8ea85e09d4875973c4bfDaisuke Miyakawa
72518e9992a5e5f515566aa9ea861b2e1a3c2450ebcGilles Debunne            case WifiManager.WIFI_STATE_ENABLING:
726f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman                removeConnectedAccessPointPreference();
72741456785482f41e82e8fd1b2538749da73303980Stephen Chen                mAccessPointsPreferenceCategory.removeAll();
72819b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen                addMessagePreference(R.string.wifi_starting);
72946a8e6c143604213aa6b1efa4fadbd6d542b3223Maurice Lam                setProgressBarVisible(true);
73018e9992a5e5f515566aa9ea861b2e1a3c2450ebcGilles Debunne                break;
73118e9992a5e5f515566aa9ea861b2e1a3c2450ebcGilles Debunne
732e78c1879058571c00879f0d3e756951302adf513Gilles Debunne            case WifiManager.WIFI_STATE_DISABLING:
73319b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen                removeConnectedAccessPointPreference();
73419b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen                mAccessPointsPreferenceCategory.removeAll();
735e78c1879058571c00879f0d3e756951302adf513Gilles Debunne                addMessagePreference(R.string.wifi_stopping);
736e78c1879058571c00879f0d3e756951302adf513Gilles Debunne                break;
737e78c1879058571c00879f0d3e756951302adf513Gilles Debunne
738e78c1879058571c00879f0d3e756951302adf513Gilles Debunne            case WifiManager.WIFI_STATE_DISABLED:
7397e59f73e9cc3d93b8baa1c3e4aa59347d859dfb4Robert Greenwalt                setOffMessage();
740015c7706e49100870ab0f3786e4e2e0de9e641abFan Zhang                setAdditionalSettingsSummaries();
74146a8e6c143604213aa6b1efa4fadbd6d542b3223Maurice Lam                setProgressBarVisible(false);
742e78c1879058571c00879f0d3e756951302adf513Gilles Debunne                break;
743d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa        }
744d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    }
74548090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh
74619b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    /**
74745bb3a1a77d0c548b5112ea7bfce48bab7361a07Sundeep Ghuman     * Called when the connection state of wifi has changed.
74819b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen     */
74919b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    @Override
75019b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    public void onConnectedChanged() {
75119b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        changeNextButtonState(mWifiTracker.isConnected());
75219b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen    }
75319b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen
754c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    /** Helper method to return whether an AccessPoint is disabled due to a wrong password */
755c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    private static boolean isDisabledByWrongPassword(AccessPoint accessPoint) {
756c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        WifiConfiguration config = accessPoint.getConfig();
757c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        if (config == null) {
758c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent            return false;
759c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        }
760c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        WifiConfiguration.NetworkSelectionStatus networkStatus =
761c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent                config.getNetworkSelectionStatus();
762c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        if (networkStatus == null || networkStatus.isNetworkEnabled()) {
763c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent            return false;
764c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        }
765c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        int reason = networkStatus.getNetworkSelectionDisableReason();
766c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent        return WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD == reason;
767c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent    }
768c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent
769f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman    private void updateAccessPointPreferences() {
77019b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        // in case state has changed
77119b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        if (!mWifiManager.isWifiEnabled()) {
77219b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen            return;
77319b74d92139ee4cd47afe07685bcceddec190d13Stephen Chen        }
774f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        // AccessPoints are sorted by the WifiTracker
775f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        final List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
776381987a8ce7fd979cc6438fe815d29712a9ce52aSundeep Ghuman        if (WifiTracker.sVerboseLogging) {
777381987a8ce7fd979cc6438fe815d29712a9ce52aSundeep Ghuman            Log.i(TAG, "updateAccessPoints called for: " + accessPoints);
7784ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman        }
779f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman
780f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        boolean hasAvailableAccessPoints = false;
781f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        mAccessPointsPreferenceCategory.removePreference(mStatusMessagePreference);
782f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        cacheRemoveAllPrefs(mAccessPointsPreferenceCategory);
783f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman
784f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        int index =
785f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                configureConnectedAccessPointPreferenceCategory(accessPoints) ? 1 : 0;
78611478573ec0ffd02cb3812d9c2a87c31893c3846Stephen Chen        int numAccessPoints = accessPoints.size();
78711478573ec0ffd02cb3812d9c2a87c31893c3846Stephen Chen        for (; index < numAccessPoints; index++) {
788f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            AccessPoint accessPoint = accessPoints.get(index);
789f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            // Ignore access points that are out of range.
790f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            if (accessPoint.isReachable()) {
79114cc864122dae1b515c92fbade644d2a1ce27e71Sundeep Ghuman                String key = AccessPointPreference.generatePreferenceKey(accessPoint);
792f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                hasAvailableAccessPoints = true;
793f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                LongPressAccessPointPreference pref =
794f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                        (LongPressAccessPointPreference) getCachedPreference(key);
795f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                if (pref != null) {
796f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                    pref.setOrder(index);
797f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                    continue;
798f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                }
799f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                LongPressAccessPointPreference preference =
800f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                        createLongPressActionPointPreference(accessPoint);
801f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                preference.setKey(key);
802f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                preference.setOrder(index);
803f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr())
804f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                        && accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) {
805c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent                    if (!accessPoint.isSaved() || isDisabledByWrongPassword(accessPoint)) {
806c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent                        onPreferenceTreeClick(preference);
807c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent                        mOpenSsid = null;
808c00fedc6395dfe852b389df1e5ad87f015e43360Antony Sargent                    }
809f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                }
810f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                mAccessPointsPreferenceCategory.addPreference(preference);
811f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                accessPoint.setListener(WifiSettings.this);
812f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman                preference.refresh();
813f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            }
814f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        }
815f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        removeCachedPrefs(mAccessPointsPreferenceCategory);
81611478573ec0ffd02cb3812d9c2a87c31893c3846Stephen Chen        mAddPreference.setOrder(index);
81711478573ec0ffd02cb3812d9c2a87c31893c3846Stephen Chen        mAccessPointsPreferenceCategory.addPreference(mAddPreference);
81860c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman        setAdditionalSettingsSummaries();
819f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman
820f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        if (!hasAvailableAccessPoints) {
821f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            setProgressBarVisible(true);
822f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            Preference pref = new Preference(getPrefContext());
823f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            pref.setSelectable(false);
824f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            pref.setSummary(R.string.wifi_empty_list_wifi_on);
825f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            pref.setOrder(index++);
826f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            pref.setKey(PREF_KEY_EMPTY_WIFI_LIST);
827f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            mAccessPointsPreferenceCategory.addPreference(pref);
828f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        } else {
829f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman            // Continuing showing progress bar for an additional delay to overlap with animation
8305e0c60d2ac8d0ede4f372a85e700de2a3098b2cfAmin Shaikh            getView().postDelayed(mHideProgressBarRunnable, 1700 /* delay millis */);
831f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman        }
832f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman    }
833f35a55dee468be819b924b1f8cfb575d5f74c4a9Sundeep Ghuman
834f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    @NonNull
835f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private LongPressAccessPointPreference createLongPressActionPointPreference(
836f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            AccessPoint accessPoint) {
837f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        return new LongPressAccessPointPreference(accessPoint, getPrefContext(), mUserBadgeCache,
838f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman                false, R.drawable.ic_wifi_signal_0, this);
839f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    }
840f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
841f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    /**
842f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     * Configure the ConnectedAccessPointPreferenceCategory and return true if the Category was
843f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     * shown.
844f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     */
845f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private boolean configureConnectedAccessPointPreferenceCategory(
846f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            List<AccessPoint> accessPoints) {
847f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        if (accessPoints.size() == 0) {
848f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            removeConnectedAccessPointPreference();
849f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            return false;
850f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        }
851f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
852f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        AccessPoint connectedAp = accessPoints.get(0);
853f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        if (!connectedAp.isActive()) {
854f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            removeConnectedAccessPointPreference();
855f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            return false;
856f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        }
857f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
858f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        // Is the preference category empty?
859f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        if (mConnectedAccessPointPreferenceCategory.getPreferenceCount() == 0) {
860f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            addConnectedAccessPointPreference(connectedAp);
861f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            return true;
862f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        }
863f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
864f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        // Is the previous currently connected SSID different from the new one?
86569fa52807416ba349d3fd6d7c8811742f5dd74e2Eric Schwarzenbach        AccessPointPreference preference = (AccessPointPreference)
86669fa52807416ba349d3fd6d7c8811742f5dd74e2Eric Schwarzenbach            (mConnectedAccessPointPreferenceCategory.getPreference(0));
86769fa52807416ba349d3fd6d7c8811742f5dd74e2Eric Schwarzenbach        // The AccessPoints need to be the same reference to ensure that updates are reflected
86869fa52807416ba349d3fd6d7c8811742f5dd74e2Eric Schwarzenbach        // in the UI.
86969fa52807416ba349d3fd6d7c8811742f5dd74e2Eric Schwarzenbach        if (preference.getAccessPoint() != connectedAp) {
870f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            removeConnectedAccessPointPreference();
871f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            addConnectedAccessPointPreference(connectedAp);
872f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman            return true;
873f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        }
874f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
87507db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        // Else same AP is connected, simply refresh the connected access point preference
87607db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        // (first and only access point in this category).
87707db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        ((LongPressAccessPointPreference) mConnectedAccessPointPreferenceCategory.getPreference(0))
87807db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman                .refresh();
879f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        return true;
880f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    }
881f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
882f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    /**
883f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     * Creates a Preference for the given {@link AccessPoint} and adds it to the
884f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     * {@link #mConnectedAccessPointPreferenceCategory}.
885f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman     */
886f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private void addConnectedAccessPointPreference(AccessPoint connectedAp) {
8875a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        final LongPressAccessPointPreference pref = getOrCreatePreference(connectedAp);
88807db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman
88907db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        // Save the state of the current access point in the bundle so that we can restore it
89007db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        // in the Wifi Network Details Fragment
89107db40434b7ba496e473826e21e2e959fea365e5Sundeep Ghuman        pref.getAccessPoint().saveWifiState(pref.getExtras());
8925a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach
8935a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        // Launch details page on click.
8945a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        pref.setOnPreferenceClickListener(preference -> {
8955a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach            SettingsActivity activity = (SettingsActivity) WifiSettings.this.getActivity();
8965a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach            activity.startPreferencePanel(this,
8975a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach                    WifiNetworkDetailsFragment.class.getName(), pref.getExtras(),
8985a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach                    R.string.wifi_details_title, null, null, 0);
8995a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach            return true;
9005a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        });
9010b61e3028c0617e7f39ac821d49f00166cfaad0bSundeep Ghuman        pref.refresh();
90260c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman
903f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        mConnectedAccessPointPreferenceCategory.addPreference(pref);
904f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        mConnectedAccessPointPreferenceCategory.setVisible(true);
905055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman        if (mClickedConnect) {
906055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman            mClickedConnect = false;
907055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman            scrollToPreference(mConnectedAccessPointPreferenceCategory);
908055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman        }
909f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    }
910f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
9115a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach    private LongPressAccessPointPreference getOrCreatePreference(AccessPoint ap) {
9125a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        LongPressAccessPointPreference pref = (LongPressAccessPointPreference)
9135a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach                getCachedPreference(AccessPointPreference.generatePreferenceKey(ap));
9145a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        if (pref == null) {
9155a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach            pref = createLongPressActionPointPreference(ap);
9165a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        }
9175a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach        return pref;
9185a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach    }
9195a7e46dd800dac5ce863b5b337592bca21dc0c2bEric Schwarzenbach
920f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    /** Removes all preferences and hide the {@link #mConnectedAccessPointPreferenceCategory}. */
921f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    private void removeConnectedAccessPointPreference() {
922f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        mConnectedAccessPointPreferenceCategory.removeAll();
923f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        mConnectedAccessPointPreferenceCategory.setVisible(false);
924f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman    }
925f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman
92660c1d4ec6498a026fca50fdc8c9ef7d31458251cSundeep Ghuman    private void setAdditionalSettingsSummaries() {
9279d2712cd78514eb35ba1d480738841f3c203ecfdStephen Chen        mAdditionalSettingsPreferenceCategory.addPreference(mConfigureWifiSettingsPreference);
928a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen        final int defaultWakeupAvailable = getResources().getInteger(
929a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen                com.android.internal.R.integer.config_wifi_wakeup_available);
930a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen        boolean wifiWakeupAvailable = Settings.Global.getInt(
931a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen                getContentResolver(), Settings.Global.WIFI_WAKEUP_AVAILABLE, defaultWakeupAvailable)
932a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen                == 1;
933a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen        if (wifiWakeupAvailable) {
9348ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen            mConfigureWifiSettingsPreference.setSummary(getString(
9358ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                    isWifiWakeupEnabled()
936a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen                    ? R.string.wifi_configure_settings_preference_summary_wakeup_on
937a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen                    : R.string.wifi_configure_settings_preference_summary_wakeup_off));
938a99b86c56a060179b1d90937ebfba897778eaa46Stephen Chen        }
93904cae3b66a794e37f56398727d3e0cbfd348ed8aAmin Shaikh        int numSavedNetworks = mWifiTracker.getNumSavedNetworks();
94004cae3b66a794e37f56398727d3e0cbfd348ed8aAmin Shaikh        if (numSavedNetworks > 0) {
94141456785482f41e82e8fd1b2538749da73303980Stephen Chen            mAdditionalSettingsPreferenceCategory.addPreference(mSavedNetworksPreference);
94204cae3b66a794e37f56398727d3e0cbfd348ed8aAmin Shaikh            mSavedNetworksPreference.setSummary(
94304cae3b66a794e37f56398727d3e0cbfd348ed8aAmin Shaikh                    getResources().getQuantityString(R.plurals.wifi_saved_access_points_summary,
94404cae3b66a794e37f56398727d3e0cbfd348ed8aAmin Shaikh                            numSavedNetworks, numSavedNetworks));
94541456785482f41e82e8fd1b2538749da73303980Stephen Chen        } else {
94641456785482f41e82e8fd1b2538749da73303980Stephen Chen            mAdditionalSettingsPreferenceCategory.removePreference(mSavedNetworksPreference);
94741456785482f41e82e8fd1b2538749da73303980Stephen Chen        }
94841456785482f41e82e8fd1b2538749da73303980Stephen Chen    }
94941456785482f41e82e8fd1b2538749da73303980Stephen Chen
9508ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen    private boolean isWifiWakeupEnabled() {
9518ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
9528ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen        ContentResolver contentResolver = getContentResolver();
9538ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen        return Settings.Global.getInt(contentResolver,
9548ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                        Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1
9558ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                && Settings.Global.getInt(contentResolver,
9568ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                        Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1
9578ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                && Settings.Global.getInt(contentResolver,
9588ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                        Settings.Global.AIRPLANE_MODE_ON, 0) == 0
9598ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                && Settings.Global.getInt(contentResolver,
9608ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                        Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1
9618ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen                && !powerManager.isPowerSaveMode();
9628ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen    }
9638ffcbccbce2dea0e5ba23137d9098fb6dd938e59Stephen Chen
9647e59f73e9cc3d93b8baa1c3e4aa59347d859dfb4Robert Greenwalt    private void setOffMessage() {
9650820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        final CharSequence title = getText(R.string.wifi_empty_list_wifi_off);
96655601622d5ddd9d9ef0ace09ade96e999879bff7Lifu Tang        // Don't use WifiManager.isScanAlwaysAvailable() to check the Wi-Fi scanning mode. Instead,
96755601622d5ddd9d9ef0ace09ade96e999879bff7Lifu Tang        // read the system settings directly. Because when the device is in Airplane mode, even if
96855601622d5ddd9d9ef0ace09ade96e999879bff7Lifu Tang        // Wi-Fi scanning mode is on, WifiManager.isScanAlwaysAvailable() still returns "off".
9690820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        final boolean wifiScanningMode = Settings.Global.getInt(getActivity().getContentResolver(),
9700820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1;
9710820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        final CharSequence description = wifiScanningMode ? getText(R.string.wifi_scan_notify_text)
9720820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                : getText(R.string.wifi_scan_notify_text_scanning_off);
9730820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        final LinkifyUtils.OnClickListener clickListener = new LinkifyUtils.OnClickListener() {
9740820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh            @Override
9750820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh            public void onClick() {
9760820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                final SettingsActivity activity = (SettingsActivity) getActivity();
9770820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                activity.startPreferencePanel(WifiSettings.this,
9780820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                        ScanningSettings.class.getName(),
9790820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh                        null, R.string.location_scanning_screen_title, null, null, 0);
9800820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh            }
9810820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        };
9820820ee350f7dd6609b51bc0322d600b966e938a7Amin Shaikh        mStatusMessagePreference.setText(title, description, clickListener);
983f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        removeConnectedAccessPointPreference();
98441456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAccessPointsPreferenceCategory.removeAll();
98541456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAccessPointsPreferenceCategory.addPreference(mStatusMessagePreference);
9867e59f73e9cc3d93b8baa1c3e4aa59347d859dfb4Robert Greenwalt    }
9877e59f73e9cc3d93b8baa1c3e4aa59347d859dfb4Robert Greenwalt
988e78c1879058571c00879f0d3e756951302adf513Gilles Debunne    private void addMessagePreference(int messageId) {
98941456785482f41e82e8fd1b2538749da73303980Stephen Chen        mStatusMessagePreference.setTitle(messageId);
990f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman        removeConnectedAccessPointPreference();
99141456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAccessPointsPreferenceCategory.removeAll();
99241456785482f41e82e8fd1b2538749da73303980Stephen Chen        mAccessPointsPreferenceCategory.addPreference(mStatusMessagePreference);
993e78c1879058571c00879f0d3e756951302adf513Gilles Debunne    }
994e78c1879058571c00879f0d3e756951302adf513Gilles Debunne
99546a8e6c143604213aa6b1efa4fadbd6d542b3223Maurice Lam    protected void setProgressBarVisible(boolean visible) {
99628c3f6bb71da1727fdc39bdc622518b1daeb3b0aMaurice Lam        if (mProgressHeader != null) {
99762a3220efb7dfbae6d3ef6de68a07e3c07117ca1Fan Zhang            mProgressHeader.setVisibility(visible ? View.VISIBLE : View.GONE);
99848090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh        }
999fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    }
1000e78c1879058571c00879f0d3e756951302adf513Gilles Debunne
10019f59de930ae14566673d4d57597b836556820b29Maurice Lam    /**
10029f59de930ae14566673d4d57597b836556820b29Maurice Lam     * Renames/replaces "Next" button when appropriate. "Next" button usually exists in
10039f59de930ae14566673d4d57597b836556820b29Maurice Lam     * Wifi setup screens, not in usual wifi settings screen.
10049f59de930ae14566673d4d57597b836556820b29Maurice Lam     *
10059f59de930ae14566673d4d57597b836556820b29Maurice Lam     * @param enabled true when the device is connected to a wifi network.
10069f59de930ae14566673d4d57597b836556820b29Maurice Lam     */
10079f59de930ae14566673d4d57597b836556820b29Maurice Lam    private void changeNextButtonState(boolean enabled) {
10089f59de930ae14566673d4d57597b836556820b29Maurice Lam        if (mEnableNextOnConnection && hasNextButton()) {
10099f59de930ae14566673d4d57597b836556820b29Maurice Lam            getNextButton().setEnabled(enabled);
10109f59de930ae14566673d4d57597b836556820b29Maurice Lam        }
10119f59de930ae14566673d4d57597b836556820b29Maurice Lam    }
10129f59de930ae14566673d4d57597b836556820b29Maurice Lam
1013eca454ebc208c1b0d0c28dfa7669ee1cd8233d29Russell Brenner    @Override
10142662df843327dd938225c75722d0bbf3b01068f7Maurice Lam    public void onForget(WifiDialog dialog) {
10152662df843327dd938225c75722d0bbf3b01068f7Maurice Lam        forget();
10162662df843327dd938225c75722d0bbf3b01068f7Maurice Lam    }
10172662df843327dd938225c75722d0bbf3b01068f7Maurice Lam
10182662df843327dd938225c75722d0bbf3b01068f7Maurice Lam    @Override
10192662df843327dd938225c75722d0bbf3b01068f7Maurice Lam    public void onSubmit(WifiDialog dialog) {
10202662df843327dd938225c75722d0bbf3b01068f7Maurice Lam        if (mDialog != null) {
10212662df843327dd938225c75722d0bbf3b01068f7Maurice Lam            submit(mDialog.getController());
1022d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa        }
102358b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa    }
1024d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa
102558b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa    /* package */ void submit(WifiConfigController configController) {
1026b3024fa6a0359f18b93fd8aaf197fce59f806edeIrfan Sheriff
102790380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        final WifiConfiguration config = configController.getConfig();
102890380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff
102990380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        if (config == null) {
103090380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            if (mSelectedAccessPoint != null
1031fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                    && mSelectedAccessPoint.isSaved()) {
1032d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                connect(mSelectedAccessPoint.getConfig(), true /* isSavedNetwork */);
103390380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff            }
1034771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam        } else if (configController.getMode() == WifiConfigUiBase.MODE_MODIFY) {
10353bf0db8d10fee87c6c2cb7f012dcf97d1ce458f3Sanket Padawe            mWifiManager.save(config, mSaveListener);
103690380120e72a9bebd2fd198372c423afc9b0eeecIrfan Sheriff        } else {
10373bf0db8d10fee87c6c2cb7f012dcf97d1ce458f3Sanket Padawe            mWifiManager.save(config, mSaveListener);
1038e0ece963c67fd3594d284820718c3f316981ac7fSanket Padawe            if (mSelectedAccessPoint != null) { // Not an "Add network"
1039d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                connect(config, false /* isSavedNetwork */);
1040e0ece963c67fd3594d284820718c3f316981ac7fSanket Padawe            }
1041d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa        }
1042d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa
1043fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk        mWifiTracker.resumeScanning();
1044d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    }
1045d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa
1046d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    /* package */ void forget() {
1047aa71afe5978b1340a2d702f3a75d22baf8c96348Fan Zhang        mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_FORGET);
1048fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk        if (!mSelectedAccessPoint.isSaved()) {
10493d32af264254660150c5f2d63a64b3462a9378ddSanket Padawe            if (mSelectedAccessPoint.getNetworkInfo() != null &&
10503d32af264254660150c5f2d63a64b3462a9378ddSanket Padawe                    mSelectedAccessPoint.getNetworkInfo().getState() != State.DISCONNECTED) {
10515ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                // Network is active but has no network ID - must be ephemeral.
10525ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                mWifiManager.disableEphemeralNetwork(
1053c68b46c1f22e1679b3b4a31c20c7951ca1f119f0Jason Monk                        AccessPoint.convertToQuotedString(mSelectedAccessPoint.getSsidStr()));
10545ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson            } else {
10555ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                // Should not happen, but a monkey seems to trigger it
10565ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                Log.e(TAG, "Failed to forget invalid network " + mSelectedAccessPoint.getConfig());
10575ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson                return;
10585ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson            }
10594ec020a4242fe392aa3be0a465d55414e1b164faPeter Qiu        } else if (mSelectedAccessPoint.getConfig().isPasspoint()) {
10604ec020a4242fe392aa3be0a465d55414e1b164faPeter Qiu            mWifiManager.removePasspointConfiguration(mSelectedAccessPoint.getConfig().FQDN);
10615ead6b92fdcfd5da4841509be534efa6264a4cccJeff Davidson        } else {
1062fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk            mWifiManager.forget(mSelectedAccessPoint.getConfig().networkId, mForgetListener);
10633ab5c6838f01cd6306c0ca2728f3c01e29a8318fIrfan Sheriff        }
10643ab5c6838f01cd6306c0ca2728f3c01e29a8318fIrfan Sheriff
1065fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk        mWifiTracker.resumeScanning();
10669f59de930ae14566673d4d57597b836556820b29Maurice Lam
10679f59de930ae14566673d4d57597b836556820b29Maurice Lam        // We need to rename/replace "Next" button in wifi setup context.
10689f59de930ae14566673d4d57597b836556820b29Maurice Lam        changeNextButtonState(false);
1069d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    }
1070d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa
1071d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen    protected void connect(final WifiConfiguration config, boolean isSavedNetwork) {
1072d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen        // Log subtype if configuration is a saved network.
107379f347602186a4057b3f29cab9bed0fd229d3cb1Eric Schwarzenbach        mMetricsFeatureProvider.action(getVisibilityLogger(), MetricsEvent.ACTION_WIFI_CONNECT,
1074d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                isSavedNetwork);
10756a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam        mWifiManager.connect(config, mConnectListener);
1076055079ba380bd9810c52acaaea5068fc0c835972Sundeep Ghuman        mClickedConnect = true;
10776a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam    }
10786a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam
1079d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen    protected void connect(final int networkId, boolean isSavedNetwork) {
1080d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen        // Log subtype if configuration is a saved network.
1081d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen        mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_CONNECT,
1082d9e2c946af8cebeaf9fa81ca5657d3932ee56d1bStephen Chen                isSavedNetwork);
10836a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam        mWifiManager.connect(networkId, mConnectListener);
10846a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam    }
10856a627652cbac48a7d5bea5bc81772f90d7d82af9Maurice Lam
108658b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa    /**
108758b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa     * Called when "add network" button is pressed.
108858b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa     */
1089d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    /* package */ void onAddNetworkPressed() {
1090aa71afe5978b1340a2d702f3a75d22baf8c96348Fan Zhang        mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_ADD_NETWORK);
109158b0291fe6e6e47bf3d7457d8cf2648b8ce4a1aeDaisuke Miyakawa        // No exact access point is selected.
1092d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa        mSelectedAccessPoint = null;
1093771fac591f6e00221713e6eb763148e3d191d7c2Maurice Lam        showDialog(null, WifiConfigUiBase.MODE_CONNECT);
1094d36699282cbd0a6897f425106081d3f2c0db55d4Daisuke Miyakawa    }
1095b962d6566c99edf9950b5d05c5f08c56d1f850feDaisuke Miyakawa
1096b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    @Override
1097b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    protected int getHelpResource() {
109844a49ed67e96c739eabf09520ef432c7f421b486Russell Brenner        return R.string.help_url_wifi;
1099b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    }
1100fc86669031e9c11efa688cbf95e53c77dbc0a03dRussell Brenner
1101fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    @Override
1102b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling    public void onAccessPointChanged(final AccessPoint accessPoint) {
11034ae59dde433751666d8bb10f509a6e13f5eed28fSundeep Ghuman        Log.d(TAG, "onAccessPointChanged (singular) callback initiated");
1104b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling        View view = getView();
1105b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling        if (view != null) {
1106b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling            view.post(new Runnable() {
1107b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                @Override
1108b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                public void run() {
1109b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                    Object tag = accessPoint.getTag();
1110b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                    if (tag != null) {
1111b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                        ((LongPressAccessPointPreference) tag).refresh();
1112b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                    }
1113b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling                }
1114b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling            });
1115b8011dbb936df94f54bbce75282a2caf477346dcDoris Ling        }
1116fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    }
1117fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk
1118fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    @Override
1119fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    public void onLevelChanged(AccessPoint accessPoint) {
112099b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler        ((LongPressAccessPointPreference) accessPoint.getTag()).onLevelChanged();
1121fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk    }
1122fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk
1123fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
112445f754e50618076213ed6911ff1e6c37b4db0467Fabrice Di Meglio        new BaseSearchIndexProvider() {
1125b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio            @Override
112651bfee595c3ce587e2e26565fd9e8f4ae02c3482Fabrice Di Meglio            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
112799b255c8a57d17630fe16bf2e8e1859ce3e5cec6Tony Mantler                final List<SearchIndexableRaw> result = new ArrayList<>();
11286c9e03fd6d834009df10869a9701eee0129f915aFabrice Di Meglio                final Resources res = context.getResources();
1129b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio
1130b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                // Add fragment title
1131fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio                SearchIndexableRaw data = new SearchIndexableRaw(context);
11326c9e03fd6d834009df10869a9701eee0129f915aFabrice Di Meglio                data.title = res.getString(R.string.wifi_settings);
1133fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio                data.screenTitle = res.getString(R.string.wifi_settings);
1134e6b336d1a05752b546319a33a5f5b5c65bccf4e9Fabrice Di Meglio                data.keywords = res.getString(R.string.keywords_wifi);
1135b759516cbf26c9bb5f71c7f93e764fac73f48701Matthew Fritze                data.key = DATA_KEY_REFERENCE;
1136b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                result.add(data);
1137b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio
1138fc1b00cfe439b0c462b3acbba709c3cbc1132a1fJason Monk                // Add saved Wi-Fi access points
1139f200005086681c9a3063f7ece9556ce838d9c334Sundeep Ghuman                final List<AccessPoint> accessPoints =
1140a2fd30765171fa4299919c569ee3c1577fe2f0dbVinit Deshpande                        WifiTracker.getCurrentAccessPoints(context, true, false, false);
1141b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                for (AccessPoint accessPoint : accessPoints) {
1142fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio                    data = new SearchIndexableRaw(context);
1143c68b46c1f22e1679b3b4a31c20c7951ca1f119f0Jason Monk                    data.title = accessPoint.getSsidStr();
1144fa7dc240e978b1b77bb5c80a78cd1f8c77648301Fabrice Di Meglio                    data.screenTitle = res.getString(R.string.wifi_settings);
11455f3442af6f1d1141f8f4dff033d4176214281b81Fabrice Di Meglio                    data.enabled = enabled;
1146b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                    result.add(data);
1147b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                }
1148b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio
1149b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio                return result;
1150b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio            }
1151b8dfbf140986a9c0781f268ea40914063f026436Fabrice Di Meglio        };
115289403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban
115389403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban    /**
11545de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * Returns true if the config is not editable through Settings.
11555de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * @param context Context of caller
115689403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban     * @param config The WiFi config.
11575de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * @return true if the config is not editable through Settings.
115889403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban     */
1159289af93896190038de3bdd7527625bb45d37dae5Charles He    public static boolean isEditabilityLockedDown(Context context, WifiConfiguration config) {
11605de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        return !canModifyNetwork(context, config);
11615de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban    }
11625de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban
11635de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban    /**
11645de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * This method is a stripped version of WifiConfigStore.canModifyNetwork.
11655de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * TODO: refactor to have only one method.
11665de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * @param context Context of caller
11675de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * @param config The WiFi config.
11685de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     * @return true if Settings can modify the config.
11695de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban     */
11705de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban    static boolean canModifyNetwork(Context context, WifiConfiguration config) {
117189403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        if (config == null) {
11725de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban            return true;
117389403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        }
11745de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban
117589403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
117689403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban                Context.DEVICE_POLICY_SERVICE);
11775de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban
11785de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        // Check if device has DPM capability. If it has and dpm is still null, then we
11795de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        // treat this case with suspicion and bail out.
11805de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        final PackageManager pm = context.getPackageManager();
11815de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        if (pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN) && dpm == null) {
118289403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban            return false;
118389403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        }
11845de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban
11855de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        boolean isConfigEligibleForLockdown = false;
11865de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        if (dpm != null) {
11874cfe39f5397d037d106abf07db31b0e01484bd92Makoto Onuki            final ComponentName deviceOwner = dpm.getDeviceOwnerComponentOnAnyUser();
11884cfe39f5397d037d106abf07db31b0e01484bd92Makoto Onuki            if (deviceOwner != null) {
11894cfe39f5397d037d106abf07db31b0e01484bd92Makoto Onuki                final int deviceOwnerUserId = dpm.getDeviceOwnerUserId();
11905de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                try {
1191df828405b107fd6a2602580cf7e8f43723c69d58Jeff Sharkey                    final int deviceOwnerUid = pm.getPackageUidAsUser(deviceOwner.getPackageName(),
11924cfe39f5397d037d106abf07db31b0e01484bd92Makoto Onuki                            deviceOwnerUserId);
11935de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                    isConfigEligibleForLockdown = deviceOwnerUid == config.creatorUid;
11945de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                } catch (NameNotFoundException e) {
11955de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                    // don't care
11965de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                }
11975de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban            }
11981a2aa9942f2c14bdaf46ce4911f9838dc21652baZoltan Szatmary-Ban        }
11995de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        if (!isConfigEligibleForLockdown) {
12005de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban            return true;
120189403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban        }
12025de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban
12035de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        final ContentResolver resolver = context.getContentResolver();
12045de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        final boolean isLockdownFeatureEnabled = Settings.Global.getInt(resolver,
12055de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban                Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0;
12065de33f402a9117fbabbde5d04bf1016be5f9f126Zoltan Szatmary-Ban        return !isLockdownFeatureEnabled;
120789403529adf7584ded2bdadf985fde12a346ec3bZoltan Szatmary-Ban    }
12083069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
1209c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling    private static class SummaryProvider
1210c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            implements SummaryLoader.SummaryProvider, OnSummaryChangeListener {
12113069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
12123069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        private final Context mContext;
12133069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        private final SummaryLoader mSummaryLoader;
12143069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
1215c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling        @VisibleForTesting
1216c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling        WifiSummaryUpdater mSummaryHelper;
1217c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling
12183069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        public SummaryProvider(Context context, SummaryLoader summaryLoader) {
12193069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk            mContext = context;
12203069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk            mSummaryLoader = summaryLoader;
1221c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            mSummaryHelper = new WifiSummaryUpdater(mContext, this);
12223069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        }
12233069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
12243069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
12253069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        @Override
12263069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        public void setListening(boolean listening) {
1227c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            mSummaryHelper.register(listening);
12283069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        }
12293069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
12303069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        @Override
1231c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling        public void onSummaryChanged(String summary) {
1232c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924Doris Ling            mSummaryLoader.setSummary(this, summary);
12333069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        }
12343069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk    }
12353069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk
12363069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
12373069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk            = new SummaryLoader.SummaryProviderFactory() {
12383069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        @Override
12393069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
12403069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk                                                                   SummaryLoader summaryLoader) {
12413069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk            return new SummaryProvider(activity, summaryLoader);
12423069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk        }
12433069581512bcb9687cb8d79675f6c4950c9ac087Jason Monk    };
124448090d4066fdf3084a4fb31277e785149c1bce37Chia-chi Yeh}
1245