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; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 29 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.IBinder; |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 31 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.os.ServiceManager; |
| 34 | import android.os.SystemClock; |
| 35 | import android.os.UEventObserver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.provider.Settings; |
| 38 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 39 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | import java.io.File; |
| 42 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import java.io.FileOutputStream; |
| 44 | import java.io.IOException; |
| 45 | import java.io.PrintWriter; |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * <p>BatteryService monitors the charging status, and charge level of the device |
| 50 | * battery. When these values change this service broadcasts the new values |
| 51 | * to all {@link android.content.BroadcastReceiver IntentReceivers} that are |
| 52 | * watching the {@link android.content.Intent#ACTION_BATTERY_CHANGED |
| 53 | * BATTERY_CHANGED} action.</p> |
| 54 | * <p>The new values are stored in the Intent data and can be retrieved by |
| 55 | * calling {@link android.content.Intent#getExtra Intent.getExtra} with the |
| 56 | * following keys:</p> |
| 57 | * <p>"scale" - int, the maximum value for the charge level</p> |
| 58 | * <p>"level" - int, charge level, from 0 through "scale" inclusive</p> |
| 59 | * <p>"status" - String, the current charging status.<br /> |
| 60 | * <p>"health" - String, the current battery health.<br /> |
| 61 | * <p>"present" - boolean, true if the battery is present<br /> |
| 62 | * <p>"icon-small" - int, suggested small icon to use for this state</p> |
| 63 | * <p>"plugged" - int, 0 if the device is not plugged in; 1 if plugged |
| 64 | * into an AC power adapter; 2 if plugged in via USB.</p> |
| 65 | * <p>"voltage" - int, current battery voltage in millivolts</p> |
| 66 | * <p>"temperature" - int, current battery temperature in tenths of |
| 67 | * a degree Centigrade</p> |
| 68 | * <p>"technology" - String, the type of battery installed, e.g. "Li-ion"</p> |
| 69 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 70 | public final class BatteryService extends Binder { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | private static final String TAG = BatteryService.class.getSimpleName(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 72 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 73 | private static final boolean DEBUG = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 74 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 75 | private static final int BATTERY_SCALE = 100; // battery capacity is a percentage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
| 77 | // Used locally for determining when to make a last ditch effort to log |
| 78 | // discharge stats before the device dies. |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 79 | private int mCriticalBatteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| 81 | private static final int DUMP_MAX_LENGTH = 24 * 1024; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 82 | 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] | 83 | private static final String BATTERY_STATS_SERVICE_NAME = "batteryinfo"; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private static final String DUMPSYS_DATA_PATH = "/data/system/"; |
| 86 | |
| 87 | // This should probably be exposed in the API, though it's not critical |
| 88 | private static final int BATTERY_PLUGGED_NONE = 0; |
| 89 | |
| 90 | private final Context mContext; |
| 91 | private final IBatteryStats mBatteryStats; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 92 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 93 | private final Object mLock = new Object(); |
| 94 | |
| 95 | /* Begin native fields: All of these fields are set by native code. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private boolean mAcOnline; |
| 97 | private boolean mUsbOnline; |
Brian Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 98 | private boolean mWirelessOnline; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | private int mBatteryStatus; |
| 100 | private int mBatteryHealth; |
| 101 | private boolean mBatteryPresent; |
| 102 | private int mBatteryLevel; |
| 103 | private int mBatteryVoltage; |
| 104 | private int mBatteryTemperature; |
| 105 | private String mBatteryTechnology; |
| 106 | private boolean mBatteryLevelCritical; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 107 | /* End native fields. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| 109 | private int mLastBatteryStatus; |
| 110 | private int mLastBatteryHealth; |
| 111 | private boolean mLastBatteryPresent; |
| 112 | private int mLastBatteryLevel; |
| 113 | private int mLastBatteryVoltage; |
| 114 | private int mLastBatteryTemperature; |
| 115 | private boolean mLastBatteryLevelCritical; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 116 | |
| 117 | private int mInvalidCharger; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 118 | private int mLastInvalidCharger; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 119 | |
| 120 | private int mLowBatteryWarningLevel; |
| 121 | private int mLowBatteryCloseWarningLevel; |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 122 | private int mShutdownBatteryTemperature; |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | private int mPlugType; |
| 125 | private int mLastPlugType = -1; // Extra state so we can detect first run |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | private long mDischargeStartTime; |
| 128 | private int mDischargeStartLevel; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 129 | |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 130 | private boolean mUpdatesStopped; |
| 131 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 132 | private Led mLed; |
| 133 | |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 134 | private boolean mSentLowBatteryBroadcast = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 135 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 136 | private native void native_update(); |
| 137 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 138 | public BatteryService(Context context, LightsService lights) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | mContext = context; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 140 | mLed = new Led(context, lights); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | mBatteryStats = BatteryStatsService.getService(); |
| 142 | |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 143 | mCriticalBatteryLevel = mContext.getResources().getInteger( |
| 144 | com.android.internal.R.integer.config_criticalBatteryWarningLevel); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 145 | mLowBatteryWarningLevel = mContext.getResources().getInteger( |
| 146 | com.android.internal.R.integer.config_lowBatteryWarningLevel); |
| 147 | mLowBatteryCloseWarningLevel = mContext.getResources().getInteger( |
| 148 | com.android.internal.R.integer.config_lowBatteryCloseWarningLevel); |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 149 | mShutdownBatteryTemperature = mContext.getResources().getInteger( |
| 150 | com.android.internal.R.integer.config_shutdownBatteryTemperature); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 151 | |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 152 | mPowerSupplyObserver.startObserving("SUBSYSTEM=power_supply"); |
| 153 | |
| 154 | // watch for invalid charger messages if the invalid_charger switch exists |
| 155 | if (new File("/sys/devices/virtual/switch/invalid_charger/state").exists()) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 156 | mInvalidChargerObserver.startObserving( |
| 157 | "DEVPATH=/devices/virtual/switch/invalid_charger"); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
| 160 | // set initial status |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 161 | synchronized (mLock) { |
| 162 | updateLocked(); |
| 163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 166 | void systemReady() { |
| 167 | // check our power situation now that it is safe to display the shutdown dialog. |
| 168 | synchronized (mLock) { |
| 169 | shutdownIfNoPowerLocked(); |
| 170 | shutdownIfOverTempLocked(); |
| 171 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Returns true if the device is plugged into any of the specified plug types. |
| 176 | */ |
| 177 | public boolean isPowered(int plugTypeSet) { |
| 178 | synchronized (mLock) { |
| 179 | return isPoweredLocked(plugTypeSet); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private boolean isPoweredLocked(int plugTypeSet) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | // assume we are powered if battery state is unknown so |
| 185 | // the "stay on while plugged in" option will work. |
| 186 | if (mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN) { |
| 187 | return true; |
| 188 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 189 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_AC) != 0 && mAcOnline) { |
| 190 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 192 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_USB) != 0 && mUsbOnline) { |
| 193 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 195 | if ((plugTypeSet & BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0 && mWirelessOnline) { |
| 196 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 198 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 201 | /** |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 202 | * Returns the current plug type. |
| 203 | */ |
| 204 | public int getPlugType() { |
| 205 | synchronized (mLock) { |
| 206 | return mPlugType; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 211 | * Returns battery level as a percentage. |
| 212 | */ |
| 213 | public int getBatteryLevel() { |
| 214 | synchronized (mLock) { |
| 215 | return mBatteryLevel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Returns true if battery level is below the first warning threshold. |
| 221 | */ |
| 222 | public boolean isBatteryLow() { |
| 223 | synchronized (mLock) { |
| 224 | return mBatteryPresent && mBatteryLevel <= mLowBatteryWarningLevel; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 228 | private void shutdownIfNoPowerLocked() { |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 229 | // shut down gracefully if our battery is critically low and we are not powered. |
| 230 | // wait until the system has booted before attempting to display the shutdown dialog. |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 231 | if (mBatteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY) |
| 232 | && ActivityManagerNative.isSystemReady()) { |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 233 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 234 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
| 235 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 236 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
Mike Lockwood | 07a500f | 2009-08-12 09:56:44 -0400 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 240 | private void shutdownIfOverTempLocked() { |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 241 | // shut down gracefully if temperature is too high (> 68.0C by default) |
| 242 | // wait until the system has booted before attempting to display the |
| 243 | // shutdown dialog. |
| 244 | if (mBatteryTemperature > mShutdownBatteryTemperature |
| 245 | && ActivityManagerNative.isSystemReady()) { |
Eric Olsen | 6a362a9 | 2010-03-26 15:38:41 -0700 | [diff] [blame] | 246 | Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); |
| 247 | intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false); |
| 248 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 249 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
Eric Olsen | 6a362a9 | 2010-03-26 15:38:41 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 253 | private void updateLocked() { |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 254 | if (!mUpdatesStopped) { |
| 255 | // Update the values of mAcOnline, et. all. |
| 256 | native_update(); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 257 | |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 258 | // Process the new values. |
| 259 | processValuesLocked(); |
| 260 | } |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 263 | private void processValuesLocked() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 264 | boolean logOutlier = false; |
| 265 | long dischargeDuration = 0; |
Joe Onorato | a7e4cf9b | 2009-07-28 18:18:20 -0700 | [diff] [blame] | 266 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 267 | mBatteryLevelCritical = (mBatteryLevel <= mCriticalBatteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | if (mAcOnline) { |
| 269 | mPlugType = BatteryManager.BATTERY_PLUGGED_AC; |
| 270 | } else if (mUsbOnline) { |
| 271 | mPlugType = BatteryManager.BATTERY_PLUGGED_USB; |
Brian Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 272 | } else if (mWirelessOnline) { |
| 273 | mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } else { |
| 275 | mPlugType = BATTERY_PLUGGED_NONE; |
| 276 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 277 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 278 | if (DEBUG) { |
| 279 | Slog.d(TAG, "Processing new values: " |
| 280 | + "mAcOnline=" + mAcOnline |
| 281 | + ", mUsbOnline=" + mUsbOnline |
| 282 | + ", mWirelessOnline=" + mWirelessOnline |
| 283 | + ", mBatteryStatus=" + mBatteryStatus |
| 284 | + ", mBatteryHealth=" + mBatteryHealth |
| 285 | + ", mBatteryPresent=" + mBatteryPresent |
| 286 | + ", mBatteryLevel=" + mBatteryLevel |
| 287 | + ", mBatteryTechnology=" + mBatteryTechnology |
| 288 | + ", mBatteryVoltage=" + mBatteryVoltage |
| 289 | + ", mBatteryTemperature=" + mBatteryTemperature |
| 290 | + ", mBatteryLevelCritical=" + mBatteryLevelCritical |
| 291 | + ", mPlugType=" + mPlugType); |
| 292 | } |
| 293 | |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 294 | // Let the battery stats keep track of the current level. |
| 295 | try { |
| 296 | mBatteryStats.setBatteryState(mBatteryStatus, mBatteryHealth, |
| 297 | mPlugType, mBatteryLevel, mBatteryTemperature, |
| 298 | mBatteryVoltage); |
| 299 | } catch (RemoteException e) { |
| 300 | // Should never happen. |
| 301 | } |
Brian Muramatsu | f3c74f3 | 2012-08-31 15:14:48 -0700 | [diff] [blame] | 302 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 303 | shutdownIfNoPowerLocked(); |
| 304 | shutdownIfOverTempLocked(); |
Dianne Hackborn | 6b7b484 | 2010-06-14 17:17:44 -0700 | [diff] [blame] | 305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | if (mBatteryStatus != mLastBatteryStatus || |
| 307 | mBatteryHealth != mLastBatteryHealth || |
| 308 | mBatteryPresent != mLastBatteryPresent || |
| 309 | mBatteryLevel != mLastBatteryLevel || |
| 310 | mPlugType != mLastPlugType || |
| 311 | mBatteryVoltage != mLastBatteryVoltage || |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 312 | mBatteryTemperature != mLastBatteryTemperature || |
| 313 | mInvalidCharger != mLastInvalidCharger) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | if (mPlugType != mLastPlugType) { |
| 316 | if (mLastPlugType == BATTERY_PLUGGED_NONE) { |
| 317 | // discharging -> charging |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | // There's no value in this data unless we've discharged at least once and the |
| 320 | // battery level has changed; so don't log until it does. |
| 321 | if (mDischargeStartTime != 0 && mDischargeStartLevel != mBatteryLevel) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 322 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 323 | logOutlier = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 324 | EventLog.writeEvent(EventLogTags.BATTERY_DISCHARGE, dischargeDuration, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | mDischargeStartLevel, mBatteryLevel); |
| 326 | // make sure we see a discharge event before logging again |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 327 | mDischargeStartTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } |
| 329 | } else if (mPlugType == BATTERY_PLUGGED_NONE) { |
| 330 | // charging -> discharging or we just powered up |
| 331 | mDischargeStartTime = SystemClock.elapsedRealtime(); |
| 332 | mDischargeStartLevel = mBatteryLevel; |
| 333 | } |
| 334 | } |
| 335 | if (mBatteryStatus != mLastBatteryStatus || |
| 336 | mBatteryHealth != mLastBatteryHealth || |
| 337 | mBatteryPresent != mLastBatteryPresent || |
| 338 | mPlugType != mLastPlugType) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 339 | EventLog.writeEvent(EventLogTags.BATTERY_STATUS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | mBatteryStatus, mBatteryHealth, mBatteryPresent ? 1 : 0, |
| 341 | mPlugType, mBatteryTechnology); |
| 342 | } |
| 343 | if (mBatteryLevel != mLastBatteryLevel || |
| 344 | mBatteryVoltage != mLastBatteryVoltage || |
| 345 | mBatteryTemperature != mLastBatteryTemperature) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 346 | EventLog.writeEvent(EventLogTags.BATTERY_LEVEL, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | mBatteryLevel, mBatteryVoltage, mBatteryTemperature); |
| 348 | } |
| 349 | if (mBatteryLevelCritical && !mLastBatteryLevelCritical && |
| 350 | mPlugType == BATTERY_PLUGGED_NONE) { |
| 351 | // We want to make sure we log discharge cycle outliers |
| 352 | // if the battery is about to die. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 353 | dischargeDuration = SystemClock.elapsedRealtime() - mDischargeStartTime; |
| 354 | logOutlier = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 356 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 357 | final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE; |
| 358 | final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE; |
| 359 | |
| 360 | /* The ACTION_BATTERY_LOW broadcast is sent in these situations: |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 361 | * - is just un-plugged (previously was plugged) and battery level is |
| 362 | * less than or equal to WARNING, or |
| 363 | * - is not plugged and battery level falls to WARNING boundary |
| 364 | * (becomes <= mLowBatteryWarningLevel). |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 365 | */ |
| 366 | final boolean sendBatteryLow = !plugged |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 367 | && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN |
| 368 | && mBatteryLevel <= mLowBatteryWarningLevel |
| 369 | && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 370 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 371 | sendIntentLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 372 | |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 373 | // Separate broadcast is sent for power connected / not connected |
| 374 | // since the standard intent will not wake any applications and some |
| 375 | // applications may want to have smart behavior based on this. |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 376 | Intent statusIntent = new Intent(); |
| 377 | statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 378 | if (mPlugType != 0 && mLastPlugType == 0) { |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 379 | statusIntent.setAction(Intent.ACTION_POWER_CONNECTED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 380 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 381 | } |
| 382 | else if (mPlugType == 0 && mLastPlugType != 0) { |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 383 | statusIntent.setAction(Intent.ACTION_POWER_DISCONNECTED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 384 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
Christopher Tate | 06ba554 | 2009-04-09 16:03:56 -0700 | [diff] [blame] | 385 | } |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 386 | |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 387 | if (sendBatteryLow) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 388 | mSentLowBatteryBroadcast = true; |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 389 | statusIntent.setAction(Intent.ACTION_BATTERY_LOW); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 390 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
Mike Lockwood | d81b1f4 | 2009-09-25 09:32:19 -0400 | [diff] [blame] | 391 | } else if (mSentLowBatteryBroadcast && mLastBatteryLevel >= mLowBatteryCloseWarningLevel) { |
Dianne Hackborn | 8ec5b83 | 2009-07-01 21:19:35 -0700 | [diff] [blame] | 392 | mSentLowBatteryBroadcast = false; |
Christopher Tate | 93dc9fe | 2009-07-16 13:25:49 -0700 | [diff] [blame] | 393 | statusIntent.setAction(Intent.ACTION_BATTERY_OKAY); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 394 | mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL); |
Mihai Preda | a82842f | 2009-04-29 15:05:56 +0200 | [diff] [blame] | 395 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 396 | |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 397 | // Update the battery LED |
| 398 | mLed.updateLightsLocked(); |
| 399 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 400 | // This needs to be done after sendIntent() so that we get the lastest battery stats. |
| 401 | if (logOutlier && dischargeDuration != 0) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 402 | logOutlierLocked(dischargeDuration); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 403 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 404 | |
Dianne Hackborn | 99f7eb45 | 2009-09-22 17:27:53 -0700 | [diff] [blame] | 405 | mLastBatteryStatus = mBatteryStatus; |
| 406 | mLastBatteryHealth = mBatteryHealth; |
| 407 | mLastBatteryPresent = mBatteryPresent; |
| 408 | mLastBatteryLevel = mBatteryLevel; |
| 409 | mLastPlugType = mPlugType; |
| 410 | mLastBatteryVoltage = mBatteryVoltage; |
| 411 | mLastBatteryTemperature = mBatteryTemperature; |
| 412 | mLastBatteryLevelCritical = mBatteryLevelCritical; |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 413 | mLastInvalidCharger = mInvalidCharger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 417 | private void sendIntentLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | // Pack up the values and broadcast them to everyone |
| 419 | Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 420 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 421 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 422 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 423 | int icon = getIconLocked(mBatteryLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 425 | intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryStatus); |
| 426 | intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryHealth); |
| 427 | intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryPresent); |
| 428 | intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryLevel); |
| 429 | intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); |
| 430 | intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); |
| 431 | intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType); |
| 432 | intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryVoltage); |
| 433 | intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryTemperature); |
| 434 | intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryTechnology); |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 435 | intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 437 | if (DEBUG) { |
| 438 | Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. level:" + mBatteryLevel + |
| 439 | ", scale:" + BATTERY_SCALE + ", status:" + mBatteryStatus + |
| 440 | ", health:" + mBatteryHealth + ", present:" + mBatteryPresent + |
| 441 | ", voltage: " + mBatteryVoltage + |
| 442 | ", temperature: " + mBatteryTemperature + |
| 443 | ", technology: " + mBatteryTechnology + |
| 444 | ", AC powered:" + mAcOnline + ", USB powered:" + mUsbOnline + |
| 445 | ", Wireless powered:" + mWirelessOnline + |
| 446 | ", icon:" + icon + ", invalid charger:" + mInvalidCharger); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 449 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 452 | private void logBatteryStatsLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 454 | if (batteryInfoService == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 456 | DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 457 | if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return; |
| 458 | |
| 459 | File dumpFile = null; |
| 460 | FileOutputStream dumpStream = null; |
| 461 | try { |
| 462 | // dump the service to a file |
| 463 | dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump"); |
| 464 | dumpStream = new FileOutputStream(dumpFile); |
| 465 | batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS); |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 466 | FileUtils.sync(dumpStream); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 467 | |
| 468 | // add dump file to drop box |
| 469 | db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT); |
| 470 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 471 | Slog.e(TAG, "failed to dump battery service", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 472 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 473 | Slog.e(TAG, "failed to write dumpsys file", e); |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 474 | } finally { |
| 475 | // make sure we clean up |
| 476 | if (dumpStream != null) { |
| 477 | try { |
| 478 | dumpStream.close(); |
| 479 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 480 | Slog.e(TAG, "failed to close dumpsys output stream"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 482 | } |
| 483 | if (dumpFile != null && !dumpFile.delete()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 484 | Slog.e(TAG, "failed to delete temporary dumpsys file: " |
Dan Egnor | 18e9396 | 2010-02-10 19:27:58 -0800 | [diff] [blame] | 485 | + dumpFile.getAbsolutePath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 489 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 490 | private void logOutlierLocked(long duration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | ContentResolver cr = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 492 | String dischargeThresholdString = Settings.Global.getString(cr, |
| 493 | Settings.Global.BATTERY_DISCHARGE_THRESHOLD); |
| 494 | String durationThresholdString = Settings.Global.getString(cr, |
| 495 | Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | if (dischargeThresholdString != null && durationThresholdString != null) { |
| 498 | try { |
| 499 | long durationThreshold = Long.parseLong(durationThresholdString); |
| 500 | int dischargeThreshold = Integer.parseInt(dischargeThresholdString); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 501 | if (duration <= durationThreshold && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | mDischargeStartLevel - mBatteryLevel >= dischargeThreshold) { |
| 503 | // If the discharge cycle is bad enough we want to know about it. |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 504 | logBatteryStatsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 506 | if (DEBUG) Slog.v(TAG, "duration threshold: " + durationThreshold + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | " discharge threshold: " + dischargeThreshold); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 508 | if (DEBUG) Slog.v(TAG, "duration: " + duration + " discharge: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | (mDischargeStartLevel - mBatteryLevel)); |
| 510 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 511 | Slog.e(TAG, "Invalid DischargeThresholds GService string: " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | durationThresholdString + " or " + dischargeThresholdString); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 518 | private int getIconLocked(int level) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 520 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 521 | } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | return com.android.internal.R.drawable.stat_sys_battery; |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 523 | } else if (mBatteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING |
| 524 | || mBatteryStatus == BatteryManager.BATTERY_STATUS_FULL) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 525 | if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY) |
| 526 | && mBatteryLevel >= 100) { |
Joe Onorato | 794be40 | 2010-11-21 19:22:25 -0800 | [diff] [blame] | 527 | return com.android.internal.R.drawable.stat_sys_battery_charge; |
| 528 | } else { |
| 529 | return com.android.internal.R.drawable.stat_sys_battery; |
| 530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } else { |
| 532 | return com.android.internal.R.drawable.stat_sys_battery_unknown; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 538 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 539 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | pw.println("Permission Denial: can't dump Battery service from from pid=" |
| 542 | + Binder.getCallingPid() |
| 543 | + ", uid=" + Binder.getCallingUid()); |
| 544 | return; |
| 545 | } |
| 546 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 547 | synchronized (mLock) { |
| 548 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 549 | pw.println("Current Battery Service state:"); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 550 | if (mUpdatesStopped) { |
| 551 | pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); |
| 552 | } |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 553 | pw.println(" AC powered: " + mAcOnline); |
| 554 | pw.println(" USB powered: " + mUsbOnline); |
Brian Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 555 | pw.println(" Wireless powered: " + mWirelessOnline); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 556 | pw.println(" status: " + mBatteryStatus); |
| 557 | pw.println(" health: " + mBatteryHealth); |
| 558 | pw.println(" present: " + mBatteryPresent); |
| 559 | pw.println(" level: " + mBatteryLevel); |
| 560 | pw.println(" scale: " + BATTERY_SCALE); |
| 561 | pw.println(" voltage:" + mBatteryVoltage); |
| 562 | pw.println(" temperature: " + mBatteryTemperature); |
| 563 | pw.println(" technology: " + mBatteryTechnology); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 564 | } else if (args.length == 3 && "set".equals(args[0])) { |
| 565 | String key = args[1]; |
| 566 | String value = args[2]; |
| 567 | try { |
| 568 | boolean update = true; |
| 569 | if ("ac".equals(key)) { |
| 570 | mAcOnline = Integer.parseInt(value) != 0; |
| 571 | } else if ("usb".equals(key)) { |
| 572 | mUsbOnline = Integer.parseInt(value) != 0; |
| 573 | } else if ("wireless".equals(key)) { |
| 574 | mWirelessOnline = Integer.parseInt(value) != 0; |
| 575 | } else if ("status".equals(key)) { |
| 576 | mBatteryStatus = Integer.parseInt(value); |
| 577 | } else if ("level".equals(key)) { |
| 578 | mBatteryLevel = Integer.parseInt(value); |
| 579 | } else if ("invalid".equals(key)) { |
| 580 | mInvalidCharger = Integer.parseInt(value); |
| 581 | } else { |
| 582 | pw.println("Unknown set option: " + key); |
| 583 | update = false; |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 584 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 585 | if (update) { |
| 586 | mUpdatesStopped = true; |
| 587 | processValuesLocked(); |
| 588 | } |
| 589 | } catch (NumberFormatException ex) { |
| 590 | pw.println("Bad value: " + value); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 591 | } |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 592 | } else if (args.length == 1 && "reset".equals(args[0])) { |
| 593 | mUpdatesStopped = false; |
| 594 | updateLocked(); |
| 595 | } else { |
| 596 | pw.println("Dump current battery state, or:"); |
| 597 | pw.println(" set ac|usb|wireless|status|level|invalid <value>"); |
| 598 | pw.println(" reset"); |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 599 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 602 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 603 | private final UEventObserver mPowerSupplyObserver = new UEventObserver() { |
| 604 | @Override |
| 605 | public void onUEvent(UEventObserver.UEvent event) { |
| 606 | synchronized (mLock) { |
| 607 | updateLocked(); |
| 608 | } |
| 609 | } |
| 610 | }; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 611 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 612 | private final UEventObserver mInvalidChargerObserver = new UEventObserver() { |
| 613 | @Override |
| 614 | public void onUEvent(UEventObserver.UEvent event) { |
| 615 | final int invalidCharger = "1".equals(event.get("SWITCH_STATE")) ? 1 : 0; |
| 616 | synchronized (mLock) { |
| 617 | if (mInvalidCharger != invalidCharger) { |
| 618 | mInvalidCharger = invalidCharger; |
| 619 | updateLocked(); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | }; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 624 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 625 | private final class Led { |
| 626 | private final LightsService.Light mBatteryLight; |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 627 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 628 | private final int mBatteryLowARGB; |
| 629 | private final int mBatteryMediumARGB; |
| 630 | private final int mBatteryFullARGB; |
| 631 | private final int mBatteryLedOn; |
| 632 | private final int mBatteryLedOff; |
| 633 | |
| 634 | public Led(Context context, LightsService lights) { |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 635 | mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY); |
| 636 | |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 637 | mBatteryLowARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 638 | com.android.internal.R.integer.config_notificationsBatteryLowARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 639 | mBatteryMediumARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 640 | com.android.internal.R.integer.config_notificationsBatteryMediumARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 641 | mBatteryFullARGB = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 642 | com.android.internal.R.integer.config_notificationsBatteryFullARGB); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 643 | mBatteryLedOn = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 644 | com.android.internal.R.integer.config_notificationsBatteryLedOn); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 645 | mBatteryLedOff = context.getResources().getInteger( |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 646 | com.android.internal.R.integer.config_notificationsBatteryLedOff); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Synchronize on BatteryService. |
| 651 | */ |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 652 | public void updateLightsLocked() { |
Joe Onorato | de1b359 | 2010-10-25 20:36:47 -0700 | [diff] [blame] | 653 | final int level = mBatteryLevel; |
| 654 | final int status = mBatteryStatus; |
| 655 | if (level < mLowBatteryWarningLevel) { |
| 656 | if (status == BatteryManager.BATTERY_STATUS_CHARGING) { |
| 657 | // Solid red when battery is charging |
| 658 | mBatteryLight.setColor(mBatteryLowARGB); |
| 659 | } else { |
| 660 | // Flash red when battery is low and not charging |
| 661 | mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED, |
| 662 | mBatteryLedOn, mBatteryLedOff); |
| 663 | } |
| 664 | } else if (status == BatteryManager.BATTERY_STATUS_CHARGING |
| 665 | || status == BatteryManager.BATTERY_STATUS_FULL) { |
| 666 | if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) { |
| 667 | // Solid green when full or charging and nearly full |
| 668 | mBatteryLight.setColor(mBatteryFullARGB); |
| 669 | } else { |
| 670 | // Solid orange when charging and halfway full |
| 671 | mBatteryLight.setColor(mBatteryMediumARGB); |
| 672 | } |
| 673 | } else { |
| 674 | // No lights if not charging and not low |
| 675 | mBatteryLight.turnOff(); |
| 676 | } |
| 677 | } |
| 678 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | } |