Merge "Make ThrottleService notice policy changes." into froyo

This commit is contained in:
Robert Greenwalt
2010-04-09 13:00:05 -07:00
committed by Android (Google) Code Review
2 changed files with 65 additions and 20 deletions

View File

@@ -73,6 +73,12 @@ public class ThrottleManager
*/ */
public static final String EXTRA_THROTTLE_LEVEL = "level"; 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} // {@hide}
public static final int DIRECTION_TX = 0; public static final int DIRECTION_TX = 0;
// {@hide} // {@hide}
@@ -103,6 +109,8 @@ public class ThrottleManager
// @hide // @hide
public static final int PERIOD_SECOND = 11; 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 * returns a long of the ms from the epoch to the time the current cycle ends for the
* named interface * named interface
@@ -147,7 +155,7 @@ public class ThrottleManager
/** /**
* returns the number of bytes read+written after which a particular cliff * 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} * {@hide}
*/ */
public long getCliffThreshold(String iface, int cliff) { 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. * 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} * {@hide}
*/ */
public int getCliffLevel(String iface, int cliff) { public int getCliffLevel(String iface, int cliff) {

View File

@@ -22,12 +22,14 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.net.IThrottleManager; import android.net.IThrottleManager;
import android.net.ThrottleManager; import android.net.ThrottleManager;
import android.os.Binder; import android.os.Binder;
@@ -98,8 +100,6 @@ public class ThrottleService extends IThrottleManager.Stub {
private DataRecorder mRecorder; private DataRecorder mRecorder;
private int mThrottleLevel; // 0 for none, 1 for first throttle val, 2 for next, etc
private String mPolicyIface; private String mPolicyIface;
private static final int NOTIFICATION_WARNING = 2; private static final int NOTIFICATION_WARNING = 2;
@@ -109,6 +109,12 @@ public class ThrottleService extends IThrottleManager.Stub {
private Notification mThrottlingNotification; private Notification mThrottlingNotification;
private boolean mWarningNotificationSent = false; 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) { public ThrottleService(Context context) {
if (DBG) Slog.d(TAG, "Starting ThrottleService"); if (DBG) Slog.d(TAG, "Starting ThrottleService");
mContext = context; mContext = context;
@@ -126,6 +132,38 @@ public class ThrottleService extends IThrottleManager.Stub {
Context.NOTIFICATION_SERVICE); 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() { private void enforceAccessPermission() {
mContext.enforceCallingOrSelfPermission( mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.ACCESS_NETWORK_STATE, android.Manifest.permission.ACCESS_NETWORK_STATE,
@@ -145,7 +183,7 @@ public class ThrottleService extends IThrottleManager.Stub {
//TODO - a better name? getCliffByteCountThreshold? //TODO - a better name? getCliffByteCountThreshold?
public synchronized long getCliffThreshold(String iface, int cliff) { public synchronized long getCliffThreshold(String iface, int cliff) {
enforceAccessPermission(); enforceAccessPermission();
if ((cliff == 0) && iface.equals(mPolicyIface)) { if ((cliff == 1) && iface.equals(mPolicyIface)) {
return mPolicyThreshold; return mPolicyThreshold;
} }
return 0; return 0;
@@ -153,7 +191,7 @@ public class ThrottleService extends IThrottleManager.Stub {
// TODO - a better name? getThrottleRate? // TODO - a better name? getThrottleRate?
public synchronized int getCliffLevel(String iface, int cliff) { public synchronized int getCliffLevel(String iface, int cliff) {
enforceAccessPermission(); enforceAccessPermission();
if ((cliff == 0) && iface.equals(mPolicyIface)) { if ((cliff == 1) && iface.equals(mPolicyIface)) {
return mPolicyThrottleValue; return mPolicyThrottleValue;
} }
return 0; return 0;
@@ -179,7 +217,7 @@ public class ThrottleService extends IThrottleManager.Stub {
// TODO - a better name - getCurrentThrottleRate? // TODO - a better name - getCurrentThrottleRate?
public synchronized int getThrottle(String iface) { public synchronized int getThrottle(String iface) {
enforceAccessPermission(); enforceAccessPermission();
if (iface.equals(mPolicyIface) && (mThrottleLevel == 1)) { if (iface.equals(mPolicyIface) && (mThrottleIndex == 1)) {
return mPolicyThrottleValue; return mPolicyThrottleValue;
} }
return 0; return 0;
@@ -208,6 +246,9 @@ public class ThrottleService extends IThrottleManager.Stub {
mThread.start(); mThread.start();
mHandler = new MyHandler(mThread.getLooper()); mHandler = new MyHandler(mThread.getLooper());
mHandler.obtainMessage(EVENT_REBOOT_RECOVERY).sendToTarget(); 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 // check for sim change TODO
// reregister for notification of policy change // reregister for notification of policy change
// register for roaming indication change mThrottleIndex = THROTTLE_INDEX_UNINITIALIZED;
// check for roaming TODO
mRecorder = new DataRecorder(mContext, ThrottleService.this); mRecorder = new DataRecorder(mContext, ThrottleService.this);
@@ -300,14 +340,10 @@ public class ThrottleService extends IThrottleManager.Stub {
", resetDay=" + mPolicyResetDay + ", noteType=" + ", resetDay=" + mPolicyResetDay + ", noteType=" +
mPolicyNotificationsAllowedMask); mPolicyNotificationsAllowedMask);
Calendar end = calculatePeriodEnd(); onResetAlarm();
Calendar start = calculatePeriodStart(end);
mRecorder.setNextPeriod(start,end); Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION);
mContext.sendBroadcast(broadcast);
mAlarmManager.cancel(mPendingResetIntent);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(),
mPendingResetIntent);
} }
private void onPollAlarm() { private void onPollAlarm() {
@@ -357,9 +393,9 @@ public class ThrottleService extends IThrottleManager.Stub {
// check if we need to throttle // check if we need to throttle
if (currentTotal > mPolicyThreshold) { if (currentTotal > mPolicyThreshold) {
if (mThrottleLevel != 1) { if (mThrottleIndex != 1) {
synchronized (ThrottleService.this) { synchronized (ThrottleService.this) {
mThrottleLevel = 1; mThrottleIndex = 1;
} }
if (DBG) Slog.d(TAG, "Threshold " + mPolicyThreshold + " exceeded!"); if (DBG) Slog.d(TAG, "Threshold " + mPolicyThreshold + " exceeded!");
try { try {
@@ -429,9 +465,9 @@ public class ThrottleService extends IThrottleManager.Stub {
private synchronized void clearThrottleAndNotification() { private synchronized void clearThrottleAndNotification() {
if (mThrottleLevel == 1) { if (mThrottleIndex != THROTTLE_INDEX_UNTHROTTLED) {
synchronized (ThrottleService.this) { synchronized (ThrottleService.this) {
mThrottleLevel = 0; mThrottleIndex = THROTTLE_INDEX_UNTHROTTLED;
} }
try { try {
mNMService.setInterfaceThrottle(mPolicyIface, -1, -1); mNMService.setInterfaceThrottle(mPolicyIface, -1, -1);
@@ -499,6 +535,7 @@ public class ThrottleService extends IThrottleManager.Stub {
mRecorder.setNextPeriod(start,end); mRecorder.setNextPeriod(start,end);
mAlarmManager.cancel(mPendingResetIntent);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(), mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(),
mPendingResetIntent); mPendingResetIntent);
} }