1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.datausage; 16 17 import android.app.backup.BackupManager; 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.net.NetworkPolicy; 21 import android.net.NetworkPolicyManager; 22 import android.net.NetworkTemplate; 23 import android.net.wifi.WifiConfiguration; 24 import android.net.wifi.WifiManager; 25 import android.os.Bundle; 26 import android.support.v14.preference.SwitchPreference; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceCategory; 29 import android.telephony.TelephonyManager; 30 31 import com.android.internal.logging.MetricsProto.MetricsEvent; 32 import com.android.settings.R; 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settings.search.BaseSearchIndexProvider; 35 import com.android.settings.search.Indexable; 36 import com.android.settings.search.SearchIndexableRaw; 37 import com.android.settingslib.NetworkPolicyEditor; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 import static android.net.NetworkPolicy.LIMIT_DISABLED; 43 import static android.net.wifi.WifiInfo.removeDoubleQuotes; 44 import static com.android.settings.datausage.DataUsageList.hasReadyMobileRadio; 45 import static com.android.settings.datausage.DataUsageSummary.hasWifiRadio; 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 64 protected int getMetricsCategory() { 65 return MetricsEvent.NET_DATA_USAGE_METERED; 66 } 67 68 @Override 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 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 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(getPrefContext(), template); 112 pref.setTitle(tele.getNetworkOperatorName()); 113 return pref; 114 } 115 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 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 150 protected void notifyChanged() { 151 super.notifyChanged(); 152 if (!mBinding) { 153 mPolicyEditor.setPolicyMetered(mTemplate, isChecked()); 154 // Stage the backup of the SettingsProvider package which backs this up 155 BackupManager.dataChanged("com.android.providers.settings"); 156 } 157 } 158 } 159 160 /** 161 * For search 162 */ 163 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 164 new BaseSearchIndexProvider() { 165 @Override 166 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 167 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 168 final Resources res = context.getResources(); 169 170 // Add fragment title 171 SearchIndexableRaw data = new SearchIndexableRaw(context); 172 data.title = res.getString(R.string.data_usage_menu_metered); 173 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 174 result.add(data); 175 176 // Body 177 data = new SearchIndexableRaw(context); 178 data.title = res.getString(R.string.data_usage_metered_body); 179 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 180 result.add(data); 181 182 if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) { 183 // Mobile networks category 184 data = new SearchIndexableRaw(context); 185 data.title = res.getString(R.string.data_usage_metered_mobile); 186 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 187 result.add(data); 188 189 final TelephonyManager tele = TelephonyManager.from(context); 190 191 data = new SearchIndexableRaw(context); 192 data.title = tele.getNetworkOperatorName(); 193 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 194 result.add(data); 195 } 196 197 // Wi-Fi networks category 198 data = new SearchIndexableRaw(context); 199 data.title = res.getString(R.string.data_usage_metered_wifi); 200 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 201 result.add(data); 202 203 final WifiManager wifiManager = 204 (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 205 if (hasWifiRadio(context) && wifiManager.isWifiEnabled()) { 206 for (WifiConfiguration config : wifiManager.getConfiguredNetworks()) { 207 if (config.SSID != null) { 208 final String networkId = config.SSID; 209 210 data = new SearchIndexableRaw(context); 211 data.title = removeDoubleQuotes(networkId); 212 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 213 result.add(data); 214 } 215 } 216 } else { 217 data = new SearchIndexableRaw(context); 218 data.title = res.getString(R.string.data_usage_metered_wifi_disabled); 219 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 220 result.add(data); 221 } 222 223 return result; 224 } 225 226 @Override 227 public List<String> getNonIndexableKeys(Context context) { 228 final ArrayList<String> result = new ArrayList<String>(); 229 if (!SHOW_MOBILE_CATEGORY || !hasReadyMobileRadio(context)) { 230 result.add("mobile"); 231 } 232 233 return result; 234 } 235 }; 236 237 } 238