Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c23f8686 authored by lesl's avatar lesl Committed by Automerger Merge Worker
Browse files

carrier data usage: Use carrier template as default policy am: b8b282e5

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14782679

Change-Id: I8bdde6aaeea04c4c70a37d5a71ad31a5590323a9
parents e1a613ad b8b282e5
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -514,6 +514,10 @@ public class NetworkTemplate implements Parcelable {
        return mSubscriberIdMatchRule;
    }

    public int getMeteredness() {
        return mMetered;
    }

    /**
     * Test if given {@link NetworkIdentity} matches this template.
     */
+81 −41
Original line number Diff line number Diff line
@@ -99,8 +99,12 @@ import static android.net.NetworkPolicyManager.resolveNetworkId;
import static android.net.NetworkPolicyManager.uidPoliciesToString;
import static android.net.NetworkPolicyManager.uidRulesToString;
import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkTemplate.MATCH_CARRIER;
import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.NetworkTemplate.buildTemplateCarrierMetered;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.net.netstats.provider.NetworkStatsProvider.QUOTA_UNLIMITED;
@@ -349,7 +353,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    private static final int VERSION_SWITCH_UID = 10;
    private static final int VERSION_ADDED_CYCLE = 11;
    private static final int VERSION_ADDED_NETWORK_TYPES = 12;
    private static final int VERSION_LATEST = VERSION_ADDED_NETWORK_TYPES;
    private static final int VERSION_SUPPORTED_CARRIER_USAGE = 13;
    private static final int VERSION_LATEST = VERSION_SUPPORTED_CARRIER_USAGE;

    @VisibleForTesting
    public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING;
@@ -374,7 +379,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground";
    private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate";
    private static final String ATTR_SUBSCRIBER_ID = "subscriberId";
    private static final String ATTR_SUBSCRIBER_ID_MATCH_RULE = "subscriberIdMatchRule";
    private static final String ATTR_NETWORK_ID = "networkId";
    private static final String ATTR_TEMPLATE_METERED = "templateMetered";
    @Deprecated private static final String ATTR_CYCLE_DAY = "cycleDay";
    @Deprecated private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone";
    private static final String ATTR_CYCLE_START = "cycleStart";
