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 static android.net.wifi.WifiInfo.removeDoubleQuotes; 18 19 import android.app.backup.BackupManager; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.net.NetworkPolicyManager; 23 import android.net.wifi.WifiConfiguration; 24 import android.net.wifi.WifiManager; 25 import android.os.Bundle; 26 import android.support.v7.preference.DropDownPreference; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceCategory; 29 import android.text.TextUtils; 30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 31 import com.android.settings.R; 32 import com.android.settings.SettingsPreferenceFragment; 33 import com.android.settings.search.BaseSearchIndexProvider; 34 import com.android.settings.search.Indexable; 35 import com.android.settings.search.SearchIndexableRaw; 36 import com.android.settingslib.NetworkPolicyEditor; 37 import java.util.ArrayList; 38 import java.util.List; 39 40 /** 41 * Panel to configure {@link WifiConfiguration#meteredOverride}. 42 */ 43 public class DataUsageMeteredSettings extends SettingsPreferenceFragment implements Indexable { 44 45 private NetworkPolicyManager mPolicyManager; 46 private WifiManager mWifiManager; 47 48 private NetworkPolicyEditor mPolicyEditor; 49 50 private PreferenceCategory mMobileCategory; 51 private PreferenceCategory mWifiCategory; 52 private Preference mWifiDisabled; 53 54 @Override getMetricsCategory()55 public int getMetricsCategory() { 56 return MetricsEvent.NET_DATA_USAGE_METERED; 57 } 58 59 @Override onCreate(Bundle icicle)60 public void onCreate(Bundle icicle) { 61 super.onCreate(icicle); 62 final Context context = getActivity(); 63 64 mPolicyManager = NetworkPolicyManager.from(context); 65 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 66 67 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); 68 mPolicyEditor.read(); 69 70 addPreferencesFromResource(R.xml.data_usage_metered_prefs); 71 mMobileCategory = (PreferenceCategory) findPreference("mobile"); 72 mWifiCategory = (PreferenceCategory) findPreference("wifi"); 73 mWifiDisabled = findPreference("wifi_disabled"); 74 75 updateNetworks(context); 76 } 77 updateNetworks(Context context)78 private void updateNetworks(Context context) { 79 getPreferenceScreen().removePreference(mMobileCategory); 80 81 mWifiCategory.removeAll(); 82 if (DataUsageUtils.hasWifiRadio(context) && mWifiManager.isWifiEnabled()) { 83 for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) { 84 final Preference pref = new MeteredPreference(getPrefContext(), config); 85 if (!TextUtils.isEmpty(pref.getTitle())) { 86 mWifiCategory.addPreference(pref); 87 } 88 } 89 } else { 90 mWifiCategory.addPreference(mWifiDisabled); 91 } 92 } 93 94 private class MeteredPreference extends DropDownPreference { 95 private final WifiConfiguration mConfig; 96 MeteredPreference(Context context, WifiConfiguration config)97 public MeteredPreference(Context context, WifiConfiguration config) { 98 super(context); 99 mConfig = config; 100 101 setPersistent(false); 102 setEntries(new CharSequence[] { 103 getString(R.string.data_usage_metered_auto), 104 getString(R.string.data_usage_metered_yes), 105 getString(R.string.data_usage_metered_no), 106 }); 107 setEntryValues(new CharSequence[] { 108 Integer.toString(WifiConfiguration.METERED_OVERRIDE_NONE), 109 Integer.toString(WifiConfiguration.METERED_OVERRIDE_METERED), 110 Integer.toString(WifiConfiguration.METERED_OVERRIDE_NOT_METERED), 111 }); 112 setValue(Integer.toString(mConfig.meteredOverride)); 113 setTitle(NetworkPolicyManager.resolveNetworkId(mConfig)); 114 setSummary(getEntries()[mConfig.meteredOverride]); 115 116 setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 117 @Override 118 public boolean onPreferenceChange(Preference preference, Object newValue) { 119 mConfig.meteredOverride = Integer.parseInt((String) newValue); 120 setSummary(getEntries()[mConfig.meteredOverride]); 121 122 mWifiManager.updateNetwork(mConfig); 123 // Stage the backup of the SettingsProvider package which backs this up 124 BackupManager.dataChanged("com.android.providers.settings"); 125 return true; 126 } 127 }); 128 } 129 } 130 131 /** 132 * For search 133 */ 134 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 135 new BaseSearchIndexProvider() { 136 @Override 137 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 138 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 139 final Resources res = context.getResources(); 140 141 // Add fragment title 142 SearchIndexableRaw data = new SearchIndexableRaw(context); 143 data.title = res.getString(R.string.data_usage_menu_metered); 144 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 145 result.add(data); 146 147 // Body 148 data = new SearchIndexableRaw(context); 149 data.title = res.getString(R.string.data_usage_metered_body); 150 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 151 result.add(data); 152 153 // Wi-Fi networks category 154 data = new SearchIndexableRaw(context); 155 data.title = res.getString(R.string.data_usage_metered_wifi); 156 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 157 result.add(data); 158 159 final WifiManager wifiManager = 160 (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 161 if (DataUsageUtils.hasWifiRadio(context) && wifiManager.isWifiEnabled()) { 162 for (WifiConfiguration config : wifiManager.getConfiguredNetworks()) { 163 if (config.SSID != null) { 164 final String networkId = config.SSID; 165 166 data = new SearchIndexableRaw(context); 167 data.title = removeDoubleQuotes(networkId); 168 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 169 result.add(data); 170 } 171 } 172 } else { 173 data = new SearchIndexableRaw(context); 174 data.title = res.getString(R.string.data_usage_metered_wifi_disabled); 175 data.screenTitle = res.getString(R.string.data_usage_menu_metered); 176 result.add(data); 177 } 178 179 return result; 180 } 181 182 @Override 183 public List<String> getNonIndexableKeys(Context context) { 184 final List<String> result = super.getNonIndexableKeys(context); 185 result.add("mobile"); 186 return result; 187 } 188 }; 189 } 190