The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 com.android.internal.app.IBatteryStats; |
| 20 | import com.android.server.am.BatteryStatsService; |
| 21 | |
| 22 | import android.app.ActivityManagerNative; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.os.BatteryManager; |
| 28 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.IBinder; |
| 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
| 32 | import android.os.SystemClock; |
| 33 | import android.os.UEventObserver; |
| 34 | import android.provider.Checkin; |
| 35 | import android.provider.Settings; |
| 36 | import android.util.EventLog; |
| 37 | import android.util.Log; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.io.FileDescriptor; |
| 41 | import java.io.FileInputStream; |
| 42 | import java.io.FileOutputStream; |
| 43 | import java.io.IOException; |
| 44 | import java.io.PrintWriter; |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * <p>BatteryService monitors the charging status, and charge level of the device |
| 49 | * battery. When these values change this service broadcasts the new values |
| 50 | * to all {@link android.content.BroadcastReceiver IntentReceivers} that are |
| 51 | * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED |
| 52 | * BATTERY_CHANGED} action.</p> |
| 53 | * <p>The new values are stored in the Intent data and can be retrieved by |
| 54 | * calling {@link android.content.Intent#getExtra Intent.getExtra} with the |
| 55 | * following keys:</p> |
| 56 | * <p>"scale" - int, the maximum value for the charge level</p> |
| 57 | * <p>"level" - int, charge level, from 0 through "scale" inclusive</p> |
| 58 | * <p>"status" - String, the current charging status.<br /> |
| 59 | * <p>"health" - String, the current battery health.<br /> |
| 60 | * <p>"present" - boolean, true if the battery is present<br /> |
| 61 | * <p>"icon-small" - int, suggested small icon to use for this state</p> |
| 62 | * <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged |
| 63 | * into an AC power adapter; 2 if plugged in via USB.</p> |
| 64 | * <p>"voltage" - int, current battery voltage in millivolts</p> |
| 65 | * <p>"temperature" - int, current battery temperature in tenths of |
| 66 | * a degree Centigrade</p> |
| 67 | * <p>"technology" - String, the type of battery installed, e.g. "Li-ion"</p> |
| 68 | */ |
| 69 | class BatteryService extends Binder { |
| 70 | private static final String TAG = BatteryService.class.getSimpleName(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | private static final boolean LOCAL_LOGV = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | static final int BATTERY_SCALE = 100; // battery capacity is a percentage |
| 75 | |
| 76 | // Used locally for determining when to make a last ditch effort to log |
| 77 | // discharge stats before the device dies. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 78 | private static final int CRITICAL_BATTERY_LEVEL = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | private static final int DUMP_MAX_LENGTH = 24 * 1024; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 81 | private static final String[] DUMPSYS_ARGS = new String[] { "--checkin", "-u" }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo"; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static final String DUMPSYS_DATA_PATH = "/data/system/"; |
| 85 | |
| 86 | // This should probably be exposed in the API, though it's not critical |
| 87 | private static final int BATTERY_PLUGGED_NONE = 0; |
| 88 | |
| 89 | private final Context mContext; |
| 90 | private final IBatteryStats mBatteryStats; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 91 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | private boolean mAcOnline; |
| 93 | private boolean mUsbOnline; |
| 94 | private int mBatteryStatus; |
| 95 | private int mBatteryHealth; |
| 96 | private boolean mBatteryPresent; |
| 97 | private int mBatteryLevel; |
| 98 | private int mBatteryVoltage; |
| 99 | private int mBatteryTemperature; |
| 100 | private String mBatteryTechnology; |
| 101 | private boolean mBatteryLevelCritical; |
| 102 | |
| 103 | private int mLastBatteryStatus; |
| 104 | private int mLastBatteryHealth; |
| 105 | private boolean mLastBatteryPresent; |
| 106 | private int mLastBatteryLevel; |
| 107 | private int mLastBatteryVoltage; |
| 108 | private int mLastBatteryTemperature; |
| 109 | private boolean mLastBatteryLevelCritical; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 110 | |
| 111 | private int mLowBatteryWarningLevel; |
| 112 | private int mLowBatteryCloseWarningLevel; |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | private int mPlugType; |
| 115 | private int mLastPlugType = -1; // Extra state so we can detect first run |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private long mDischargeStartTime; |
| 118 | private int mDischargeStartLevel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 119 | |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 120 | private boolean mSentLowBatteryBroadcast = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | public BatteryService(Context context) { |
| 123 | mContext = context; |
| 124 | mBatteryStats = BatteryStatsService.getService(); |
| 125 | |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 126 | mLowBatteryWarningLevel = mContext.getResources().getInteger( |
| 127 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 128 | mLowBatteryCloseWarningLevel = mContext.getResources().getInteger( |
| 129 | com.android.internal.R.integer.config_lowBatteryCloseWarningLevel); |
| 130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | mUEventObserver.startObserving("SUBSYSTEM=power_supply"); |
| 132 | |
| 133 | // set initial status |
| 134 | update(); |
| 135 | } |
| 136 | |
| 137 | final boolean isPowered() { |
| 138 | // assume we are powered if battery state is unknown so the "stay on while plugged in" option will work. |
| 139 | return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 140 | } |
| 141 | |
| 142 | final boolean isPowered(int plugTypeSet) { |
| 143 | // assume we are powered if battery state is unknown so |
| 144 | // the "stay on while plugged in" option will work. |
| 145 | if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
| 146 | return true; |
| 147 | } |
| 148 | if (plugTypeSet == 0) { |
| 149 | return false; |
| 150 | } |
| 151 | int plugTypeBit = 0; |
| 152 | if (mAcOnline) { |
| 153 | plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC; |
| 154 | } |
| 155 | if (mUsbOnline) { |
| 156 | plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB; |
| 157 | } |
| 158 | return (plugTypeSet & plugTypeBit) != 0; |
| 159 | } |
| 160 | |
| 161 | final int getPlugType() { |
| 162 | return mPlugType; |
| 163 | } |
| 164 | |
| 165 | private UEventObserver mUEventObserver = new UEventObserver() { |
| 166 | @Override |
| 167 | public void onUEvent(UEventObserver.UEvent event) { |
| 168 | update(); |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | // returns battery level as a percentage |
| 173 | final int getBatteryLevel() { |
| 174 | return mBatteryLevel; |
| 175 | } |
| 176 | |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 177 | void systemReady() { |
| 178 | // check our power situation now that it is safe to display the shutdown dialog. |
| 179 | shutdownIfNoPower(); |
| 180 | } |
| 181 | |
| 182 | private final void shutdownIfNoPower() { |
| 183 | // shut down gracefully if our battery is critically low and we are not powered. |
| 184 | // wait until the system has booted before attempting to display the shutdown dialog. |
| 185 | if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) { |
| 186 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 187 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
| 188 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 189 | mContext.startActivity(intent); |
| 190 | } |
| 191 | } |
| 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | private native void native_update(); |
| 194 | |
| 195 | private synchronized final void update() { |
| 196 | native_update(); |
| 197 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 198 | boolean logOutlier = false; |
| 199 | long dischargeDuration = 0; |
Joe Onorato | a7e4cf9b | 2009-07-28 18:18:20 -0700 | [diff] [blame] | 200 | |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 201 | shutdownIfNoPower(); |
| 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | mBatteryLevelCritical = mBatteryLevel <= CRITICAL_BATTERY_LEVEL; |
| 204 | if (mAcOnline) { |
| 205 | mPlugType = BatteryManager.BATTERY_PLUGGED_AC; |
| 206 | } else if (mUsbOnline) { |
| 207 | mPlugType = BatteryManager.BATTERY_PLUGGED_USB; |
| 208 | } else { |
| 209 | mPlugType = BATTERY_PLUGGED_NONE; |
| 210 | } |
| 211 | if (mBatteryStatus != mLastBatteryStatus || |
| 212 | mBatteryHealth != mLastBatteryHealth || |
| 213 | mBatteryPresent != mLastBatteryPresent || |
| 214 | mBatteryLevel != mLastBatteryLevel || |
| 215 | mPlugType != mLastPlugType || |
| 216 | mBatteryVoltage != mLastBatteryVoltage || |
| 217 | mBatteryTemperature != mLastBatteryTemperature) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | if (mPlugType != mLastPlugType) { |
| 220 | if (mLastPlugType == BATTERY_PLUGGED_NONE) { |
| 221 | // discharging -> charging |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // There's no value in this data unless we've discharged at least once and the |
| 224 | // battery level has changed; so don't log until it does. |
| 225 | if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 226 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 227 | logOutlier = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 228 | EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | mDischargeStartLevel, mBatteryLevel); |
| 230 | // make sure we see a discharge event before logging again |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 231 | mDischargeStartTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | } else if (mPlugType == BATTERY_PLUGGED_NONE) { |
| 234 | // charging -> discharging or we just powered up |
| 235 | mDischargeStartTime = SystemClock.elapsedRealtime(); |
| 236 | mDischargeStartLevel = mBatteryLevel; |
| 237 | } |
| 238 | } |
| 239 | if (mBatteryStatus != mLastBatteryStatus || |
| 240 | mBatteryHealth != mLastBatteryHealth || |
| 241 | mBatteryPresent != mLastBatteryPresent || |
| 242 | mPlugType != mLastPlugType) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 243 | EventLog.writeEvent(EventLogTags.BATTERY_STATUS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0, |
| 245 | mPlugType, mBatteryTechnology); |
| 246 | } |
| 247 | if (mBatteryLevel != mLastBatteryLevel || |
| 248 | mBatteryVoltage != mLastBatteryVoltage || |
| 249 | mBatteryTemperature != mLastBatteryTemperature) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 250 | EventLog.writeEvent(EventLogTags.BATTERY_LEVEL, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | mBatteryLevel, mBatteryVoltage, mBatteryTemperature); |
| 252 | } |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 253 | if (mBatteryLevel != mLastBatteryLevel && mPlugType == BATTERY_PLUGGED_NONE) { |
| 254 | // If the battery level has changed and we are on battery, update the current level. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 255 | // This is used for discharge cycle tracking so this shouldn't be updated while the |
Evan Millar | 633a174 | 2009-04-02 16:36:33 -0700 | [diff] [blame] | 256 | // battery is charging. |
| 257 | try { |
| 258 | mBatteryStats.recordCurrentLevel(mBatteryLevel); |
| 259 | } catch (RemoteException e) { |
| 260 | // Should never happen. |
| 261 | } |
| 262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | if (mBatteryLevelCritical && !mLastBatteryLevelCritical && |
| 264 | mPlugType == BATTERY_PLUGGED_NONE) { |
| 265 | // We want to make sure we log discharge cycle outliers |
| 266 | // if the battery is about to die. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 267 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 268 | logOutlier = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 270 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 271 | final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE; |
| 272 | final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE; |
| 273 | |
| 274 | /* The ACTION_BATTERY_LOW broadcast is sent in these situations: |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 275 | * - is just un-plugged (previously was plugged) and battery level is |
| 276 | * less than or equal to WARNING, or |
| 277 | * - is not plugged and battery level falls to WARNING boundary |
| 278 | * (becomes <= mLowBatteryWarningLevel). |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 279 | */ |
| 280 | final boolean sendBatteryLow = !plugged |
| 281 | && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 282 | && mBatteryLevel <= mLowBatteryWarningLevel |
| 283 | && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 284 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 285 | sendIntent(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 286 | |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 287 | // Separate broadcast is sent for power connected / not connected |
| 288 | // since the standard intent will not wake any applications and some |
| 289 | // applications may want to have smart behavior based on this. |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 290 | Intent statusIntent = new Intent(); |
| 291 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 292 | if (mPlugType != 0 && mLastPlugType == 0) { |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 293 | statusIntent.setAction(Intent.ACTION_POWER_CONNECTED); |
| 294 | mContext.sendBroadcast(statusIntent); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 295 | } |
| 296 | else if (mPlugType == 0 && mLastPlugType != 0) { |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 297 | statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED); |
| 298 | mContext.sendBroadcast(statusIntent); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 299 | } |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 300 | |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 301 | if (sendBatteryLow) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 302 | mSentLowBatteryBroadcast = true; |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 303 | statusIntent.setAction(Intent.ACTION_BATTERY_LOW); |
| 304 | mContext.sendBroadcast(statusIntent); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 305 | } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 306 | mSentLowBatteryBroadcast = false; |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 307 | statusIntent.setAction(Intent.ACTION_BATTERY_OKAY); |
| 308 | mContext.sendBroadcast(statusIntent); |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 309 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 310 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 311 | // This needs to be done after sendIntent() so that we get the lastest battery stats. |
| 312 | if (logOutlier && dischargeDuration != 0) { |
| 313 | logOutlier(dischargeDuration); |
| 314 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 315 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 316 | mLastBatteryStatus = mBatteryStatus; |
| 317 | mLastBatteryHealth = mBatteryHealth; |
| 318 | mLastBatteryPresent = mBatteryPresent; |
| 319 | mLastBatteryLevel = mBatteryLevel; |
| 320 | mLastPlugType = mPlugType; |
| 321 | mLastBatteryVoltage = mBatteryVoltage; |
| 322 | mLastBatteryTemperature = mBatteryTemperature; |
| 323 | mLastBatteryLevelCritical = mBatteryLevelCritical; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | private final void sendIntent() { |
| 328 | // Pack up the values and broadcast them to everyone |
| 329 | Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 330 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 331 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | try { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 333 | mBatteryStats.setOnBattery(mPlugType == BATTERY_PLUGGED_NONE, mBatteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } catch (RemoteException e) { |
| 335 | // Should never happen. |
| 336 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | int icon = getIcon(mBatteryLevel); |
| 339 | |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 340 | intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus); |
| 341 | intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth); |
| 342 | intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent); |
| 343 | intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel); |
| 344 | intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); |
| 345 | intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); |
| 346 | intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType); |
| 347 | intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage); |
| 348 | intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature); |
| 349 | intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | |
| 351 | if (false) { |
| 352 | Log.d(TAG, "updateBattery level:" + mBatteryLevel + |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 353 | " scale:" + BATTERY_SCALE + " status:" + mBatteryStatus + |
| 354 | " health:" + mBatteryHealth + " present:" + mBatteryPresent + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | " voltage: " + mBatteryVoltage + |
| 356 | " temperature: " + mBatteryTemperature + |
| 357 | " technology: " + mBatteryTechnology + |
| 358 | " AC powered:" + mAcOnline + " USB powered:" + mUsbOnline + |
| 359 | " icon:" + icon ); |
| 360 | } |
| 361 | |
| 362 | ActivityManagerNative.broadcastStickyIntent(intent, null); |
| 363 | } |
| 364 | |
| 365 | private final void logBatteryStats() { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME); |
| 368 | if (batteryInfoService != null) { |
| 369 | byte[] buffer = new byte[DUMP_MAX_LENGTH]; |
| 370 | File dumpFile = null; |
| 371 | FileOutputStream dumpStream = null; |
| 372 | try { |
| 373 | // dump the service to a file |
| 374 | dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump"); |
| 375 | dumpStream = new FileOutputStream(dumpFile); |
| 376 | batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS); |
| 377 | dumpStream.getFD().sync(); |
| 378 | |
| 379 | // read dumped file above into buffer truncated to DUMP_MAX_LENGTH |
| 380 | // and insert into events table. |
| 381 | int length = (int) Math.min(dumpFile.length(), DUMP_MAX_LENGTH); |
| 382 | FileInputStream fileInputStream = new FileInputStream(dumpFile); |
| 383 | int nread = fileInputStream.read(buffer, 0, length); |
| 384 | if (nread > 0) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 385 | Checkin.logEvent(mContext.getContentResolver(), |
| 386 | Checkin.Events.Tag.BATTERY_DISCHARGE_INFO, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | new String(buffer, 0, nread)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 388 | if (LOCAL_LOGV) Log.v(TAG, "dumped " + nread + "b from " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | batteryInfoService + "to log"); |
| 390 | if (LOCAL_LOGV) Log.v(TAG, "actual dump:" + new String(buffer, 0, nread)); |
| 391 | } |
| 392 | } catch (RemoteException e) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 393 | Log.e(TAG, "failed to dump service '" + BATTERY_STATS_SERVICE_NAME + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | "':" + e); |
| 395 | } catch (IOException e) { |
| 396 | Log.e(TAG, "failed to write dumpsys file: " + e); |
| 397 | } finally { |
| 398 | // make sure we clean up |
| 399 | if (dumpStream != null) { |
| 400 | try { |
| 401 | dumpStream.close(); |
| 402 | } catch (IOException e) { |
| 403 | Log.e(TAG, "failed to close dumpsys output stream"); |
| 404 | } |
| 405 | } |
| 406 | if (dumpFile != null && !dumpFile.delete()) { |
| 407 | Log.e(TAG, "failed to delete temporary dumpsys file: " |
| 408 | + dumpFile.getAbsolutePath()); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | private final void logOutlier(long duration) { |
| 415 | ContentResolver cr = mContext.getContentResolver(); |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 416 | String dischargeThresholdString = Settings.Secure.getString(cr, |
| 417 | Settings.Secure.BATTERY_DISCHARGE_THRESHOLD); |
| 418 | String durationThresholdString = Settings.Secure.getString(cr, |
| 419 | Settings.Secure.BATTERY_DISCHARGE_DURATION_THRESHOLD); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | if (dischargeThresholdString != null && durationThresholdString != null) { |
| 422 | try { |
| 423 | long durationThreshold = Long.parseLong(durationThresholdString); |
| 424 | int dischargeThreshold = Integer.parseInt(dischargeThresholdString); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 425 | if (duration <= durationThreshold && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) { |
| 427 | // If the discharge cycle is bad enough we want to know about it. |
| 428 | logBatteryStats(); |
| 429 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 430 | if (LOCAL_LOGV) Log.v(TAG, "duration threshold: " + durationThreshold + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | " discharge threshold: " + dischargeThreshold); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 432 | if (LOCAL_LOGV) Log.v(TAG, "duration: " + duration + " discharge: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | (mDischargeStartLevel - mBatteryLevel)); |
| 434 | } catch (NumberFormatException e) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 435 | Log.e(TAG, "Invalid DischargeThresholds GService string: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | durationThresholdString + " or " + dischargeThresholdString); |
| 437 | return; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | private final int getIcon(int level) { |
| 443 | if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 444 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
| 445 | } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING || |
| 446 | mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING || |
| 447 | mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
| 448 | return com.android.internal.R.drawable.stat_sys_battery; |
| 449 | } else { |
| 450 | return com.android.internal.R.drawable.stat_sys_battery_unknown; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 456 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 457 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | pw.println("Permission Denial: can't dump Battery service from from pid=" |
| 460 | + Binder.getCallingPid() |
| 461 | + ", uid=" + Binder.getCallingUid()); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | synchronized (this) { |
| 466 | pw.println("Current Battery Service state:"); |
| 467 | pw.println(" AC powered: " + mAcOnline); |
| 468 | pw.println(" USB powered: " + mUsbOnline); |
| 469 | pw.println(" status: " + mBatteryStatus); |
| 470 | pw.println(" health: " + mBatteryHealth); |
| 471 | pw.println(" present: " + mBatteryPresent); |
| 472 | pw.println(" level: " + mBatteryLevel); |
| 473 | pw.println(" scale: " + BATTERY_SCALE); |
| 474 | pw.println(" voltage:" + mBatteryVoltage); |
| 475 | pw.println(" temperature: " + mBatteryTemperature); |
| 476 | pw.println(" technology: " + mBatteryTechnology); |
| 477 | } |
| 478 | } |
| 479 | } |