Offer to "merge" subscribers for data usage.

When picking a template to use for querying statistics and setting
policies, normalize it so we widen the template to cover any merged
subscribers.

Also remove some code related to long-dormant split policies.

Bug: 18012787
Change-Id: I1e07dc09016df21ecfd7b3fad49759fa57570391
diff --git a/src/com/android/settings/DataUsageSummary.java b/src/com/android/settings/DataUsageSummary.java
index 1c17ce5..c24e0d6 100644
--- a/src/com/android/settings/DataUsageSummary.java
+++ b/src/com/android/settings/DataUsageSummary.java
@@ -769,8 +769,15 @@
             if (LOGD) Log.d(TAG, "updateBody() mobile tab");
             setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
             setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
-            mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context,getSubId(currentTab)));
             mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
+
+            // Match mobile traffic for this subscriber, but normalize it to
+            // catch any other merged subscribers.
+            mTemplate = buildTemplateMobileAll(
+                    getActiveSubscriberId(context, getSubId(currentTab)));
+            mTemplate = NetworkTemplate.normalize(mTemplate,
+                    mTelephonyManager.getMergedSubscriberIds());
+
         } else if (TAB_3G.equals(currentTab)) {
             if (LOGD) Log.d(TAG, "updateBody() 3g tab");
             setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
@@ -1394,30 +1401,6 @@
         }
     };
 
