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.content.Intent; 21 import android.net.wifi.WifiInfo; 22 import android.net.wifi.WifiManager; 23 import android.net.wifi.WifiWatchdogStateMachine; 24 import android.os.Bundle; 25 import android.preference.CheckBoxPreference; 26 import android.preference.ListPreference; 27 import android.preference.Preference; 28 import android.preference.PreferenceScreen; 29 import android.provider.Settings; 30 import android.provider.Settings.Global; 31 import android.security.Credentials; 32 import android.text.TextUtils; 33 import android.util.Log; 34 import android.widget.Toast; 35 36 import com.android.settings.R; 37 import com.android.settings.SettingsPreferenceFragment; 38 import com.android.settings.Utils; 39 40 public class AdvancedWifiSettings extends SettingsPreferenceFragment 41 implements Preference.OnPreferenceChangeListener { 42 43 private static final String TAG = "AdvancedWifiSettings"; 44 private static final String KEY_MAC_ADDRESS = "mac_address"; 45 private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address"; 46 private static final String KEY_FREQUENCY_BAND = "frequency_band"; 47 private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks"; 48 private static final String KEY_SLEEP_POLICY = "sleep_policy"; 49 private static final String KEY_POOR_NETWORK_DETECTION = "wifi_poor_network_detection"; 50 private static final String KEY_SCAN_ALWAYS_AVAILABLE = "wifi_scan_always_available"; 51 private static final String KEY_INSTALL_CREDENTIALS = "install_credentials"; 52 private static final String KEY_SUSPEND_OPTIMIZATIONS = "suspend_optimizations"; 53 54 private WifiManager mWifiManager; 55 56 @Override 57 public void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 addPreferencesFromResource(R.xml.wifi_advanced_settings); 60 } 61 62 @Override 63 public void onActivityCreated(Bundle savedInstanceState) { 64 super.onActivityCreated(savedInstanceState); 65 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 66 } 67 68 @Override 69 public void onResume() { 70 super.onResume(); 71 initPreferences(); 72 refreshWifiInfo(); 73 } 74 75 private void initPreferences() { 76 CheckBoxPreference notifyOpenNetworks = 77 (CheckBoxPreference) findPreference(KEY_NOTIFY_OPEN_NETWORKS); 78 notifyOpenNetworks.setChecked(Settings.Global.getInt(getContentResolver(), 79 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1); 80 notifyOpenNetworks.setEnabled(mWifiManager.isWifiEnabled()); 81 82 CheckBoxPreference poorNetworkDetection = 83 (CheckBoxPreference) findPreference(KEY_POOR_NETWORK_DETECTION); 84 if (poorNetworkDetection != null) { 85 if (Utils.isWifiOnly(getActivity())) { 86 getPreferenceScreen().removePreference(poorNetworkDetection); 87 } else { 88 poorNetworkDetection.setChecked(Global.getInt(getContentResolver(), 89 Global.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, 90 WifiWatchdogStateMachine.DEFAULT_POOR_NETWORK_AVOIDANCE_ENABLED ? 91 1 : 0) == 1); 92 } 93 } 94 95 CheckBoxPreference scanAlwaysAvailable = 96 (CheckBoxPreference) findPreference(KEY_SCAN_ALWAYS_AVAILABLE); 97 scanAlwaysAvailable.setChecked(Global.getInt(getContentResolver(), 98 Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1); 99 100 Intent intent=new Intent(Credentials.INSTALL_AS_USER_ACTION); 101 intent.setClassName("com.android.certinstaller", 102 "com.android.certinstaller.CertInstallerMain"); 103 intent.putExtra(Credentials.EXTRA_INSTALL_AS_UID, android.os.Process.WIFI_UID); 104 Preference pref = findPreference(KEY_INSTALL_CREDENTIALS); 105 pref.setIntent(intent); 106 107 CheckBoxPreference suspendOptimizations = 108 (CheckBoxPreference) findPreference(KEY_SUSPEND_OPTIMIZATIONS); 109 suspendOptimizations.setChecked(Global.getInt(getContentResolver(), 110 Global.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 1) == 1); 111 112 ListPreference frequencyPref = (ListPreference) findPreference(KEY_FREQUENCY_BAND); 113 114 if (mWifiManager.isDualBandSupported()) { 115 frequencyPref.setOnPreferenceChangeListener(this); 116 int value = mWifiManager.getFrequencyBand(); 117 if (value != -1) { 118 frequencyPref.setValue(String.valueOf(value)); 119 updateFrequencyBandSummary(frequencyPref, value); 120 } else { 121 Log.e(TAG, "Failed to fetch frequency band"); 122 } 123 } else { 124 if (frequencyPref != null) { 125 // null if it has already been removed before resume 126 getPreferenceScreen().removePreference(frequencyPref); 127 } 128 } 129 130 ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY); 131 if (sleepPolicyPref != null) { 132 if (Utils.isWifiOnly(getActivity())) { 133 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only); 134 } 135 sleepPolicyPref.setOnPreferenceChangeListener(this); 136 int value = Settings.Global.getInt(getContentResolver(), 137 Settings.Global.WIFI_SLEEP_POLICY, 138 Settings.Global.WIFI_SLEEP_POLICY_NEVER); 139 String stringValue = String.valueOf(value); 140 sleepPolicyPref.setValue(stringValue); 141 updateSleepPolicySummary(sleepPolicyPref, stringValue); 142 } 143 } 144 145 private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) { 146 if (value != null) { 147 String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values); 148 final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ? 149 R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries; 150 String[] summaries = getResources().getStringArray(summaryArrayResId); 151 for (int i = 0; i < values.length; i++) { 152 if (value.equals(values[i])) { 153 if (i < summaries.length) { 154 sleepPolicyPref.setSummary(summaries[i]); 155 return; 156 } 157 } 158 } 159 } 160 161 sleepPolicyPref.setSummary(""); 162 Log.e(TAG, "Invalid sleep policy value: " + value); 163 } 164 165 private void updateFrequencyBandSummary(Preference frequencyBandPref, int index) { 166 String[] summaries = getResources().getStringArray(R.array.wifi_frequency_band_entries); 167 frequencyBandPref.setSummary(summaries[index]); 168 } 169 170 @Override 171 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { 172 String key = preference.getKey(); 173 174 if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) { 175 Global.putInt(getContentResolver(), 176 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 177 ((CheckBoxPreference) preference).isChecked() ? 1 : 0); 178 } else if (KEY_POOR_NETWORK_DETECTION.equals(key)) { 179 Global.putInt(getContentResolver(), 180 Global.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, 181 ((CheckBoxPreference) preference).isChecked() ? 1 : 0); 182 } else if (KEY_SUSPEND_OPTIMIZATIONS.equals(key)) { 183 Global.putInt(getContentResolver(), 184 Global.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 185 ((CheckBoxPreference) preference).isChecked() ? 1 : 0); 186 } else if (KEY_SCAN_ALWAYS_AVAILABLE.equals(key)) { 187 Global.putInt(getContentResolver(), 188 Global.WIFI_SCAN_ALWAYS_AVAILABLE, 189 ((CheckBoxPreference) preference).isChecked() ? 1 : 0); 190 } else { 191 return super.onPreferenceTreeClick(screen, preference); 192 } 193 return true; 194 } 195 196 @Override 197 public boolean onPreferenceChange(Preference preference, Object newValue) { 198 String key = preference.getKey(); 199 200 if (KEY_FREQUENCY_BAND.equals(key)) { 201 try { 202 int value = Integer.parseInt((String) newValue); 203 mWifiManager.setFrequencyBand(value, true); 204 updateFrequencyBandSummary(preference, value); 205 } catch (NumberFormatException e) { 206 Toast.makeText(getActivity(), R.string.wifi_setting_frequency_band_error, 207 Toast.LENGTH_SHORT).show(); 208 return false; 209 } 210 } 211 212 if (KEY_SLEEP_POLICY.equals(key)) { 213 try { 214 String stringValue = (String) newValue; 215 Settings.Global.putInt(getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY, 216 Integer.parseInt(stringValue)); 217 updateSleepPolicySummary(preference, stringValue); 218 } catch (NumberFormatException e) { 219 Toast.makeText(getActivity(), R.string.wifi_setting_sleep_policy_error, 220 Toast.LENGTH_SHORT).show(); 221 return false; 222 } 223 } 224 225 return true; 226 } 227 228 private void refreshWifiInfo() { 229 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 230 231 Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS); 232 String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress(); 233 wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress 234 : getActivity().getString(R.string.status_unavailable)); 235 236 Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS); 237 String ipAddress = Utils.getWifiIpAddresses(getActivity()); 238 wifiIpAddressPref.setSummary(ipAddress == null ? 239 getActivity().getString(R.string.status_unavailable) : ipAddress); 240 } 241 242 } 243