Home | History | Annotate | Download | only in wifi
      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  */
     16 package com.android.settings.wifi;
     17 
     18 import android.content.BroadcastReceiver;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.IntentFilter;
     22 import android.net.NetworkScoreManager;
     23 import android.net.NetworkScorerAppManager;
     24 import android.net.wifi.WifiConfiguration;
     25 import android.net.wifi.WifiInfo;
     26 import android.net.wifi.WifiManager;
     27 import android.os.Bundle;
     28 import android.os.UserManager;
     29 import android.provider.Settings;
     30 import android.support.v14.preference.SwitchPreference;
     31 import android.support.v7.preference.ListPreference;
     32 import android.support.v7.preference.Preference;
     33 import android.text.TextUtils;
     34 import android.util.Log;
     35 import android.widget.Toast;
     36 import com.android.internal.logging.MetricsProto.MetricsEvent;
     37 import com.android.settings.AppListSwitchPreference;
     38 import com.android.settings.R;
     39 import com.android.settings.SettingsPreferenceFragment;
     40 import com.android.settings.Utils;
     41 
     42 import java.util.Collection;
     43 import java.util.List;
     44 
     45 public class ConfigureWifiSettings extends SettingsPreferenceFragment
     46         implements Preference.OnPreferenceChangeListener {
     47     private static final String TAG = "ConfigureWifiSettings";
     48 
     49     private static final String KEY_MAC_ADDRESS = "mac_address";
     50     private static final String KEY_SAVED_NETWORKS = "saved_networks";
     51     private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
     52     private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
     53     private static final String KEY_SLEEP_POLICY = "sleep_policy";
     54     private static final String KEY_WIFI_ASSISTANT = "wifi_assistant";
     55 
     56     private WifiManager mWifiManager;
     57     private NetworkScoreManager mNetworkScoreManager;
     58     private AppListSwitchPreference mWifiAssistantPreference;
     59 
     60     private IntentFilter mFilter;
     61 
     62     @Override
     63     public void onCreate(Bundle icicle) {
     64         super.onCreate(icicle);
     65         addPreferencesFromResource(R.xml.wifi_configure_settings);
     66     }
     67 
     68     @Override
     69     public void onActivityCreated(Bundle savedInstanceState) {
     70         super.onActivityCreated(savedInstanceState);
     71         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     72         mFilter = new IntentFilter();
     73         mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
     74         mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
     75         mNetworkScoreManager =
     76                 (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE);
     77     }
     78 
     79     @Override
     80     public void onResume() {
     81         super.onResume();
     82         initPreferences();
     83         getActivity().registerReceiver(mReceiver, mFilter);
     84         refreshWifiInfo();
     85     }
     86 
     87     @Override
     88     public void onPause() {
     89         super.onPause();
     90         getActivity().unregisterReceiver(mReceiver);
     91     }
     92 
     93     private void initPreferences() {
     94         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
     95         if (configs == null || configs.size() == 0) {
     96             removePreference(KEY_SAVED_NETWORKS);
     97         }
     98 
     99         SwitchPreference notifyOpenNetworks =
    100                 (SwitchPreference) findPreference(KEY_NOTIFY_OPEN_NETWORKS);
    101         notifyOpenNetworks.setChecked(Settings.Global.getInt(getContentResolver(),
    102                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
    103         notifyOpenNetworks.setEnabled(mWifiManager.isWifiEnabled());
    104 
    105         final Context context = getActivity();
    106         mWifiAssistantPreference = (AppListSwitchPreference) findPreference(KEY_WIFI_ASSISTANT);
    107         Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers =
    108                 NetworkScorerAppManager.getAllValidScorers(context);
    109         if (UserManager.get(context).isAdminUser() && !scorers.isEmpty()) {
    110             mWifiAssistantPreference.setOnPreferenceChangeListener(this);
    111             initWifiAssistantPreference(scorers);
    112         } else if (mWifiAssistantPreference != null) {
    113             getPreferenceScreen().removePreference(mWifiAssistantPreference);
    114         }
    115 
    116         ListPreference sleepPolicyPref = (ListPreference) findPreference(KEY_SLEEP_POLICY);
    117         if (sleepPolicyPref != null) {
    118             if (Utils.isWifiOnly(context)) {
    119                 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
    120             }
    121             sleepPolicyPref.setOnPreferenceChangeListener(this);
    122             int value = Settings.Global.getInt(getContentResolver(),
    123                     Settings.Global.WIFI_SLEEP_POLICY,
    124                     Settings.Global.WIFI_SLEEP_POLICY_NEVER);
    125             String stringValue = String.valueOf(value);
    126             sleepPolicyPref.setValue(stringValue);
    127             updateSleepPolicySummary(sleepPolicyPref, stringValue);
    128         }
    129     }
    130 
    131     private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
    132         if (value != null) {
    133             String[] values = getResources().getStringArray(R.array.wifi_sleep_policy_values);
    134             final int summaryArrayResId = Utils.isWifiOnly(getActivity()) ?
    135                     R.array.wifi_sleep_policy_entries_wifi_only : R.array.wifi_sleep_policy_entries;
    136             String[] summaries = getResources().getStringArray(summaryArrayResId);
    137             for (int i = 0; i < values.length; i++) {
    138                 if (value.equals(values[i])) {
    139                     if (i < summaries.length) {
    140                         sleepPolicyPref.setSummary(summaries[i]);
    141                         return;
    142                     }
    143                 }
    144             }
    145         }
    146 
    147         sleepPolicyPref.setSummary("");
    148         Log.e(TAG, "Invalid sleep policy value: " + value);
    149     }
    150 
    151     @Override
    152     public boolean onPreferenceTreeClick(Preference preference) {
    153         String key = preference.getKey();
    154 
    155         if (KEY_NOTIFY_OPEN_NETWORKS.equals(key)) {
    156             Settings.Global.putInt(getContentResolver(),
    157                     Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
    158                     ((SwitchPreference) preference).isChecked() ? 1 : 0);
    159         } else {
    160             return super.onPreferenceTreeClick(preference);
    161         }
    162         return true;
    163     }
    164 
    165     @Override
    166     public boolean onPreferenceChange(Preference preference, Object newValue) {
    167         final Context context = getActivity();
    168         String key = preference.getKey();
    169 
    170         if (KEY_WIFI_ASSISTANT.equals(key)) {
    171             NetworkScorerAppManager.NetworkScorerAppData wifiAssistant =
    172                     NetworkScorerAppManager.getScorer(context, (String) newValue);
    173             if (wifiAssistant == null) {
    174                 mNetworkScoreManager.setActiveScorer(null);
    175                 return true;
    176             }
    177 
    178             Intent intent = new Intent();
    179             if (wifiAssistant.mConfigurationActivityClassName != null) {
    180                 // App has a custom configuration activity; launch that.
    181                 // This custom activity will be responsible for launching the system
    182                 // dialog.
    183                 intent.setClassName(wifiAssistant.mPackageName,
    184                         wifiAssistant.mConfigurationActivityClassName);
    185             } else {
    186                 // Fall back on the system dialog.
    187                 intent.setAction(NetworkScoreManager.ACTION_CHANGE_ACTIVE);
    188                 intent.putExtra(NetworkScoreManager.EXTRA_PACKAGE_NAME,
    189                         wifiAssistant.mPackageName);
    190             }
    191 
    192             startActivity(intent);
    193             // Don't update the preference widget state until the child activity returns.
    194             // It will be updated in onResume after the activity finishes.
    195             return false;
    196         }
    197 
    198         if (KEY_SLEEP_POLICY.equals(key)) {
    199             try {
    200                 String stringValue = (String) newValue;
    201                 Settings.Global.putInt(getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY,
    202                         Integer.parseInt(stringValue));
    203                 updateSleepPolicySummary(preference, stringValue);
    204             } catch (NumberFormatException e) {
    205                 Toast.makeText(context, R.string.wifi_setting_sleep_policy_error,
    206                         Toast.LENGTH_SHORT).show();
    207                 return false;
    208             }
    209         }
    210 
    211         return true;
    212     }
    213 
    214     private void refreshWifiInfo() {
    215         final Context context = getActivity();
    216         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    217 
    218         Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
    219         String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
    220         wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
    221                 : context.getString(R.string.status_unavailable));
    222         wifiMacAddressPref.setSelectable(false);
    223 
    224         Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
    225         String ipAddress = Utils.getWifiIpAddresses(context);
    226         wifiIpAddressPref.setSummary(ipAddress == null ?
    227                 context.getString(R.string.status_unavailable) : ipAddress);
    228         wifiIpAddressPref.setSelectable(false);
    229     }
    230 
    231     private void initWifiAssistantPreference(
    232             Collection<NetworkScorerAppManager.NetworkScorerAppData> scorers) {
    233         int count = scorers.size();
    234         String[] packageNames = new String[count];
    235         int i = 0;
    236         for (NetworkScorerAppManager.NetworkScorerAppData scorer : scorers) {
    237             packageNames[i] = scorer.mPackageName;
    238             i++;
    239         }
    240         mWifiAssistantPreference.setPackageNames(packageNames,
    241                 mNetworkScoreManager.getActiveScorerPackage());
    242     }
    243 
    244     @Override
    245     protected int getMetricsCategory() {
    246         return MetricsEvent.CONFIGURE_WIFI;
    247     }
    248 
    249     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    250         @Override
    251         public void onReceive(Context context, Intent intent) {
    252             String action = intent.getAction();
    253             if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
    254                 action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
    255                 refreshWifiInfo();
    256             }
    257         }
    258     };
    259 }
    260