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; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.res.Resources; |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 30 | import android.database.ContentObserver; |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 31 | import android.net.INetworkManagementEventObserver; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 32 | import android.net.IThrottleManager; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 33 | import android.net.NetworkStats; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 34 | import android.net.ThrottleManager; |
| 35 | import android.os.Binder; |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 36 | import android.os.Environment; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.HandlerThread; |
| 39 | import android.os.IBinder; |
| 40 | import android.os.INetworkManagementService; |
| 41 | import android.os.Looper; |
| 42 | import android.os.Message; |
| 43 | import android.os.RemoteException; |
| 44 | import android.os.ServiceManager; |
| 45 | import android.os.SystemClock; |
| 46 | import android.os.SystemProperties; |
| 47 | import android.provider.Settings; |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 48 | import android.telephony.TelephonyManager; |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 49 | import android.text.TextUtils; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 50 | import android.util.NtpTrustedTime; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 51 | import android.util.Slog; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 52 | import android.util.TrustedTime; |
Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 53 | |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 54 | import com.android.internal.R; |
| 55 | import com.android.internal.telephony.TelephonyProperties; |
| 56 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 57 | import java.io.BufferedWriter; |
| 58 | import java.io.File; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 59 | import java.io.FileDescriptor; |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 60 | import java.io.FileInputStream; |
| 61 | import java.io.FileWriter; |
| 62 | import java.io.IOException; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 63 | import java.io.PrintWriter; |
| 64 | import java.util.Calendar; |
| 65 | import java.util.GregorianCalendar; |
| 66 | import java.util.Random; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 67 | import java.util.concurrent.atomic.AtomicInteger; |
| 68 | import java.util.concurrent.atomic.AtomicLong; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 69 | |
| 70 | // TODO - add comments - reference the ThrottleManager for public API |
| 71 | public class ThrottleService extends IThrottleManager.Stub { |
| 72 | |
| 73 | private static final String TESTING_ENABLED_PROPERTY = "persist.throttle.testing"; |
| 74 | |
| 75 | private static final String TAG = "ThrottleService"; |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 76 | private static final boolean DBG = true; |
| 77 | private static final boolean VDBG = false; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 78 | private Handler mHandler; |
| 79 | private HandlerThread mThread; |
| 80 | |
| 81 | private Context mContext; |
| 82 | |
Robert Greenwalt | fb9896b | 2010-04-22 15:39:38 -0700 | [diff] [blame] | 83 | private static final int INITIAL_POLL_DELAY_SEC = 90; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 84 | private static final int TESTING_POLLING_PERIOD_SEC = 60 * 1; |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 85 | private static final int TESTING_RESET_PERIOD_SEC = 60 * 10; |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 86 | private static final long TESTING_THRESHOLD = 1 * 1024 * 1024; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 87 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 88 | private static final long MAX_NTP_CACHE_AGE = 24 * 60 * 60 * 1000; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 89 | |
| 90 | private long mMaxNtpCacheAge = MAX_NTP_CACHE_AGE; |
| 91 | |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 92 | private int mPolicyPollPeriodSec; |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 93 | private AtomicLong mPolicyThreshold; |
| 94 | private AtomicInteger mPolicyThrottleValue; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 95 | private int mPolicyResetDay; // 1-28 |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 96 | private int mPolicyNotificationsAllowedMask; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 97 | |
| 98 | private long mLastRead; // read byte count from last poll |
| 99 | private long mLastWrite; // write byte count from last poll |
| 100 | |
| 101 | private static final String ACTION_POLL = "com.android.server.ThrottleManager.action.POLL"; |
| 102 | private static int POLL_REQUEST = 0; |
| 103 | private PendingIntent mPendingPollIntent; |
| 104 | private static final String ACTION_RESET = "com.android.server.ThorottleManager.action.RESET"; |
| 105 | private static int RESET_REQUEST = 1; |
| 106 | private PendingIntent mPendingResetIntent; |
| 107 | |
| 108 | private INetworkManagementService mNMService; |
| 109 | private AlarmManager mAlarmManager; |
| 110 | private NotificationManager mNotificationManager; |
| 111 | |
| 112 | private DataRecorder mRecorder; |
| 113 | |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 114 | private String mIface; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 115 | |
| 116 | private static final int NOTIFICATION_WARNING = 2; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 117 | |
| 118 | private Notification mThrottlingNotification; |
| 119 | private boolean mWarningNotificationSent = false; |
| 120 | |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 121 | private InterfaceObserver mInterfaceObserver; |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 122 | private SettingsObserver mSettingsObserver; |
| 123 | |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 124 | private AtomicInteger mThrottleIndex; // 0 for none, 1 for first throttle val, 2 for next, etc |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 125 | private static final int THROTTLE_INDEX_UNINITIALIZED = -1; |
| 126 | private static final int THROTTLE_INDEX_UNTHROTTLED = 0; |
| 127 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 128 | private Intent mPollStickyBroadcast; |
| 129 | |
| 130 | private TrustedTime mTime; |
| 131 | |
| 132 | private static INetworkManagementService getNetworkManagementService() { |
| 133 | final IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 134 | return INetworkManagementService.Stub.asInterface(b); |
| 135 | } |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 136 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 137 | public ThrottleService(Context context) { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 138 | this(context, getNetworkManagementService(), NtpTrustedTime.getInstance(context), |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 139 | context.getResources().getString(R.string.config_datause_iface)); |
| 140 | } |
| 141 | |
| 142 | public ThrottleService(Context context, INetworkManagementService nmService, TrustedTime time, |
| 143 | String iface) { |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 144 | if (VDBG) Slog.v(TAG, "Starting ThrottleService"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 145 | mContext = context; |
| 146 | |
Robert Greenwalt | 24488bd | 2010-05-10 16:56:43 -0700 | [diff] [blame] | 147 | mPolicyThreshold = new AtomicLong(); |
| 148 | mPolicyThrottleValue = new AtomicInteger(); |
| 149 | mThrottleIndex = new AtomicInteger(); |
| 150 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 151 | mIface = iface; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 152 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 153 | Intent pollIntent = new Intent(ACTION_POLL, null); |
| 154 | mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0); |
| 155 | Intent resetIntent = new Intent(ACTION_RESET, null); |
| 156 | mPendingResetIntent = PendingIntent.getBroadcast(mContext, RESET_REQUEST, resetIntent, 0); |
| 157 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 158 | mNMService = nmService; |
| 159 | mTime = time; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 160 | |
| 161 | mNotificationManager = (NotificationManager)mContext.getSystemService( |
| 162 | Context.NOTIFICATION_SERVICE); |
| 163 | } |
| 164 | |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 165 | private static class InterfaceObserver extends INetworkManagementEventObserver.Stub { |
| 166 | private int mMsg; |
| 167 | private Handler mHandler; |
| 168 | private String mIface; |
| 169 | |
| 170 | InterfaceObserver(Handler handler, int msg, String iface) { |
| 171 | super(); |
| 172 | mHandler = handler; |
| 173 | mMsg = msg; |
| 174 | mIface = iface; |
| 175 | } |
| 176 | |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 177 | public void interfaceStatusChanged(String iface, boolean up) { |
| 178 | if (up) { |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 179 | if (TextUtils.equals(iface, mIface)) { |
| 180 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 185 | public void interfaceLinkStateChanged(String iface, boolean up) { |
| 186 | } |
| 187 | |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 188 | public void interfaceAdded(String iface) { |
| 189 | // TODO - an interface added in the UP state should also trigger a StatusChanged |
| 190 | // notification.. |
| 191 | if (TextUtils.equals(iface, mIface)) { |
| 192 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | public void interfaceRemoved(String iface) {} |
JP Abgrall | 12b933d | 2011-07-14 18:09:22 -0700 | [diff] [blame] | 197 | public void limitReached(String limitName, String iface) {} |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 201 | private static class SettingsObserver extends ContentObserver { |
| 202 | private int mMsg; |
| 203 | private Handler mHandler; |
| 204 | SettingsObserver(Handler handler, int msg) { |
| 205 | super(handler); |
| 206 | mHandler = handler; |
| 207 | mMsg = msg; |
| 208 | } |
| 209 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 210 | void register(Context context) { |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 211 | ContentResolver resolver = context.getContentResolver(); |
| 212 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 213 | Settings.Secure.THROTTLE_POLLING_SEC), false, this); |
| 214 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 215 | Settings.Secure.THROTTLE_THRESHOLD_BYTES), false, this); |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 216 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 217 | Settings.Secure.THROTTLE_VALUE_KBITSPS), false, this); |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 218 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 219 | Settings.Secure.THROTTLE_RESET_DAY), false, this); |
| 220 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 221 | Settings.Secure.THROTTLE_NOTIFICATION_TYPE), false, this); |
| 222 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 223 | Settings.Secure.THROTTLE_HELP_URI), false, this); |
Robert Greenwalt | d1055a2 | 2010-05-25 15:54:52 -0700 | [diff] [blame] | 224 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 225 | Settings.Secure.THROTTLE_MAX_NTP_CACHE_AGE_SEC), false, this); |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 228 | void unregister(Context context) { |
| 229 | final ContentResolver resolver = context.getContentResolver(); |
| 230 | resolver.unregisterContentObserver(this); |
| 231 | } |
| 232 | |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public void onChange(boolean selfChange) { |
| 235 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 236 | } |
| 237 | } |
| 238 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 239 | private void enforceAccessPermission() { |
| 240 | mContext.enforceCallingOrSelfPermission( |
| 241 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 242 | "ThrottleService"); |
| 243 | } |
| 244 | |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 245 | private long ntpToWallTime(long ntpTime) { |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 246 | // get time quickly without worrying about trusted state |
| 247 | long bestNow = mTime.hasCache() ? mTime.currentTimeMillis() |
| 248 | : System.currentTimeMillis(); |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 249 | long localNow = System.currentTimeMillis(); |
| 250 | return localNow + (ntpTime - bestNow); |
| 251 | } |
| 252 | |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 253 | // TODO - fetch for the iface |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 254 | // return time in the local, system wall time, correcting for the use of ntp |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 255 | |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 256 | public long getResetTime(String iface) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 257 | enforceAccessPermission(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 258 | long resetTime = 0; |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 259 | if (mRecorder != null) { |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 260 | resetTime = mRecorder.getPeriodEnd(); |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 261 | } |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 262 | resetTime = ntpToWallTime(resetTime); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 263 | return resetTime; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 264 | } |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 265 | |
| 266 | // TODO - fetch for the iface |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 267 | // return time in the local, system wall time, correcting for the use of ntp |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 268 | public long getPeriodStartTime(String iface) { |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 269 | long startTime = 0; |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 270 | enforceAccessPermission(); |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 271 | if (mRecorder != null) { |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 272 | startTime = mRecorder.getPeriodStart(); |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 273 | } |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 274 | startTime = ntpToWallTime(startTime); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 275 | return startTime; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 276 | } |
| 277 | //TODO - a better name? getCliffByteCountThreshold? |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 278 | // TODO - fetch for the iface |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 279 | public long getCliffThreshold(String iface, int cliff) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 280 | enforceAccessPermission(); |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 281 | if (cliff == 1) { |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 282 | return mPolicyThreshold.get(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | // TODO - a better name? getThrottleRate? |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 287 | // TODO - fetch for the iface |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 288 | public int getCliffLevel(String iface, int cliff) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 289 | enforceAccessPermission(); |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 290 | if (cliff == 1) { |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 291 | return mPolicyThrottleValue.get(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Irfan Sheriff | c9b6851 | 2010-04-08 14:12:33 -0700 | [diff] [blame] | 296 | public String getHelpUri() { |
| 297 | enforceAccessPermission(); |
| 298 | return Settings.Secure.getString(mContext.getContentResolver(), |
| 299 | Settings.Secure.THROTTLE_HELP_URI); |
| 300 | } |
| 301 | |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 302 | // TODO - fetch for the iface |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 303 | public long getByteCount(String iface, int dir, int period, int ago) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 304 | enforceAccessPermission(); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 305 | if ((period == ThrottleManager.PERIOD_CYCLE) && (mRecorder != null)) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 306 | if (dir == ThrottleManager.DIRECTION_TX) return mRecorder.getPeriodTx(ago); |
| 307 | if (dir == ThrottleManager.DIRECTION_RX) return mRecorder.getPeriodRx(ago); |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | // TODO - a better name - getCurrentThrottleRate? |
Irfan Sheriff | cf28236 | 2010-04-16 16:53:20 -0700 | [diff] [blame] | 313 | // TODO - fetch for the iface |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 314 | public int getThrottle(String iface) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 315 | enforceAccessPermission(); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 316 | if (mThrottleIndex.get() == 1) { |
| 317 | return mPolicyThrottleValue.get(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | void systemReady() { |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 323 | if (VDBG) Slog.v(TAG, "systemReady"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 324 | mContext.registerReceiver( |
| 325 | new BroadcastReceiver() { |
| 326 | @Override |
| 327 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 328 | dispatchPoll(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 329 | } |
| 330 | }, new IntentFilter(ACTION_POLL)); |
| 331 | |
| 332 | mContext.registerReceiver( |
| 333 | new BroadcastReceiver() { |
| 334 | @Override |
| 335 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 336 | dispatchReset(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 337 | } |
| 338 | }, new IntentFilter(ACTION_RESET)); |
| 339 | |
Robert Greenwalt | c76c15e | 2010-06-16 14:42:16 -0700 | [diff] [blame] | 340 | // use a new thread as we don't want to stall the system for file writes |
| 341 | mThread = new HandlerThread(TAG); |
| 342 | mThread.start(); |
| 343 | mHandler = new MyHandler(mThread.getLooper()); |
| 344 | mHandler.obtainMessage(EVENT_REBOOT_RECOVERY).sendToTarget(); |
| 345 | |
| 346 | mInterfaceObserver = new InterfaceObserver(mHandler, EVENT_IFACE_UP, mIface); |
| 347 | try { |
| 348 | mNMService.registerObserver(mInterfaceObserver); |
| 349 | } catch (RemoteException e) { |
| 350 | Slog.e(TAG, "Could not register InterfaceObserver " + e); |
| 351 | } |
| 352 | |
| 353 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_POLICY_CHANGED); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 354 | mSettingsObserver.register(mContext); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 357 | void shutdown() { |
| 358 | // TODO: eventually connect with ShutdownThread to persist stats during |
| 359 | // graceful shutdown. |
| 360 | |
| 361 | if (mThread != null) { |
| 362 | mThread.quit(); |
| 363 | } |
| 364 | |
| 365 | if (mSettingsObserver != null) { |
| 366 | mSettingsObserver.unregister(mContext); |
| 367 | } |
| 368 | |
| 369 | if (mPollStickyBroadcast != null) { |
| 370 | mContext.removeStickyBroadcast(mPollStickyBroadcast); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void dispatchPoll() { |
| 375 | mHandler.obtainMessage(EVENT_POLL_ALARM).sendToTarget(); |
| 376 | } |
| 377 | |
| 378 | void dispatchReset() { |
| 379 | mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget(); |
| 380 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 381 | |
| 382 | private static final int EVENT_REBOOT_RECOVERY = 0; |
| 383 | private static final int EVENT_POLICY_CHANGED = 1; |
| 384 | private static final int EVENT_POLL_ALARM = 2; |
| 385 | private static final int EVENT_RESET_ALARM = 3; |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 386 | private static final int EVENT_IFACE_UP = 4; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 387 | private class MyHandler extends Handler { |
| 388 | public MyHandler(Looper l) { |
| 389 | super(l); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void handleMessage(Message msg) { |
| 394 | switch (msg.what) { |
| 395 | case EVENT_REBOOT_RECOVERY: |
| 396 | onRebootRecovery(); |
| 397 | break; |
| 398 | case EVENT_POLICY_CHANGED: |
| 399 | onPolicyChanged(); |
| 400 | break; |
| 401 | case EVENT_POLL_ALARM: |
| 402 | onPollAlarm(); |
| 403 | break; |
| 404 | case EVENT_RESET_ALARM: |
| 405 | onResetAlarm(); |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 406 | break; |
| 407 | case EVENT_IFACE_UP: |
| 408 | onIfaceUp(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | private void onRebootRecovery() { |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 413 | if (VDBG) Slog.v(TAG, "onRebootRecovery"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 414 | // check for sim change TODO |
| 415 | // reregister for notification of policy change |
| 416 | |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 417 | mThrottleIndex.set(THROTTLE_INDEX_UNINITIALIZED); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 418 | |
| 419 | mRecorder = new DataRecorder(mContext, ThrottleService.this); |
| 420 | |
| 421 | // get policy |
| 422 | mHandler.obtainMessage(EVENT_POLICY_CHANGED).sendToTarget(); |
Robert Greenwalt | fb9896b | 2010-04-22 15:39:38 -0700 | [diff] [blame] | 423 | |
| 424 | // if we poll now we won't have network connectivity or even imsi access |
| 425 | // queue up a poll to happen in a little while - after ntp and imsi are avail |
| 426 | // TODO - make this callback based (ie, listen for notificaitons) |
| 427 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_POLL_ALARM), |
| 428 | INITIAL_POLL_DELAY_SEC * 1000); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // check for new policy info (threshold limit/value/etc) |
| 432 | private void onPolicyChanged() { |
| 433 | boolean testing = SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true"); |
| 434 | |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 435 | int pollingPeriod = mContext.getResources().getInteger( |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 436 | R.integer.config_datause_polling_period_sec); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 437 | mPolicyPollPeriodSec = Settings.Secure.getInt(mContext.getContentResolver(), |
| 438 | Settings.Secure.THROTTLE_POLLING_SEC, pollingPeriod); |
| 439 | |
| 440 | // TODO - remove testing stuff? |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 441 | long defaultThreshold = mContext.getResources().getInteger( |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 442 | R.integer.config_datause_threshold_bytes); |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 443 | int defaultValue = mContext.getResources().getInteger( |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 444 | R.integer.config_datause_throttle_kbitsps); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 445 | long threshold = Settings.Secure.getLong(mContext.getContentResolver(), |
| 446 | Settings.Secure.THROTTLE_THRESHOLD_BYTES, defaultThreshold); |
| 447 | int value = Settings.Secure.getInt(mContext.getContentResolver(), |
| 448 | Settings.Secure.THROTTLE_VALUE_KBITSPS, defaultValue); |
| 449 | |
| 450 | mPolicyThreshold.set(threshold); |
| 451 | mPolicyThrottleValue.set(value); |
| 452 | if (testing) { |
| 453 | mPolicyPollPeriodSec = TESTING_POLLING_PERIOD_SEC; |
| 454 | mPolicyThreshold.set(TESTING_THRESHOLD); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 455 | } |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 456 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 457 | mPolicyResetDay = Settings.Secure.getInt(mContext.getContentResolver(), |
| 458 | Settings.Secure.THROTTLE_RESET_DAY, -1); |
| 459 | if (mPolicyResetDay == -1 || |
| 460 | ((mPolicyResetDay < 1) || (mPolicyResetDay > 28))) { |
| 461 | Random g = new Random(); |
| 462 | mPolicyResetDay = 1 + g.nextInt(28); // 1-28 |
| 463 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 464 | Settings.Secure.THROTTLE_RESET_DAY, mPolicyResetDay); |
| 465 | } |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 466 | if (mIface == null) { |
| 467 | mPolicyThreshold.set(0); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 470 | int defaultNotificationType = mContext.getResources().getInteger( |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 471 | R.integer.config_datause_notification_type); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 472 | mPolicyNotificationsAllowedMask = Settings.Secure.getInt(mContext.getContentResolver(), |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 473 | Settings.Secure.THROTTLE_NOTIFICATION_TYPE, defaultNotificationType); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 474 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 475 | final int maxNtpCacheAgeSec = Settings.Secure.getInt(mContext.getContentResolver(), |
| 476 | Settings.Secure.THROTTLE_MAX_NTP_CACHE_AGE_SEC, |
| 477 | (int) (MAX_NTP_CACHE_AGE / 1000)); |
| 478 | mMaxNtpCacheAge = maxNtpCacheAgeSec * 1000; |
Robert Greenwalt | d1055a2 | 2010-05-25 15:54:52 -0700 | [diff] [blame] | 479 | |
Robert Greenwalt | 687f2a0 | 2010-06-08 10:10:54 -0700 | [diff] [blame] | 480 | if (VDBG || (mPolicyThreshold.get() != 0)) { |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 481 | Slog.d(TAG, "onPolicyChanged testing=" + testing +", period=" + |
Robert Greenwalt | 687f2a0 | 2010-06-08 10:10:54 -0700 | [diff] [blame] | 482 | mPolicyPollPeriodSec + ", threshold=" + mPolicyThreshold.get() + |
| 483 | ", value=" + mPolicyThrottleValue.get() + ", resetDay=" + mPolicyResetDay + |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 484 | ", noteType=" + mPolicyNotificationsAllowedMask + ", mMaxNtpCacheAge=" + |
| 485 | mMaxNtpCacheAge); |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 486 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 487 | |
Robert Greenwalt | 5bf16d6 | 2010-04-23 13:15:44 -0700 | [diff] [blame] | 488 | // force updates |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 489 | mThrottleIndex.set(THROTTLE_INDEX_UNINITIALIZED); |
Robert Greenwalt | 5bf16d6 | 2010-04-23 13:15:44 -0700 | [diff] [blame] | 490 | |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 491 | onResetAlarm(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 492 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 493 | onPollAlarm(); |
| 494 | |
Robert Greenwalt | 81aa097 | 2010-04-09 09:36:09 -0700 | [diff] [blame] | 495 | Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION); |
| 496 | mContext.sendBroadcast(broadcast); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | private void onPollAlarm() { |
| 500 | long now = SystemClock.elapsedRealtime(); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 501 | long next = now + mPolicyPollPeriodSec * 1000; |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 502 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 503 | // when trusted cache outdated, try refreshing |
| 504 | if (mTime.getCacheAge() > mMaxNtpCacheAge) { |
| 505 | if (mTime.forceRefresh()) { |
| 506 | if (VDBG) Slog.d(TAG, "updated trusted time, reseting alarm"); |
| 507 | dispatchReset(); |
| 508 | } |
| 509 | } |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 510 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 511 | long incRead = 0; |
| 512 | long incWrite = 0; |
| 513 | try { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 514 | final NetworkStats stats = mNMService.getNetworkStatsSummaryDev(); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 515 | final int index = stats.findIndex(mIface, NetworkStats.UID_ALL, |
| 516 | NetworkStats.SET_DEFAULT, NetworkStats.TAG_NONE); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 517 | |
| 518 | if (index != -1) { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 519 | final NetworkStats.Entry entry = stats.getValues(index, null); |
| 520 | incRead = entry.rxBytes - mLastRead; |
| 521 | incWrite = entry.txBytes - mLastWrite; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 522 | } else { |
| 523 | // missing iface, assume stats are 0 |
| 524 | Slog.w(TAG, "unable to find stats for iface " + mIface); |
| 525 | } |
| 526 | |
Robert Greenwalt | 8c7e609 | 2010-04-14 17:31:20 -0700 | [diff] [blame] | 527 | // handle iface resets - on some device the 3g iface comes and goes and gets |
| 528 | // totals reset to 0. Deal with it |
| 529 | if ((incRead < 0) || (incWrite < 0)) { |
| 530 | incRead += mLastRead; |
| 531 | incWrite += mLastWrite; |
| 532 | mLastRead = 0; |
| 533 | mLastWrite = 0; |
| 534 | } |
Jeff Sharkey | 558a232 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 535 | } catch (IllegalStateException e) { |
| 536 | Slog.e(TAG, "problem during onPollAlarm: " + e); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 537 | } catch (RemoteException e) { |
Jeff Sharkey | 558a232 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 538 | Slog.e(TAG, "problem during onPollAlarm: " + e); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 539 | } |
Jeff Sharkey | 558a232 | 2011-08-24 15:42:09 -0700 | [diff] [blame] | 540 | |
Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 541 | // don't count this data if we're roaming. |
| 542 | boolean roaming = "true".equals( |
| 543 | SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING)); |
| 544 | if (!roaming) { |
| 545 | mRecorder.addData(incRead, incWrite); |
| 546 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 547 | |
| 548 | long periodRx = mRecorder.getPeriodRx(0); |
| 549 | long periodTx = mRecorder.getPeriodTx(0); |
| 550 | long total = periodRx + periodTx; |
Robert Greenwalt | 687f2a0 | 2010-06-08 10:10:54 -0700 | [diff] [blame] | 551 | if (VDBG || (mPolicyThreshold.get() != 0)) { |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 552 | Slog.d(TAG, "onPollAlarm - roaming =" + roaming + |
Robert Greenwalt | 5f99689 | 2010-04-08 16:19:24 -0700 | [diff] [blame] | 553 | ", read =" + incRead + ", written =" + incWrite + ", new total =" + total); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 554 | } |
| 555 | mLastRead += incRead; |
| 556 | mLastWrite += incWrite; |
| 557 | |
| 558 | checkThrottleAndPostNotification(total); |
| 559 | |
| 560 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_POLL_ACTION); |
| 561 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_READ, periodRx); |
| 562 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_WRITE, periodTx); |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 563 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_START, getPeriodStartTime(mIface)); |
| 564 | broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_END, getResetTime(mIface)); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 565 | mContext.sendStickyBroadcast(broadcast); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 566 | mPollStickyBroadcast = broadcast; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 567 | |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 568 | mAlarmManager.cancel(mPendingPollIntent); |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 569 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, next, mPendingPollIntent); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 572 | private void onIfaceUp() { |
| 573 | // if we were throttled before, be sure and set it again - the iface went down |
| 574 | // (and may have disappeared all together) and these settings were lost |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 575 | if (mThrottleIndex.get() == 1) { |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 576 | try { |
| 577 | mNMService.setInterfaceThrottle(mIface, -1, -1); |
| 578 | mNMService.setInterfaceThrottle(mIface, |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 579 | mPolicyThrottleValue.get(), mPolicyThrottleValue.get()); |
Robert Greenwalt | fee4683 | 2010-05-06 12:25:13 -0700 | [diff] [blame] | 580 | } catch (Exception e) { |
| 581 | Slog.e(TAG, "error setting Throttle: " + e); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 586 | private void checkThrottleAndPostNotification(long currentTotal) { |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 587 | // is throttling enabled? |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 588 | long threshold = mPolicyThreshold.get(); |
| 589 | if (threshold == 0) { |
Robert Greenwalt | cce8337 | 2010-04-23 17:35:29 -0700 | [diff] [blame] | 590 | clearThrottleAndNotification(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 591 | return; |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // have we spoken with an ntp server yet? |
| 595 | // this is controversial, but we'd rather err towards not throttling |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 596 | if (!mTime.hasCache()) { |
| 597 | Slog.w(TAG, "missing trusted time, skipping throttle check"); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 598 | return; |
| 599 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 600 | |
| 601 | // check if we need to throttle |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 602 | if (currentTotal > threshold) { |
| 603 | if (mThrottleIndex.get() != 1) { |
| 604 | mThrottleIndex.set(1); |
| 605 | if (DBG) Slog.d(TAG, "Threshold " + threshold + " exceeded!"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 606 | try { |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 607 | mNMService.setInterfaceThrottle(mIface, |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 608 | mPolicyThrottleValue.get(), mPolicyThrottleValue.get()); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 609 | } catch (Exception e) { |
| 610 | Slog.e(TAG, "error setting Throttle: " + e); |
| 611 | } |
| 612 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 613 | mNotificationManager.cancel(R.drawable.stat_sys_throttled); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 614 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 615 | postNotification(R.string.throttled_notification_title, |
| 616 | R.string.throttled_notification_message, |
| 617 | R.drawable.stat_sys_throttled, |
Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 618 | Notification.FLAG_ONGOING_EVENT); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 619 | |
| 620 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_ACTION); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 621 | broadcast.putExtra(ThrottleManager.EXTRA_THROTTLE_LEVEL, |
| 622 | mPolicyThrottleValue.get()); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 623 | mContext.sendStickyBroadcast(broadcast); |
| 624 | |
| 625 | } // else already up! |
| 626 | } else { |
Robert Greenwalt | 5bf16d6 | 2010-04-23 13:15:44 -0700 | [diff] [blame] | 627 | clearThrottleAndNotification(); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 628 | if ((mPolicyNotificationsAllowedMask & NOTIFICATION_WARNING) != 0) { |
| 629 | // check if we should warn about throttle |
Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 630 | // pretend we only have 1/2 the time remaining that we actually do |
| 631 | // if our burn rate in the period so far would have us exceed the limit |
| 632 | // in that 1/2 window, warn the user. |
| 633 | // this gets more generous in the early to middle period and converges back |
| 634 | // to the limit as we move toward the period end. |
| 635 | |
| 636 | // adding another factor - it must be greater than the total cap/4 |
| 637 | // else we may get false alarms very early in the period.. in the first |
| 638 | // tenth of a percent of the period if we used more than a tenth of a percent |
| 639 | // of the cap we'd get a warning and that's not desired. |
| 640 | long start = mRecorder.getPeriodStart(); |
| 641 | long end = mRecorder.getPeriodEnd(); |
| 642 | long periodLength = end - start; |
| 643 | long now = System.currentTimeMillis(); |
| 644 | long timeUsed = now - start; |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 645 | long warningThreshold = 2*threshold*timeUsed/(timeUsed+periodLength); |
| 646 | if ((currentTotal > warningThreshold) && (currentTotal > threshold/4)) { |
Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 647 | if (mWarningNotificationSent == false) { |
| 648 | mWarningNotificationSent = true; |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 649 | mNotificationManager.cancel(R.drawable.stat_sys_throttled); |
| 650 | postNotification(R.string.throttle_warning_notification_title, |
| 651 | R.string.throttle_warning_notification_message, |
| 652 | R.drawable.stat_sys_throttled, |
Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 653 | 0); |
| 654 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 655 | } else { |
Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 656 | if (mWarningNotificationSent == true) { |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 657 | mNotificationManager.cancel(R.drawable.stat_sys_throttled); |
Robert Greenwalt | e2c0ce0 | 2010-04-09 12:31:46 -0700 | [diff] [blame] | 658 | mWarningNotificationSent =false; |
| 659 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 665 | private void postNotification(int titleInt, int messageInt, int icon, int flags) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 666 | Intent intent = new Intent(); |
| 667 | // TODO - fix up intent |
Robert Greenwalt | 2a7b730 | 2010-04-12 14:56:31 -0700 | [diff] [blame] | 668 | intent.setClassName("com.android.phone", "com.android.phone.DataUsage"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 669 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); |
| 670 | |
| 671 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 672 | |
| 673 | Resources r = Resources.getSystem(); |
| 674 | CharSequence title = r.getText(titleInt); |
| 675 | CharSequence message = r.getText(messageInt); |
| 676 | if (mThrottlingNotification == null) { |
| 677 | mThrottlingNotification = new Notification(); |
| 678 | mThrottlingNotification.when = 0; |
| 679 | // TODO - fixup icon |
| 680 | mThrottlingNotification.icon = icon; |
| 681 | mThrottlingNotification.defaults &= ~Notification.DEFAULT_SOUND; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 682 | } |
Robert Greenwalt | c87dc6d | 2010-04-08 16:00:26 -0700 | [diff] [blame] | 683 | mThrottlingNotification.flags = flags; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 684 | mThrottlingNotification.tickerText = title; |
| 685 | mThrottlingNotification.setLatestEventInfo(mContext, title, message, pi); |
| 686 | |
| 687 | mNotificationManager.notify(mThrottlingNotification.icon, mThrottlingNotification); |
| 688 | } |
| 689 | |
| 690 | |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 691 | private void clearThrottleAndNotification() { |
| 692 | if (mThrottleIndex.get() != THROTTLE_INDEX_UNTHROTTLED) { |
| 693 | mThrottleIndex.set(THROTTLE_INDEX_UNTHROTTLED); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 694 | try { |
Robert Greenwalt | d3bb93f | 2010-04-12 19:20:55 -0700 | [diff] [blame] | 695 | mNMService.setInterfaceThrottle(mIface, -1, -1); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 696 | } catch (Exception e) { |
| 697 | Slog.e(TAG, "error clearing Throttle: " + e); |
| 698 | } |
| 699 | Intent broadcast = new Intent(ThrottleManager.THROTTLE_ACTION); |
| 700 | broadcast.putExtra(ThrottleManager.EXTRA_THROTTLE_LEVEL, -1); |
| 701 | mContext.sendStickyBroadcast(broadcast); |
Robert Greenwalt | 5bf16d6 | 2010-04-23 13:15:44 -0700 | [diff] [blame] | 702 | mNotificationManager.cancel(R.drawable.stat_sys_throttled); |
| 703 | mWarningNotificationSent = false; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 704 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 707 | private Calendar calculatePeriodEnd(long now) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 708 | Calendar end = GregorianCalendar.getInstance(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 709 | end.setTimeInMillis(now); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 710 | int day = end.get(Calendar.DAY_OF_MONTH); |
| 711 | end.set(Calendar.DAY_OF_MONTH, mPolicyResetDay); |
| 712 | end.set(Calendar.HOUR_OF_DAY, 0); |
| 713 | end.set(Calendar.MINUTE, 0); |
Robert Greenwalt | 8c7e609 | 2010-04-14 17:31:20 -0700 | [diff] [blame] | 714 | end.set(Calendar.SECOND, 0); |
| 715 | end.set(Calendar.MILLISECOND, 0); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 716 | if (day >= mPolicyResetDay) { |
| 717 | int month = end.get(Calendar.MONTH); |
| 718 | if (month == Calendar.DECEMBER) { |
| 719 | end.set(Calendar.YEAR, end.get(Calendar.YEAR) + 1); |
| 720 | month = Calendar.JANUARY - 1; |
| 721 | } |
| 722 | end.set(Calendar.MONTH, month + 1); |
| 723 | } |
| 724 | |
| 725 | // TODO - remove! |
| 726 | if (SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true")) { |
| 727 | end = GregorianCalendar.getInstance(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 728 | end.setTimeInMillis(now); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 729 | end.add(Calendar.SECOND, TESTING_RESET_PERIOD_SEC); |
| 730 | } |
| 731 | return end; |
| 732 | } |
| 733 | private Calendar calculatePeriodStart(Calendar end) { |
| 734 | Calendar start = (Calendar)end.clone(); |
| 735 | int month = end.get(Calendar.MONTH); |
| 736 | if (end.get(Calendar.MONTH) == Calendar.JANUARY) { |
| 737 | month = Calendar.DECEMBER + 1; |
| 738 | start.set(Calendar.YEAR, start.get(Calendar.YEAR) - 1); |
| 739 | } |
| 740 | start.set(Calendar.MONTH, month - 1); |
| 741 | |
| 742 | // TODO - remove!! |
| 743 | if (SystemProperties.get(TESTING_ENABLED_PROPERTY).equals("true")) { |
| 744 | start = (Calendar)end.clone(); |
| 745 | start.add(Calendar.SECOND, -TESTING_RESET_PERIOD_SEC); |
| 746 | } |
| 747 | return start; |
| 748 | } |
| 749 | |
| 750 | private void onResetAlarm() { |
Robert Greenwalt | 687f2a0 | 2010-06-08 10:10:54 -0700 | [diff] [blame] | 751 | if (VDBG || (mPolicyThreshold.get() != 0)) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 752 | Slog.d(TAG, "onResetAlarm - last period had " + mRecorder.getPeriodRx(0) + |
| 753 | " bytes read and " + mRecorder.getPeriodTx(0) + " written"); |
| 754 | } |
| 755 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 756 | // when trusted cache outdated, try refreshing |
| 757 | if (mTime.getCacheAge() > mMaxNtpCacheAge) { |
| 758 | mTime.forceRefresh(); |
| 759 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 760 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 761 | // as long as we have a trusted time cache, we always reset alarms, |
| 762 | // even if the refresh above failed. |
| 763 | if (mTime.hasCache()) { |
| 764 | final long now = mTime.currentTimeMillis(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 765 | Calendar end = calculatePeriodEnd(now); |
| 766 | Calendar start = calculatePeriodStart(end); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 767 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 768 | if (mRecorder.setNextPeriod(start, end)) { |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 769 | onPollAlarm(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 770 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 771 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 772 | mAlarmManager.cancel(mPendingResetIntent); |
| 773 | long offset = end.getTimeInMillis() - now; |
| 774 | // use Elapsed realtime so clock changes don't fool us. |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 775 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 776 | SystemClock.elapsedRealtime() + offset, |
| 777 | mPendingResetIntent); |
| 778 | } else { |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 779 | if (VDBG) Slog.d(TAG, "no trusted time, not resetting period"); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 780 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | // records bytecount data for a given time and accumulates it into larger time windows |
| 785 | // for logging and other purposes |
| 786 | // |
| 787 | // since time can be changed (user or network action) we will have to track the time of the |
| 788 | // last recording and deal with it. |
| 789 | private static class DataRecorder { |
| 790 | long[] mPeriodRxData; |
| 791 | long[] mPeriodTxData; |
| 792 | int mCurrentPeriod; |
| 793 | int mPeriodCount; |
| 794 | |
| 795 | Calendar mPeriodStart; |
| 796 | Calendar mPeriodEnd; |
| 797 | |
| 798 | ThrottleService mParent; |
| 799 | Context mContext; |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 800 | String mImsi = null; |
| 801 | |
| 802 | TelephonyManager mTelephonyManager; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 803 | |
| 804 | DataRecorder(Context context, ThrottleService parent) { |
| 805 | mContext = context; |
| 806 | mParent = parent; |
| 807 | |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 808 | mTelephonyManager = (TelephonyManager)mContext.getSystemService( |
| 809 | Context.TELEPHONY_SERVICE); |
| 810 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 811 | synchronized (mParent) { |
| 812 | mPeriodCount = 6; |
| 813 | mPeriodRxData = new long[mPeriodCount]; |
| 814 | mPeriodTxData = new long[mPeriodCount]; |
| 815 | |
| 816 | mPeriodStart = Calendar.getInstance(); |
| 817 | mPeriodEnd = Calendar.getInstance(); |
| 818 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 819 | retrieve(); |
| 820 | } |
| 821 | } |
| 822 | |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 823 | boolean setNextPeriod(Calendar start, Calendar end) { |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 824 | // TODO - how would we deal with a dual-IMSI device? |
| 825 | checkForSubscriberId(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 826 | boolean startNewPeriod = true; |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 827 | |
Robert Greenwalt | 27fba67 | 2010-04-26 12:29:14 -0700 | [diff] [blame] | 828 | if (start.equals(mPeriodStart) && end.equals(mPeriodEnd)) { |
| 829 | // same endpoints - keep collecting |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 830 | if (VDBG) { |
Robert Greenwalt | 27fba67 | 2010-04-26 12:29:14 -0700 | [diff] [blame] | 831 | Slog.d(TAG, "same period (" + start.getTimeInMillis() + "," + |
| 832 | end.getTimeInMillis() +") - ammending data"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 833 | } |
Robert Greenwalt | 27fba67 | 2010-04-26 12:29:14 -0700 | [diff] [blame] | 834 | startNewPeriod = false; |
| 835 | } else { |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 836 | if (VDBG) { |
Robert Greenwalt | 27fba67 | 2010-04-26 12:29:14 -0700 | [diff] [blame] | 837 | if(start.equals(mPeriodEnd) || start.after(mPeriodEnd)) { |
| 838 | Slog.d(TAG, "next period (" + start.getTimeInMillis() + "," + |
| 839 | end.getTimeInMillis() + ") - old end was " + |
| 840 | mPeriodEnd.getTimeInMillis() + ", following"); |
| 841 | } else { |
| 842 | Slog.d(TAG, "new period (" + start.getTimeInMillis() + "," + |
| 843 | end.getTimeInMillis() + ") replacing old (" + |
| 844 | mPeriodStart.getTimeInMillis() + "," + |
| 845 | mPeriodEnd.getTimeInMillis() + ")"); |
| 846 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 847 | } |
| 848 | synchronized (mParent) { |
| 849 | ++mCurrentPeriod; |
| 850 | if (mCurrentPeriod >= mPeriodCount) mCurrentPeriod = 0; |
| 851 | mPeriodRxData[mCurrentPeriod] = 0; |
| 852 | mPeriodTxData[mCurrentPeriod] = 0; |
| 853 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 854 | } |
| 855 | setPeriodStart(start); |
| 856 | setPeriodEnd(end); |
| 857 | record(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 858 | return startNewPeriod; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | public long getPeriodEnd() { |
| 862 | synchronized (mParent) { |
| 863 | return mPeriodEnd.getTimeInMillis(); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | private void setPeriodEnd(Calendar end) { |
| 868 | synchronized (mParent) { |
| 869 | mPeriodEnd = end; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | public long getPeriodStart() { |
| 874 | synchronized (mParent) { |
| 875 | return mPeriodStart.getTimeInMillis(); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | private void setPeriodStart(Calendar start) { |
| 880 | synchronized (mParent) { |
| 881 | mPeriodStart = start; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | public int getPeriodCount() { |
| 886 | synchronized (mParent) { |
| 887 | return mPeriodCount; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | private void zeroData(int field) { |
| 892 | synchronized (mParent) { |
| 893 | for(int period = 0; period<mPeriodCount; period++) { |
| 894 | mPeriodRxData[period] = 0; |
| 895 | mPeriodTxData[period] = 0; |
| 896 | } |
| 897 | mCurrentPeriod = 0; |
| 898 | } |
| 899 | |
| 900 | } |
| 901 | |
| 902 | // if time moves backward accumulate all read/write that's lost into the now |
| 903 | // otherwise time moved forward. |
| 904 | void addData(long bytesRead, long bytesWritten) { |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 905 | checkForSubscriberId(); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 906 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 907 | synchronized (mParent) { |
| 908 | mPeriodRxData[mCurrentPeriod] += bytesRead; |
| 909 | mPeriodTxData[mCurrentPeriod] += bytesWritten; |
| 910 | } |
| 911 | record(); |
| 912 | } |
| 913 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 914 | private File getDataFile() { |
| 915 | File dataDir = Environment.getDataDirectory(); |
| 916 | File throttleDir = new File(dataDir, "system/throttle"); |
| 917 | throttleDir.mkdirs(); |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 918 | String mImsi = mTelephonyManager.getSubscriberId(); |
| 919 | File dataFile; |
| 920 | if (mImsi == null) { |
| 921 | dataFile = useMRUFile(throttleDir); |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 922 | if (VDBG) Slog.v(TAG, "imsi not available yet, using " + dataFile); |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 923 | } else { |
| 924 | String imsiHash = Integer.toString(mImsi.hashCode()); |
| 925 | dataFile = new File(throttleDir, imsiHash); |
| 926 | } |
| 927 | // touch the file so it's not LRU |
| 928 | dataFile.setLastModified(System.currentTimeMillis()); |
| 929 | checkAndDeleteLRUDataFile(throttleDir); |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 930 | return dataFile; |
| 931 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 932 | |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 933 | // TODO - get broadcast (TelephonyIntents.ACTION_SIM_STATE_CHANGED) instead of polling |
| 934 | private void checkForSubscriberId() { |
| 935 | if (mImsi != null) return; |
| 936 | |
| 937 | mImsi = mTelephonyManager.getSubscriberId(); |
| 938 | if (mImsi == null) return; |
| 939 | |
Robert Greenwalt | 5a671d0 | 2010-06-07 16:43:16 -0700 | [diff] [blame] | 940 | if (VDBG) Slog.d(TAG, "finally have imsi - retreiving data"); |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 941 | retrieve(); |
| 942 | } |
| 943 | |
| 944 | private final static int MAX_SIMS_SUPPORTED = 3; |
| 945 | |
| 946 | private void checkAndDeleteLRUDataFile(File dir) { |
| 947 | File[] files = dir.listFiles(); |
| 948 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 949 | if (files == null || files.length <= MAX_SIMS_SUPPORTED) return; |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 950 | if (DBG) Slog.d(TAG, "Too many data files"); |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 951 | do { |
| 952 | File oldest = null; |
| 953 | for (File f : files) { |
| 954 | if ((oldest == null) || (oldest.lastModified() > f.lastModified())) { |
| 955 | oldest = f; |
| 956 | } |
| 957 | } |
| 958 | if (oldest == null) return; |
Robert Greenwalt | bf7de39 | 2010-04-21 17:09:38 -0700 | [diff] [blame] | 959 | if (DBG) Slog.d(TAG, " deleting " + oldest); |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 960 | oldest.delete(); |
| 961 | files = dir.listFiles(); |
| 962 | } while (files.length > MAX_SIMS_SUPPORTED); |
| 963 | } |
| 964 | |
| 965 | private File useMRUFile(File dir) { |
| 966 | File newest = null; |
| 967 | File[] files = dir.listFiles(); |
| 968 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 969 | if (files != null) { |
| 970 | for (File f : files) { |
| 971 | if ((newest == null) || (newest.lastModified() < f.lastModified())) { |
| 972 | newest = f; |
| 973 | } |
Robert Greenwalt | e6e9882 | 2010-04-15 08:27:14 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | if (newest == null) { |
| 977 | newest = new File(dir, "temp"); |
| 978 | } |
| 979 | return newest; |
| 980 | } |
| 981 | |
| 982 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 983 | private static final int DATA_FILE_VERSION = 1; |
| 984 | |
| 985 | private void record() { |
| 986 | // 1 int version |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 987 | // 1 int mPeriodCount |
| 988 | // 13*6 long[PERIOD_COUNT] mPeriodRxData |
| 989 | // 13*6 long[PERIOD_COUNT] mPeriodTxData |
| 990 | // 1 int mCurrentPeriod |
| 991 | // 13 long periodStartMS |
| 992 | // 13 long periodEndMS |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 993 | // 200 chars max |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 994 | StringBuilder builder = new StringBuilder(); |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 995 | builder.append(DATA_FILE_VERSION); |
| 996 | builder.append(":"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 997 | builder.append(mPeriodCount); |
| 998 | builder.append(":"); |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 999 | for(int i = 0; i < mPeriodCount; i++) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1000 | builder.append(mPeriodRxData[i]); |
| 1001 | builder.append(":"); |
| 1002 | } |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 1003 | for(int i = 0; i < mPeriodCount; i++) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1004 | builder.append(mPeriodTxData[i]); |
| 1005 | builder.append(":"); |
| 1006 | } |
| 1007 | builder.append(mCurrentPeriod); |
| 1008 | builder.append(":"); |
| 1009 | builder.append(mPeriodStart.getTimeInMillis()); |
| 1010 | builder.append(":"); |
| 1011 | builder.append(mPeriodEnd.getTimeInMillis()); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1012 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 1013 | BufferedWriter out = null; |
| 1014 | try { |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 1015 | out = new BufferedWriter(new FileWriter(getDataFile()), 256); |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 1016 | out.write(builder.toString()); |
| 1017 | } catch (IOException e) { |
| 1018 | Slog.e(TAG, "Error writing data file"); |
| 1019 | return; |
| 1020 | } finally { |
| 1021 | if (out != null) { |
| 1022 | try { |
| 1023 | out.close(); |
| 1024 | } catch (Exception e) {} |
| 1025 | } |
| 1026 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | private void retrieve() { |
Robert Greenwalt | 05d0673 | 2010-04-19 11:10:38 -0700 | [diff] [blame] | 1030 | // clean out any old data first. If we fail to read we don't want old stuff |
| 1031 | zeroData(0); |
| 1032 | |
Robert Greenwalt | b8912f5 | 2010-04-09 17:27:26 -0700 | [diff] [blame] | 1033 | File f = getDataFile(); |
| 1034 | byte[] buffer; |
| 1035 | FileInputStream s = null; |
| 1036 | try { |
| 1037 | buffer = new byte[(int)f.length()]; |
| 1038 | s = new FileInputStream(f); |
| 1039 | s.read(buffer); |
| 1040 | } catch (IOException e) { |
| 1041 | Slog.e(TAG, "Error reading data file"); |
| 1042 | return; |
| 1043 | } finally { |
| 1044 | if (s != null) { |
| 1045 | try { |
| 1046 | s.close(); |
| 1047 | } catch (Exception e) {} |
| 1048 | } |
| 1049 | } |
| 1050 | String data = new String(buffer); |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 1051 | if (data == null || data.length() == 0) { |
| 1052 | if (DBG) Slog.d(TAG, "data file empty"); |
| 1053 | return; |
| 1054 | } |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1055 | String[] parsed = data.split(":"); |
| 1056 | int parsedUsed = 0; |
| 1057 | if (parsed.length < 6) { |
| 1058 | Slog.e(TAG, "reading data file with insufficient length - ignoring"); |
| 1059 | return; |
| 1060 | } |
| 1061 | |
Robert Greenwalt | 9e3983f | 2010-05-11 07:06:13 -0700 | [diff] [blame] | 1062 | int periodCount; |
| 1063 | long[] periodRxData; |
| 1064 | long[] periodTxData; |
| 1065 | int currentPeriod; |
| 1066 | Calendar periodStart; |
| 1067 | Calendar periodEnd; |
| 1068 | try { |
| 1069 | if (Integer.parseInt(parsed[parsedUsed++]) != DATA_FILE_VERSION) { |
| 1070 | Slog.e(TAG, "reading data file with bad version - ignoring"); |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | periodCount = Integer.parseInt(parsed[parsedUsed++]); |
| 1075 | if (parsed.length != 5 + (2 * periodCount)) { |
| 1076 | Slog.e(TAG, "reading data file with bad length (" + parsed.length + |
| 1077 | " != " + (5 + (2 * periodCount)) + ") - ignoring"); |
| 1078 | return; |
| 1079 | } |
| 1080 | periodRxData = new long[periodCount]; |
| 1081 | for (int i = 0; i < periodCount; i++) { |
| 1082 | periodRxData[i] = Long.parseLong(parsed[parsedUsed++]); |
| 1083 | } |
| 1084 | periodTxData = new long[periodCount]; |
| 1085 | for (int i = 0; i < periodCount; i++) { |
| 1086 | periodTxData[i] = Long.parseLong(parsed[parsedUsed++]); |
| 1087 | } |
| 1088 | |
| 1089 | currentPeriod = Integer.parseInt(parsed[parsedUsed++]); |
| 1090 | |
| 1091 | periodStart = new GregorianCalendar(); |
| 1092 | periodStart.setTimeInMillis(Long.parseLong(parsed[parsedUsed++])); |
| 1093 | periodEnd = new GregorianCalendar(); |
| 1094 | periodEnd.setTimeInMillis(Long.parseLong(parsed[parsedUsed++])); |
| 1095 | } catch (Exception e) { |
| 1096 | Slog.e(TAG, "Error parsing data file - ignoring"); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1097 | return; |
| 1098 | } |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1099 | synchronized (mParent) { |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1100 | mPeriodCount = periodCount; |
| 1101 | mPeriodRxData = periodRxData; |
| 1102 | mPeriodTxData = periodTxData; |
Robert Greenwalt | 9e3983f | 2010-05-11 07:06:13 -0700 | [diff] [blame] | 1103 | mCurrentPeriod = currentPeriod; |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1104 | mPeriodStart = periodStart; |
| 1105 | mPeriodEnd = periodEnd; |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | long getPeriodRx(int which) { |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1110 | synchronized (mParent) { |
| 1111 | if (which > mPeriodCount) return 0; |
| 1112 | which = mCurrentPeriod - which; |
| 1113 | if (which < 0) which += mPeriodCount; |
| 1114 | return mPeriodRxData[which]; |
| 1115 | } |
| 1116 | } |
| 1117 | long getPeriodTx(int which) { |
| 1118 | synchronized (mParent) { |
| 1119 | if (which > mPeriodCount) return 0; |
| 1120 | which = mCurrentPeriod - which; |
| 1121 | if (which < 0) which += mPeriodCount; |
| 1122 | return mPeriodTxData[which]; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | @Override |
| 1128 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1129 | if (mContext.checkCallingOrSelfPermission( |
| 1130 | android.Manifest.permission.DUMP) |
| 1131 | != PackageManager.PERMISSION_GRANTED) { |
| 1132 | pw.println("Permission Denial: can't dump ThrottleService " + |
| 1133 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1134 | Binder.getCallingUid()); |
| 1135 | return; |
| 1136 | } |
| 1137 | pw.println(); |
| 1138 | |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1139 | pw.println("The threshold is " + mPolicyThreshold.get() + |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1140 | ", after which you experince throttling to " + |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1141 | mPolicyThrottleValue.get() + "kbps"); |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1142 | pw.println("Current period is " + |
| 1143 | (mRecorder.getPeriodEnd() - mRecorder.getPeriodStart())/1000 + " seconds long " + |
Robert Greenwalt | 7171ea8 | 2010-04-14 22:37:12 -0700 | [diff] [blame] | 1144 | "and ends in " + (getResetTime(mIface) - System.currentTimeMillis()) / 1000 + |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1145 | " seconds."); |
| 1146 | pw.println("Polling every " + mPolicyPollPeriodSec + " seconds"); |
Robert Greenwalt | 39e163f | 2010-05-07 16:52:17 -0700 | [diff] [blame] | 1147 | pw.println("Current Throttle Index is " + mThrottleIndex.get()); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 1148 | pw.println("mMaxNtpCacheAge=" + mMaxNtpCacheAge); |
Robert Greenwalt | 8c7e609 | 2010-04-14 17:31:20 -0700 | [diff] [blame] | 1149 | |
Robert Greenwalt | 9e696c2 | 2010-04-01 14:45:18 -0700 | [diff] [blame] | 1150 | for (int i = 0; i < mRecorder.getPeriodCount(); i++) { |
| 1151 | pw.println(" Period[" + i + "] - read:" + mRecorder.getPeriodRx(i) + ", written:" + |
| 1152 | mRecorder.getPeriodTx(i)); |
| 1153 | } |
| 1154 | } |
| 1155 | } |