Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2012 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.net;
     18 
     19 import static android.net.NetworkPolicy.LIMIT_DISABLED;
     20 import static android.net.wifi.WifiInfo.removeDoubleQuotes;
     21 import static com.android.settings.DataUsageSummary.hasReadyMobileRadio;
     22 import static com.android.settings.DataUsageSummary.hasWifiRadio;
     23 
     24 import android.content.Context;
     25 import android.net.NetworkPolicy;
     26 import android.net.NetworkPolicyManager;
     27 import android.net.NetworkTemplate;
     28 import android.net.wifi.WifiConfiguration;
     29 import android.net.wifi.WifiManager;
     30 import android.os.Bundle;
     31 import android.preference.CheckBoxPreference;
     32 import android.preference.Preference;
     33 import android.preference.PreferenceCategory;
     34 import android.telephony.TelephonyManager;
     35 
     36 import com.android.settings.R;
     37 import com.android.settings.SettingsPreferenceFragment;
     38 
     39 /**
     40  * Panel to configure {@link NetworkPolicy#metered} for networks.
     41  */
     42 public class DataUsageMeteredSettings extends SettingsPreferenceFragment {
     43 
     44     private static final boolean SHOW_MOBILE_CATEGORY = false;
     45 
     46     private NetworkPolicyManager mPolicyManager;
     47     private WifiManager mWifiManager;
     48 
     49     private NetworkPolicyEditor mPolicyEditor;
     50 
     51     private PreferenceCategory mMobileCategory;
     52     private PreferenceCategory mWifiCategory;
     53     private Preference mWifiDisabled;
     54 
     55     @Override
     56     public void onCreate(Bundle icicle) {
     57         super.onCreate(icicle);
     58         final Context context = getActivity();
     59 
     60         mPolicyManager = NetworkPolicyManager.from(context);
     61         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     62 
     63         mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
     64         mPolicyEditor.read();
     65 
     66         addPreferencesFromResource(R.xml.data_usage_metered_prefs);
     67         mMobileCategory = (PreferenceCategory) findPreference("mobile");
     68         mWifiCategory = (PreferenceCategory) findPreference("wifi");
     69         mWifiDisabled = findPreference("wifi_disabled");
     70 
     71         updateNetworks(context);
     72     }
     73 
     74     private void updateNetworks(Context context) {
     75         if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) {
     76             mMobileCategory.removeAll();
     77             mMobileCategory.addPreference(buildMobilePref(context));
     78         } else {
     79             getPreferenceScreen().removePreference(mMobileCategory);
     80         }
     81 
     82         mWifiCategory.removeAll();
     83         if (hasWifiRadio(context) && mWifiManager.isWifiEnabled()) {
     84             for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
     85                 if (config.SSID != null) {
     86                     mWifiCategory.addPreference(buildWifiPref(context, config));
     87                 }
     88             }
     89         } else {
     90             mWifiCategory.addPreference(mWifiDisabled);
     91         }
     92     }
     93 
     94     private Preference buildMobilePref(Context context) {
     95         final TelephonyManager tele = TelephonyManager.from(context);
     96         final NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(
     97                 tele.getSubscriberId());
     98         final MeteredPreference pref = new MeteredPreference(context, template);
     99         pref.setTitle(tele.getNetworkOperatorName());
    100         return pref;
    101     }
    102 
    103     private Preference buildWifiPref(Context context, WifiConfiguration config) {
    104         final String networkId = config.SSID;
    105         final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(networkId);
    106         final MeteredPreference pref = new MeteredPreference(context, template);
    107         pref.setTitle(removeDoubleQuotes(networkId));
    108         return pref;
    109     }
    110 
    111     private class MeteredPreference extends CheckBoxPreference {
    112         private final NetworkTemplate mTemplate;
    113         private boolean mBinding;
    114 
    115         public MeteredPreference(Context context, NetworkTemplate template) {
    116             super(context);
    117             mTemplate = template;
    118 
    119             setPersistent(false);
    120 
    121             mBinding = true;
    122             final NetworkPolicy policy = mPolicyEditor.getPolicyMaybeUnquoted(template);
    123             if (policy != null) {
    124                 if (policy.limitBytes != LIMIT_DISABLED) {
    125                     setChecked(true);
    126                     setEnabled(false);
    127                 } else {
    128                     setChecked(policy.metered);
    129                 }
    130             } else {
    131                 setChecked(false);
    132             }
    133             mBinding = false;
    134         }
    135 
    136         @Override
    137         protected void notifyChanged() {
    138             super.notifyChanged();
    139             if (!mBinding) {
    140                 mPolicyEditor.setPolicyMetered(mTemplate, isChecked());
    141             }
    142         }
    143     }
    144 }
    145