diff options
author | Jeff Sharkey <jsharkey@android.com> | 2012-04-03 21:13:25 -0700 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2012-04-05 14:31:43 -0700 |
commit | 313f7d8c5759fd29a98c710ceb9339d27b8e62fa (patch) | |
tree | 9275d0f9e2627535813e517a8e99319a7941c5d5 /src/com/android/settings/net | |
parent | 9f55ae28789754dafeed5c68468d45135d46199e (diff) | |
download | packages_apps_settings-313f7d8c5759fd29a98c710ceb9339d27b8e62fa.zip packages_apps_settings-313f7d8c5759fd29a98c710ceb9339d27b8e62fa.tar.gz packages_apps_settings-313f7d8c5759fd29a98c710ceb9339d27b8e62fa.tar.bz2 |
Controls to set expensive (metered) networks.
Add UI to change metered flag on NetworkPolicy, and support Wi-Fi
policies per-SSID. Create Wi-Fi policies as needed, but leave cycle
undefined.
Only show and mutate mobile policies when SIM state is ready.
Bug: 3001465, 3291052
Change-Id: I481a202fe0e68fc2f5adfd3b3a6f40347d2b168c
Diffstat (limited to 'src/com/android/settings/net')
-rw-r--r-- | src/com/android/settings/net/DataUsageMeteredSettings.java | 133 | ||||
-rw-r--r-- | src/com/android/settings/net/NetworkPolicyEditor.java | 99 |
2 files changed, 204 insertions, 28 deletions
diff --git a/src/com/android/settings/net/DataUsageMeteredSettings.java b/src/com/android/settings/net/DataUsageMeteredSettings.java new file mode 100644 index 0000000..d069a71 --- /dev/null +++ b/src/com/android/settings/net/DataUsageMeteredSettings.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.net; + +import static com.android.settings.DataUsageSummary.hasReadyMobileRadio; +import static com.android.settings.DataUsageSummary.hasWifiRadio; + +import android.content.Context; +import android.net.NetworkPolicy; +import android.net.NetworkPolicyManager; +import android.net.NetworkTemplate; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.Preference; +import android.preference.PreferenceCategory; +import android.telephony.TelephonyManager; + +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; + +/** + * Panel to configure {@link NetworkPolicy#metered} for networks. + */ +public class DataUsageMeteredSettings extends SettingsPreferenceFragment { + + private NetworkPolicyManager mPolicyManager; + private WifiManager mWifiManager; + + private NetworkPolicyEditor mPolicyEditor; + + private PreferenceCategory mMobileCategory; + private PreferenceCategory mWifiCategory; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + final Context context = getActivity(); + + mPolicyManager = NetworkPolicyManager.from(context); + mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + + mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); + mPolicyEditor.read(); + + addPreferencesFromResource(R.xml.data_usage_metered_prefs); + mMobileCategory = (PreferenceCategory) findPreference("mobile"); + mWifiCategory = (PreferenceCategory) findPreference("wifi"); + + updateNetworks(context); + + } + + private void updateNetworks(Context context) { + if (hasReadyMobileRadio(context)) { + mMobileCategory.removeAll(); + mMobileCategory.addPreference(buildMobilePref(context)); + } else { + getPreferenceScreen().removePreference(mMobileCategory); + } + + if (hasWifiRadio(context)) { + mWifiCategory.removeAll(); + for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) { + if (config.SSID != null) { + mWifiCategory.addPreference(buildWifiPref(context, config)); + } + } + } else { + getPreferenceScreen().removePreference(mWifiCategory); + } + } + + private Preference buildMobilePref(Context context) { + final TelephonyManager tele = TelephonyManager.from(context); + final NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll( + tele.getSubscriberId()); + final MeteredPreference pref = new MeteredPreference(context, template); + pref.setTitle(tele.getNetworkOperatorName()); + return pref; + } + + private Preference buildWifiPref(Context context, WifiConfiguration config) { + final String networkId = removeDoubleQuotes(config.SSID); + final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(networkId); + final MeteredPreference pref = new MeteredPreference(context, template); + pref.setTitle(networkId); + return pref; + } + + private class MeteredPreference extends CheckBoxPreference { + private final NetworkTemplate mTemplate; + + public MeteredPreference(Context context, NetworkTemplate template) { + super(context); + mTemplate = template; + + setPersistent(false); + setChecked(mPolicyEditor.getPolicyMetered(mTemplate)); + } + + @Override + protected void notifyChanged() { + super.notifyChanged(); + mPolicyEditor.setPolicyMetered(mTemplate, isChecked()); + } + } + + + private static String removeDoubleQuotes(String string) { + final int length = string.length(); + if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) { + return string.substring(1, length - 1); + } + return string; + } + +} diff --git a/src/com/android/settings/net/NetworkPolicyEditor.java b/src/com/android/settings/net/NetworkPolicyEditor.java index 80fcdb5..07a2afb 100644 --- a/src/com/android/settings/net/NetworkPolicyEditor.java +++ b/src/com/android/settings/net/NetworkPolicyEditor.java @@ -16,6 +16,7 @@ package com.android.settings.net; +import static android.net.NetworkPolicy.CYCLE_NONE; import static android.net.NetworkPolicy.LIMIT_DISABLED; import static android.net.NetworkPolicy.SNOOZE_NEVER; import static android.net.NetworkPolicy.WARNING_DISABLED; @@ -27,11 +28,10 @@ import static android.net.NetworkTemplate.buildTemplateMobile4g; import static android.net.NetworkTemplate.buildTemplateMobileAll; import static com.android.internal.util.Preconditions.checkNotNull; -import android.net.INetworkPolicyManager; import android.net.NetworkPolicy; +import android.net.NetworkPolicyManager; import android.net.NetworkTemplate; import android.os.AsyncTask; -import android.os.RemoteException; import android.text.format.Time; import com.android.internal.util.Objects; @@ -43,27 +43,23 @@ import java.util.HashSet; /** * Utility class to modify list of {@link NetworkPolicy}. Specifically knows - * about which policies can coexist. Not thread safe. + * about which policies can coexist. This editor offers thread safety when + * talking with {@link NetworkPolicyManager}. */ public class NetworkPolicyEditor { // TODO: be more robust when missing policies from service public static final boolean ENABLE_SPLIT_POLICIES = false; - private INetworkPolicyManager mPolicyService; + private NetworkPolicyManager mPolicyManager; private ArrayList<NetworkPolicy> mPolicies = Lists.newArrayList(); - public NetworkPolicyEditor(INetworkPolicyManager policyService) { - mPolicyService = checkNotNull(policyService); + public NetworkPolicyEditor(NetworkPolicyManager policyManager) { + mPolicyManager = checkNotNull(policyManager); } public void read() { - final NetworkPolicy[] policies; - try { - policies = mPolicyService.getNetworkPolicies(); - } catch (RemoteException e) { - throw new RuntimeException("problem reading policies", e); - } + final NetworkPolicy[] policies = mPolicyManager.getNetworkPolicies(); boolean modified = false; mPolicies.clear(); @@ -78,12 +74,6 @@ public class NetworkPolicyEditor { modified = true; } - // drop any WIFI policies that were defined - if (policy.template.getMatchRule() == MATCH_WIFI) { - modified = true; - continue; - } - mPolicies.add(policy); } @@ -109,11 +99,7 @@ public class NetworkPolicyEditor { } public void write(NetworkPolicy[] policies) { - try { - mPolicyService.setNetworkPolicies(policies); - } catch (RemoteException e) { - throw new RuntimeException("problem writing policies", e); - } + mPolicyManager.setNetworkPolicies(policies); } public boolean hasLimitedPolicy(NetworkTemplate template) { @@ -142,13 +128,24 @@ public class NetworkPolicyEditor { @Deprecated private static NetworkPolicy buildDefaultPolicy(NetworkTemplate template) { // TODO: move this into framework to share with NetworkPolicyManagerService - final Time time = new Time(); - time.setToNow(); - final int cycleDay = time.monthDay; - final String cycleTimezone = time.timezone; + final int cycleDay; + final String cycleTimezone; + final boolean metered; + + if (template.getMatchRule() == MATCH_WIFI) { + cycleDay = CYCLE_NONE; + cycleTimezone = Time.TIMEZONE_UTC; + metered = false; + } else { + final Time time = new Time(); + time.setToNow(); + cycleDay = time.monthDay; + cycleTimezone = time.timezone; + metered = true; + } return new NetworkPolicy(template, cycleDay, cycleTimezone, WARNING_DISABLED, - LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, false); + LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, metered, true); } public int getPolicyCycleDay(NetworkTemplate template) { @@ -188,6 +185,52 @@ public class NetworkPolicyEditor { writeAsync(); } + public boolean getPolicyMetered(NetworkTemplate template) { + final NetworkPolicy policy = getPolicy(template); + if (policy != null) { + return policy.metered; + } else { + return false; + } + } + + public void setPolicyMetered(NetworkTemplate template, boolean metered) { + boolean modified = false; + + NetworkPolicy policy = getPolicy(template); + if (metered) { + if (policy == null) { + policy = buildDefaultPolicy(template); + policy.metered = true; + policy.inferred = false; + mPolicies.add(policy); + modified = true; + } else if (!policy.metered) { + policy.metered = true; + policy.inferred = false; + modified = true; + } + + } else { + if (policy == null) { + // ignore when policy doesn't exist + } else if (policy.template.getMatchRule() == MATCH_WIFI + && policy.warningBytes == WARNING_DISABLED + && policy.limitBytes == LIMIT_DISABLED) { + // when WIFI goes unmetered, and no other warning/limit for + // policy, clean it up. + mPolicies.remove(policy); + modified = true; + } else if (policy.metered) { + policy.metered = false; + policy.inferred = false; + modified = true; + } + } + + if (modified) writeAsync(); + } + /** * Remove any split {@link NetworkPolicy}. */ |