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

Commit 81aa0971 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Make ThrottleService notice policy changes.

Added ContentObserver to watch relevant Secure Settings.
Also added new policy-change broadcast to let settings know.
Lastly reorged things a bit so that all of our broadcasts are sent at boot so the sticky ones
are populated.

bug:2576057
Change-Id: Ie11ffb057de9c801a5088612cd464ea062f3a666
parent cb270410
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -73,6 +73,12 @@ public class ThrottleManager
     */
    public static final String EXTRA_THROTTLE_LEVEL = "level";

    /**
     * Broadcast on boot and whenever the settings change.
     * {@hide}
     */
    public static final String POLICY_CHANGED_ACTION = "android.net.thrott.POLICY_CHANGED_ACTION";

    // {@hide}
    public static final int DIRECTION_TX = 0;
    // {@hide}
@@ -103,6 +109,8 @@ public class ThrottleManager
    // @hide
    public static final int PERIOD_SECOND = 11;

    

    /**
     * returns a long of the ms from the epoch to the time the current cycle ends for the
     * named interface
@@ -147,7 +155,7 @@ public class ThrottleManager

    /**
     * returns the number of bytes read+written after which a particular cliff
     * takes effect on the named iface.  Currently only cliff #0 is supported (1 step)
     * takes effect on the named iface.  Currently only cliff #1 is supported (1 step)
     * {@hide}
     */
    public long getCliffThreshold(String iface, int cliff) {
@@ -160,7 +168,7 @@ public class ThrottleManager

    /**
     * returns the thottling bandwidth (bps) for a given cliff # on the named iface.
     * only cliff #0 is currently supported.
     * only cliff #1 is currently supported.
     * {@hide}
     */
    public int getCliffLevel(String iface, int cliff) {
+55 −18
Original line number Diff line number Diff line
@@ -22,12 +22,14 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.net.IThrottleManager;
import android.net.ThrottleManager;
import android.os.Binder;
@@ -98,8 +100,6 @@ public class ThrottleService extends IThrottleManager.Stub {

    private DataRecorder mRecorder;

    private int mThrottleLevel; // 0 for none, 1 for first throttle val, 2 for next, etc

    private String mPolicyIface;

    private static final int NOTIFICATION_WARNING   = 2;
@@ -109,6 +109,12 @@ public class ThrottleService extends IThrottleManager.Stub {
    private Notification mThrottlingNotification;
    private boolean mWarningNotificationSent = false;

    private SettingsObserver mSettingsObserver;

    private int mThrottleIndex; // 0 for none, 1 for first throttle val, 2 for next, etc
    private static final int THROTTLE_INDEX_UNINITIALIZED = -1;
    private static final int THROTTLE_INDEX_UNTHROTTLED   =  0;

    public ThrottleService(Context context) {
        if (DBG) Slog.d(TAG, "Starting ThrottleService");
        mContext = context;
@@ -126,6 +132,38 @@ public class ThrottleService extends IThrottleManager.Stub {
                Context.NOTIFICATION_SERVICE);
    }

    private static class SettingsObserver extends ContentObserver {
        private int mMsg;
        private Handler mHandler;
        SettingsObserver(Handler handler, int msg) {
            super(handler);
            mHandler = handler;
            mMsg = msg;
        }

        void observe(Context context) {
            ContentResolver resolver = context.getContentResolver();
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_POLLING_SEC), false, this);
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_THRESHOLD), false, this);
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_VALUE), false, this);
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_RESET_DAY), false, this);
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_NOTIFICATION_TYPE), false, this);
            resolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.THROTTLE_IFACE), false, this);
                    // TODO - add help url
        }

        @Override
        public void onChange(boolean selfChange) {
            mHandler.obtainMessage(mMsg).sendToTarget();
        }
    }

    private void enforceAccessPermission() {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_NETWORK_STATE,
@@ -145,7 +183,7 @@ public class ThrottleService extends IThrottleManager.Stub {
    //TODO - a better name?  getCliffByteCountThreshold?
    public synchronized long getCliffThreshold(String iface, int cliff) {
        enforceAccessPermission();
        if ((cliff == 0) && iface.equals(mPolicyIface)) {
        if ((cliff == 1) && iface.equals(mPolicyIface)) {
            return mPolicyThreshold;
        }
        return 0;
@@ -153,7 +191,7 @@ public class ThrottleService extends IThrottleManager.Stub {
    // TODO - a better name? getThrottleRate?
    public synchronized int getCliffLevel(String iface, int cliff) {
        enforceAccessPermission();
        if ((cliff == 0) && iface.equals(mPolicyIface)) {
        if ((cliff == 1) && iface.equals(mPolicyIface)) {
            return mPolicyThrottleValue;
        }
        return 0;
@@ -179,7 +217,7 @@ public class ThrottleService extends IThrottleManager.Stub {
    // TODO - a better name - getCurrentThrottleRate?
    public synchronized int getThrottle(String iface) {
        enforceAccessPermission();
        if (iface.equals(mPolicyIface) && (mThrottleLevel == 1)) {
        if (iface.equals(mPolicyIface) && (mThrottleIndex == 1)) {
            return mPolicyThrottleValue;
        }
        return 0;
@@ -208,6 +246,9 @@ public class ThrottleService extends IThrottleManager.Stub {
        mThread.start();
        mHandler = new MyHandler(mThread.getLooper());
        mHandler.obtainMessage(EVENT_REBOOT_RECOVERY).sendToTarget();

        mSettingsObserver = new SettingsObserver(mHandler, EVENT_POLICY_CHANGED);
        mSettingsObserver.observe(mContext);
    }


@@ -242,8 +283,7 @@ public class ThrottleService extends IThrottleManager.Stub {
            // check for sim change TODO
            // reregister for notification of policy change

            // register for roaming indication change
            // check for roaming TODO
            mThrottleIndex = THROTTLE_INDEX_UNINITIALIZED;

            mRecorder = new DataRecorder(mContext, ThrottleService.this);

@@ -300,14 +340,10 @@ public class ThrottleService extends IThrottleManager.Stub {
                    ", resetDay=" + mPolicyResetDay + ", noteType=" +
                    mPolicyNotificationsAllowedMask);

            Calendar end = calculatePeriodEnd();
            Calendar start = calculatePeriodStart(end);

            mRecorder.setNextPeriod(start,end);
            onResetAlarm();

            mAlarmManager.cancel(mPendingResetIntent);
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(),
                    mPendingResetIntent);
            Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION);
            mContext.sendBroadcast(broadcast);
        }

        private void onPollAlarm() {
@@ -357,9 +393,9 @@ public class ThrottleService extends IThrottleManager.Stub {

            // check if we need to throttle
            if (currentTotal > mPolicyThreshold) {
                if (mThrottleLevel != 1) {
                if (mThrottleIndex != 1) {
                    synchronized (ThrottleService.this) {
                        mThrottleLevel = 1;
                        mThrottleIndex = 1;
                    }
                    if (DBG) Slog.d(TAG, "Threshold " + mPolicyThreshold + " exceeded!");
                    try {
@@ -429,9 +465,9 @@ public class ThrottleService extends IThrottleManager.Stub {


        private synchronized void clearThrottleAndNotification() {
            if (mThrottleLevel == 1) {
            if (mThrottleIndex != THROTTLE_INDEX_UNTHROTTLED) {
                synchronized (ThrottleService.this) {
                    mThrottleLevel = 0;
                    mThrottleIndex = THROTTLE_INDEX_UNTHROTTLED;
                }
                try {
                    mNMService.setInterfaceThrottle(mPolicyIface, -1, -1);
@@ -499,6 +535,7 @@ public class ThrottleService extends IThrottleManager.Stub {

            mRecorder.setNextPeriod(start,end);

            mAlarmManager.cancel(mPendingResetIntent);
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(),
                    mPendingResetIntent);
        }