| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.AlarmManager; |
| 20 | import android.app.Notification; |
| 21 | import android.app.NotificationManager; |
| 22 | import android.app.PendingIntent; |
| 23 | import android.app.Service; |
| 24 | import android.content.BroadcastReceiver; |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.res.Resources; |
| 31 | import android.content.SharedPreferences; |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 32 | import android.database.ContentObserver; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 33 | import android.net.IThrottleManager; |
| 34 | import android.net.ThrottleManager; |
| 35 | import android.os.Binder; |
| 36 | import android.os.Handler; |
| 37 | import android.os.HandlerThread; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.INetworkManagementService; |
| 40 | import android.os.Looper; |
| 41 | import android.os.Message; |
| 42 | import android.os.RemoteException; |
| 43 | import android.os.ServiceManager; |
| 44 | import android.os.SystemClock; |
| 45 | import android.os.SystemProperties; |
| 46 | import android.provider.Settings; |
| 47 | import android.util.Slog; |
| 48 | |
| Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 49 | import com.android.internal.telephony.TelephonyProperties; |
| 50 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 51 | import java.io.FileDescriptor; |
| 52 | import java.io.PrintWriter; |
| 53 | import java.util.Calendar; |
| 54 | import java.util.GregorianCalendar; |
| 55 | import java.util.Random; |
| 56 | |
| 57 | // TODO - add comments - reference the ThrottleManager for public API |
| 58 | public class ThrottleService extends IThrottleManager.Stub { |
| 59 | |
| 60 | private static final String TESTING_ENABLED_PROPERTY = "persist.throttle.testing"; |
| 61 | |
| 62 | private static final String TAG = "ThrottleService"; |
| 63 | private static boolean DBG = true; |
| 64 | private Handler mHandler; |
| 65 | private HandlerThread mThread; |
| 66 | |
| 67 | private Context mContext; |
| 68 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 69 | private static final int TESTING_POLLING_PERIOD_SEC = 60 * 1; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 70 | private static final int TESTING_RESET_PERIOD_SEC = 60 * 3; |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 71 | private static final long TESTING_THRESHOLD = 1 * 1024 * 1024; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 72 | |
| 73 | private static final int PERIOD_COUNT = 6; |
| 74 | |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 75 | private int mPolicyPollPeriodSec; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 76 | private long mPolicyThreshold; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 77 | private int mPolicyThrottleValue; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 78 | private int mPolicyResetDay; // 1-28 |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 79 | private int mPolicyNotificationsAllowedMask; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 80 | |
| 81 | private long mLastRead; // read byte count from last poll |
| 82 | private long mLastWrite; // write byte count from last poll |
| 83 | |
| 84 | private static final String ACTION_POLL = "com.android.server.ThrottleManager.action.POLL"; |
| 85 | private static int POLL_REQUEST = 0; |
| 86 | private PendingIntent mPendingPollIntent; |
| 87 | private static final String ACTION_RESET = "com.android.server.ThorottleManager.action.RESET"; |
| 88 | private static int RESET_REQUEST = 1; |
| 89 | private PendingIntent mPendingResetIntent; |
| 90 | |
| 91 | private INetworkManagementService mNMService; |
| 92 | private AlarmManager mAlarmManager; |
| 93 | private NotificationManager mNotificationManager; |
| 94 | |
| 95 | private DataRecorder mRecorder; |
| 96 | |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 97 | private String mIface; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 98 | |
| 99 | private static final int NOTIFICATION_WARNING = 2; |
| 100 | private static final int NOTIFICATION_ALL = 0xFFFFFFFF; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 101 | |
| 102 | private Notification mThrottlingNotification; |
| 103 | private boolean mWarningNotificationSent = false; |
| 104 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 105 | private SettingsObserver mSettingsObserver; |
| 106 | |
| 107 | private int mThrottleIndex; // 0 for none, 1 for first throttle val, 2 for next, etc |
| 108 | private static final int THROTTLE_INDEX_UNINITIALIZED = -1; |
| 109 | private static final int THROTTLE_INDEX_UNTHROTTLED = 0; |
| 110 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 111 | public ThrottleService(Context context) { |
| 112 | if (DBG) Slog.d(TAG, "Starting ThrottleService"); |
| 113 | mContext = context; |
| 114 | |
| 115 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 116 | Intent pollIntent = new Intent(ACTION_POLL, null); |
| 117 | mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0); |
| 118 | Intent resetIntent = new Intent(ACTION_RESET, null); |
| 119 | mPendingResetIntent = PendingIntent.getBroadcast(mContext, RESET_REQUEST, resetIntent, 0); |
| 120 | |
| 121 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 122 | mNMService = INetworkManagementService.Stub.asInterface(b); |
| 123 | |
| 124 | mNotificationManager = (NotificationManager)mContext.getSystemService( |
| 125 | Context.NOTIFICATION_SERVICE); |
| 126 | } |
| 127 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 128 | private static class SettingsObserver extends ContentObserver { |
| 129 | private int mMsg; |
| 130 | private Handler mHandler; |
| 131 | SettingsObserver(Handler handler, int msg) { |
| 132 | super(handler); |
| 133 | mHandler = handler; |
| 134 | mMsg = msg; |
| 135 | } |
| 136 | |
| 137 | void observe(Context context) { |
| 138 | ContentResolver resolver = context.getContentResolver(); |
| 139 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 140 | Settings.Secure.THROTTLE_POLLING_SEC), false, this); |
| 141 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 142 | Settings.Secure.THROTTLE_THRESHOLD_BYTES), false, this); |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 143 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 144 | Settings.Secure.THROTTLE_VALUE_KBITSPS), false, this); |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 145 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 146 | Settings.Secure.THROTTLE_RESET_DAY), false, this); |
| 147 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 148 | Settings.Secure.THROTTLE_NOTIFICATION_TYPE), false, this); |
| 149 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 150 | Settings.Secure.THROTTLE_HELP_URI), false, this); |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onChange(boolean selfChange) { |
| 155 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 156 | } |
| 157 | } |
| 158 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 159 | private void enforceAccessPermission() { |
| 160 | mContext.enforceCallingOrSelfPermission( |
| 161 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 162 | "ThrottleService"); |
| 163 | } |
| 164 | |
| 165 | public synchronized long getResetTime(String iface) { |
| 166 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 167 | if ((iface != null) && |
| 168 | iface.equals(mIface) && |
| 169 | (mRecorder != null)) { |
| 170 | mRecorder.getPeriodEnd(); |
| 171 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | public synchronized long getPeriodStartTime(String iface) { |
| 175 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 176 | if ((iface != null) && |
| 177 | iface.equals(mIface) && |
| 178 | (mRecorder != null)) { |
| 179 | mRecorder.getPeriodStart(); |
| 180 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | //TODO - a better name? getCliffByteCountThreshold? |
| 184 | public synchronized long getCliffThreshold(String iface, int cliff) { |
| 185 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 186 | if ((iface != null) && (cliff == 1) && iface.equals(mIface)) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 187 | return mPolicyThreshold; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | // TODO - a better name? getThrottleRate? |
| 192 | public synchronized int getCliffLevel(String iface, int cliff) { |
| 193 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 194 | if ((iface != null) && (cliff == 1) && iface.equals(mIface)) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 195 | return mPolicyThrottleValue; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| Irfan Sheriff | c9b6851 | 2010-04-08 14:12:33 -0700 | [diff] [blame] | 200 | public String getHelpUri() { |
| 201 | enforceAccessPermission(); |
| 202 | return Settings.Secure.getString(mContext.getContentResolver(), |
| 203 | Settings.Secure.THROTTLE_HELP_URI); |
| 204 | } |
| 205 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 206 | public synchronized long getByteCount(String iface, int dir, int period, int ago) { |
| 207 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 208 | if ((iface != null) && |
| 209 | iface.equals(mIface) && |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 210 | (period == ThrottleManager.PERIOD_CYCLE) && |
| 211 | (mRecorder != null)) { |
| 212 | if (dir == ThrottleManager.DIRECTION_TX) return mRecorder.getPeriodTx(ago); |
| 213 | if (dir == ThrottleManager.DIRECTION_RX) return mRecorder.getPeriodRx(ago); |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | // TODO - a better name - getCurrentThrottleRate? |
| 219 | public synchronized int getThrottle(String iface) { |
| 220 | enforceAccessPermission(); |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 221 | if ((iface != null) && iface.equals(mIface) && (mThrottleIndex == 1)) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 222 | return mPolicyThrottleValue; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void systemReady() { |
| 228 | if (DBG) Slog.d(TAG, "systemReady"); |
| 229 | mContext.registerReceiver( |
| 230 | new BroadcastReceiver() { |
| 231 | @Override |
| 232 | public void onReceive(Context context, Intent intent) { |
| 233 | mHandler.obtainMessage(EVENT_POLL_ALARM).sendToTarget(); |
| 234 | } |
| 235 | }, new IntentFilter(ACTION_POLL)); |
| 236 | |
| 237 | mContext.registerReceiver( |
| 238 | new BroadcastReceiver() { |
| 239 | @Override |
| 240 | public void onReceive(Context context, Intent intent) { |
| 241 | mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget(); |
| 242 | } |
| 243 | }, new IntentFilter(ACTION_RESET)); |
| 244 | |
| 245 | // use a new thread as we don't want to stall the system for file writes |
| 246 | mThread = new HandlerThread(TAG); |
| 247 | mThread.start(); |
| 248 | mHandler = new MyHandler(mThread.getLooper()); |
| 249 | mHandler.obtainMessage(EVENT_REBOOT_RECOVERY).sendToTarget(); |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 250 | |
| 251 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_POLICY_CHANGED); |
| 252 | mSettingsObserver.observe(mContext); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
| 256 | private static final int EVENT_REBOOT_RECOVERY = 0; |
| 257 | private static final int EVENT_POLICY_CHANGED = 1; |
| 258 | private static final int EVENT_POLL_ALARM = 2; |
| 259 | private static final int EVENT_RESET_ALARM = 3; |
| 260 | private class MyHandler extends Handler { |
| 261 | public MyHandler(Looper l) { |
| 262 | super(l); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void handleMessage(Message msg) { |
| 267 | switch (msg.what) { |
| 268 | case EVENT_REBOOT_RECOVERY: |
| 269 | onRebootRecovery(); |
| 270 | break; |
| 271 | case EVENT_POLICY_CHANGED: |
| 272 | onPolicyChanged(); |
| 273 | break; |
| 274 | case EVENT_POLL_ALARM: |
| 275 | onPollAlarm(); |
| 276 | break; |
| 277 | case EVENT_RESET_ALARM: |
| 278 | onResetAlarm(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | private void onRebootRecovery() { |
| 283 | if (DBG) Slog.d(TAG, "onRebootRecovery"); |
| 284 | // check for sim change TODO |
| 285 | // reregister for notification of policy change |
| 286 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 287 | mThrottleIndex = THROTTLE_INDEX_UNINITIALIZED; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 288 | |
| 289 | mRecorder = new DataRecorder(mContext, ThrottleService.this); |
| 290 | |
| 291 | // get policy |
| 292 | mHandler.obtainMessage(EVENT_POLICY_CHANGED).sendToTarget(); |
| 293 | |
| 294 | // evaluate current conditions |
| 295 | mHandler.obtainMessage(EVENT_POLL_ALARM).sendToTarget(); |
| 296 | } |
| 297 | |
| 298 | private void onSimChange() { |
| 299 | // TODO |
| 300 | } |
| 301 | |
| 302 | // check for new policy info (threshold limit/value/etc) |
| 303 | private void onPolicyChanged() { |
| 304 | boolean testing = SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true"); |
| 305 | |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 306 | int pollingPeriod = mContext.getResources().getInteger( |
| 307 | com.android.internal.R.integer.config_datause_polling_period_sec); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 308 | mPolicyPollPeriodSec = Settings.Secure.getInt(mContext.getContentResolver(), |
| 309 | Settings.Secure.THROTTLE_POLLING_SEC, pollingPeriod); |
| 310 | |
| 311 | // TODO - remove testing stuff? |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 312 | long defaultThreshold = mContext.getResources().getInteger( |
| 313 | com.android.internal.R.integer.config_datause_threshold_bytes); |
| 314 | int defaultValue = mContext.getResources().getInteger( |
| 315 | com.android.internal.R.integer.config_datause_throttle_kbitsps); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 316 | synchronized (ThrottleService.this) { |
| 317 | mPolicyThreshold = Settings.Secure.getLong(mContext.getContentResolver(), |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 318 | Settings.Secure.THROTTLE_THRESHOLD_BYTES, defaultThreshold); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 319 | mPolicyThrottleValue = Settings.Secure.getInt(mContext.getContentResolver(), |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 320 | Settings.Secure.THROTTLE_VALUE_KBITSPS, defaultValue); |
| 321 | if (testing) { |
| 322 | mPolicyPollPeriodSec = TESTING_POLLING_PERIOD_SEC; |
| 323 | mPolicyThreshold = TESTING_THRESHOLD; |
| 324 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 325 | } |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 326 | |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 327 | mPolicyResetDay = Settings.Secure.getInt(mContext.getContentResolver(), |
| 328 | Settings.Secure.THROTTLE_RESET_DAY, -1); |
| 329 | if (mPolicyResetDay == -1 || |
| 330 | ((mPolicyResetDay < 1) || (mPolicyResetDay > 28))) { |
| 331 | Random g = new Random(); |
| 332 | mPolicyResetDay = 1 + g.nextInt(28); // 1-28 |
| 333 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 334 | Settings.Secure.THROTTLE_RESET_DAY, mPolicyResetDay); |
| 335 | } |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 336 | mIface = mContext.getResources().getString( |
| 337 | com.android.internal.R.string.config_datause_iface); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 338 | synchronized (ThrottleService.this) { |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 339 | if (mIface == null) { |
| 340 | mPolicyThreshold = 0; |
| 341 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 344 | int defaultNotificationType = mContext.getResources().getInteger( |
| 345 | com.android.internal.R.integer.config_datause_notification_type); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 346 | mPolicyNotificationsAllowedMask = Settings.Secure.getInt(mContext.getContentResolver(), |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 347 | Settings.Secure.THROTTLE_NOTIFICATION_TYPE, defaultNotificationType); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 348 | |
| 349 | Slog.d(TAG, "onPolicyChanged testing=" + testing +", period=" + mPolicyPollPeriodSec + |
| 350 | ", threshold=" + mPolicyThreshold + ", value=" + mPolicyThrottleValue + |
| 351 | ", resetDay=" + mPolicyResetDay + ", noteType=" + |
| 352 | mPolicyNotificationsAllowedMask); |
| 353 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 354 | onResetAlarm(); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 355 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 356 | Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION); |
| 357 | mContext.sendBroadcast(broadcast); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private void onPollAlarm() { |
| 361 | long now = SystemClock.elapsedRealtime(); |
| 362 | long next = now + mPolicyPollPeriodSec*1000; |
| 363 | long incRead = 0; |
| 364 | long incWrite = 0; |
| 365 | try { |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 366 | incRead = mNMService.getInterfaceRxCounter(mIface) - mLastRead; |
| 367 | incWrite = mNMService.getInterfaceTxCounter(mIface) - mLastWrite; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 368 | } catch (RemoteException e) { |
| 369 | Slog.e(TAG, "got remoteException in onPollAlarm:" + e); |
| 370 | } |
| Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 371 | // don't count this data if we're roaming. |
| 372 | boolean roaming = "true".equals( |
| 373 | SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING)); |
| 374 | if (!roaming) { |
| 375 | mRecorder.addData(incRead, incWrite); |
| 376 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 377 | |
| 378 | long periodRx = mRecorder.getPeriodRx(0); |
| 379 | long periodTx = mRecorder.getPeriodTx(0); |
| 380 | long total = periodRx + periodTx; |
| 381 | if (DBG) { |
| Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 382 | Slog.d(TAG, "onPollAlarm - now =" + now + ", roaming =" + roaming + |
| 383 | ", read =" + incRead + ", written =" + incWrite + ", new total =" + total); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 384 | } |
| 385 | mLastRead += incRead; |
| 386 | mLastWrite += incWrite; |
| 387 | |
| 388 | checkThrottleAndPostNotification(total); |
| 389 | |
| 390 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_POLL_ACTION); |
| 391 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_READ, periodRx); |
| 392 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_WRITE, periodTx); |
| 393 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_START, mRecorder.getPeriodStart()); |
| 394 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_END, mRecorder.getPeriodEnd()); |
| 395 | mContext.sendStickyBroadcast(broadcast); |
| 396 | |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 397 | mAlarmManager.cancel(mPendingPollIntent); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 398 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, next, mPendingPollIntent); |
| 399 | } |
| 400 | |
| 401 | private void checkThrottleAndPostNotification(long currentTotal) { |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 402 | // is throttling enabled? |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 403 | if (mPolicyThreshold == 0) |
| 404 | return; |
| 405 | |
| 406 | // check if we need to throttle |
| 407 | if (currentTotal > mPolicyThreshold) { |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 408 | if (mThrottleIndex != 1) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 409 | synchronized (ThrottleService.this) { |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 410 | mThrottleIndex = 1; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 411 | } |
| 412 | if (DBG) Slog.d(TAG, "Threshold " + mPolicyThreshold + " exceeded!"); |
| 413 | try { |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 414 | mNMService.setInterfaceThrottle(mIface, |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 415 | mPolicyThrottleValue, mPolicyThrottleValue); |
| 416 | } catch (Exception e) { |
| 417 | Slog.e(TAG, "error setting Throttle: " + e); |
| 418 | } |
| 419 | |
| 420 | mNotificationManager.cancel(com.android.internal.R.drawable. |
| 421 | stat_sys_throttle_warning); |
| 422 | |
| 423 | postNotification(com.android.internal.R.string.throttled_notification_title, |
| 424 | com.android.internal.R.string.throttled_notification_message, |
| Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 425 | com.android.internal.R.drawable.stat_sys_throttled, |
| 426 | Notification.FLAG_ONGOING_EVENT); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 427 | |
| 428 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_ACTION); |
| 429 | broadcast.putExtra(ThrottleManager.EXTRA_THROTTLE_LEVEL, mPolicyThrottleValue); |
| 430 | mContext.sendStickyBroadcast(broadcast); |
| 431 | |
| 432 | } // else already up! |
| 433 | } else { |
| 434 | if ((mPolicyNotificationsAllowedMask & NOTIFICATION_WARNING) != 0) { |
| 435 | // check if we should warn about throttle |
| Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 436 | // pretend we only have 1/2 the time remaining that we actually do |
| 437 | // if our burn rate in the period so far would have us exceed the limit |
| 438 | // in that 1/2 window, warn the user. |
| 439 | // this gets more generous in the early to middle period and converges back |
| 440 | // to the limit as we move toward the period end. |
| 441 | |
| 442 | // adding another factor - it must be greater than the total cap/4 |
| 443 | // else we may get false alarms very early in the period.. in the first |
| 444 | // tenth of a percent of the period if we used more than a tenth of a percent |
| 445 | // of the cap we'd get a warning and that's not desired. |
| 446 | long start = mRecorder.getPeriodStart(); |
| 447 | long end = mRecorder.getPeriodEnd(); |
| 448 | long periodLength = end - start; |
| 449 | long now = System.currentTimeMillis(); |
| 450 | long timeUsed = now - start; |
| 451 | long warningThreshold = 2*mPolicyThreshold*timeUsed/(timeUsed+periodLength); |
| 452 | if ((currentTotal > warningThreshold) && (currentTotal > mPolicyThreshold/4)) { |
| 453 | if (mWarningNotificationSent == false) { |
| 454 | mWarningNotificationSent = true; |
| 455 | mNotificationManager.cancel(com.android.internal.R.drawable. |
| 456 | stat_sys_throttle_warning); |
| 457 | postNotification(com.android.internal.R.string. |
| 458 | throttle_warning_notification_title, |
| 459 | com.android.internal.R.string. |
| 460 | throttle_warning_notification_message, |
| 461 | com.android.internal.R.drawable.stat_sys_throttle_warning, |
| 462 | 0); |
| 463 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 464 | } else { |
| Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 465 | if (mWarningNotificationSent == true) { |
| 466 | mNotificationManager.cancel(com.android.internal.R.drawable. |
| 467 | stat_sys_throttle_warning); |
| 468 | mWarningNotificationSent =false; |
| 469 | } |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 475 | private void postNotification(int titleInt, int messageInt, int icon, int flags) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 476 | Intent intent = new Intent(); |
| 477 | // TODO - fix up intent |
| Robert Greenwalt | 2a7b730 | 2010-04-12 14:56:31 -0700 | [diff] [blame] | 478 | intent.setClassName("com.android.phone", "com.android.phone.DataUsage"); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 479 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); |
| 480 | |
| 481 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 482 | |
| 483 | Resources r = Resources.getSystem(); |
| 484 | CharSequence title = r.getText(titleInt); |
| 485 | CharSequence message = r.getText(messageInt); |
| 486 | if (mThrottlingNotification == null) { |
| 487 | mThrottlingNotification = new Notification(); |
| 488 | mThrottlingNotification.when = 0; |
| 489 | // TODO - fixup icon |
| 490 | mThrottlingNotification.icon = icon; |
| 491 | mThrottlingNotification.defaults &= ~Notification.DEFAULT_SOUND; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 492 | } |
| Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 493 | mThrottlingNotification.flags = flags; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 494 | mThrottlingNotification.tickerText = title; |
| 495 | mThrottlingNotification.setLatestEventInfo(mContext, title, message, pi); |
| 496 | |
| 497 | mNotificationManager.notify(mThrottlingNotification.icon, mThrottlingNotification); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | private synchronized void clearThrottleAndNotification() { |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 502 | if (mThrottleIndex != THROTTLE_INDEX_UNTHROTTLED) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 503 | synchronized (ThrottleService.this) { |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 504 | mThrottleIndex = THROTTLE_INDEX_UNTHROTTLED; |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 505 | } |
| 506 | try { |
| Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 507 | mNMService.setInterfaceThrottle(mIface, -1, -1); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 508 | } catch (Exception e) { |
| 509 | Slog.e(TAG, "error clearing Throttle: " + e); |
| 510 | } |
| 511 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_ACTION); |
| 512 | broadcast.putExtra(ThrottleManager.EXTRA_THROTTLE_LEVEL, -1); |
| 513 | mContext.sendStickyBroadcast(broadcast); |
| 514 | } |
| 515 | mNotificationManager.cancel(com.android.internal.R.drawable.stat_sys_throttle_warning); |
| 516 | mNotificationManager.cancel(com.android.internal.R.drawable.stat_sys_throttled); |
| 517 | mWarningNotificationSent = false; |
| 518 | } |
| 519 | |
| 520 | private Calendar calculatePeriodEnd() { |
| 521 | Calendar end = GregorianCalendar.getInstance(); |
| 522 | int day = end.get(Calendar.DAY_OF_MONTH); |
| 523 | end.set(Calendar.DAY_OF_MONTH, mPolicyResetDay); |
| 524 | end.set(Calendar.HOUR_OF_DAY, 0); |
| 525 | end.set(Calendar.MINUTE, 0); |
| 526 | if (day >= mPolicyResetDay) { |
| 527 | int month = end.get(Calendar.MONTH); |
| 528 | if (month == Calendar.DECEMBER) { |
| 529 | end.set(Calendar.YEAR, end.get(Calendar.YEAR) + 1); |
| 530 | month = Calendar.JANUARY - 1; |
| 531 | } |
| 532 | end.set(Calendar.MONTH, month + 1); |
| 533 | } |
| 534 | |
| 535 | // TODO - remove! |
| 536 | if (SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true")) { |
| 537 | end = GregorianCalendar.getInstance(); |
| 538 | end.add(Calendar.SECOND, TESTING_RESET_PERIOD_SEC); |
| 539 | } |
| 540 | return end; |
| 541 | } |
| 542 | private Calendar calculatePeriodStart(Calendar end) { |
| 543 | Calendar start = (Calendar)end.clone(); |
| 544 | int month = end.get(Calendar.MONTH); |
| 545 | if (end.get(Calendar.MONTH) == Calendar.JANUARY) { |
| 546 | month = Calendar.DECEMBER + 1; |
| 547 | start.set(Calendar.YEAR, start.get(Calendar.YEAR) - 1); |
| 548 | } |
| 549 | start.set(Calendar.MONTH, month - 1); |
| 550 | |
| 551 | // TODO - remove!! |
| 552 | if (SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true")) { |
| 553 | start = (Calendar)end.clone(); |
| 554 | start.add(Calendar.SECOND, -TESTING_RESET_PERIOD_SEC); |
| 555 | } |
| 556 | return start; |
| 557 | } |
| 558 | |
| 559 | private void onResetAlarm() { |
| 560 | if (DBG) { |
| 561 | Slog.d(TAG, "onResetAlarm - last period had " + mRecorder.getPeriodRx(0) + |
| 562 | " bytes read and " + mRecorder.getPeriodTx(0) + " written"); |
| 563 | } |
| 564 | |
| 565 | Calendar end = calculatePeriodEnd(); |
| 566 | Calendar start = calculatePeriodStart(end); |
| 567 | |
| 568 | clearThrottleAndNotification(); |
| 569 | |
| 570 | mRecorder.setNextPeriod(start,end); |
| 571 | |
| Robert Greenwalt | 81aa0971d | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 572 | mAlarmManager.cancel(mPendingResetIntent); |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 573 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, end.getTimeInMillis(), |
| 574 | mPendingResetIntent); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // records bytecount data for a given time and accumulates it into larger time windows |
| 579 | // for logging and other purposes |
| 580 | // |
| 581 | // since time can be changed (user or network action) we will have to track the time of the |
| 582 | // last recording and deal with it. |
| 583 | private static class DataRecorder { |
| 584 | long[] mPeriodRxData; |
| 585 | long[] mPeriodTxData; |
| 586 | int mCurrentPeriod; |
| 587 | int mPeriodCount; |
| 588 | |
| 589 | Calendar mPeriodStart; |
| 590 | Calendar mPeriodEnd; |
| 591 | |
| 592 | ThrottleService mParent; |
| 593 | Context mContext; |
| 594 | SharedPreferences mSharedPreferences; |
| 595 | |
| 596 | DataRecorder(Context context, ThrottleService parent) { |
| 597 | mContext = context; |
| 598 | mParent = parent; |
| 599 | |
| 600 | synchronized (mParent) { |
| 601 | mPeriodCount = 6; |
| 602 | mPeriodRxData = new long[mPeriodCount]; |
| 603 | mPeriodTxData = new long[mPeriodCount]; |
| 604 | |
| 605 | mPeriodStart = Calendar.getInstance(); |
| 606 | mPeriodEnd = Calendar.getInstance(); |
| 607 | |
| 608 | mSharedPreferences = mContext.getSharedPreferences("ThrottleData", |
| 609 | android.content.Context.MODE_PRIVATE); |
| 610 | |
| 611 | zeroData(0); |
| 612 | retrieve(); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | void setNextPeriod(Calendar start, Calendar end) { |
| 617 | if (DBG) { |
| 618 | Slog.d(TAG, "setting next period to " + start.getTimeInMillis() + |
| 619 | " --until-- " + end.getTimeInMillis()); |
| 620 | } |
| 621 | // if we roll back in time to a previous period, toss out the current data |
| 622 | // if we roll forward to the next period, advance to the next |
| 623 | |
| 624 | if (end.before(mPeriodStart)) { |
| 625 | if (DBG) { |
| 626 | Slog.d(TAG, " old start was " + mPeriodStart.getTimeInMillis() + ", wiping"); |
| 627 | } |
| 628 | synchronized (mParent) { |
| 629 | mPeriodRxData[mCurrentPeriod] = 0; |
| 630 | mPeriodTxData[mCurrentPeriod] = 0; |
| 631 | } |
| 632 | } else if(start.after(mPeriodEnd)) { |
| 633 | if (DBG) { |
| 634 | Slog.d(TAG, " old end was " + mPeriodEnd.getTimeInMillis() + ", following"); |
| 635 | } |
| 636 | synchronized (mParent) { |
| 637 | ++mCurrentPeriod; |
| 638 | if (mCurrentPeriod >= mPeriodCount) mCurrentPeriod = 0; |
| 639 | mPeriodRxData[mCurrentPeriod] = 0; |
| 640 | mPeriodTxData[mCurrentPeriod] = 0; |
| 641 | } |
| 642 | } else { |
| 643 | if (DBG) Slog.d(TAG, " we fit - ammending to last period"); |
| 644 | } |
| 645 | setPeriodStart(start); |
| 646 | setPeriodEnd(end); |
| 647 | record(); |
| 648 | } |
| 649 | |
| 650 | public long getPeriodEnd() { |
| 651 | synchronized (mParent) { |
| 652 | return mPeriodEnd.getTimeInMillis(); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | private void setPeriodEnd(Calendar end) { |
| 657 | synchronized (mParent) { |
| 658 | mPeriodEnd = end; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | public long getPeriodStart() { |
| 663 | synchronized (mParent) { |
| 664 | return mPeriodStart.getTimeInMillis(); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | private void setPeriodStart(Calendar start) { |
| 669 | synchronized (mParent) { |
| 670 | mPeriodStart = start; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | public int getPeriodCount() { |
| 675 | synchronized (mParent) { |
| 676 | return mPeriodCount; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | private void zeroData(int field) { |
| 681 | synchronized (mParent) { |
| 682 | for(int period = 0; period<mPeriodCount; period++) { |
| 683 | mPeriodRxData[period] = 0; |
| 684 | mPeriodTxData[period] = 0; |
| 685 | } |
| 686 | mCurrentPeriod = 0; |
| 687 | } |
| 688 | |
| 689 | } |
| 690 | |
| 691 | // if time moves backward accumulate all read/write that's lost into the now |
| 692 | // otherwise time moved forward. |
| 693 | void addData(long bytesRead, long bytesWritten) { |
| 694 | synchronized (mParent) { |
| 695 | mPeriodRxData[mCurrentPeriod] += bytesRead; |
| 696 | mPeriodTxData[mCurrentPeriod] += bytesWritten; |
| 697 | } |
| 698 | record(); |
| 699 | } |
| 700 | |
| 701 | private void record() { |
| 702 | // serialize into a secure setting |
| 703 | |
| 704 | // 1 int mPeriodCount |
| 705 | // 13*6 long[PERIOD_COUNT] mPeriodRxData |
| 706 | // 13*6 long[PERIOD_COUNT] mPeriodTxData |
| 707 | // 1 int mCurrentPeriod |
| 708 | // 13 long periodStartMS |
| 709 | // 13 long periodEndMS |
| 710 | // 199 chars max |
| 711 | StringBuilder builder = new StringBuilder(); |
| 712 | builder.append(mPeriodCount); |
| 713 | builder.append(":"); |
| 714 | for(int i=0; i < mPeriodCount; i++) { |
| 715 | builder.append(mPeriodRxData[i]); |
| 716 | builder.append(":"); |
| 717 | } |
| 718 | for(int i=0; i < mPeriodCount; i++) { |
| 719 | builder.append(mPeriodTxData[i]); |
| 720 | builder.append(":"); |
| 721 | } |
| 722 | builder.append(mCurrentPeriod); |
| 723 | builder.append(":"); |
| 724 | builder.append(mPeriodStart.getTimeInMillis()); |
| 725 | builder.append(":"); |
| 726 | builder.append(mPeriodEnd.getTimeInMillis()); |
| 727 | builder.append(":"); |
| 728 | |
| 729 | SharedPreferences.Editor editor = mSharedPreferences.edit(); |
| 730 | |
| 731 | editor.putString("Data", builder.toString()); |
| 732 | editor.commit(); |
| 733 | } |
| 734 | |
| 735 | private void retrieve() { |
| 736 | String data = mSharedPreferences.getString("Data", ""); |
| 737 | // String data = Settings.Secure.getString(mContext.getContentResolver(), |
| 738 | // Settings.Secure.THROTTLE_VALUE); |
| 739 | if (data == null || data.length() == 0) return; |
| 740 | |
| 741 | synchronized (mParent) { |
| 742 | String[] parsed = data.split(":"); |
| 743 | int parsedUsed = 0; |
| 744 | if (parsed.length < 6) return; |
| 745 | |
| 746 | mPeriodCount = Integer.parseInt(parsed[parsedUsed++]); |
| 747 | if (parsed.length != 4 + (2 * mPeriodCount)) return; |
| 748 | |
| 749 | mPeriodRxData = new long[mPeriodCount]; |
| 750 | for(int i=0; i < mPeriodCount; i++) { |
| 751 | mPeriodRxData[i] = Long.parseLong(parsed[parsedUsed++]); |
| 752 | } |
| 753 | mPeriodTxData = new long[mPeriodCount]; |
| 754 | for(int i=0; i < mPeriodCount; i++) { |
| 755 | mPeriodTxData[i] = Long.parseLong(parsed[parsedUsed++]); |
| 756 | } |
| 757 | mCurrentPeriod = Integer.parseInt(parsed[parsedUsed++]); |
| 758 | mPeriodStart = new GregorianCalendar(); |
| 759 | mPeriodStart.setTimeInMillis(Long.parseLong(parsed[parsedUsed++])); |
| 760 | mPeriodEnd = new GregorianCalendar(); |
| 761 | mPeriodEnd.setTimeInMillis(Long.parseLong(parsed[parsedUsed++])); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | long getPeriodRx(int which) { |
| Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 766 | synchronized (mParent) { |
| 767 | if (which > mPeriodCount) return 0; |
| 768 | which = mCurrentPeriod - which; |
| 769 | if (which < 0) which += mPeriodCount; |
| 770 | return mPeriodRxData[which]; |
| 771 | } |
| 772 | } |
| 773 | long getPeriodTx(int which) { |
| 774 | synchronized (mParent) { |
| 775 | if (which > mPeriodCount) return 0; |
| 776 | which = mCurrentPeriod - which; |
| 777 | if (which < 0) which += mPeriodCount; |
| 778 | return mPeriodTxData[which]; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 785 | if (mContext.checkCallingOrSelfPermission( |
| 786 | android.Manifest.permission.DUMP) |
| 787 | != PackageManager.PERMISSION_GRANTED) { |
| 788 | pw.println("Permission Denial: can't dump ThrottleService " + |
| 789 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 790 | Binder.getCallingUid()); |
| 791 | return; |
| 792 | } |
| 793 | pw.println(); |
| 794 | |
| 795 | pw.println("The threshold is " + mPolicyThreshold + |
| 796 | ", after which you experince throttling to " + |
| 797 | mPolicyThrottleValue + "kbps"); |
| 798 | pw.println("Current period is " + |
| 799 | (mRecorder.getPeriodEnd() - mRecorder.getPeriodStart())/1000 + " seconds long " + |
| 800 | "and ends in " + (mRecorder.getPeriodEnd() - System.currentTimeMillis()) / 1000 + |
| 801 | " seconds."); |
| 802 | pw.println("Polling every " + mPolicyPollPeriodSec + " seconds"); |
| 803 | for (int i = 0; i < mRecorder.getPeriodCount(); i++) { |
| 804 | pw.println(" Period[" + i + "] - read:" + mRecorder.getPeriodRx(i) + ", written:" + |
| 805 | mRecorder.getPeriodTx(i)); |
| 806 | } |
| 807 | } |
| 808 | } |