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.content.res.Resources; 26 import android.net.NetworkPolicy; 27 import android.net.NetworkPolicyManager; 28 import android.net.NetworkTemplate; 29 import android.net.wifi.WifiConfiguration; 30 import android.net.wifi.WifiManager; 31 import android.os.Bundle; 32 import android.preference.Preference; 33 import android.preference.PreferenceCategory; 34 import android.preference.SwitchPreference; 35 import android.telephony.TelephonyManager; 36 37 import com.android.internal.logging.MetricsLogger; 38 import com.android.settings.R; 39 import com.android.settings.SettingsPreferenceFragment; 40 import com.android.settings.search.BaseSearchIndexProvider; 41 import com.android.settings.search.Indexable; 42 import com.android.settings.search.SearchIndexableRaw; 43 44 import java.util.ArrayList; 45 import java.util.List; 46 47 /** 48 * Panel to configure {@link NetworkPolicy#metered} for networks. 49 */ 50 public class DataUsageMeteredSettings extends SettingsPreferenceFragment implements Indexable { 51 52 private static final boolean SHOW_MOBILE_CATEGORY = false; 53 54 private NetworkPolicyManager mPolicyManager; 55 private WifiManager mWifiManager; 56 57 private NetworkPolicyEditor mPolicyEditor; 58 59 private PreferenceCategory mMobileCategory; 60 private PreferenceCategory mWifiCategory; 61 private Preference mWifiDisabled; 62 63 @Override getMetricsCategory()64 protected int getMetricsCategory() { 65 return MetricsLogger.NET_DATA_USAGE_METERED; 66 } 67 68 @Override onCreate(Bundle icicle)69 public void onCreate(Bundle icicle) { 70 super.onCreate(icicle); 71 final Context context = getActivity(); 72 73 mPolicyManager = NetworkPolicyManager.from(context); 74 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 75 76 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); 77 mPolicyEditor.read(); 78 79 addPreferencesFromResource(R.xml.data_usage_metered_prefs); 80 mMobileCategory = (PreferenceCategory) findPreference("mobile"); 81 mWifiCategory = (PreferenceCategory) findPreference("wifi"); 82 mWifiDisabled = findPreference("wifi_disabled"); 83 84 updateNetworks(context); 85 } 86 updateNetworks(Context context)87 private void updateNetworks(Context context) { 88 if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) { 89 mMobileCategory.removeAll(); 90 mMobileCategory.addPreference(buildMobilePref(context)); 91 } else { 92 getPreferenceScreen().removePreference(mMobileCategory); 93 } 94 95 mWifiCategory.removeAll(); 96 if (hasWifiRadio(context) && mWifiManager.isWifiEnabled()) { 97 for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) { 98 if (config.SSID != null) { 99 mWifiCategory.addPreference(buildWifiPref(context, config)); 100 } 101 } 102 } else { 103 mWifiCategory.addPreference(mWifiDisabled); 104 } 105 } 106 buildMobilePref(Context context)107 private Preference buildMobilePref(Context context) { 108 final TelephonyManager tele = TelephonyManager.from(context); 109 final NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll( 110 tele.getSubscriberId()); 111 final MeteredPreference pref = new MeteredPreference(context, template); 112 pref.setTitle(tele.getNetworkOperatorName()); 113 return pref; 114 } 115 buildWifiPref(Context context, WifiConfiguration config)116 private Preference buildWifiPref(Context context, WifiConfiguration config) { 117 final String networkId = config.SSID; 118 final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(networkId); 119 final MeteredPreference pref = new MeteredPreference(context, template); 120 pref.setTitle(removeDoubleQuotes(networkId)); 121 return pref; 122 } 123 124 private class MeteredPreference extends SwitchPreference { 125 private final NetworkTemplate mTemplate; 126 private boolean mBinding; 127 MeteredPreference(Context context, NetworkTemplate template)128 public MeteredPreference(Context context, NetworkTemplate template) { 129 super(context); 130 mTemplate = template; 131 132 setPersistent(false); 133 134 mBinding = true; 135 final NetworkPolicy policy = mPolicyEditor.getPolicyMaybeUnquoted(template); 136 if (policy != null) { 137 if (policy.limitBytes != LIMIT_DISABLED) { 138 setChecked(true); 139 setEnabled(false); 140 } else { 141 setChecked(policy.metered); 142 } 143 } else { 144 setChecked(false); 145 } 146 mBinding = false; 147 } 148 149 @Override notifyChanged()150 protected void notifyChanged() { 151 super.notifyChanged(); 152 if (!mBinding) { 153 mPolicyEditor.setPolicyMetered(mTemplate, isChecked()); 154 } 155 } 156 } 157 158 /** 159 * For search 160 */ 161 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 162 new BaseSearchIndexProvider() { 163 @Override 164 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 165 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 166 final Resources res = context.getResources(); 167 168 // Add fragment title 169 SearchIndexableRaw data = new SearchIndexableRaw(context); 170 data.title = res.getString(R.string.data_usage_menu_metered); 171 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 172 result.add(data); 173 174 // Body 175 data = new SearchIndexableRaw(context); 176 data.title = res.getString(R.string.data_usage_metered_body); 177 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 178 result.add(data); 179 180 if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) { 181 // Mobile networks category 182 data = new SearchIndexableRaw(context); 183 data.title = res.getString(R.string.data_usage_metered_mobile); 184 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 185 result.add(data); 186 187 final TelephonyManager tele = TelephonyManager.from(context); 188 189 data = new SearchIndexableRaw(context); 190 data.title = tele.getNetworkOperatorName(); 191 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 192 result.add(data); 193 } 194 195 // Wi-Fi networks category 196 data = new SearchIndexableRaw(context); 197 data.title = res.getString(R.string.data_usage_metered_wifi); 198 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 199 result.add(data); 200 201 final WifiManager wifiManager = 202 (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 203 if (hasWifiRadio(context) && wifiManager.isWifiEnabled()) { 204 for (WifiConfiguration config : wifiManager.getConfiguredNetworks()) { 205 if (config.SSID != null) { 206 final String networkId = config.SSID; 207 208 data = new SearchIndexableRaw(context); 209 data.title = removeDoubleQuotes(networkId); 210 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 211 result.add(data); 212 } 213 } 214 } else { 215 data = new SearchIndexableRaw(context); 216 data.title = res.getString(R.string.data_usage_metered_wifi_disabled); 217 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 218 result.add(data); 219 } 220 221 return result; 222 } 223 224 @Override 225 public List<String> getNonIndexableKeys(Context context) { 226 final ArrayList<String> result = new ArrayList<String>(); 227 if (!SHOW_MOBILE_CATEGORY || !hasReadyMobileRadio(context)) { 228 result.add("mobile"); 229 } 230 231 return result; 232 } 233 }; 234 235 } 236