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

Commit 53313d7e authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Iterate NetworkPolicy towards SubscriptionPlan.

Add new SubscriptionPlan API to describe the various types of carrier
data plans.  Internally the OS will only use the first plan for
driving policy, but it will blindly plumb through the details for
Settings to display any secondary plans.

As part of getting Settings ready to roll towards SubscriptionPlan,
reduce references to NetworkPolicy internal fields.  All usage cycle
details are now obtained from an Iterator which converts to
SubscriptionPlan under the hood.

Replace all data usage cycle calculation with new SubscriptionPlan
implementation, and retrofit large suite of existing tests to
exercise and verify the new logic.

Offer a debugging property that can be used to return "fake" plan
examples for testing.

Bug: 63391323
Test: bit FrameworksServicesTests:com.android.server.NetworkPolicyManagerServiceTest
Exempt-From-Owner-Approval: Bug 63673347
Change-Id: I889c653980eeb7887abdfa4f5b6986f35855ee6d
parent 43d2a170
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ package android {
    field public static final java.lang.String MANAGE_CA_CERTIFICATES = "android.permission.MANAGE_CA_CERTIFICATES";
    field public static final java.lang.String MANAGE_DEVICE_ADMINS = "android.permission.MANAGE_DEVICE_ADMINS";
    field public static final java.lang.String MANAGE_DOCUMENTS = "android.permission.MANAGE_DOCUMENTS";
    field public static final java.lang.String MANAGE_FALLBACK_SUBSCRIPTION_PLANS = "android.permission.MANAGE_FALLBACK_SUBSCRIPTION_PLANS";
    field public static final java.lang.String MANAGE_OWN_CALLS = "android.permission.MANAGE_OWN_CALLS";
    field public static final java.lang.String MANAGE_USB = "android.permission.MANAGE_USB";
    field public static final java.lang.String MANAGE_USERS = "android.permission.MANAGE_USERS";
+6 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.net.NetworkPolicy;
import android.net.NetworkQuotaInfo;
import android.net.NetworkState;
import android.net.NetworkTemplate;
import android.telephony.SubscriptionPlan;

/**
 * Interface that creates and modifies network policy rules.
@@ -67,5 +68,10 @@ interface INetworkPolicyManager {

    NetworkQuotaInfo getNetworkQuotaInfo(in NetworkState state);

    SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage);
    void setSubscriptionPlans(int subId, in SubscriptionPlan[] plans, String callingPackage);

    String getSubscriptionPlanOwner(int subId);

    void factoryReset(String subscriber);
}
+11 −8
Original line number Diff line number Diff line
@@ -46,17 +46,20 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
    public static final long SNOOZE_NEVER = -1;

    public NetworkTemplate template;
    public int cycleDay;
    public String cycleTimezone;
    public long warningBytes;
    public long limitBytes;
    public long lastWarningSnooze;
    public long lastLimitSnooze;
    @Deprecated public boolean metered;
    public boolean inferred;
    @Deprecated public int cycleDay = CYCLE_NONE;
    @Deprecated public String cycleTimezone = "UTC";
    public long warningBytes = WARNING_DISABLED;
    public long limitBytes = LIMIT_DISABLED;
    public long lastWarningSnooze = SNOOZE_NEVER;
    public long lastLimitSnooze = SNOOZE_NEVER;
    @Deprecated public boolean metered = true;
    public boolean inferred = false;

    private static final long DEFAULT_MTU = 1500;

    public NetworkPolicy() {
    }

    @Deprecated
    public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone,
            long warningBytes, long limitBytes, boolean metered) {
+6 −69
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package android.net;

import static android.content.pm.PackageManager.GET_SIGNATURES;
import static android.net.NetworkPolicy.CYCLE_NONE;

import android.annotation.SystemService;
import android.app.ActivityManager;
@@ -30,13 +29,15 @@ import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.os.RemoteException;
import android.os.UserHandle;
import android.telephony.SubscriptionPlan;
import android.util.DebugUtils;
import android.util.Pair;

import com.google.android.collect.Sets;

import java.util.Calendar;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.TimeZone;
import java.util.Iterator;

/**
 * Manager for creating and modifying network policy rules.
@@ -251,73 +252,9 @@ public class NetworkPolicyManager {
        }
    }

    /**
     * Compute the last cycle boundary for the given {@link NetworkPolicy}. For
     * example, if cycle day is 20th, and today is June 15th, it will return May
     * 20th. When cycle day doesn't exist in current month, it snaps to the 1st
     * of following month.
     *
     * @hide
     */
    public static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) {
        if (policy.cycleDay == CYCLE_NONE) {
            throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
        }

        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(policy.cycleTimezone));
        cal.setTimeInMillis(currentTime);
        snapToCycleDay(cal, policy.cycleDay);

        if (cal.getTimeInMillis() >= currentTime) {
            // Cycle boundary is beyond now, use last cycle boundary
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.MONTH, -1);
            snapToCycleDay(cal, policy.cycleDay);
        }

        return cal.getTimeInMillis();
    }

    /** {@hide} */
    public static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) {
        if (policy.cycleDay == CYCLE_NONE) {
            throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
        }

        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(policy.cycleTimezone));
        cal.setTimeInMillis(currentTime);
        snapToCycleDay(cal, policy.cycleDay);

        if (cal.getTimeInMillis() <= currentTime) {
            // Cycle boundary is before now, use next cycle boundary
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.MONTH, 1);
            snapToCycleDay(cal, policy.cycleDay);
        }

        return cal.getTimeInMillis();
    }

    /**
     * Snap to the cycle day for the current month given; when cycle day doesn't
     * exist, it snaps to last second of current month.
     *
     * @hide
     */
    public static void snapToCycleDay(Calendar cal, int cycleDay) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        if (cycleDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            cal.add(Calendar.MONTH, 1);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.add(Calendar.SECOND, -1);
        } else {
            cal.set(Calendar.DAY_OF_MONTH, cycleDay);
        }
    public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) {
        return SubscriptionPlan.convert(policy).cycleIterator();
    }

    /**
+6 −0
Original line number Diff line number Diff line
@@ -3139,6 +3139,12 @@
    <permission android:name="android.permission.MANAGE_NETWORK_POLICY"
        android:protectionLevel="signature" />

    <!-- @SystemApi Allows an application to manage fallback subscription plans.
         Note that another app providing plans for an explicit HNI will always
         take precidence over these fallback plans. @hide -->
    <permission android:name="android.permission.MANAGE_FALLBACK_SUBSCRIPTION_PLANS"
        android:protectionLevel="signature|privileged" />

    <!-- @SystemApi Allows an application to account its network traffic against other UIDs. Used
         by system services like download manager and media server. Not for use by
         third party apps. @hide -->
Loading