-    @Deprecated
-    private boolean isMobilePolicySplit() {
-        final Context context = getActivity();
-        boolean retVal;
-        if (hasReadyMobileRadio(context)) {
-            final TelephonyManager tele = TelephonyManager.from(context);
-            retVal = mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
-        } else {
-            retVal = false;
-        }
-        if (LOGD) Log.d(TAG, "isMobilePolicySplit: retVal=" + retVal);
-        return retVal;
-    }
-
-    @Deprecated
-    private void setMobilePolicySplit(boolean split) {
-        final Context context = getActivity();
-        if (hasReadyMobileRadio(context)) {
-            final TelephonyManager tele = TelephonyManager.from(context);
-            if (LOGD) Log.d(TAG, "setMobilePolicySplit: split=" + split);
-            mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
-        }
-    }
-
     private static String getActiveSubscriberId(Context context) {
         final TelephonyManager tele = TelephonyManager.from(context);
         final String actualSubscriberId = tele.getSubscriberId();
diff --git a/src/com/android/settings/net/NetworkPolicyEditor.java b/src/com/android/settings/net/NetworkPolicyEditor.java
index ad2afa0..1268c3f 100644
--- a/src/com/android/settings/net/NetworkPolicyEditor.java
+++ b/src/com/android/settings/net/NetworkPolicyEditor.java
@@ -20,12 +20,7 @@
 import static android.net.NetworkPolicy.LIMIT_DISABLED;
 import static android.net.NetworkPolicy.SNOOZE_NEVER;
 import static android.net.NetworkPolicy.WARNING_DISABLED;
-import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
-import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
 import static android.net.NetworkTemplate.MATCH_WIFI;
-import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
-import static android.net.NetworkTemplate.buildTemplateMobile4g;
-import static android.net.NetworkTemplate.buildTemplateMobileAll;
 import static com.android.internal.util.Preconditions.checkNotNull;
 
 import android.net.NetworkPolicy;
@@ -37,11 +32,8 @@
 import android.text.format.Time;
 
 import com.google.android.collect.Lists;
-import com.google.android.collect.Sets;
 
 import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Objects;
 
 /**
  * Utility class to modify list of {@link NetworkPolicy}. Specifically knows
@@ -79,11 +71,6 @@
             mPolicies.add(policy);
         }
 
-        // force combine any split policies when disabled
-        if (!ENABLE_SPLIT_POLICIES) {
-            modified |= forceMobilePolicyCombined();
-        }
-
         // when we cleaned policies above, write back changes
         if (modified) writeAsync();
     }
@@ -247,108 +234,6 @@
     }
 
     /**
-     * Remove any split {@link NetworkPolicy}.
-     */
-    private boolean forceMobilePolicyCombined() {
-        final HashSet<String> subscriberIds = Sets.newHashSet();
-        for (NetworkPolicy policy : mPolicies) {
-            subscriberIds.add(policy.template.getSubscriberId());
-        }
-
-        boolean modified = false;
-        for (String subscriberId : subscriberIds) {
-            modified |= setMobilePolicySplitInternal(subscriberId, false);
-        }
-        return modified;
-    }
-
-    @Deprecated
-    public boolean isMobilePolicySplit(String subscriberId) {
-        boolean has3g = false;
-        boolean has4g = false;
-        for (NetworkPolicy policy : mPolicies) {
-            final NetworkTemplate template = policy.template;
-            if (Objects.equals(subscriberId, template.getSubscriberId())) {
-                switch (template.getMatchRule()) {
-                    case MATCH_MOBILE_3G_LOWER:
-                        has3g = true;
-                        break;
-                    case MATCH_MOBILE_4G:
-                        has4g = true;
-                        break;
-                }
-            }
-        }
-        return has3g && has4g;
-    }
-
-    @Deprecated
-    public void setMobilePolicySplit(String subscriberId, boolean split) {
-        if (setMobilePolicySplitInternal(subscriberId, split)) {
-            writeAsync();
-        }
-    }
-
-    /**
-     * Mutate {@link NetworkPolicy} for given subscriber, combining or splitting
-     * the policy as requested.
-     *
-     * @return {@code true} when any {@link NetworkPolicy} was mutated.
-     */
-    @Deprecated
-    private boolean setMobilePolicySplitInternal(String subscriberId, boolean split) {
-        final boolean beforeSplit = isMobilePolicySplit(subscriberId);
-
-        final NetworkTemplate template3g = buildTemplateMobile3gLower(subscriberId);
-        final NetworkTemplate template4g = buildTemplateMobile4g(subscriberId);
-        final NetworkTemplate templateAll = buildTemplateMobileAll(subscriberId);
-
-        if (split == beforeSplit) {
-            // already in requested state; skip
-            return false;
-
-        } else if (beforeSplit && !split) {
-            // combine, picking most restrictive policy
-            final NetworkPolicy policy3g = getPolicy(template3g);
-            final NetworkPolicy policy4g = getPolicy(template4g);
-
-            NetworkPolicy restrictive = null;
-            if ((policy3g == null) && (policy4g == null)) {
-                return false;
-            } else if (policy3g == null) {
-                restrictive = policy4g;
-            } else if (policy4g == null) {
-                restrictive = policy3g;
-            } else {
-                restrictive = policy3g.compareTo(policy4g) < 0 ? policy3g : policy4g;
-            }
-            mPolicies.remove(policy3g);
-            mPolicies.remove(policy4g);
-            mPolicies.add(new NetworkPolicy(templateAll, restrictive.cycleDay,
-                    restrictive.cycleTimezone, restrictive.warningBytes, restrictive.limitBytes,
-                    SNOOZE_NEVER, SNOOZE_NEVER, restrictive.metered, restrictive.inferred));
-            return true;
-
-        } else if (!beforeSplit && split) {
-            // duplicate existing policy into two rules
-            final NetworkPolicy policyAll = getPolicy(templateAll);
-            if (policyAll == null) {
-                return false;
-            }
-            mPolicies.remove(policyAll);
-            mPolicies.add(new NetworkPolicy(template3g, policyAll.cycleDay, policyAll.cycleTimezone,
-                    policyAll.warningBytes, policyAll.limitBytes, SNOOZE_NEVER, SNOOZE_NEVER,
-                    policyAll.metered, policyAll.inferred));
-            mPolicies.add(new NetworkPolicy(template4g, policyAll.cycleDay, policyAll.cycleTimezone,
-                    policyAll.warningBytes, policyAll.limitBytes, SNOOZE_NEVER, SNOOZE_NEVER,
-                    policyAll.metered, policyAll.inferred));
-            return true;
-        } else {
-            return false;
-        }
-    }
-
-    /**
      * Build a revised {@link NetworkTemplate} that matches the same rule, but
      * with an unquoted {@link NetworkTemplate#getNetworkId()}. Used to work
      * around legacy bugs.