@@ -1451,7 +1458,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
     */
    @GuardedBy("mNetworkPoliciesSecondLock")
    private int findRelevantSubIdNL(NetworkTemplate template) {
        // Mobile template is relevant when any active subscriber matches
        // Carrier template is relevant when any active subscriber matches
        for (int i = 0; i < mSubIdToSubscriberId.size(); i++) {
            final int subId = mSubIdToSubscriberId.keyAt(i);
            final String subscriberId = mSubIdToSubscriberId.valueAt(i);
@@ -1530,6 +1537,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            }
            case TYPE_LIMIT: {
                switch (policy.template.getMatchRule()) {
                    case MATCH_CARRIER:
                    case MATCH_MOBILE:
                        title = res.getText(R.string.data_usage_mobile_limit_title);
                        break;
@@ -1558,6 +1566,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            }
            case TYPE_LIMIT_SNOOZED: {
                switch (policy.template.getMatchRule()) {
                    case MATCH_CARRIER:
                    case MATCH_MOBILE:
                        title = res.getText(R.string.data_usage_mobile_limit_snoozed_title);
                        break;
@@ -1655,7 +1664,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

        synchronized (mUidRulesFirstLock) {
            synchronized (mNetworkPoliciesSecondLock) {
                ensureActiveMobilePolicyAL();
                ensureActiveCarrierPolicyAL();
                normalizePoliciesNL();
                updateNetworkEnabledNL();
                updateNetworkRulesNL();
@@ -1680,17 +1689,17 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    }

    /**
     * Update mobile policies with data cycle information from {@link CarrierConfigManager}
     * Update carrier policies with data cycle information from {@link CarrierConfigManager}
     * if necessary.
     *
     * @param subId that has its associated NetworkPolicy updated if necessary
     * @return if any policies were updated
     */
    @GuardedBy("mNetworkPoliciesSecondLock")
    private boolean maybeUpdateMobilePolicyCycleAL(int subId, String subscriberId) {
        if (LOGV) Slog.v(TAG, "maybeUpdateMobilePolicyCycleAL()");
    private boolean maybeUpdateCarrierPolicyCycleAL(int subId, String subscriberId) {
        if (LOGV) Slog.v(TAG, "maybeUpdateCarrierPolicyCycleAL()");

        // find and update the mobile NetworkPolicy for this subscriber id
        // find and update the carrier NetworkPolicy for this subscriber id
        boolean policyUpdated = false;
        final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true,
@@ -1699,21 +1708,21 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            final NetworkTemplate template = mNetworkPolicy.keyAt(i);
            if (template.matches(probeIdent)) {
                final NetworkPolicy policy = mNetworkPolicy.valueAt(i);
                policyUpdated |= updateDefaultMobilePolicyAL(subId, policy);
                policyUpdated |= updateDefaultCarrierPolicyAL(subId, policy);
            }
        }
        return policyUpdated;
    }

    /**
     * Returns the cycle day that should be used for a mobile NetworkPolicy.
     * Returns the cycle day that should be used for a carrier NetworkPolicy.
     *
     * It attempts to get an appropriate cycle day from the passed in CarrierConfig. If it's unable
     * to do so, it returns the fallback value.
     *
     * @param config The CarrierConfig to read the value from.
     * @param fallbackCycleDay to return if the CarrierConfig can't be read.
     * @return cycleDay to use in the mobile NetworkPolicy.
     * @return cycleDay to use in the carrier NetworkPolicy.
     */
    @VisibleForTesting
    int getCycleDayFromCarrierConfig(@Nullable PersistableBundle config,
@@ -1738,14 +1747,14 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    }

    /**
     * Returns the warning bytes that should be used for a mobile NetworkPolicy.
     * Returns the warning bytes that should be used for a carrier NetworkPolicy.
     *
     * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable
     * to do so, it returns the fallback value.
     *
     * @param config The CarrierConfig to read the value from.
     * @param fallbackWarningBytes to return if the CarrierConfig can't be read.
     * @return warningBytes to use in the mobile NetworkPolicy.
     * @return warningBytes to use in the carrier NetworkPolicy.
     */
    @VisibleForTesting
    long getWarningBytesFromCarrierConfig(@Nullable PersistableBundle config,
@@ -1771,14 +1780,14 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    }

    /**
     * Returns the limit bytes that should be used for a mobile NetworkPolicy.
     * Returns the limit bytes that should be used for a carrier NetworkPolicy.
     *
     * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable
     * to do so, it returns the fallback value.
     *
     * @param config The CarrierConfig to read the value from.
     * @param fallbackLimitBytes to return if the CarrierConfig can't be read.
     * @return limitBytes to use in the mobile NetworkPolicy.
     * @return limitBytes to use in the carrier NetworkPolicy.
     */
    @VisibleForTesting
    long getLimitBytesFromCarrierConfig(@Nullable PersistableBundle config,
@@ -1824,8 +1833,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                synchronized (mNetworkPoliciesSecondLock) {
                    final String subscriberId = mSubIdToSubscriberId.get(subId, null);
                    if (subscriberId != null) {
                        ensureActiveMobilePolicyAL(subId, subscriberId);
                        maybeUpdateMobilePolicyCycleAL(subId, subscriberId);
                        ensureActiveCarrierPolicyAL(subId, subscriberId);
                        maybeUpdateCarrierPolicyCycleAL(subId, subscriberId);
                    } else {
                        Slog.wtf(TAG, "Missing subscriberId for subId " + subId);
                    }
@@ -1911,10 +1920,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        // TODO: reach into ConnectivityManager to proactively disable bringing
        // up this network, since we know that traffic will be blocked.

        if (template.getMatchRule() == MATCH_MOBILE) {
            // If mobile data usage hits the limit or if the user resumes the data, we need to
        if (template.getMatchRule() == MATCH_MOBILE
                || template.getMatchRule() == MATCH_CARRIER) {
            // If carrier data usage hits the limit or if the user resumes the data, we need to
            // notify telephony.

            // TODO: It needs to check if it matches the merged WIFI and notify to wifi module.
            final IntArray matchingSubIds = new IntArray();
            synchronized (mNetworkPoliciesSecondLock) {
                for (int i = 0; i < mSubIdToSubscriberId.size(); i++) {
@@ -2174,7 +2185,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                        .truncatedTo(ChronoUnit.DAYS)
                        .toInstant().toEpochMilli();
                final long totalBytes = getTotalBytes(
                        NetworkTemplate.buildTemplateMobileAll(snapshot.getSubscriberId()),
                        buildTemplateCarrierMetered(snapshot.getSubscriberId()),
                        start, startOfDay);
                final long remainingBytes = limitBytes - totalBytes;
                // Number of remaining days including current day
@@ -2200,31 +2211,31 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

    /**
     * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we
     * have at least a default mobile policy defined.
     * have at least a default carrier policy defined.
     */
    @GuardedBy("mNetworkPoliciesSecondLock")
    private void ensureActiveMobilePolicyAL() {
        if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyAL()");
    private void ensureActiveCarrierPolicyAL() {
        if (LOGV) Slog.v(TAG, "ensureActiveCarrierPolicyAL()");
        if (mSuppressDefaultPolicy) return;

        for (int i = 0; i < mSubIdToSubscriberId.size(); i++) {
            final int subId = mSubIdToSubscriberId.keyAt(i);
            final String subscriberId = mSubIdToSubscriberId.valueAt(i);

            ensureActiveMobilePolicyAL(subId, subscriberId);
            ensureActiveCarrierPolicyAL(subId, subscriberId);
        }
    }

    /**
     * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we
     * have at least a default mobile policy defined.
     * have at least a default carrier policy defined.
     *
     * @param subId to build a default policy for
     * @param subscriberId that we check for an existing policy
     * @return true if a mobile network policy was added, or false one already existed.
     * @return true if a carrier network policy was added, or false one already existed.
     */
    @GuardedBy("mNetworkPoliciesSecondLock")
    private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) {
    private boolean ensureActiveCarrierPolicyAL(int subId, String subscriberId) {
        // Poke around to see if we already have a policy
        final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true,
@@ -2243,7 +2254,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        Slog.i(TAG, "No policy for subscriber "
                + NetworkIdentityUtils.scrubSubscriberId(subscriberId)
                + "; generating default policy");
        final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId);
        final NetworkPolicy policy = buildDefaultCarrierPolicy(subId, subscriberId);
        addNetworkPolicyAL(policy);
        return true;
    }
@@ -2263,8 +2274,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
    }

    @VisibleForTesting
    NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) {
        final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
    NetworkPolicy buildDefaultCarrierPolicy(int subId, String subscriberId) {
        final NetworkTemplate template = buildTemplateCarrierMetered(subscriberId);
        final RecurrenceRule cycleRule = NetworkPolicy
                .buildRule(ZonedDateTime.now().getDayOfMonth(), ZoneId.systemDefault());
        final NetworkPolicy policy = new NetworkPolicy(template, cycleRule,
@@ -2272,7 +2283,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                SNOOZE_NEVER, SNOOZE_NEVER, true, true);
        synchronized (mUidRulesFirstLock) {
            synchronized (mNetworkPoliciesSecondLock) {
                updateDefaultMobilePolicyAL(subId, policy);
                updateDefaultCarrierPolicyAL(subId, policy);
            }
        }
        return policy;
@@ -2286,7 +2297,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
     * @return if the policy was modified
     */
    @GuardedBy("mNetworkPoliciesSecondLock")
    private boolean updateDefaultMobilePolicyAL(int subId, NetworkPolicy policy) {
    private boolean updateDefaultCarrierPolicyAL(int subId, NetworkPolicy policy) {
        if (!policy.inferred) {
            if (LOGD) Slog.d(TAG, "Ignoring user-defined policy " + policy);
            return false;
@@ -2372,14 +2383,33 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                        mLoadedRestrictBackground = (version >= VERSION_ADDED_RESTRICT_BACKGROUND)
                                && readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                    } else if (TAG_NETWORK_POLICY.equals(tag)) {
                        final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE);
                        int templateType = readIntAttribute(in, ATTR_NETWORK_TEMPLATE);
                        final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID);
                        final String networkId;
                        final int subscriberIdMatchRule;
                        final int templateMeteredness;
                        if (version >= VERSION_ADDED_NETWORK_ID) {
                            networkId = in.getAttributeValue(null, ATTR_NETWORK_ID);
                        } else {
                            networkId = null;
                        }

                        if (version >= VERSION_SUPPORTED_CARRIER_USAGE) {
                            subscriberIdMatchRule = readIntAttribute(in,
                                    ATTR_SUBSCRIBER_ID_MATCH_RULE);
                            templateMeteredness = readIntAttribute(in, ATTR_TEMPLATE_METERED);

                        } else {
                            subscriberIdMatchRule = NetworkTemplate.SUBSCRIBER_ID_MATCH_RULE_EXACT;
                            if (templateType == MATCH_MOBILE) {
                                Log.d(TAG, "Update template match rule from mobile to carrier and"
                                        + " force to metered");
                                templateType = MATCH_CARRIER;
                                templateMeteredness = METERED_YES;
                            } else {
                                templateMeteredness = METERED_ALL;
                            }
                        }
                        final RecurrenceRule cycleRule;
                        if (version >= VERSION_ADDED_CYCLE) {
                            final String start = readStringAttribute(in, ATTR_CYCLE_START);
@@ -2413,7 +2443,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                        if (version >= VERSION_ADDED_METERED) {
                            metered = readBooleanAttribute(in, ATTR_METERED);
                        } else {
                            switch (networkTemplate) {
                            switch (templateType) {
                                case MATCH_MOBILE:
                                    metered = true;
                                    break;
@@ -2433,9 +2463,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                        } else {
                            inferred = false;
                        }

                        final NetworkTemplate template = new NetworkTemplate(networkTemplate,
                                subscriberId, networkId);
                        final NetworkTemplate template = new NetworkTemplate(templateType,
                                subscriberId, new String[] { subscriberId },
                                networkId, templateMeteredness, NetworkStats.ROAMING_ALL,
                                NetworkStats.DEFAULT_NETWORK_ALL, NetworkTemplate.NETWORK_TYPE_ALL,
                                NetworkTemplate.OEM_MANAGED_ALL, subscriberIdMatchRule);
                        if (template.isPersistable()) {
                            mNetworkPolicy.put(template, new NetworkPolicy(template, cycleRule,
                                    warningBytes, limitBytes, lastWarningSnooze,
@@ -2642,10 +2674,14 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                if (subscriberId != null) {
                    out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId);
                }
                writeIntAttribute(out, ATTR_SUBSCRIBER_ID_MATCH_RULE,
                        template.getSubscriberIdMatchRule());
                final String networkId = template.getNetworkId();
                if (networkId != null) {
                    out.attribute(null, ATTR_NETWORK_ID, networkId);
                }
                writeIntAttribute(out, ATTR_TEMPLATE_METERED,
                        template.getMeteredness());
                writeStringAttribute(out, ATTR_CYCLE_START,
                        RecurrenceRule.convertZonedDateTime(policy.cycleRule.start));
                writeStringAttribute(out, ATTR_CYCLE_END,
@@ -3513,8 +3549,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

                    final String subscriberId = mSubIdToSubscriberId.get(subId, null);
                    if (subscriberId != null) {
                        ensureActiveMobilePolicyAL(subId, subscriberId);
                        maybeUpdateMobilePolicyCycleAL(subId, subscriberId);
                        ensureActiveCarrierPolicyAL(subId, subscriberId);
                        maybeUpdateCarrierPolicyCycleAL(subId, subscriberId);
                    } else {
                        Slog.wtf(TAG, "Missing subscriberId for subId " + subId);
                    }
@@ -5575,11 +5611,15 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
            return;
        }

        // Turn mobile data limit off
        // Turn carrier/mobile data limit off
        NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName());
        NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber);
        NetworkTemplate templateCarrier = buildTemplateCarrierMetered(subscriber);
        NetworkTemplate templateMobile = buildTemplateMobileAll(subscriber);
        for (NetworkPolicy policy : policies) {
            if (policy.template.equals(template)) {
            //  All policies loaded from disk will be carrier templates, and setting will also only
            //  set carrier templates, but we clear mobile templates just in case one is set by
            //  some other caller
            if (policy.template.equals(templateCarrier) || policy.template.equals(templateMobile)) {
                policy.limitBytes = NetworkPolicy.LIMIT_DISABLED;
                policy.inferred = false;
                policy.clearSnooze();
+6 −0
Original line number Diff line number Diff line
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<policy-list version="12" restrictBackground="false">
  <network-policy networkTemplate="1" subscriberId="466977604504520" cycleStart="2020-01-09T00:00+08:00[Asia/Taipei]" cyclePeriod="P1M" warningBytes="2147483648" limitBytes="-1" lastWarningSnooze="-1" lastLimitSnooze="-1" metered="true" inferred="true" />
</policy-list>
<whitelist />
+5 −0
Original line number Diff line number Diff line
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<policy-list version="13" restrictBackground="false">
<network-policy networkTemplate="4" subscriberIdMatchRule="1" networkId="TEST_SSID" templateMetered="0" cycleStart="2020-01-09T00:00+08:00[Asia/Taipei]" cyclePeriod="P1M" warningBytes="2147483648" limitBytes="-1" lastWarningSnooze="-1" lastLimitSnooze="-1" metered="true" inferred="true" />
</policy-list>
<whitelist />