Home | History | Annotate | Download | only in wifi
      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_ENABLE_WIFI_WATCHDOG = "wifi_enable_watchdog_service";
     47 
     48     private WifiManager mWifiManager;
     49 
     50     @Override
     51     public void onCreate(Bundle savedInstanceState) {
     52         super.onCreate(savedInstanceState);
     53         addPreferencesFromResource(R.xml.wifi_advanced_settings);
     54     }
     55 
     56     @Override
     57     public void onActivityCreated(Bundle savedInstanceState) {
     58         super.onActivityCreated(savedInstanceState);
     59         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     60     }
     61 
     62     @Override
     63     public void onResume() {
     64         super.onResume();
     65         initPreferences();
     66         refreshWifiInfo();
     67     }
     68 
     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 watchdogEnabled =
     77             (CheckBoxPreference) findPreference(KEY_ENABLE_WIFI_WATCHDOG);
     78         watchdogEnabled.setChecked(Secure.getInt(getContentResolver(),
     79                 Secure.WIFI_WATCHDOG_ON, 1) == 1);
     80 
     81         ListPreference frequencyPref = (ListPreference) findPreference(KEY_FREQUENCY_BAND);
     82 
     83         if (mWifiManager.isDualBandSupported()) {
     84             frequencyPref.setOnPreferenceChangeListener(this);
     85             int value = mWifiManager.getFrequencyBand();
     86             if (value != -1) {
     87                 frequencyPref.setValue(String.valueOf(value));
     88             } else {
     89                 Log.e(TAG, "Failed to fetch frequency band");
     90             }
     91         } else {
     92             if (frequencyPref != null) {
     93                 // null if it has already been removed before resume
     94                 getPreferenceScreen().removePreference(frequencyPref);
     95             }
     96         }
     97 
     98         ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
     99         if (sleepPolicyPref != null) {
    100             if (Utils.isWifiOnly(getActivity())) {
    101                 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
    102             }
    103             sleepPolicyPref.setOnPreferenceChangeListener(this);
    104             int value = Settings.System.getInt(getContentResolver(),
    105                     Settings.System.WIFI_SLEEP_POLICY,
    106                     Settings.System.WIFI_SLEEP_POLICY_NEVER);
    107             String stringValue = String.valueOf(value);
    108             sleepPolicyPref.setValue(stringValue);
    109             updateSleepPolicySummary(sleepPolicyPref, stringValue);
    110         }
    111     }
    112 
    113     private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
    114         if (value != null) {
    115             String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
    116             final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
    117                     R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
    118             String[] summaries = getResources().getStringArray(summaryArrayResId);
    119             for (int i = 0; i < values.length; i++) {
    120                 if (value.equals(values[i])) {
    121                     if (i < summaries.length) {
    122                         sleepPolicyPref.setSummary(summaries[i]);
    123                         return;
    124                     }
    125                 }
    126             }
    127         }
    128 
    129         sleepPolicyPref.setSummary("");
    130         Log.e(TAG, "Invalid sleep policy value: " + value);
    131     }
    132 
    133     @Override
    134     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
    135         String key = preference.getKey();
    136 
    137         if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
    138             Secure.putInt(getContentResolver(),
    139                     Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
    140                     ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
    141         } else if (KEY_ENABLE_WIFI_WATCHDOG.equals(key)) {
    142             Secure.putInt(getContentResolver(),
    143                     Secure.WIFI_WATCHDOG_ON,
    144                     ((CheckBoxPreference) preference).isChecked() ? 1 : 0);
    145         } else {
    146             return super.onPreferenceTreeClick(screen, preference);
    147         }
    148         return true;
    149     }
    150 
    151     @Override
    152     public boolean onPreferenceChange(Preference preference, Object newValue) {
    153         String key = preference.getKey();
    154 
    155         if (KEY_FREQUENCY_BAND.equals(key)) {
    156             try {
    157                 mWifiManager.setFrequencyBand(Integer.parseInt((String) newValue), true);
    158             } catch (NumberFormatException e) {
    159                 Toast.makeText(getActivity(), R.string.wifi_setting_frequency_band_error,
    160                         Toast.LENGTH_SHORT).show();
    161                 return false;
    162             }
    163         }
    164 
    165         if (KEY_SLEEP_POLICY.equals(key)) {
    166             try {
    167                 String stringValue = (String) newValue;
    168                 Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY,
    169                         Integer.parseInt(stringValue));
    170                 updateSleepPolicySummary(preference, stringValue);
    171             } catch (NumberFormatException e) {
    172                 Toast.makeText(getActivity(), R.string.wifi_setting_sleep_policy_error,
    173                         Toast.LENGTH_SHORT).show();
    174                 return false;
    175             }
    176         }
    177 
    178         return true;
    179     }
    180 
    181     private void refreshWifiInfo() {
    182         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    183 
    184         Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
    185         String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
    186         wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
    187                 : getActivity().getString(R.string.status_unavailable));
    188 
    189         Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
    190         String ipAddress = Utils.getWifiIpAddresses(getActivity());
    191         wifiIpAddressPref.setSummary(ipAddress == null ?
    192                 getActivity().getString(R.string.status_unavailable) : ipAddress);
    193     }
    194 
    195 }
    196