Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.internal.os; |
| 18 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.content.IntentFilter; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 22 | import android.hardware.SensorManager; |
| 23 | import android.net.ConnectivityManager; |
| 24 | import android.os.BatteryStats; |
| 25 | import android.os.BatteryStats.Uid; |
| 26 | import android.os.Bundle; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 27 | import android.os.MemoryFile; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 28 | import android.os.Parcel; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 29 | import android.os.ParcelFileDescriptor; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 30 | import android.os.Process; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.os.SystemClock; |
| 34 | import android.os.UserHandle; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.util.SparseArray; |
| 38 | |
| 39 | import com.android.internal.app.IBatteryStats; |
| 40 | import com.android.internal.os.BatterySipper.DrainType; |
| 41 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 42 | import java.io.File; |
| 43 | import java.io.FileInputStream; |
| 44 | import java.io.FileOutputStream; |
| 45 | import java.io.IOException; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 48 | import java.util.Comparator; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 49 | import java.util.List; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * A helper class for retrieving the power usage information for all applications and services. |
| 53 | * |
| 54 | * The caller must initialize this class as soon as activity object is ready to use (for example, in |
| 55 | * onAttach() for Fragment), call create() in onCreate() and call destroy() in onDestroy(). |
| 56 | */ |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 57 | public final class BatteryStatsHelper { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 58 | static final boolean DEBUG = false; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 59 | |
| 60 | private static final String TAG = BatteryStatsHelper.class.getSimpleName(); |
| 61 | |
| 62 | private static BatteryStats sStatsXfer; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 63 | private static Intent sBatteryBroadcastXfer; |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 64 | private static ArrayMap<File, BatteryStats> sFileXfer = new ArrayMap<>(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 65 | |
| 66 | final private Context mContext; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 67 | final private boolean mCollectBatteryBroadcast; |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 68 | final private boolean mWifiOnly; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 69 | |
| 70 | private IBatteryStats mBatteryInfo; |
| 71 | private BatteryStats mStats; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 72 | private Intent mBatteryBroadcast; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 73 | private PowerProfile mPowerProfile; |
| 74 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 75 | /** |
| 76 | * List of apps using power. |
| 77 | */ |
| 78 | private final List<BatterySipper> mUsageList = new ArrayList<>(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 80 | /** |
| 81 | * List of apps using wifi power. |
| 82 | */ |
| 83 | private final List<BatterySipper> mWifiSippers = new ArrayList<>(); |
| 84 | |
| 85 | /** |
| 86 | * List of apps using bluetooth power. |
| 87 | */ |
| 88 | private final List<BatterySipper> mBluetoothSippers = new ArrayList<>(); |
| 89 | |
| 90 | private final SparseArray<List<BatterySipper>> mUserSippers = new SparseArray<>(); |
| 91 | |
| 92 | private final List<BatterySipper> mMobilemsppList = new ArrayList<>(); |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 93 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 94 | private int mStatsType = BatteryStats.STATS_SINCE_CHARGED; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 95 | |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 96 | long mRawRealtime; |
| 97 | long mRawUptime; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 98 | long mBatteryRealtime; |
| 99 | long mBatteryUptime; |
| 100 | long mTypeBatteryRealtime; |
| 101 | long mTypeBatteryUptime; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 102 | long mBatteryTimeRemaining; |
| 103 | long mChargeTimeRemaining; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 104 | |
| 105 | private long mStatsPeriod = 0; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 106 | |
| 107 | // The largest entry by power. |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 108 | private double mMaxPower = 1; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 109 | |
| 110 | // The largest real entry by power (not undercounted or overcounted). |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 111 | private double mMaxRealPower = 1; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 112 | |
| 113 | // Total computed power. |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 114 | private double mComputedPower; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 115 | private double mTotalPower; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 116 | private double mMinDrainedPower; |
| 117 | private double mMaxDrainedPower; |
| 118 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 119 | PowerCalculator mCpuPowerCalculator; |
| 120 | PowerCalculator mWakelockPowerCalculator; |
| 121 | MobileRadioPowerCalculator mMobileRadioPowerCalculator; |
| 122 | PowerCalculator mWifiPowerCalculator; |
| 123 | PowerCalculator mBluetoothPowerCalculator; |
| 124 | PowerCalculator mSensorPowerCalculator; |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 125 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 126 | public static boolean checkWifiOnly(Context context) { |
| 127 | ConnectivityManager cm = (ConnectivityManager)context.getSystemService( |
| 128 | Context.CONNECTIVITY_SERVICE); |
| 129 | return !cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
| 130 | } |
| 131 | |
Adam Lesinski | 1739076 | 2015-04-10 13:17:47 -0700 | [diff] [blame] | 132 | public static boolean checkHasWifiPowerReporting(BatteryStats stats, PowerProfile profile) { |
| 133 | return stats.hasWifiActivityReporting() && |
| 134 | profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_IDLE) != 0 && |
| 135 | profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_RX) != 0 && |
| 136 | profile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX) != 0; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 137 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | e283d33 | 2015-04-16 12:29:25 -0700 | [diff] [blame] | 139 | public static boolean checkHasBluetoothPowerReporting(BatteryStats stats, |
| 140 | PowerProfile profile) { |
| 141 | return stats.hasBluetoothActivityReporting() && |
| 142 | profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE) != 0 && |
| 143 | profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX) != 0 && |
| 144 | profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_TX) != 0; |
| 145 | } |
| 146 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 147 | public BatteryStatsHelper(Context context) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 148 | this(context, true); |
| 149 | } |
| 150 | |
| 151 | public BatteryStatsHelper(Context context, boolean collectBatteryBroadcast) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 152 | this(context, collectBatteryBroadcast, checkWifiOnly(context)); |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public BatteryStatsHelper(Context context, boolean collectBatteryBroadcast, boolean wifiOnly) { |
| 156 | mContext = context; |
| 157 | mCollectBatteryBroadcast = collectBatteryBroadcast; |
| 158 | mWifiOnly = wifiOnly; |
| 159 | } |
| 160 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 161 | public void storeStatsHistoryInFile(String fname) { |
| 162 | synchronized (sFileXfer) { |
| 163 | File path = makeFilePath(mContext, fname); |
| 164 | sFileXfer.put(path, this.getStats()); |
| 165 | FileOutputStream fout = null; |
| 166 | try { |
| 167 | fout = new FileOutputStream(path); |
| 168 | Parcel hist = Parcel.obtain(); |
| 169 | getStats().writeToParcelWithoutUids(hist, 0); |
| 170 | byte[] histData = hist.marshall(); |
| 171 | fout.write(histData); |
| 172 | } catch (IOException e) { |
| 173 | Log.w(TAG, "Unable to write history to file", e); |
| 174 | } finally { |
| 175 | if (fout != null) { |
| 176 | try { |
| 177 | fout.close(); |
| 178 | } catch (IOException e) { |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public static BatteryStats statsFromFile(Context context, String fname) { |
| 186 | synchronized (sFileXfer) { |
| 187 | File path = makeFilePath(context, fname); |
| 188 | BatteryStats stats = sFileXfer.get(path); |
| 189 | if (stats != null) { |
| 190 | return stats; |
| 191 | } |
| 192 | FileInputStream fin = null; |
| 193 | try { |
| 194 | fin = new FileInputStream(path); |
| 195 | byte[] data = readFully(fin); |
| 196 | Parcel parcel = Parcel.obtain(); |
| 197 | parcel.unmarshall(data, 0, data.length); |
| 198 | parcel.setDataPosition(0); |
| 199 | return com.android.internal.os.BatteryStatsImpl.CREATOR.createFromParcel(parcel); |
| 200 | } catch (IOException e) { |
| 201 | Log.w(TAG, "Unable to read history to file", e); |
| 202 | } finally { |
| 203 | if (fin != null) { |
| 204 | try { |
| 205 | fin.close(); |
| 206 | } catch (IOException e) { |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | return getStats(IBatteryStats.Stub.asInterface( |
| 212 | ServiceManager.getService(BatteryStats.SERVICE_NAME))); |
| 213 | } |
| 214 | |
| 215 | public static void dropFile(Context context, String fname) { |
| 216 | makeFilePath(context, fname).delete(); |
| 217 | } |
| 218 | |
| 219 | private static File makeFilePath(Context context, String fname) { |
| 220 | return new File(context.getFilesDir(), fname); |
| 221 | } |
| 222 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 223 | /** Clears the current stats and forces recreating for future use. */ |
| 224 | public void clearStats() { |
| 225 | mStats = null; |
| 226 | } |
| 227 | |
| 228 | public BatteryStats getStats() { |
| 229 | if (mStats == null) { |
| 230 | load(); |
| 231 | } |
| 232 | return mStats; |
| 233 | } |
| 234 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 235 | public Intent getBatteryBroadcast() { |
| 236 | if (mBatteryBroadcast == null && mCollectBatteryBroadcast) { |
| 237 | load(); |
| 238 | } |
| 239 | return mBatteryBroadcast; |
| 240 | } |
| 241 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 242 | public PowerProfile getPowerProfile() { |
| 243 | return mPowerProfile; |
| 244 | } |
| 245 | |
| 246 | public void create(BatteryStats stats) { |
| 247 | mPowerProfile = new PowerProfile(mContext); |
| 248 | mStats = stats; |
| 249 | } |
| 250 | |
| 251 | public void create(Bundle icicle) { |
| 252 | if (icicle != null) { |
| 253 | mStats = sStatsXfer; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 254 | mBatteryBroadcast = sBatteryBroadcastXfer; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 255 | } |
| 256 | mBatteryInfo = IBatteryStats.Stub.asInterface( |
| 257 | ServiceManager.getService(BatteryStats.SERVICE_NAME)); |
| 258 | mPowerProfile = new PowerProfile(mContext); |
| 259 | } |
| 260 | |
| 261 | public void storeState() { |
| 262 | sStatsXfer = mStats; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 263 | sBatteryBroadcastXfer = mBatteryBroadcast; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | public static String makemAh(double power) { |
Adam Lesinski | e283d33 | 2015-04-16 12:29:25 -0700 | [diff] [blame] | 267 | if (power == 0) return "0"; |
| 268 | else if (power < .00001) return String.format("%.8f", power); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 269 | else if (power < .0001) return String.format("%.7f", power); |
| 270 | else if (power < .001) return String.format("%.6f", power); |
| 271 | else if (power < .01) return String.format("%.5f", power); |
| 272 | else if (power < .1) return String.format("%.4f", power); |
| 273 | else if (power < 1) return String.format("%.3f", power); |
| 274 | else if (power < 10) return String.format("%.2f", power); |
| 275 | else if (power < 100) return String.format("%.1f", power); |
| 276 | else return String.format("%.0f", power); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Refreshes the power usage list. |
| 281 | */ |
| 282 | public void refreshStats(int statsType, int asUser) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 283 | SparseArray<UserHandle> users = new SparseArray<>(1); |
Zoltan Szatmary-Ban | c3b07a0 | 2014-07-01 17:11:07 +0100 | [diff] [blame] | 284 | users.put(asUser, new UserHandle(asUser)); |
| 285 | refreshStats(statsType, users); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Refreshes the power usage list. |
| 290 | */ |
| 291 | public void refreshStats(int statsType, List<UserHandle> asUsers) { |
| 292 | final int n = asUsers.size(); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 293 | SparseArray<UserHandle> users = new SparseArray<>(n); |
Zoltan Szatmary-Ban | c3b07a0 | 2014-07-01 17:11:07 +0100 | [diff] [blame] | 294 | for (int i = 0; i < n; ++i) { |
| 295 | UserHandle userHandle = asUsers.get(i); |
| 296 | users.put(userHandle.getIdentifier(), userHandle); |
| 297 | } |
| 298 | refreshStats(statsType, users); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Refreshes the power usage list. |
| 303 | */ |
| 304 | public void refreshStats(int statsType, SparseArray<UserHandle> asUsers) { |
| 305 | refreshStats(statsType, asUsers, SystemClock.elapsedRealtime() * 1000, |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 306 | SystemClock.uptimeMillis() * 1000); |
| 307 | } |
| 308 | |
Zoltan Szatmary-Ban | c3b07a0 | 2014-07-01 17:11:07 +0100 | [diff] [blame] | 309 | public void refreshStats(int statsType, SparseArray<UserHandle> asUsers, long rawRealtimeUs, |
| 310 | long rawUptimeUs) { |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 311 | // Initialize mStats if necessary. |
| 312 | getStats(); |
| 313 | |
| 314 | mMaxPower = 0; |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 315 | mMaxRealPower = 0; |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 316 | mComputedPower = 0; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 317 | mTotalPower = 0; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 318 | |
| 319 | mUsageList.clear(); |
| 320 | mWifiSippers.clear(); |
| 321 | mBluetoothSippers.clear(); |
| 322 | mUserSippers.clear(); |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 323 | mMobilemsppList.clear(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 324 | |
| 325 | if (mStats == null) { |
| 326 | return; |
| 327 | } |
| 328 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 329 | if (mCpuPowerCalculator == null) { |
| 330 | mCpuPowerCalculator = new CpuPowerCalculator(mPowerProfile); |
| 331 | } |
| 332 | mCpuPowerCalculator.reset(); |
| 333 | |
| 334 | if (mWakelockPowerCalculator == null) { |
| 335 | mWakelockPowerCalculator = new WakelockPowerCalculator(mPowerProfile); |
| 336 | } |
| 337 | mWakelockPowerCalculator.reset(); |
| 338 | |
| 339 | if (mMobileRadioPowerCalculator == null) { |
| 340 | mMobileRadioPowerCalculator = new MobileRadioPowerCalculator(mPowerProfile, mStats); |
| 341 | } |
| 342 | mMobileRadioPowerCalculator.reset(mStats); |
| 343 | |
| 344 | if (mWifiPowerCalculator == null) { |
Adam Lesinski | 1739076 | 2015-04-10 13:17:47 -0700 | [diff] [blame] | 345 | if (checkHasWifiPowerReporting(mStats, mPowerProfile)) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 346 | mWifiPowerCalculator = new WifiPowerCalculator(mPowerProfile); |
| 347 | } else { |
| 348 | mWifiPowerCalculator = new WifiPowerEstimator(mPowerProfile); |
| 349 | } |
| 350 | } |
| 351 | mWifiPowerCalculator.reset(); |
| 352 | |
| 353 | if (mBluetoothPowerCalculator == null) { |
Adam Lesinski | e283d33 | 2015-04-16 12:29:25 -0700 | [diff] [blame] | 354 | if (checkHasBluetoothPowerReporting(mStats, mPowerProfile)) { |
| 355 | mBluetoothPowerCalculator = new BluetoothPowerCalculator(); |
| 356 | } else { |
| 357 | mBluetoothPowerCalculator = new BluetoothPowerCalculator(); |
| 358 | } |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 359 | } |
| 360 | mBluetoothPowerCalculator.reset(); |
| 361 | |
| 362 | if (mSensorPowerCalculator == null) { |
| 363 | mSensorPowerCalculator = new SensorPowerCalculator(mPowerProfile, |
| 364 | (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE)); |
| 365 | } |
| 366 | mSensorPowerCalculator.reset(); |
| 367 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 368 | mStatsType = statsType; |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 369 | mRawUptime = rawUptimeUs; |
| 370 | mRawRealtime = rawRealtimeUs; |
| 371 | mBatteryUptime = mStats.getBatteryUptime(rawUptimeUs); |
| 372 | mBatteryRealtime = mStats.getBatteryRealtime(rawRealtimeUs); |
| 373 | mTypeBatteryUptime = mStats.computeBatteryUptime(rawUptimeUs, mStatsType); |
| 374 | mTypeBatteryRealtime = mStats.computeBatteryRealtime(rawRealtimeUs, mStatsType); |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 375 | mBatteryTimeRemaining = mStats.computeBatteryTimeRemaining(rawRealtimeUs); |
| 376 | mChargeTimeRemaining = mStats.computeChargeTimeRemaining(rawRealtimeUs); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 377 | |
| 378 | if (DEBUG) { |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 379 | Log.d(TAG, "Raw time: realtime=" + (rawRealtimeUs/1000) + " uptime=" |
| 380 | + (rawUptimeUs/1000)); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 381 | Log.d(TAG, "Battery time: realtime=" + (mBatteryRealtime/1000) + " uptime=" |
| 382 | + (mBatteryUptime/1000)); |
| 383 | Log.d(TAG, "Battery type time: realtime=" + (mTypeBatteryRealtime/1000) + " uptime=" |
| 384 | + (mTypeBatteryUptime/1000)); |
| 385 | } |
| 386 | mMinDrainedPower = (mStats.getLowDischargeAmountSinceCharge() |
| 387 | * mPowerProfile.getBatteryCapacity()) / 100; |
| 388 | mMaxDrainedPower = (mStats.getHighDischargeAmountSinceCharge() |
| 389 | * mPowerProfile.getBatteryCapacity()) / 100; |
| 390 | |
Zoltan Szatmary-Ban | c3b07a0 | 2014-07-01 17:11:07 +0100 | [diff] [blame] | 391 | processAppUsage(asUsers); |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 392 | |
| 393 | // Before aggregating apps in to users, collect all apps to sort by their ms per packet. |
| 394 | for (int i=0; i<mUsageList.size(); i++) { |
| 395 | BatterySipper bs = mUsageList.get(i); |
| 396 | bs.computeMobilemspp(); |
| 397 | if (bs.mobilemspp != 0) { |
| 398 | mMobilemsppList.add(bs); |
| 399 | } |
| 400 | } |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 401 | |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 402 | for (int i=0; i<mUserSippers.size(); i++) { |
| 403 | List<BatterySipper> user = mUserSippers.valueAt(i); |
| 404 | for (int j=0; j<user.size(); j++) { |
| 405 | BatterySipper bs = user.get(j); |
| 406 | bs.computeMobilemspp(); |
| 407 | if (bs.mobilemspp != 0) { |
| 408 | mMobilemsppList.add(bs); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | Collections.sort(mMobilemsppList, new Comparator<BatterySipper>() { |
| 413 | @Override |
| 414 | public int compare(BatterySipper lhs, BatterySipper rhs) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 415 | return Double.compare(rhs.mobilemspp, lhs.mobilemspp); |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 416 | } |
| 417 | }); |
| 418 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 419 | processMiscUsage(); |
| 420 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 421 | Collections.sort(mUsageList); |
| 422 | |
| 423 | // At this point, we've sorted the list so we are guaranteed the max values are at the top. |
| 424 | // We have only added real powers so far. |
| 425 | if (!mUsageList.isEmpty()) { |
| 426 | mMaxRealPower = mMaxPower = mUsageList.get(0).totalPowerMah; |
| 427 | final int usageListCount = mUsageList.size(); |
| 428 | for (int i = 0; i < usageListCount; i++) { |
| 429 | mComputedPower += mUsageList.get(i).totalPowerMah; |
| 430 | } |
| 431 | } |
| 432 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 433 | if (DEBUG) { |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 434 | Log.d(TAG, "Accuracy: total computed=" + makemAh(mComputedPower) + ", min discharge=" |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 435 | + makemAh(mMinDrainedPower) + ", max discharge=" + makemAh(mMaxDrainedPower)); |
| 436 | } |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 437 | |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 438 | mTotalPower = mComputedPower; |
| 439 | if (mStats.getLowDischargeAmountSinceCharge() > 1) { |
| 440 | if (mMinDrainedPower > mComputedPower) { |
| 441 | double amount = mMinDrainedPower - mComputedPower; |
| 442 | mTotalPower = mMinDrainedPower; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 443 | BatterySipper bs = new BatterySipper(DrainType.UNACCOUNTED, null, amount); |
| 444 | |
| 445 | // Insert the BatterySipper in its sorted position. |
| 446 | int index = Collections.binarySearch(mUsageList, bs); |
| 447 | if (index < 0) { |
| 448 | index = -(index + 1); |
| 449 | } |
| 450 | mUsageList.add(index, bs); |
| 451 | mMaxPower = Math.max(mMaxPower, amount); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 452 | } else if (mMaxDrainedPower < mComputedPower) { |
| 453 | double amount = mComputedPower - mMaxDrainedPower; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 454 | |
| 455 | // Insert the BatterySipper in its sorted position. |
| 456 | BatterySipper bs = new BatterySipper(DrainType.OVERCOUNTED, null, amount); |
| 457 | int index = Collections.binarySearch(mUsageList, bs); |
| 458 | if (index < 0) { |
| 459 | index = -(index + 1); |
| 460 | } |
| 461 | mUsageList.add(index, bs); |
| 462 | mMaxPower = Math.max(mMaxPower, amount); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 463 | } |
| 464 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Zoltan Szatmary-Ban | c3b07a0 | 2014-07-01 17:11:07 +0100 | [diff] [blame] | 467 | private void processAppUsage(SparseArray<UserHandle> asUsers) { |
| 468 | final boolean forAllUsers = (asUsers.get(UserHandle.USER_ALL) != null); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 469 | mStatsPeriod = mTypeBatteryRealtime; |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 470 | |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 471 | final SparseArray<? extends Uid> uidStats = mStats.getUidStats(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 472 | final int NU = uidStats.size(); |
| 473 | for (int iu = 0; iu < NU; iu++) { |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 474 | final Uid u = uidStats.valueAt(iu); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 475 | final BatterySipper app = new BatterySipper(BatterySipper.DrainType.APP, u, 0); |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 476 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 477 | mCpuPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
| 478 | mWakelockPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
| 479 | mMobileRadioPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
| 480 | mWifiPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
| 481 | mBluetoothPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
| 482 | mSensorPowerCalculator.calculateApp(app, u, mRawRealtime, mRawUptime, mStatsType); |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 483 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 484 | final double totalPower = app.sumPower(); |
| 485 | if (DEBUG && totalPower != 0) { |
| 486 | Log.d(TAG, String.format("UID %d: total power=%s", u.getUid(), |
| 487 | makemAh(totalPower))); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 490 | // Add the app to the list if it is consuming power. |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 491 | if (totalPower != 0 || u.getUid() == 0) { |
| 492 | // |
| 493 | // Add the app to the app list, WiFi, Bluetooth, etc, or into "Other Users" list. |
| 494 | // |
| 495 | final int uid = app.getUid(); |
| 496 | final int userId = UserHandle.getUserId(uid); |
| 497 | if (uid == Process.WIFI_UID) { |
| 498 | mWifiSippers.add(app); |
| 499 | } else if (uid == Process.BLUETOOTH_UID) { |
| 500 | mBluetoothSippers.add(app); |
| 501 | } else if (!forAllUsers && asUsers.get(userId) == null |
| 502 | && UserHandle.getAppId(uid) >= Process.FIRST_APPLICATION_UID) { |
| 503 | // We are told to just report this user's apps as one large entry. |
| 504 | List<BatterySipper> list = mUserSippers.get(userId); |
| 505 | if (list == null) { |
| 506 | list = new ArrayList<>(); |
| 507 | mUserSippers.put(userId, list); |
| 508 | } |
| 509 | list.add(app); |
Bart Sears | e9b9b73 | 2015-04-07 06:14:04 +0000 | [diff] [blame] | 510 | } else { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 511 | mUsageList.add(app); |
Bart Sears | e9b9b73 | 2015-04-07 06:14:04 +0000 | [diff] [blame] | 512 | } |
Bart Sears | e9b9b73 | 2015-04-07 06:14:04 +0000 | [diff] [blame] | 513 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 514 | if (uid == 0) { |
| 515 | // The device has probably been awake for longer than the screen on |
| 516 | // time and application wake lock time would account for. Assign |
| 517 | // this remainder to the OS, if possible. |
| 518 | mWakelockPowerCalculator.calculateRemaining(app, mStats, mRawRealtime, |
| 519 | mRawUptime, mStatsType); |
| 520 | app.sumPower(); |
| 521 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | private void addPhoneUsage() { |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 527 | long phoneOnTimeMs = mStats.getPhoneOnTime(mRawRealtime, mStatsType) / 1000; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 528 | double phoneOnPower = mPowerProfile.getAveragePower(PowerProfile.POWER_RADIO_ACTIVE) |
| 529 | * phoneOnTimeMs / (60*60*1000); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 530 | if (phoneOnPower != 0) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 531 | addEntry(BatterySipper.DrainType.PHONE, phoneOnTimeMs, phoneOnPower); |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 532 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | private void addScreenUsage() { |
| 536 | double power = 0; |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 537 | long screenOnTimeMs = mStats.getScreenOnTime(mRawRealtime, mStatsType) / 1000; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 538 | power += screenOnTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_SCREEN_ON); |
| 539 | final double screenFullPower = |
| 540 | mPowerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL); |
| 541 | for (int i = 0; i < BatteryStats.NUM_SCREEN_BRIGHTNESS_BINS; i++) { |
| 542 | double screenBinPower = screenFullPower * (i + 0.5f) |
| 543 | / BatteryStats.NUM_SCREEN_BRIGHTNESS_BINS; |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 544 | long brightnessTime = mStats.getScreenBrightnessTime(i, mRawRealtime, mStatsType) |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 545 | / 1000; |
| 546 | double p = screenBinPower*brightnessTime; |
| 547 | if (DEBUG && p != 0) { |
| 548 | Log.d(TAG, "Screen bin #" + i + ": time=" + brightnessTime |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 549 | + " power=" + makemAh(p / (60 * 60 * 1000))); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 550 | } |
| 551 | power += p; |
| 552 | } |
| 553 | power /= (60*60*1000); // To hours |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 554 | if (power != 0) { |
| 555 | addEntry(BatterySipper.DrainType.SCREEN, screenOnTimeMs, power); |
| 556 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | private void addRadioUsage() { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 560 | BatterySipper radio = new BatterySipper(BatterySipper.DrainType.CELL, null, 0); |
| 561 | mMobileRadioPowerCalculator.calculateRemaining(radio, mStats, mRawRealtime, mRawUptime, |
| 562 | mStatsType); |
| 563 | radio.sumPower(); |
| 564 | if (radio.totalPowerMah > 0) { |
| 565 | mUsageList.add(radio); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | private void aggregateSippers(BatterySipper bs, List<BatterySipper> from, String tag) { |
| 570 | for (int i=0; i<from.size(); i++) { |
| 571 | BatterySipper wbs = from.get(i); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 572 | if (DEBUG) Log.d(TAG, tag + " adding sipper " + wbs + ": cpu=" + wbs.cpuTimeMs); |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 573 | bs.add(wbs); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 574 | } |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 575 | bs.computeMobilemspp(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 578 | private void addIdleUsage() { |
| 579 | long idleTimeMs = (mTypeBatteryRealtime |
Dianne Hackborn | 97ae538 | 2014-03-05 16:43:25 -0800 | [diff] [blame] | 580 | - mStats.getScreenOnTime(mRawRealtime, mStatsType)) / 1000; |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 581 | double idlePower = (idleTimeMs * mPowerProfile.getAveragePower(PowerProfile.POWER_CPU_IDLE)) |
| 582 | / (60*60*1000); |
| 583 | if (DEBUG && idlePower != 0) { |
| 584 | Log.d(TAG, "Idle: time=" + idleTimeMs + " power=" + makemAh(idlePower)); |
| 585 | } |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 586 | if (idlePower != 0) { |
| 587 | addEntry(BatterySipper.DrainType.IDLE, idleTimeMs, idlePower); |
| 588 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 591 | /** |
| 592 | * We do per-app blaming of WiFi activity. If energy info is reported from the controller, |
| 593 | * then only the WiFi process gets blamed here since we normalize power calculations and |
| 594 | * assign all the power drain to apps. If energy info is not reported, we attribute the |
| 595 | * difference between total running time of WiFi for all apps and the actual running time |
| 596 | * of WiFi to the WiFi subsystem. |
| 597 | */ |
| 598 | private void addWiFiUsage() { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 599 | BatterySipper bs = new BatterySipper(DrainType.WIFI, null, 0); |
| 600 | mWifiPowerCalculator.calculateRemaining(bs, mStats, mRawRealtime, mRawUptime, mStatsType); |
| 601 | bs.sumPower(); |
| 602 | if (bs.totalPowerMah > 0 || !mWifiSippers.isEmpty()) { |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 603 | aggregateSippers(bs, mWifiSippers, "WIFI"); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 604 | mUsageList.add(bs); |
Adam Lesinski | 33dac55 | 2015-03-09 15:24:48 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Bluetooth usage is not attributed to any apps yet, so the entire blame goes to the |
| 610 | * Bluetooth Category. |
| 611 | */ |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 612 | private void addBluetoothUsage() { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 613 | BatterySipper bs = new BatterySipper(BatterySipper.DrainType.BLUETOOTH, null, 0); |
| 614 | mBluetoothPowerCalculator.calculateRemaining(bs, mStats, mRawRealtime, mRawUptime, |
| 615 | mStatsType); |
| 616 | if (bs.sumPower() > 0) { |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 617 | aggregateSippers(bs, mBluetoothSippers, "Bluetooth"); |
| 618 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 621 | private void addFlashlightUsage() { |
| 622 | long flashlightOnTimeMs = mStats.getFlashlightOnTime(mRawRealtime, mStatsType) / 1000; |
| 623 | double flashlightPower = flashlightOnTimeMs |
| 624 | * mPowerProfile.getAveragePower(PowerProfile.POWER_FLASHLIGHT) / (60*60*1000); |
| 625 | if (flashlightPower != 0) { |
| 626 | addEntry(BatterySipper.DrainType.FLASHLIGHT, flashlightOnTimeMs, flashlightPower); |
| 627 | } |
| 628 | } |
| 629 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 630 | private void addUserUsage() { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 631 | for (int i = 0; i < mUserSippers.size(); i++) { |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 632 | final int userId = mUserSippers.keyAt(i); |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 633 | BatterySipper bs = new BatterySipper(DrainType.USER, null, 0); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 634 | bs.userId = userId; |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 635 | aggregateSippers(bs, mUserSippers.valueAt(i), "User"); |
| 636 | bs.sumPower(); |
| 637 | mUsageList.add(bs); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 641 | private void processMiscUsage() { |
| 642 | addUserUsage(); |
| 643 | addPhoneUsage(); |
| 644 | addScreenUsage(); |
Dianne Hackborn | abc7c49 | 2014-06-30 16:57:46 -0700 | [diff] [blame] | 645 | addFlashlightUsage(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 646 | addWiFiUsage(); |
| 647 | addBluetoothUsage(); |
| 648 | addIdleUsage(); // Not including cellular idle power |
| 649 | // Don't compute radio usage if it's a wifi-only device |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 650 | if (!mWifiOnly) { |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 651 | addRadioUsage(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | private BatterySipper addEntry(DrainType drainType, long time, double power) { |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 656 | BatterySipper bs = new BatterySipper(drainType, null, 0); |
| 657 | bs.usagePowerMah = power; |
| 658 | bs.usageTimeMs = time; |
| 659 | bs.sumPower(); |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 660 | mUsageList.add(bs); |
| 661 | return bs; |
| 662 | } |
| 663 | |
| 664 | public List<BatterySipper> getUsageList() { |
| 665 | return mUsageList; |
| 666 | } |
| 667 | |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 668 | public List<BatterySipper> getMobilemsppList() { |
| 669 | return mMobilemsppList; |
| 670 | } |
| 671 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 672 | public long getStatsPeriod() { return mStatsPeriod; } |
| 673 | |
Adam Lesinski | e08af19 | 2015-03-25 16:42:59 -0700 | [diff] [blame] | 674 | public int getStatsType() { return mStatsType; } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 675 | |
| 676 | public double getMaxPower() { return mMaxPower; } |
| 677 | |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 678 | public double getMaxRealPower() { return mMaxRealPower; } |
| 679 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 680 | public double getTotalPower() { return mTotalPower; } |
| 681 | |
Dianne Hackborn | 099bc62 | 2014-01-22 13:39:16 -0800 | [diff] [blame] | 682 | public double getComputedPower() { return mComputedPower; } |
| 683 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 684 | public double getMinDrainedPower() { |
| 685 | return mMinDrainedPower; |
| 686 | } |
| 687 | |
| 688 | public double getMaxDrainedPower() { |
| 689 | return mMaxDrainedPower; |
| 690 | } |
| 691 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 692 | public long getBatteryTimeRemaining() { return mBatteryTimeRemaining; } |
| 693 | |
| 694 | public long getChargeTimeRemaining() { return mChargeTimeRemaining; } |
| 695 | |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 696 | public static byte[] readFully(FileInputStream stream) throws java.io.IOException { |
| 697 | return readFully(stream, stream.available()); |
| 698 | } |
| 699 | |
| 700 | public static byte[] readFully(FileInputStream stream, int avail) throws java.io.IOException { |
| 701 | int pos = 0; |
| 702 | byte[] data = new byte[avail]; |
| 703 | while (true) { |
| 704 | int amt = stream.read(data, pos, data.length-pos); |
| 705 | //Log.i("foo", "Read " + amt + " bytes at " + pos |
| 706 | // + " of avail " + data.length); |
| 707 | if (amt <= 0) { |
| 708 | //Log.i("foo", "**** FINISHED READING: pos=" + pos |
| 709 | // + " len=" + data.length); |
| 710 | return data; |
| 711 | } |
| 712 | pos += amt; |
| 713 | avail = stream.available(); |
| 714 | if (avail > data.length-pos) { |
| 715 | byte[] newData = new byte[pos+avail]; |
| 716 | System.arraycopy(data, 0, newData, 0, pos); |
| 717 | data = newData; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 722 | private void load() { |
| 723 | if (mBatteryInfo == null) { |
| 724 | return; |
| 725 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 726 | mStats = getStats(mBatteryInfo); |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 727 | if (mCollectBatteryBroadcast) { |
| 728 | mBatteryBroadcast = mContext.registerReceiver(null, |
| 729 | new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); |
| 730 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 731 | } |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 732 | |
| 733 | private static BatteryStatsImpl getStats(IBatteryStats service) { |
| 734 | try { |
| 735 | ParcelFileDescriptor pfd = service.getStatisticsStream(); |
| 736 | if (pfd != null) { |
| 737 | FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd); |
| 738 | try { |
| 739 | byte[] data = readFully(fis, MemoryFile.getSize(pfd.getFileDescriptor())); |
| 740 | Parcel parcel = Parcel.obtain(); |
| 741 | parcel.unmarshall(data, 0, data.length); |
| 742 | parcel.setDataPosition(0); |
| 743 | BatteryStatsImpl stats = com.android.internal.os.BatteryStatsImpl.CREATOR |
| 744 | .createFromParcel(parcel); |
| 745 | stats.distributeWorkLocked(BatteryStats.STATS_SINCE_CHARGED); |
| 746 | return stats; |
| 747 | } catch (IOException e) { |
| 748 | Log.w(TAG, "Unable to read statistics stream", e); |
| 749 | } |
| 750 | } |
| 751 | } catch (RemoteException e) { |
| 752 | Log.w(TAG, "RemoteException:", e); |
| 753 | } |
Dianne Hackborn | d7c9289 | 2014-08-27 16:44:24 -0700 | [diff] [blame] | 754 | return new BatteryStatsImpl(); |
Dianne Hackborn | 0068d3dc | 2014-08-06 19:20:25 -0700 | [diff] [blame] | 755 | } |
Dianne Hackborn | a7c837f | 2014-01-15 16:20:44 -0800 | [diff] [blame] | 756 | } |