2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.wifi;
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;
33 import com.android.settings.R;
34 import com.android.settings.SettingsPreferenceFragment;
35 import com.android.settings.Utils;
37 public class AdvancedWifiSettings extends SettingsPreferenceFragment
38 implements Preference.OnPreferenceChangeListener {
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_ENABLE_WIFI_WATCHDOG = "wifi_enable_watchdog_service";
48 private WifiManager mWifiManager;
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53 addPreferencesFromResource(R.xml.wifi_advanced_settings);
57 public void onActivityCreated(Bundle savedInstanceState) {
58 super.onActivityCreated(savedInstanceState);
59 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
63 public void onResume() {
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());
76 CheckBoxPreference watchdogEnabled =
77 (CheckBoxPreference) findPreference(KEY_ENABLE_WIFI_WATCHDOG);
78 watchdogEnabled.setChecked(Secure.getInt(getContentResolver(),
79 Secure.WIFI_WATCHDOG_ON, 1) == 1);
81 //TODO: Bring this back after changing watchdog behavior
82 getPreferenceScreen().removePreference(watchdogEnabled);
84 ListPreference frequencyPref = (ListPreference) findPreference(KEY_FREQUENCY_BAND);
86 if (mWifiManager.isDualBandSupported()) {
87 frequencyPref.setOnPreferenceChangeListener(this);
88 int value = mWifiManager.getFrequencyBand();
90 frequencyPref.setValue(String.valueOf(value));
92 Log.e(TAG, "Failed to fetch frequency band");
95 if (frequencyPref != null) {
96 // null if it has already been removed before resume
97 getPreferenceScreen().removePreference(frequencyPref);
101 ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
102 if (sleepPolicyPref != null) {
103 if (Utils.isWifiOnly(getActivity())) {
104 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
106 sleepPolicyPref.setOnPreferenceChangeListener(this);
107 int value = Settings.System.getInt(getContentResolver(),
108 Settings.System.WIFI_SLEEP_POLICY,
109 Settings.System.WIFI_SLEEP_POLICY_NEVER);
110 String stringValue = String.valueOf(value);
111 sleepPolicyPref.setValue(stringValue);
112 updateSleepPolicySummary(sleepPolicyPref, stringValue);
116 private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
118 String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
119 final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
120 R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
121 String[] summaries = getResources().getStringArray(summaryArrayResId);
122 for (int i = 0; i < values.length; i++) {
123 if (value.equals(values[i])) {
124 if (i < summaries.length) {
125 sleepPolicyPref.setSummary(summaries[i]);
132 sleepPolicyPref.setSummary("");
133 Log.e(TAG, "Invalid sleep policy value: " + value);
137 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
138 String key = preference.getKey();
140 if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
141 Secure.putInt(getContentResolver(),
142 Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
143 ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
144 } else if (KEY_ENABLE_WIFI_WATCHDOG.equals(key)) {
145 Secure.putInt(getContentResolver(),
146 Secure.WIFI_WATCHDOG_ON,
147 ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
149 return super.onPreferenceTreeClick(screen, preference);
155 public boolean onPreferenceChange(Preference preference, Object newValue) {
156 String key = preference.getKey();
158 if (KEY_FREQUENCY_BAND.equals(key)) {
160 mWifiManager.setFrequencyBand(Integer.parseInt((String) newValue), true);
161 } catch (NumberFormatException e) {
162 Toast.makeText(getActivity(), R.string.wifi_setting_frequency_band_error,
163 Toast.LENGTH_SHORT).show();
168 if (KEY_SLEEP_POLICY.equals(key)) {
170 String stringValue = (String) newValue;
171 Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY,
172 Integer.parseInt(stringValue));
173 updateSleepPolicySummary(preference, stringValue);
174 } catch (NumberFormatException e) {
175 Toast.makeText(getActivity(), R.string.wifi_setting_sleep_policy_error,
176 Toast.LENGTH_SHORT).show();
184 private void refreshWifiInfo() {
185 WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
187 Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
188 String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
189 wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
190 : getActivity().getString(R.string.status_unavailable));
192 Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
193 String ipAddress = Utils.getWifiIpAddresses(getActivity());
194 wifiIpAddressPref.setSummary(ipAddress == null ?
195 getActivity().getString(R.string.status_unavailable) : ipAddress);