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