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

Commit 41ff7ec8 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Revise data limit notifs, watch kernel alerts.

Teach NetworkPolicy limits to "snooze" when requested by user, and
notify with both dialog and notification.  Register for network alerts
through NMS to trigger updates immediately instead of waiting for
next stats update.

Enforce that all NetworkPolicy are unique on a template basis, and
move SCREEN_ON/OFF broadcasts to background thread.  Launch SystemUI
and Settings directly instead of using actions, and include full
NetworkTemplate in extras.

Tests to verify notification and snooze behavior.

Bug: 5057979, 5023579, 4723336, 5045721
Change-Id: I03724beff94a7c0547cb5220431ba8d4cd44d077
parent a94b9ad2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.net;

import android.net.INetworkPolicyListener;
import android.net.NetworkPolicy;
import android.net.NetworkTemplate;

/**
 * Interface that creates and modifies network policy rules.
@@ -37,4 +38,6 @@ interface INetworkPolicyManager {
    void setNetworkPolicies(in NetworkPolicy[] policies);
    NetworkPolicy[] getNetworkPolicies();

    void snoozePolicy(in NetworkTemplate template);

}
+26 −5
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import static com.android.internal.util.Preconditions.checkNotNull;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.util.Objects;

/**
 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle
 * and limits to be enforced.
@@ -30,20 +32,21 @@ import android.os.Parcelable;
public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
    public static final long WARNING_DISABLED = -1;
    public static final long LIMIT_DISABLED = -1;
    public static final long SNOOZE_NEVER = -1;

    public final NetworkTemplate template;
    public int cycleDay;
    public long warningBytes;
    public long limitBytes;
    public long lastSnooze;

    // TODO: teach how to snooze limit for current cycle

    public NetworkPolicy(
            NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes) {
    public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes,
            long lastSnooze) {
        this.template = checkNotNull(template, "missing NetworkTemplate");
        this.cycleDay = cycleDay;
        this.warningBytes = warningBytes;
        this.limitBytes = limitBytes;
        this.lastSnooze = lastSnooze;
    }

    public NetworkPolicy(Parcel in) {
@@ -51,6 +54,7 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
        cycleDay = in.readInt();
        warningBytes = in.readLong();
        limitBytes = in.readLong();
        lastSnooze = in.readLong();
    }

    /** {@inheritDoc} */
@@ -59,6 +63,7 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
        dest.writeInt(cycleDay);
        dest.writeLong(warningBytes);
        dest.writeLong(limitBytes);
        dest.writeLong(lastSnooze);
    }

    /** {@inheritDoc} */
@@ -79,10 +84,26 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
        return 0;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(template, cycleDay, warningBytes, limitBytes, lastSnooze);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof NetworkPolicy) {
            final NetworkPolicy other = (NetworkPolicy) obj;
            return Objects.equal(template, other.template) && cycleDay == other.cycleDay
                    && warningBytes == other.warningBytes && limitBytes == other.limitBytes
                    && lastSnooze == other.lastSnooze;
        }
        return false;
    }

    @Override
    public String toString() {
        return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes="
                + warningBytes + ", limitBytes=" + limitBytes;
                + warningBytes + ", limitBytes=" + limitBytes + ", lastSnooze=" + lastSnooze;
    }

    public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() {
+3 −19
Original line number Diff line number Diff line
@@ -52,26 +52,10 @@ public class NetworkPolicyManager {
    private static final boolean ALLOW_PLATFORM_APP_POLICY = true;

    /**
     * {@link Intent} action launched when user selects {@link NetworkPolicy}
     * warning notification.
     * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
     * applies to.
     */
    public static final String ACTION_DATA_USAGE_WARNING =
            "android.intent.action.DATA_USAGE_WARNING";

    /**
     * {@link Intent} action launched when user selects {@link NetworkPolicy}
     * limit notification.
     */
    public static final String ACTION_DATA_USAGE_LIMIT =
            "android.intent.action.DATA_USAGE_LIMIT";

    /**
     * {@link Intent} extra included in {@link #ACTION_DATA_USAGE_WARNING} and
     * {@link #ACTION_DATA_USAGE_LIMIT} to indicate which
     * {@link NetworkTemplate} rule it applies to.
     */
    public static final String EXTRA_NETWORK_TEMPLATE =
            "android.intent.extra.NETWORK_TEMPLATE";
    public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";

    private INetworkPolicyManager mService;

+16 −1
Original line number Diff line number Diff line
@@ -212,13 +212,28 @@ interface INetworkManagementService
    /**
     * Set quota for an interface.
     */
    void setInterfaceQuota(String iface, long quota);
    void setInterfaceQuota(String iface, long quotaBytes);

    /**
     * Remove quota for an interface.
     */
    void removeInterfaceQuota(String iface);

    /**
     * Set alert for an interface; requires that iface already has quota.
     */
    void setInterfaceAlert(String iface, long alertBytes);

    /**
     * Remove alert for an interface.
     */
    void removeInterfaceAlert(String iface);

    /**
     * Set alert across all interfaces.
     */
    void setGlobalAlert(long alertBytes);

    /**
     * Control network activity of a UID over interfaces with a quota limit.
     */
+9 −0
Original line number Diff line number Diff line
@@ -3034,6 +3034,15 @@
    <!-- Notification body when data usage has exceeded limit threshold, and has been disabled. [CHAR LIMIT=32] -->
    <string name="data_usage_limit_body">tap to enable</string>

    <!-- Notification title when 2G-3G data usage has exceeded limit threshold. [CHAR LIMIT=32] -->
    <string name="data_usage_3g_limit_snoozed_title">2G-3G data limit exceeded</string>
    <!-- Notification title when 4G data usage has exceeded limit threshold. [CHAR LIMIT=32] -->
    <string name="data_usage_4g_limit_snoozed_title">4G data limit exceeded</string>
    <!-- Notification title when mobile data usage has exceeded limit threshold. [CHAR LIMIT=32] -->
    <string name="data_usage_mobile_limit_snoozed_title">Mobile data limit exceeded</string>
    <!-- Notification body when data usage has exceeded limit threshold. [CHAR LIMIT=32] -->
    <string name="data_usage_limit_snoozed_body"><xliff:g id="size" example="3.8GB">%s</xliff:g> over specified limit</string>

    <!-- SSL Certificate dialogs -->
    <!-- Title for an SSL Certificate dialog -->
    <string name="ssl_certificate">Security certificate</string>
Loading