ConfigureWifiSettings.java revision e590183dcc3c5640cf8c27fce21bc69ac8ed14e7
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.settings.wifi;
17
18import android.content.BroadcastReceiver;
19import android.content.Context;
20import android.content.Intent;
21import android.content.IntentFilter;
22import android.content.res.Resources;
23import android.net.NetworkScoreManager;
24import android.net.NetworkScorerAppManager;
25import android.net.wifi.WifiConfiguration;
26import android.net.wifi.WifiInfo;
27import android.net.wifi.WifiManager;
28import android.os.Bundle;
29import android.os.UserManager;
30import android.provider.Settings;
31import android.support.v14.preference.SwitchPreference;
32import android.support.v7.preference.ListPreference;
33import android.support.v7.preference.Preference;
34import android.text.TextUtils;
35import android.util.Log;
36import android.widget.Toast;
37import com.android.internal.logging.MetricsProto.MetricsEvent;
38import com.android.settings.AppListSwitchPreference;
39import com.android.settings.R;
40import com.android.settings.SettingsPreferenceFragment;
41import com.android.settings.Utils;
42
43import java.util.Collection;
44import java.util.List;
45
46public class ConfigureWifiSettings extends SettingsPreferenceFragment
47        implements Preference.OnPreferenceChangeListener {
48    private static final String TAG = "ConfigureWifiSettings";
49
50    private static final String KEY_MAC_ADDRESS = "mac_address";
51    private static final String KEY_SAVED_NETWORKS = "saved_networks";
52    private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
53    private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
54    private static final String KEY_SLEEP_POLICY = "sleep_policy";
55    private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
56    private static final String KEY_WIFI_ASSISTANT = "wifi_assistant";
57
58    private WifiManager mWifiManager;
59    private NetworkScoreManager mNetworkScoreManager;
60    private AppListSwitchPreference mWifiAssistantPreference;
61
62    private IntentFilter mFilter;
63
64    @Override
65    public void onCreate(Bundle icicle) {
66        super.onCreate(icicle);
67        addPreferencesFromResource(R.xml.wifi_configure_settings);
68    }
69
70    @Override
71    public void onActivityCreated(Bundle savedInstanceState) {
72        super.onActivityCreated(savedInstanceState);
73        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
74        mFilter = new IntentFilter();
75        mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
76        mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
77        mNetworkScoreManager =
78                (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE);
79    }
80
81    @Override
82    public void onResume() {
83        super.onResume();
84        initPreferences();
85        getActivity().registerReceiver(mReceiver, mFilter);
86        refreshWifiInfo();
87    }
88
89    @Override
90    public void onPause() {
91        super.onPause();
92        getActivity().unregisterReceiver(mReceiver);
93    }
94
95    private void initPreferences() {
96        List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
97        if (configs == null || configs.size() == 0) {
98            removePreference(KEY_SAVED_NETWORKS);
99        }
100
101        SwitchPreference notifyOpenNetworks =
102                (SwitchPreference) findPreference(KEY_NOTIFY_OPEN_NETWORKS);
103        notifyOpenNetworks.setChecked(Settings.Global.getInt(getContentResolver(),
104                Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
105        notifyOpenNetworks.setEnabled(mWifiManager.isWifiEnabled());
106
107        final Context context = getActivity();
108        if (avoidBadWifiConfig()) {
109            // Hide preference toggle, always avoid bad wifi networks.
110            removePreference(KEY_CELLULAR_FALLBACK);
111        } else {
112            // Show preference toggle, initialized based on current settings value.
113            boolean currentSetting = avoidBadWifiCurrentSettings();
114            SwitchPreference pref = (SwitchPreference) findPreference(KEY_CELLULAR_FALLBACK);
115            // TODO: can this ever be null? The return value of avoidBadWifiConfig() can only
116            // change if the resources change, but if that happens the activity will be recreated...
117            if (pref != null) {
118                pref.setChecked(currentSetting);
119            }
120        }
121
122        mWifiAssistantPreference = (AppListSwitchPreference) findPreference(KEY_WIFI_ASSISTANT);
123        Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers =
124                NetworkScorerAppManager.getAllValidScorers(context);
125        if (UserManager.get(context).isAdminUser() && !scorers.isEmpty()) {
126            mWifiAssistantPreference.setOnPreferenceChangeListener(this);
127            initWifiAssistantPreference(scorers);
128        } else if (mWifiAssistantPreference != null) {
129            getPreferenceScreen().removePreference(mWifiAssistantPreference);
130        }
131
132        ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
133        if (sleepPolicyPref != null) {
134            if (Utils.isWifiOnly(context)) {
135                sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
136            }
137            sleepPolicyPref.setOnPreferenceChangeListener(this);
138            int value = Settings.Global.getInt(getContentResolver(),
139                    Settings.Global.WIFI_SLEEP_POLICY,
140                    Settings.Global.WIFI_SLEEP_POLICY_NEVER);
141            String stringValue = String.valueOf(value);
142            sleepPolicyPref.setValue(stringValue);
143            updateSleepPolicySummary(sleepPolicyPref, stringValue);
144        }
145    }
146
147    private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
148        if (value != null) {
149            String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
150            final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
151                    R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
152            String[] summaries = getResources().getStringArray(summaryArrayResId);
153            for (int i = 0; i < values.length; i++) {
154                if (value.equals(values[i])) {
155                    if (i < summaries.length) {
156                        sleepPolicyPref.setSummary(summaries[i]);
157                        return;
158                    }
159                }
160            }
161        }
162
163        sleepPolicyPref.setSummary("");
164        Log.e(TAG, "Invalid sleep policy value: " + value);
165    }
166
167    private boolean avoidBadWifiConfig() {
168        return getActivity().getResources().getInteger(
169                com.android.internal.R.integer.config_networkAvoidBadWifi) == 1;
170    }
171
172    private boolean avoidBadWifiCurrentSettings() {
173        return Settings.Global.getInt(getContentResolver(),
174                Settings.Global.NETWORK_AVOID_BAD_WIFI, 0) == 1;
175    }
176
177    @Override
178    public boolean onPreferenceTreeClick(Preference preference) {
179        String key = preference.getKey();
180
181        if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
182            Settings.Global.putInt(getContentResolver(),
183                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
184                    ((SwitchPreference) preference).isChecked() ? 1 : 0);
185        } else if (KEY_CELLULAR_FALLBACK.equals(key)) {
186            String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
187            if (((SwitchPreference) preference).isChecked()) {
188                // The user wants to avoid bad wifi networks. Remember the choice.
189                Settings.Global.putInt(getContentResolver(), settingName, 1);
190            } else {
191                // Unset the setting. ConnectivityService interprets null to mean "use the carrier
192                // default". We don't set the setting to 0 because if we do, and the user switches
193                // to a carrier that does not restrict cellular fallback, then there is no way to
194                // set it to 1 again because on such a carrier the toggle is never shown.
195                Settings.Global.putString(getContentResolver(), settingName, null);
196            }
197        } else {
198            return super.onPreferenceTreeClick(preference);
199        }
200        return true;
201    }
202
203    @Override
204    public boolean onPreferenceChange(Preference preference, Object newValue) {
205        final Context context = getActivity();
206        String key = preference.getKey();
207
208        if (KEY_WIFI_ASSISTANT.equals(key)) {
209            NetworkScorerAppManager.NetworkScorerAppData wifiAssistant =
210                    NetworkScorerAppManager.getScorer(context, (String) newValue);
211            if (wifiAssistant == null) {
212                mNetworkScoreManager.setActiveScorer(null);
213                return true;
214            }
215
216            Intent intent = new Intent();
217            if (wifiAssistant.mConfigurationActivityClassName != null) {
218                // App has a custom configuration activity; launch that.
219                // This custom activity will be responsible for launching the system
220                // dialog.
221                intent.setClassName(wifiAssistant.mPackageName,
222                        wifiAssistant.mConfigurationActivityClassName);
223            } else {
224                // Fall back on the system dialog.
225                intent.setAction(NetworkScoreManager.ACTION_CHANGE_ACTIVE);
226                intent.putExtra(NetworkScoreManager.EXTRA_PACKAGE_NAME,
227                        wifiAssistant.mPackageName);
228            }
229
230            startActivity(intent);
231            // Don't update the preference widget state until the child activity returns.
232            // It will be updated in onResume after the activity finishes.
233            return false;
234        }
235
236        if (KEY_SLEEP_POLICY.equals(key)) {
237            try {
238                String stringValue = (String) newValue;
239                Settings.Global.putInt(getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY,
240                        Integer.parseInt(stringValue));
241                updateSleepPolicySummary(preference, stringValue);
242            } catch (NumberFormatException e) {
243                Toast.makeText(context, R.string.wifi_setting_sleep_policy_error,
244                        Toast.LENGTH_SHORT).show();
245                return false;
246            }
247        }
248
249        return true;
250    }
251
252    private void refreshWifiInfo() {
253        final Context context = getActivity();
254        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
255
256        Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
257        String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
258        wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
259                : context.getString(R.string.status_unavailable));
260        wifiMacAddressPref.setSelectable(false);
261
262        Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
263        String ipAddress = Utils.getWifiIpAddresses(context);
264        wifiIpAddressPref.setSummary(ipAddress == null ?
265                context.getString(R.string.status_unavailable) : ipAddress);
266        wifiIpAddressPref.setSelectable(false);
267    }
268
269    private void initWifiAssistantPreference(
270            Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers) {
271        int count = scorers.size();
272        String[] packageNames = new String[count];
273        int i = 0;
274        for (NetworkScorerAppManager.NetworkScorerAppData scorer : scorers) {
275            packageNames[i] = scorer.mPackageName;
276            i++;
277        }
278        mWifiAssistantPreference.setPackageNames(packageNames,
279                mNetworkScoreManager.getActiveScorerPackage());
280    }
281
282    @Override
283    public int getMetricsCategory() {
284        return MetricsEvent.CONFIGURE_WIFI;
285    }
286
287    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
288        @Override
289        public void onReceive(Context context, Intent intent) {
290            String action = intent.getAction();
291            if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
292                action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
293                refreshWifiInfo();
294            }
295        }
296    };
297}
298