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