• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.wifi;
18 
19 import android.content.Context;
20 import android.net.wifi.WifiInfo;
21 import android.net.wifi.WifiManager;
22 import android.os.Bundle;
23 import android.preference.CheckBoxPreference;
24 import android.preference.ListPreference;
25 import android.preference.Preference;
26 import android.preference.PreferenceScreen;
27 import android.provider.Settings;
28 import android.provider.Settings.Secure;
29 import android.text.TextUtils;
30 import android.util.Log;
31 import android.widget.Toast;
32 
33 import com.android.settings.R;
34 import com.android.settings.SettingsPreferenceFragment;
35 import com.android.settings.Utils;
36 
37 public class AdvancedWifiSettings extends SettingsPreferenceFragment
38         implements Preference.OnPreferenceChangeListener {
39 
40     private static final String TAG = "AdvancedWifiSettings";
41     private static final String KEY_MAC_ADDRESS = "mac_address";
42     private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
43     private static final String KEY_FREQUENCY_BAND = "frequency_band";
44     private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
45     private static final String KEY_SLEEP_POLICY = "sleep_policy";
46     private static final String KEY_POOR_NETWORK_DETECTION = "wifi_poor_network_detection";
47 
48     private WifiManager mWifiManager;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         addPreferencesFromResource(R.xml.wifi_advanced_settings);
54     }
55 
56     @Override
onActivityCreated(Bundle savedInstanceState)57     public void onActivityCreated(Bundle savedInstanceState) {
58         super.onActivityCreated(savedInstanceState);
59         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
60     }
61 
62     @Override
onResume()63     public void onResume() {
64         super.onResume();
65         initPreferences();
66         refreshWifiInfo();
67     }
68 
initPreferences()69     private void initPreferences() {
70         CheckBoxPreference notifyOpenNetworks =
71             (CheckBoxPreference) findPreference(KEY_NOTIFY_OPEN_NETWORKS);
72         notifyOpenNetworks.setChecked(Secure.getInt(getContentResolver(),
73                 Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
74         notifyOpenNetworks.setEnabled(mWifiManager.isWifiEnabled());
75 
76         CheckBoxPreference poorNetworkDetection =
77             (CheckBoxPreference) findPreference(KEY_POOR_NETWORK_DETECTION);
78         if (poorNetworkDetection != null) {
79             if (Utils.isWifiOnly(getActivity())) {
80                 getPreferenceScreen().removePreference(poorNetworkDetection);
81             } else {
82                 poorNetworkDetection.setChecked(Secure.getInt(getContentResolver(),
83                         Secure.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, 1) == 1);
84             }
85         }
86 
87         ListPreference frequencyPref = (ListPreference) findPreference(KEY_FREQUENCY_BAND);
88 
89         if (mWifiManager.isDualBandSupported()) {
90             frequencyPref.setOnPreferenceChangeListener(this);
91             int value = mWifiManager.getFrequencyBand();
92             if (value != -1) {
93                 frequencyPref.setValue(String.valueOf(value));
94             } else {
95                 Log.e(TAG, "Failed to fetch frequency band");
96             }
97         } else {
98             if (frequencyPref != null) {
99                 // null if it has already been removed before resume
100                 getPreferenceScreen().removePreference(frequencyPref);
101             }
102         }
103 
104         ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
105         if (sleepPolicyPref != null) {
106             if (Utils.isWifiOnly(getActivity())) {
107                 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
108             }
109             sleepPolicyPref.setOnPreferenceChangeListener(this);
110             int value = Settings.System.getInt(getContentResolver(),
111                     Settings.System.WIFI_SLEEP_POLICY,
112                     Settings.System.WIFI_SLEEP_POLICY_NEVER);
113             String stringValue = String.valueOf(value);
114             sleepPolicyPref.setValue(stringValue);
115             updateSleepPolicySummary(sleepPolicyPref, stringValue);
116         }
117     }
118 
updateSleepPolicySummary(Preference sleepPolicyPref, String value)119     private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
120         if (value != null) {
121             String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
122             final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
123                     R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
124             String[] summaries = getResources().getStringArray(summaryArrayResId);
125             for (int i = 0; i < values.length; i++) {
126                 if (value.equals(values[i])) {
127                     if (i < summaries.length) {
128                         sleepPolicyPref.setSummary(summaries[i]);
129                         return;
130                     }
131                 }
132             }
133         }
134 
135         sleepPolicyPref.setSummary("");
136         Log.e(TAG, "Invalid sleep policy value: " + value);
137     }
138 
139     @Override
onPreferenceTreeClick(PreferenceScreen screen, Preference preference)140     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
141         String key = preference.getKey();
142 
143         if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
144             Secure.putInt(getContentResolver(),
145                     Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
146                     ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
147         } else if (KEY_POOR_NETWORK_DETECTION.equals(key)) {
148             Secure.putInt(getContentResolver(),
149                     Secure.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED,
150                     ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
151         } else {
152             return super.onPreferenceTreeClick(screen, preference);
153         }
154         return true;
155     }
156 
157     @Override
onPreferenceChange(Preference preference, Object newValue)158     public boolean onPreferenceChange(Preference preference, Object newValue) {
159         String key = preference.getKey();
160 
161         if (KEY_FREQUENCY_BAND.equals(key)) {
162             try {
163                 mWifiManager.setFrequencyBand(Integer.parseInt((String) newValue), true);
164             } catch (NumberFormatException e) {
165                 Toast.makeText(getActivity(), R.string.wifi_setting_frequency_band_error,
166                         Toast.LENGTH_SHORT).show();
167                 return false;
168             }
169         }
170 
171         if (KEY_SLEEP_POLICY.equals(key)) {
172             try {
173                 String stringValue = (String) newValue;
174                 Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY,
175                         Integer.parseInt(stringValue));
176                 updateSleepPolicySummary(preference, stringValue);
177             } catch (NumberFormatException e) {
178                 Toast.makeText(getActivity(), R.string.wifi_setting_sleep_policy_error,
179                         Toast.LENGTH_SHORT).show();
180                 return false;
181             }
182         }
183 
184         return true;
185     }
186 
refreshWifiInfo()187     private void refreshWifiInfo() {
188         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
189 
190         Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
191         String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
192         wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
193                 : getActivity().getString(R.string.status_unavailable));
194 
195         Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
196         String ipAddress = Utils.getWifiIpAddresses(getActivity());
197         wifiIpAddressPref.setSummary(ipAddress == null ?
198                 getActivity().getString(R.string.status_unavailable) : ipAddress);
199     }
200 
201 }
202