10b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk/*
20b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * Copyright (C) 2015 The Android Open Source Project
30b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk *
40b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * Licensed under the Apache License, Version 2.0 (the "License");
50b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * you may not use this file except in compliance with the License.
60b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * You may obtain a copy of the License at
70b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk *
80b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
90b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk *
100b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * Unless required by applicable law or agreed to in writing, software
110b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * distributed under the License is distributed on an "AS IS" BASIS,
120b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * See the License for the specific language governing permissions and
140b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk * limitations under the License.
150b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk */
160b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkpackage com.android.settings.wifi;
170b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
180b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.content.BroadcastReceiver;
190b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.content.Context;
200b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.content.Intent;
210b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.content.IntentFilter;
22a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichiimport android.content.res.Resources;
230b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.net.NetworkScoreManager;
240b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.net.NetworkScorerAppManager;
250b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.net.wifi.WifiConfiguration;
260b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.net.wifi.WifiInfo;
270b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.net.wifi.WifiManager;
280b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.os.Bundle;
290b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.os.UserManager;
300b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.provider.Settings;
310b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.support.v14.preference.SwitchPreference;
320b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.support.v7.preference.ListPreference;
330b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.support.v7.preference.Preference;
340b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.text.TextUtils;
350b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.util.Log;
360b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport android.widget.Toast;
373e19fc5600f739e7f172fb9331cfc0f4a884d4ceJason Monkimport com.android.internal.logging.MetricsProto.MetricsEvent;
380b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport com.android.settings.AppListSwitchPreference;
390b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport com.android.settings.R;
400b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport com.android.settings.SettingsPreferenceFragment;
410b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport com.android.settings.Utils;
420b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
430b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport java.util.Collection;
440b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkimport java.util.List;
450b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
460b4fdc49fba83ad2a950681ef014b6927e438007Jason Monkpublic class ConfigureWifiSettings extends SettingsPreferenceFragment
470b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        implements Preference.OnPreferenceChangeListener {
480b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String TAG = "ConfigureWifiSettings";
490b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
500b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_MAC_ADDRESS = "mac_address";
510b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_SAVED_NETWORKS = "saved_networks";
520b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
530b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
540b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_SLEEP_POLICY = "sleep_policy";
55a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi    private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
560b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private static final String KEY_WIFI_ASSISTANT = "wifi_assistant";
570b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
580b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private WifiManager mWifiManager;
590b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private NetworkScoreManager mNetworkScoreManager;
600b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private AppListSwitchPreference mWifiAssistantPreference;
610b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
620b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private IntentFilter mFilter;
630b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
640b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
650b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public void onCreate(Bundle icicle) {
660b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        super.onCreate(icicle);
670b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        addPreferencesFromResource(R.xml.wifi_configure_settings);
680b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
690b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
700b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
710b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public void onActivityCreated(Bundle savedInstanceState) {
720b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        super.onActivityCreated(savedInstanceState);
730b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
740b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mFilter = new IntentFilter();
750b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
760b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
770b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mNetworkScoreManager =
780b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE);
790b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
800b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
810b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
820b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public void onResume() {
830b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        super.onResume();
840b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        initPreferences();
850b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        getActivity().registerReceiver(mReceiver, mFilter);
860b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        refreshWifiInfo();
870b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
880b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
890b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
900b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public void onPause() {
910b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        super.onPause();
920b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        getActivity().unregisterReceiver(mReceiver);
930b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
940b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
950b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private void initPreferences() {
960b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
970b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (configs == null || configs.size() == 0) {
980b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            removePreference(KEY_SAVED_NETWORKS);
990b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
1000b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1010b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        SwitchPreference notifyOpenNetworks =
1020b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                (SwitchPreference) findPreference(KEY_NOTIFY_OPEN_NETWORKS);
1030b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        notifyOpenNetworks.setChecked(Settings.Global.getInt(getContentResolver(),
1040b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
1050b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        notifyOpenNetworks.setEnabled(mWifiManager.isWifiEnabled());
1060b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1070b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        final Context context = getActivity();
108a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi        if (avoidBadWifiConfig()) {
109a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            // Hide preference toggle, always avoid bad wifi networks.
110a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            removePreference(KEY_CELLULAR_FALLBACK);
111a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi        } else {
112a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            // Show preference toggle, initialized based on current settings value.
113a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            boolean currentSetting = avoidBadWifiCurrentSettings();
114a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            SwitchPreference pref = (SwitchPreference) findPreference(KEY_CELLULAR_FALLBACK);
115a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            // TODO: can this ever be null? The return value of avoidBadWifiConfig() can only
116a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            // change if the resources change, but if that happens the activity will be recreated...
117a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            if (pref != null) {
118a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi                pref.setChecked(currentSetting);
119a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            }
120a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi        }
121a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi
1220b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mWifiAssistantPreference = (AppListSwitchPreference) findPreference(KEY_WIFI_ASSISTANT);
1230b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers =
1240b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                NetworkScorerAppManager.getAllValidScorers(context);
1250b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (UserManager.get(context).isAdminUser() && !scorers.isEmpty()) {
1260b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            mWifiAssistantPreference.setOnPreferenceChangeListener(this);
1270b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            initWifiAssistantPreference(scorers);
1280b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        } else if (mWifiAssistantPreference != null) {
1290b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            getPreferenceScreen().removePreference(mWifiAssistantPreference);
1300b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
1310b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1320b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
1330b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (sleepPolicyPref != null) {
1340b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            if (Utils.isWifiOnly(context)) {
1350b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
1360b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
1370b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            sleepPolicyPref.setOnPreferenceChangeListener(this);
1380b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            int value = Settings.Global.getInt(getContentResolver(),
1390b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    Settings.Global.WIFI_SLEEP_POLICY,
1400b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    Settings.Global.WIFI_SLEEP_POLICY_NEVER);
1410b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            String stringValue = String.valueOf(value);
1420b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            sleepPolicyPref.setValue(stringValue);
1430b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            updateSleepPolicySummary(sleepPolicyPref, stringValue);
1440b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
1450b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
1460b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1470b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
1480b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (value != null) {
1490b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
1500b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
1510b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
1520b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            String[] summaries = getResources().getStringArray(summaryArrayResId);
1530b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            for (int i = 0; i < values.length; i++) {
1540b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                if (value.equals(values[i])) {
1550b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    if (i < summaries.length) {
1560b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        sleepPolicyPref.setSummary(summaries[i]);
1570b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        return;
1580b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    }
1590b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                }
1600b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
1610b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
1620b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1630b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        sleepPolicyPref.setSummary("");
1640b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        Log.e(TAG, "Invalid sleep policy value: " + value);
1650b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
1660b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
167a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi    private boolean avoidBadWifiConfig() {
168a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi        return getActivity().getResources().getInteger(
169a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi                com.android.internal.R.integer.config_networkAvoidBadWifi) == 1;
170a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi    }
171a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi
172a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi    private boolean avoidBadWifiCurrentSettings() {
173c179f33c878ec0fcc7e2d71d596dc518a50349a3Lorenzo Colitti        return "1".equals(Settings.Global.getString(getContentResolver(),
174c179f33c878ec0fcc7e2d71d596dc518a50349a3Lorenzo Colitti                Settings.Global.NETWORK_AVOID_BAD_WIFI));
175a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi    }
176a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi
1770b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
1780b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public boolean onPreferenceTreeClick(Preference preference) {
1790b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String key = preference.getKey();
1800b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1810b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
1820b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            Settings.Global.putInt(getContentResolver(),
1830b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
1840b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    ((SwitchPreference) preference).isChecked() ? 1 : 0);
185a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi        } else if (KEY_CELLULAR_FALLBACK.equals(key)) {
186c179f33c878ec0fcc7e2d71d596dc518a50349a3Lorenzo Colitti            // On: avoid bad wifi. Off: prompt.
187a317684cfc456be821eaca97c4d14b6479467b1bHugo Benichi            String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
188c179f33c878ec0fcc7e2d71d596dc518a50349a3Lorenzo Colitti            Settings.Global.putString(getContentResolver(), settingName,
189c179f33c878ec0fcc7e2d71d596dc518a50349a3Lorenzo Colitti                    ((SwitchPreference) preference).isChecked() ? "1" : null);
1900b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        } else {
1910b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            return super.onPreferenceTreeClick(preference);
1920b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
1930b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        return true;
1940b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
1950b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
1960b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
1970b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public boolean onPreferenceChange(Preference preference, Object newValue) {
1980b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        final Context context = getActivity();
1990b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String key = preference.getKey();
2000b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
201fae7e6cd975c640fd2f92aba96da898f7ef42c11pkanwar        if (KEY_WIFI_ASSISTANT.equals(key)) {
2020b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            NetworkScorerAppManager.NetworkScorerAppData wifiAssistant =
2030b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                    NetworkScorerAppManager.getScorer(context, (String) newValue);
2040b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            if (wifiAssistant == null) {
2050b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                mNetworkScoreManager.setActiveScorer(null);
2060b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                return true;
2070b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
2080b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2090b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            Intent intent = new Intent();
2100b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            if (wifiAssistant.mConfigurationActivityClassName != null) {
2110b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                // App has a custom configuration activity; launch that.
2120b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                // This custom activity will be responsible for launching the system
2130b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                // dialog.
2140b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                intent.setClassName(wifiAssistant.mPackageName,
2150b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        wifiAssistant.mConfigurationActivityClassName);
2160b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            } else {
2170b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                // Fall back on the system dialog.
2180b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                intent.setAction(NetworkScoreManager.ACTION_CHANGE_ACTIVE);
2190b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                intent.putExtra(NetworkScoreManager.EXTRA_PACKAGE_NAME,
2200b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        wifiAssistant.mPackageName);
2210b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
2220b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2230b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            startActivity(intent);
2240b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            // Don't update the preference widget state until the child activity returns.
2250b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            // It will be updated in onResume after the activity finishes.
2260b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            return false;
2270b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
2280b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2290b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        if (KEY_SLEEP_POLICY.equals(key)) {
2300b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            try {
2310b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                String stringValue = (String) newValue;
2320b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                Settings.Global.putInt(getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY,
2330b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        Integer.parseInt(stringValue));
2340b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                updateSleepPolicySummary(preference, stringValue);
2350b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            } catch (NumberFormatException e) {
2360b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                Toast.makeText(context, R.string.wifi_setting_sleep_policy_error,
2370b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                        Toast.LENGTH_SHORT).show();
2380b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                return false;
2390b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
2400b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
2410b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2420b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        return true;
2430b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
2440b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2450b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private void refreshWifiInfo() {
2460b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        final Context context = getActivity();
2470b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
2480b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2490b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
2500b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
2510b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
2520b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                : context.getString(R.string.status_unavailable));
2530b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        wifiMacAddressPref.setSelectable(false);
2540b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2550b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
2560b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String ipAddress = Utils.getWifiIpAddresses(context);
2570b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        wifiIpAddressPref.setSummary(ipAddress == null ?
2580b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                context.getString(R.string.status_unavailable) : ipAddress);
2590b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        wifiIpAddressPref.setSelectable(false);
2600b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
2610b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2620b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private void initWifiAssistantPreference(
2630b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers) {
2640b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        int count = scorers.size();
2650b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String[] packageNames = new String[count];
2660b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        int i = 0;
2670b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        for (NetworkScorerAppManager.NetworkScorerAppData scorer : scorers) {
2680b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            packageNames[i] = scorer.mPackageName;
2690b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            i++;
2700b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
2710b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mWifiAssistantPreference.setPackageNames(packageNames,
2720b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                mNetworkScoreManager.getActiveScorerPackage());
2730b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
2740b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2750b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
2760b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    protected int getMetricsCategory() {
2773e19fc5600f739e7f172fb9331cfc0f4a884d4ceJason Monk        return MetricsEvent.CONFIGURE_WIFI;
2780b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
2790b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
2800b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
2810b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        @Override
2820b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        public void onReceive(Context context, Intent intent) {
2830b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            String action = intent.getAction();
2840b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
2850b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
2860b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk                refreshWifiInfo();
2870b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk            }
2880b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        }
2890b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    };
2900b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk}
291