The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import com.android.internal.app.IBatteryStats; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 20 | import com.android.internal.app.ShutdownThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.server.am.BatteryStatsService; |
| 22 | |
| 23 | import android.app.ActivityManagerNative; |
| 24 | import android.app.IActivityManager; |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ContentQueryMap; |
| 27 | import android.content.ContentResolver; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 28 | import android.content.ContentValues; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.pm.PackageManager; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 34 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 36 | import android.hardware.Sensor; |
| 37 | import android.hardware.SensorEvent; |
| 38 | import android.hardware.SensorEventListener; |
| 39 | import android.hardware.SensorManager; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 40 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.BatteryStats; |
| 42 | import android.os.Binder; |
| 43 | import android.os.Handler; |
| 44 | import android.os.HandlerThread; |
| 45 | import android.os.IBinder; |
| 46 | import android.os.IPowerManager; |
| 47 | import android.os.LocalPowerManager; |
| 48 | import android.os.Power; |
| 49 | import android.os.PowerManager; |
| 50 | import android.os.Process; |
| 51 | import android.os.RemoteException; |
| 52 | import android.os.SystemClock; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 53 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.provider.Settings.SettingNotFoundException; |
| 55 | import android.provider.Settings; |
| 56 | import android.util.EventLog; |
| 57 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 58 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.WindowManagerPolicy; |
| 60 | import static android.provider.Settings.System.DIM_SCREEN; |
| 61 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS; |
Dan Murphy | 951764b | 2009-08-27 14:59:03 -0500 | [diff] [blame] | 62 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 63 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
| 65 | import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 66 | import static android.provider.Settings.System.WINDOW_ANIMATION_SCALE; |
| 67 | import static android.provider.Settings.System.TRANSITION_ANIMATION_SCALE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | import java.io.FileDescriptor; |
| 70 | import java.io.PrintWriter; |
| 71 | import java.util.ArrayList; |
| 72 | import java.util.HashMap; |
| 73 | import java.util.Observable; |
| 74 | import java.util.Observer; |
| 75 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 76 | public class PowerManagerService extends IPowerManager.Stub |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 77 | implements LocalPowerManager, Watchdog.Monitor { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | private static final String TAG = "PowerManagerService"; |
| 80 | static final String PARTIAL_NAME = "PowerManagerService"; |
| 81 | |
| 82 | private static final boolean LOG_PARTIAL_WL = false; |
| 83 | |
| 84 | // Indicates whether touch-down cycles should be logged as part of the |
| 85 | // LOG_POWER_SCREEN_STATE log events |
| 86 | private static final boolean LOG_TOUCH_DOWNS = true; |
| 87 | |
| 88 | private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK |
| 89 | | PowerManager.SCREEN_DIM_WAKE_LOCK |
| 90 | | PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 91 | | PowerManager.FULL_WAKE_LOCK |
| 92 | | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
| 94 | // time since last state: time since last event: |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 95 | // The short keylight delay comes from secure settings; this is the default. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec |
| 97 | private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec |
| 98 | private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec |
| 99 | private static final int LONG_DIM_TIME = 7000; // t+N-5 sec |
| 100 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 101 | // How long to wait to debounce light sensor changes. |
Mike Lockwood | 9b813692 | 2009-11-06 15:53:59 -0500 | [diff] [blame] | 102 | private static final int LIGHT_SENSOR_DELAY = 2000; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 103 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 104 | // For debouncing the proximity sensor. |
| 105 | private static final int PROXIMITY_SENSOR_DELAY = 1000; |
| 106 | |
Mike Lockwood | d20ea36 | 2009-09-15 00:13:38 -0400 | [diff] [blame] | 107 | // trigger proximity if distance is less than 5 cm |
| 108 | private static final float PROXIMITY_THRESHOLD = 5.0f; |
| 109 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 110 | // Cached secure settings; see updateSettingsValues() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT; |
| 112 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 113 | // Default timeout for screen off, if not found in settings database = 15 seconds. |
| 114 | private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15000; |
| 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | // flags for setPowerState |
| 117 | private static final int SCREEN_ON_BIT = 0x00000001; |
| 118 | private static final int SCREEN_BRIGHT_BIT = 0x00000002; |
| 119 | private static final int BUTTON_BRIGHT_BIT = 0x00000004; |
| 120 | private static final int KEYBOARD_BRIGHT_BIT = 0x00000008; |
| 121 | private static final int BATTERY_LOW_BIT = 0x00000010; |
| 122 | |
| 123 | // values for setPowerState |
| 124 | |
| 125 | // SCREEN_OFF == everything off |
| 126 | private static final int SCREEN_OFF = 0x00000000; |
| 127 | |
| 128 | // SCREEN_DIM == screen on, screen backlight dim |
| 129 | private static final int SCREEN_DIM = SCREEN_ON_BIT; |
| 130 | |
| 131 | // SCREEN_BRIGHT == screen on, screen backlight bright |
| 132 | private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT; |
| 133 | |
| 134 | // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright |
| 135 | private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT; |
| 136 | |
| 137 | // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright |
| 138 | private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT; |
| 139 | |
| 140 | // used for noChangeLights in setPowerState() |
| 141 | private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT; |
| 142 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 143 | boolean mAnimateScreenLights = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | static final int ANIM_STEPS = 60/4; |
Mike Lockwood | dd9668e | 2009-10-27 15:47:02 -0400 | [diff] [blame] | 146 | // Slower animation for autobrightness changes |
| 147 | static final int AUTOBRIGHTNESS_ANIM_STEPS = 60; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
| 149 | // These magic numbers are the initial state of the LEDs at boot. Ideally |
| 150 | // we should read them from the driver, but our current hardware returns 0 |
| 151 | // for the initial value. Oops! |
| 152 | static final int INITIAL_SCREEN_BRIGHTNESS = 255; |
| 153 | static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| 154 | static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | private final int MY_UID; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 157 | private final int MY_PID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
| 159 | private boolean mDoneBooting = false; |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 160 | private boolean mBootCompleted = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | private int mStayOnConditions = 0; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 162 | private final int[] mBroadcastQueue = new int[] { -1, -1, -1 }; |
| 163 | private final int[] mBroadcastWhy = new int[3]; |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 164 | private boolean mBroadcastingScreenOff = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | private int mPartialCount = 0; |
| 166 | private int mPowerState; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 167 | // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER, |
| 168 | // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 169 | private int mScreenOffReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | private int mUserState; |
| 171 | private boolean mKeyboardVisible = false; |
| 172 | private boolean mUserActivityAllowed = true; |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 173 | private int mProximityWakeLockCount = 0; |
| 174 | private boolean mProximitySensorEnabled = false; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 175 | private boolean mProximitySensorActive = false; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 176 | private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active |
| 177 | private long mLastProximityEventTime; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 178 | private int mScreenOffTimeoutSetting; |
| 179 | private int mMaximumScreenOffTimeout = Integer.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | private int mKeylightDelay; |
| 181 | private int mDimDelay; |
| 182 | private int mScreenOffDelay; |
| 183 | private int mWakeLockState; |
| 184 | private long mLastEventTime = 0; |
| 185 | private long mScreenOffTime; |
| 186 | private volatile WindowManagerPolicy mPolicy; |
| 187 | private final LockList mLocks = new LockList(); |
| 188 | private Intent mScreenOffIntent; |
| 189 | private Intent mScreenOnIntent; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 190 | private LightsService mLightsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | private Context mContext; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 192 | private LightsService.Light mLcdLight; |
| 193 | private LightsService.Light mButtonLight; |
| 194 | private LightsService.Light mKeyboardLight; |
| 195 | private LightsService.Light mAttentionLight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | private UnsynchronizedWakeLock mBroadcastWakeLock; |
| 197 | private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock; |
| 198 | private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock; |
| 199 | private UnsynchronizedWakeLock mPreventScreenOnPartialLock; |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 200 | private UnsynchronizedWakeLock mProximityPartialLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | private HandlerThread mHandlerThread; |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 202 | private HandlerThread mScreenOffThread; |
| 203 | private Handler mScreenOffHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | private Handler mHandler; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 205 | private final TimeoutTask mTimeoutTask = new TimeoutTask(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | private final BrightnessState mScreenBrightness |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 207 | = new BrightnessState(SCREEN_BRIGHT_BIT); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 208 | private boolean mStillNeedSleepNotification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | private boolean mIsPowered = false; |
| 210 | private IActivityManager mActivityService; |
| 211 | private IBatteryStats mBatteryStats; |
| 212 | private BatteryService mBatteryService; |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 213 | private SensorManager mSensorManager; |
| 214 | private Sensor mProximitySensor; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 215 | private Sensor mLightSensor; |
| 216 | private boolean mLightSensorEnabled; |
| 217 | private float mLightSensorValue = -1; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 218 | private boolean mProxIgnoredBecauseScreenTurnedOff = false; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 219 | private int mHighestLightSensorValue = -1; |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 220 | private boolean mLightSensorPendingDecrease = false; |
| 221 | private boolean mLightSensorPendingIncrease = false; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 222 | private float mLightSensorPendingValue = -1; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 223 | private int mLightSensorScreenBrightness = -1; |
| 224 | private int mLightSensorButtonBrightness = -1; |
| 225 | private int mLightSensorKeyboardBrightness = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | private boolean mDimScreen = true; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 227 | private boolean mIsDocked = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private long mNextTimeout; |
| 229 | private volatile int mPokey = 0; |
| 230 | private volatile boolean mPokeAwakeOnSet = false; |
| 231 | private volatile boolean mInitComplete = false; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 232 | private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>(); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 233 | // mLastScreenOnTime is the time the screen was last turned on |
| 234 | private long mLastScreenOnTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | private boolean mPreventScreenOn; |
| 236 | private int mScreenBrightnessOverride = -1; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 237 | private int mButtonBrightnessOverride = -1; |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 238 | private boolean mUseSoftwareAutoBrightness; |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 239 | private boolean mAutoBrightessEnabled; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 240 | private int[] mAutoBrightnessLevels; |
| 241 | private int[] mLcdBacklightValues; |
| 242 | private int[] mButtonBacklightValues; |
| 243 | private int[] mKeyboardBacklightValues; |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 244 | private int mLightSensorWarmupTime; |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 245 | boolean mUnplugTurnsOnScreen; |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 246 | private int mWarningSpewThrottleCount; |
| 247 | private long mWarningSpewThrottleTime; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 248 | private int mAnimationSetting = ANIM_SETTING_OFF; |
| 249 | |
| 250 | // Must match with the ISurfaceComposer constants in C++. |
| 251 | private static final int ANIM_SETTING_ON = 0x01; |
| 252 | private static final int ANIM_SETTING_OFF = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | |
| 254 | // Used when logging number and duration of touch-down cycles |
| 255 | private long mTotalTouchDownTime; |
| 256 | private long mLastTouchDown; |
| 257 | private int mTouchCycles; |
| 258 | |
| 259 | // could be either static or controllable at runtime |
| 260 | private static final boolean mSpew = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 261 | private static final boolean mDebugProximitySensor = (false || mSpew); |
Mike Lockwood | dd9668e | 2009-10-27 15:47:02 -0400 | [diff] [blame] | 262 | private static final boolean mDebugLightSensor = (false || mSpew); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 263 | |
| 264 | private native void nativeInit(); |
| 265 | private native void nativeSetPowerState(boolean screenOn, boolean screenBright); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 266 | private native void nativeStartSurfaceFlingerAnimation(int mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | static PrintStream mLog; |
| 270 | static { |
| 271 | try { |
| 272 | mLog = new PrintStream("/data/power.log"); |
| 273 | } |
| 274 | catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 275 | android.util.Slog.e(TAG, "Life is hard", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | static class Log { |
| 279 | static void d(String tag, String s) { |
| 280 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 281 | android.util.Slog.d(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | static void i(String tag, String s) { |
| 284 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 285 | android.util.Slog.i(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | static void w(String tag, String s) { |
| 288 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 289 | android.util.Slog.w(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |
| 291 | static void e(String tag, String s) { |
| 292 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 293 | android.util.Slog.e(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | */ |
| 297 | |
| 298 | /** |
| 299 | * This class works around a deadlock between the lock in PowerManager.WakeLock |
| 300 | * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its |
| 301 | * mToken object so it can be accessed from any thread, but it calls into here |
| 302 | * with its lock held. This class is essentially a reimplementation of |
| 303 | * PowerManager.WakeLock, but without that extra synchronized block, because we'll |
| 304 | * only call it with our own locks held. |
| 305 | */ |
| 306 | private class UnsynchronizedWakeLock { |
| 307 | int mFlags; |
| 308 | String mTag; |
| 309 | IBinder mToken; |
| 310 | int mCount = 0; |
| 311 | boolean mRefCounted; |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 312 | boolean mHeld; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 314 | UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) { |
| 315 | mFlags = flags; |
| 316 | mTag = tag; |
| 317 | mToken = new Binder(); |
| 318 | mRefCounted = refCounted; |
| 319 | } |
| 320 | |
| 321 | public void acquire() { |
| 322 | if (!mRefCounted || mCount++ == 0) { |
| 323 | long ident = Binder.clearCallingIdentity(); |
| 324 | try { |
| 325 | PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 326 | MY_UID, MY_PID, mTag, null); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 327 | mHeld = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } finally { |
| 329 | Binder.restoreCallingIdentity(ident); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | public void release() { |
| 335 | if (!mRefCounted || --mCount == 0) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 336 | PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 337 | mHeld = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | if (mCount < 0) { |
| 340 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 341 | } |
| 342 | } |
| 343 | |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 344 | public boolean isHeld() |
| 345 | { |
| 346 | return mHeld; |
| 347 | } |
| 348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | public String toString() { |
| 350 | return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags) |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 351 | + " mCount=" + mCount + " mHeld=" + mHeld + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | private final class BatteryReceiver extends BroadcastReceiver { |
| 356 | @Override |
| 357 | public void onReceive(Context context, Intent intent) { |
| 358 | synchronized (mLocks) { |
| 359 | boolean wasPowered = mIsPowered; |
| 360 | mIsPowered = mBatteryService.isPowered(); |
| 361 | |
| 362 | if (mIsPowered != wasPowered) { |
| 363 | // update mStayOnWhilePluggedIn wake lock |
| 364 | updateWakeLockLocked(); |
| 365 | |
| 366 | // treat plugging and unplugging the devices as a user activity. |
| 367 | // users find it disconcerting when they unplug the device |
| 368 | // and it shuts off right away. |
Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 369 | // to avoid turning on the screen when unplugging, we only trigger |
| 370 | // user activity when screen was already on. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | // temporarily set mUserActivityAllowed to true so this will work |
| 372 | // even when the keyguard is on. |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 373 | // However, you can also set config_unplugTurnsOnScreen to have it |
| 374 | // turn on. Some devices want this because they don't have a |
| 375 | // charging LED. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | synchronized (mLocks) { |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 377 | if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0 || |
| 378 | mUnplugTurnsOnScreen) { |
Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 379 | forceUserActivityLocked(); |
| 380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 387 | private final class BootCompletedReceiver extends BroadcastReceiver { |
| 388 | @Override |
| 389 | public void onReceive(Context context, Intent intent) { |
| 390 | bootCompleted(); |
| 391 | } |
| 392 | } |
| 393 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 394 | private final class DockReceiver extends BroadcastReceiver { |
| 395 | @Override |
| 396 | public void onReceive(Context context, Intent intent) { |
| 397 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 398 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 399 | dockStateChanged(state); |
| 400 | } |
| 401 | } |
| 402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | /** |
| 404 | * Set the setting that determines whether the device stays on when plugged in. |
| 405 | * The argument is a bit string, with each bit specifying a power source that, |
| 406 | * when the device is connected to that source, causes the device to stay on. |
| 407 | * See {@link android.os.BatteryManager} for the list of power sources that |
| 408 | * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC} |
| 409 | * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB} |
| 410 | * @param val an {@code int} containing the bits that specify which power sources |
| 411 | * should cause the device to stay on. |
| 412 | */ |
| 413 | public void setStayOnSetting(int val) { |
| 414 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null); |
| 415 | Settings.System.putInt(mContext.getContentResolver(), |
| 416 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, val); |
| 417 | } |
| 418 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 419 | public void setMaximumScreenOffTimeount(int timeMs) { |
| 420 | mContext.enforceCallingOrSelfPermission( |
| 421 | android.Manifest.permission.WRITE_SECURE_SETTINGS, null); |
| 422 | synchronized (mLocks) { |
| 423 | mMaximumScreenOffTimeout = timeMs; |
| 424 | // recalculate everything |
| 425 | setScreenOffTimeoutsLocked(); |
| 426 | } |
| 427 | } |
| 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | private class SettingsObserver implements Observer { |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 430 | private int getInt(String name, int defValue) { |
| 431 | ContentValues values = mSettings.getValues(name); |
| 432 | Integer iVal = values != null ? values.getAsInteger(Settings.System.VALUE) : null; |
| 433 | return iVal != null ? iVal : defValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 436 | private float getFloat(String name, float defValue) { |
| 437 | ContentValues values = mSettings.getValues(name); |
| 438 | Float fVal = values != null ? values.getAsFloat(Settings.System.VALUE) : null; |
| 439 | return fVal != null ? fVal : defValue; |
| 440 | } |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | public void update(Observable o, Object arg) { |
| 443 | synchronized (mLocks) { |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 444 | // STAY_ON_WHILE_PLUGGED_IN, default to when plugged into AC |
| 445 | mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN, |
| 446 | BatteryManager.BATTERY_PLUGGED_AC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | updateWakeLockLocked(); |
| 448 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 449 | // SCREEN_OFF_TIMEOUT, default to 15 seconds |
| 450 | mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 452 | // DIM_SCREEN |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | //mDimScreen = getInt(DIM_SCREEN) != 0; |
| 454 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 455 | // SCREEN_BRIGHTNESS_MODE, default to manual |
| 456 | setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE, |
| 457 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)); |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | // recalculate everything |
| 460 | setScreenOffTimeoutsLocked(); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 461 | |
| 462 | final float windowScale = getFloat(WINDOW_ANIMATION_SCALE, 1.0f); |
| 463 | final float transitionScale = getFloat(TRANSITION_ANIMATION_SCALE, 1.0f); |
| 464 | mAnimationSetting = 0; |
| 465 | if (windowScale > 0.5f) { |
| 466 | mAnimationSetting |= ANIM_SETTING_OFF; |
| 467 | } |
| 468 | if (transitionScale > 0.5f) { |
| 469 | // Uncomment this if you want the screen-on animation. |
| 470 | // mAnimationSetting |= ANIM_SETTING_ON; |
| 471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 476 | PowerManagerService() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | // Hack to get our uid... should have a func for this. |
| 478 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 479 | MY_UID = Process.myUid(); |
| 480 | MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | Binder.restoreCallingIdentity(token); |
| 482 | |
| 483 | // XXX remove this when the kernel doesn't timeout wake locks |
| 484 | Power.setLastUserActivityTimeout(7*24*3600*1000); // one week |
| 485 | |
| 486 | // assume nothing is on yet |
| 487 | mUserState = mPowerState = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | // Add ourself to the Watchdog monitors. |
| 490 | Watchdog.getInstance().addMonitor(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | private ContentQueryMap mSettings; |
| 494 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 495 | void init(Context context, LightsService lights, IActivityManager activity, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 496 | BatteryService battery) { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 497 | mLightsService = lights; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | mContext = context; |
| 499 | mActivityService = activity; |
| 500 | mBatteryStats = BatteryStatsService.getService(); |
| 501 | mBatteryService = battery; |
| 502 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 503 | mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT); |
| 504 | mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS); |
| 505 | mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD); |
| 506 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 507 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 508 | nativeInit(); |
| 509 | synchronized (mLocks) { |
| 510 | updateNativePowerStateLocked(); |
| 511 | } |
| 512 | |
| 513 | mInitComplete = false; |
| 514 | mScreenOffThread = new HandlerThread("PowerManagerService.mScreenOffThread") { |
| 515 | @Override |
| 516 | protected void onLooperPrepared() { |
| 517 | mScreenOffHandler = new Handler(); |
| 518 | synchronized (mScreenOffThread) { |
| 519 | mInitComplete = true; |
| 520 | mScreenOffThread.notifyAll(); |
| 521 | } |
| 522 | } |
| 523 | }; |
| 524 | mScreenOffThread.start(); |
| 525 | |
| 526 | synchronized (mScreenOffThread) { |
| 527 | while (!mInitComplete) { |
| 528 | try { |
| 529 | mScreenOffThread.wait(); |
| 530 | } catch (InterruptedException e) { |
| 531 | // Ignore |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | mInitComplete = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | mHandlerThread = new HandlerThread("PowerManagerService") { |
| 538 | @Override |
| 539 | protected void onLooperPrepared() { |
| 540 | super.onLooperPrepared(); |
| 541 | initInThread(); |
| 542 | } |
| 543 | }; |
| 544 | mHandlerThread.start(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 545 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | synchronized (mHandlerThread) { |
| 547 | while (!mInitComplete) { |
| 548 | try { |
| 549 | mHandlerThread.wait(); |
| 550 | } catch (InterruptedException e) { |
| 551 | // Ignore |
| 552 | } |
| 553 | } |
| 554 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 555 | |
| 556 | nativeInit(); |
| 557 | synchronized (mLocks) { |
| 558 | updateNativePowerStateLocked(); |
| 559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | void initInThread() { |
| 563 | mHandler = new Handler(); |
| 564 | |
| 565 | mBroadcastWakeLock = new UnsynchronizedWakeLock( |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 566 | PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock( |
| 568 | PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false); |
| 569 | mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock( |
| 570 | PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false); |
| 571 | mPreventScreenOnPartialLock = new UnsynchronizedWakeLock( |
| 572 | PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 573 | mProximityPartialLock = new UnsynchronizedWakeLock( |
| 574 | PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | |
| 576 | mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON); |
| 577 | mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 578 | mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF); |
| 579 | mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 580 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 581 | Resources resources = mContext.getResources(); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 582 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 583 | mAnimateScreenLights = resources.getBoolean( |
| 584 | com.android.internal.R.bool.config_animateScreenLights); |
| 585 | |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 586 | mUnplugTurnsOnScreen = resources.getBoolean( |
| 587 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
| 588 | |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 589 | // read settings for auto-brightness |
| 590 | mUseSoftwareAutoBrightness = resources.getBoolean( |
| 591 | com.android.internal.R.bool.config_automatic_brightness_available); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 592 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 593 | mAutoBrightnessLevels = resources.getIntArray( |
| 594 | com.android.internal.R.array.config_autoBrightnessLevels); |
| 595 | mLcdBacklightValues = resources.getIntArray( |
| 596 | com.android.internal.R.array.config_autoBrightnessLcdBacklightValues); |
| 597 | mButtonBacklightValues = resources.getIntArray( |
| 598 | com.android.internal.R.array.config_autoBrightnessButtonBacklightValues); |
| 599 | mKeyboardBacklightValues = resources.getIntArray( |
| 600 | com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 601 | mLightSensorWarmupTime = resources.getInteger( |
| 602 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 603 | } |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 604 | |
| 605 | ContentResolver resolver = mContext.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null, |
| 607 | "(" + Settings.System.NAME + "=?) or (" |
| 608 | + Settings.System.NAME + "=?) or (" |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 609 | + Settings.System.NAME + "=?) or (" |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 610 | + Settings.System.NAME + "=?) or (" |
| 611 | + Settings.System.NAME + "=?) or (" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | + Settings.System.NAME + "=?)", |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 613 | new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 614 | SCREEN_BRIGHTNESS_MODE, WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | null); |
| 616 | mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler); |
| 617 | SettingsObserver settingsObserver = new SettingsObserver(); |
| 618 | mSettings.addObserver(settingsObserver); |
| 619 | |
| 620 | // pretend that the settings changed so we will get their initial state |
| 621 | settingsObserver.update(mSettings, null); |
| 622 | |
| 623 | // register for the battery changed notifications |
| 624 | IntentFilter filter = new IntentFilter(); |
| 625 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 626 | mContext.registerReceiver(new BatteryReceiver(), filter); |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 627 | filter = new IntentFilter(); |
| 628 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 629 | mContext.registerReceiver(new BootCompletedReceiver(), filter); |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 630 | filter = new IntentFilter(); |
| 631 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
| 632 | mContext.registerReceiver(new DockReceiver(), filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 634 | // Listen for secure settings changes |
| 635 | mContext.getContentResolver().registerContentObserver( |
| 636 | Settings.Secure.CONTENT_URI, true, |
| 637 | new ContentObserver(new Handler()) { |
| 638 | public void onChange(boolean selfChange) { |
| 639 | updateSettingsValues(); |
| 640 | } |
| 641 | }); |
| 642 | updateSettingsValues(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | synchronized (mHandlerThread) { |
| 645 | mInitComplete = true; |
| 646 | mHandlerThread.notifyAll(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private class WakeLock implements IBinder.DeathRecipient |
| 651 | { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 652 | WakeLock(int f, IBinder b, String t, int u, int p) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | super(); |
| 654 | flags = f; |
| 655 | binder = b; |
| 656 | tag = t; |
| 657 | uid = u == MY_UID ? Process.SYSTEM_UID : u; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 658 | pid = p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | if (u != MY_UID || ( |
| 660 | !"KEEP_SCREEN_ON_FLAG".equals(tag) |
| 661 | && !"KeyInputQueue".equals(tag))) { |
| 662 | monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK |
| 663 | ? BatteryStats.WAKE_TYPE_PARTIAL |
| 664 | : BatteryStats.WAKE_TYPE_FULL; |
| 665 | } else { |
| 666 | monitorType = -1; |
| 667 | } |
| 668 | try { |
| 669 | b.linkToDeath(this, 0); |
| 670 | } catch (RemoteException e) { |
| 671 | binderDied(); |
| 672 | } |
| 673 | } |
| 674 | public void binderDied() { |
| 675 | synchronized (mLocks) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 676 | releaseWakeLockLocked(this.binder, 0, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | final int flags; |
| 680 | final IBinder binder; |
| 681 | final String tag; |
| 682 | final int uid; |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 683 | final int pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | final int monitorType; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 685 | WorkSource ws; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | boolean activated = true; |
| 687 | int minState; |
| 688 | } |
| 689 | |
| 690 | private void updateWakeLockLocked() { |
| 691 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 692 | // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set. |
| 693 | mStayOnWhilePluggedInScreenDimLock.acquire(); |
| 694 | mStayOnWhilePluggedInPartialLock.acquire(); |
| 695 | } else { |
| 696 | mStayOnWhilePluggedInScreenDimLock.release(); |
| 697 | mStayOnWhilePluggedInPartialLock.release(); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | private boolean isScreenLock(int flags) |
| 702 | { |
| 703 | int n = flags & LOCK_MASK; |
| 704 | return n == PowerManager.FULL_WAKE_LOCK |
| 705 | || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 706 | || n == PowerManager.SCREEN_DIM_WAKE_LOCK |
| 707 | || n == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 710 | void enforceWakeSourcePermission(int uid, int pid) { |
| 711 | if (uid == Process.myUid()) { |
| 712 | return; |
| 713 | } |
| 714 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 715 | pid, uid, null); |
| 716 | } |
| 717 | |
| 718 | public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | int uid = Binder.getCallingUid(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 720 | int pid = Binder.getCallingPid(); |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 721 | if (uid != Process.myUid()) { |
| 722 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 723 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 724 | if (ws != null) { |
| 725 | enforceWakeSourcePermission(uid, pid); |
| 726 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | long ident = Binder.clearCallingIdentity(); |
| 728 | try { |
| 729 | synchronized (mLocks) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 730 | acquireWakeLockLocked(flags, lock, uid, pid, tag, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | } |
| 732 | } finally { |
| 733 | Binder.restoreCallingIdentity(ident); |
| 734 | } |
| 735 | } |
| 736 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 737 | void noteStartWakeLocked(WakeLock wl, WorkSource ws) { |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 738 | if (wl.monitorType >= 0) { |
| 739 | long origId = Binder.clearCallingIdentity(); |
| 740 | try { |
| 741 | if (ws != null) { |
| 742 | mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag, |
| 743 | wl.monitorType); |
| 744 | } else { |
| 745 | mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 746 | } |
| 747 | } catch (RemoteException e) { |
| 748 | // Ignore |
| 749 | } finally { |
| 750 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 751 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 752 | } |
| 753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 755 | void noteStopWakeLocked(WakeLock wl, WorkSource ws) { |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 756 | if (wl.monitorType >= 0) { |
| 757 | long origId = Binder.clearCallingIdentity(); |
| 758 | try { |
| 759 | if (ws != null) { |
| 760 | mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag, |
| 761 | wl.monitorType); |
| 762 | } else { |
| 763 | mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 764 | } |
| 765 | } catch (RemoteException e) { |
| 766 | // Ignore |
| 767 | } finally { |
| 768 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 769 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag, |
| 774 | WorkSource ws) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 776 | Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 779 | if (ws != null && ws.size() == 0) { |
| 780 | ws = null; |
| 781 | } |
| 782 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | int index = mLocks.getIndex(lock); |
| 784 | WakeLock wl; |
| 785 | boolean newlock; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 786 | boolean diffsource; |
| 787 | WorkSource oldsource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | if (index < 0) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 789 | wl = new WakeLock(flags, lock, tag, uid, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | switch (wl.flags & LOCK_MASK) |
| 791 | { |
| 792 | case PowerManager.FULL_WAKE_LOCK: |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 793 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 794 | wl.minState = SCREEN_BRIGHT; |
| 795 | } else { |
| 796 | wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 797 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | break; |
| 799 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 800 | wl.minState = SCREEN_BRIGHT; |
| 801 | break; |
| 802 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 803 | wl.minState = SCREEN_DIM; |
| 804 | break; |
| 805 | case PowerManager.PARTIAL_WAKE_LOCK: |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 806 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | break; |
| 808 | default: |
| 809 | // just log and bail. we're in the server, so don't |
| 810 | // throw an exception. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 811 | Slog.e(TAG, "bad wakelock type for lock '" + tag + "' " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | + " flags=" + flags); |
| 813 | return; |
| 814 | } |
| 815 | mLocks.addLock(wl); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 816 | if (ws != null) { |
| 817 | wl.ws = new WorkSource(ws); |
| 818 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | newlock = true; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 820 | diffsource = false; |
| 821 | oldsource = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } else { |
| 823 | wl = mLocks.get(index); |
| 824 | newlock = false; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 825 | oldsource = wl.ws; |
| 826 | if (oldsource != null) { |
| 827 | if (ws == null) { |
| 828 | wl.ws = null; |
| 829 | diffsource = true; |
| 830 | } else { |
| 831 | diffsource = oldsource.diff(ws); |
| 832 | } |
| 833 | } else if (ws != null) { |
| 834 | diffsource = true; |
| 835 | } else { |
| 836 | diffsource = false; |
| 837 | } |
| 838 | if (diffsource) { |
| 839 | wl.ws = new WorkSource(ws); |
| 840 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
| 842 | if (isScreenLock(flags)) { |
| 843 | // if this causes a wakeup, we reactivate all of the locks and |
| 844 | // set it to whatever they want. otherwise, we modulate that |
| 845 | // by the current state so we never turn it more on than |
| 846 | // it already is. |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 847 | if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 848 | mProximityWakeLockCount++; |
| 849 | if (mProximityWakeLockCount == 1) { |
| 850 | enableProximityLockLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } else { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 853 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 854 | int oldWakeLockState = mWakeLockState; |
| 855 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
| 856 | if (mSpew) { |
| 857 | Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState) |
| 858 | + " mWakeLockState=0x" |
| 859 | + Integer.toHexString(mWakeLockState) |
| 860 | + " previous wakeLockState=0x" |
| 861 | + Integer.toHexString(oldWakeLockState)); |
| 862 | } |
| 863 | } else { |
| 864 | if (mSpew) { |
| 865 | Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState) |
| 866 | + " mLocks.gatherState()=0x" |
| 867 | + Integer.toHexString(mLocks.gatherState()) |
| 868 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)); |
| 869 | } |
| 870 | mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 872 | setPowerState(mWakeLockState | mUserState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | } |
| 875 | else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 876 | if (newlock) { |
| 877 | mPartialCount++; |
| 878 | if (mPartialCount == 1) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 879 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME); |
| 883 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 885 | if (diffsource) { |
| 886 | // If the lock sources have changed, need to first release the |
| 887 | // old ones. |
| 888 | noteStopWakeLocked(wl, oldsource); |
| 889 | } |
| 890 | if (newlock || diffsource) { |
| 891 | noteStartWakeLocked(wl, ws); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) { |
| 896 | int uid = Binder.getCallingUid(); |
| 897 | int pid = Binder.getCallingPid(); |
| 898 | if (ws != null && ws.size() == 0) { |
| 899 | ws = null; |
| 900 | } |
| 901 | if (ws != null) { |
| 902 | enforceWakeSourcePermission(uid, pid); |
| 903 | } |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 904 | synchronized (mLocks) { |
| 905 | int index = mLocks.getIndex(lock); |
| 906 | if (index < 0) { |
| 907 | throw new IllegalArgumentException("Wake lock not active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | } |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 909 | WakeLock wl = mLocks.get(index); |
| 910 | WorkSource oldsource = wl.ws; |
| 911 | wl.ws = ws != null ? new WorkSource(ws) : null; |
| 912 | noteStopWakeLocked(wl, oldsource); |
| 913 | noteStartWakeLocked(wl, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 917 | public void releaseWakeLock(IBinder lock, int flags) { |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 918 | int uid = Binder.getCallingUid(); |
| 919 | if (uid != Process.myUid()) { |
| 920 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 921 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | |
| 923 | synchronized (mLocks) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 924 | releaseWakeLockLocked(lock, flags, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 928 | private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | WakeLock wl = mLocks.removeLock(lock); |
| 930 | if (wl == null) { |
| 931 | return; |
| 932 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 933 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 935 | Slog.d(TAG, "releaseWakeLock flags=0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | + Integer.toHexString(wl.flags) + " tag=" + wl.tag); |
| 937 | } |
| 938 | |
| 939 | if (isScreenLock(wl.flags)) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 940 | if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 941 | mProximityWakeLockCount--; |
| 942 | if (mProximityWakeLockCount == 0) { |
| 943 | if (mProximitySensorActive && |
| 944 | ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) { |
| 945 | // wait for proximity sensor to go negative before disabling sensor |
| 946 | if (mDebugProximitySensor) { |
| 947 | Slog.d(TAG, "waiting for proximity sensor to go negative"); |
| 948 | } |
| 949 | } else { |
| 950 | disableProximityLockLocked(); |
| 951 | } |
| 952 | } |
| 953 | } else { |
| 954 | mWakeLockState = mLocks.gatherState(); |
| 955 | // goes in the middle to reduce flicker |
| 956 | if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) { |
| 957 | userActivity(SystemClock.uptimeMillis(), -1, false, OTHER_EVENT, false); |
| 958 | } |
| 959 | setPowerState(mWakeLockState | mUserState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } |
| 962 | else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 963 | mPartialCount--; |
| 964 | if (mPartialCount == 0) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 965 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | Power.releaseWakeLock(PARTIAL_NAME); |
| 967 | } |
| 968 | } |
| 969 | // Unlink the lock from the binder. |
| 970 | wl.binder.unlinkToDeath(wl, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 972 | noteStopWakeLocked(wl, wl.ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | } |
| 974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | private class PokeLock implements IBinder.DeathRecipient |
| 976 | { |
| 977 | PokeLock(int p, IBinder b, String t) { |
| 978 | super(); |
| 979 | this.pokey = p; |
| 980 | this.binder = b; |
| 981 | this.tag = t; |
| 982 | try { |
| 983 | b.linkToDeath(this, 0); |
| 984 | } catch (RemoteException e) { |
| 985 | binderDied(); |
| 986 | } |
| 987 | } |
| 988 | public void binderDied() { |
| 989 | setPokeLock(0, this.binder, this.tag); |
| 990 | } |
| 991 | int pokey; |
| 992 | IBinder binder; |
| 993 | String tag; |
| 994 | boolean awakeOnSet; |
| 995 | } |
| 996 | |
| 997 | public void setPokeLock(int pokey, IBinder token, String tag) { |
| 998 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 999 | if (token == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1000 | Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) { |
| 1005 | throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT" |
| 1006 | + " and POKE_LOCK_MEDIUM_TIMEOUT"); |
| 1007 | } |
| 1008 | |
| 1009 | synchronized (mLocks) { |
| 1010 | if (pokey != 0) { |
| 1011 | PokeLock p = mPokeLocks.get(token); |
| 1012 | int oldPokey = 0; |
| 1013 | if (p != null) { |
| 1014 | oldPokey = p.pokey; |
| 1015 | p.pokey = pokey; |
| 1016 | } else { |
| 1017 | p = new PokeLock(pokey, token, tag); |
| 1018 | mPokeLocks.put(token, p); |
| 1019 | } |
| 1020 | int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1021 | int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
| 1022 | if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) { |
| 1023 | p.awakeOnSet = true; |
| 1024 | } |
| 1025 | } else { |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1026 | PokeLock rLock = mPokeLocks.remove(token); |
| 1027 | if (rLock != null) { |
| 1028 | token.unlinkToDeath(rLock, 0); |
| 1029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | int oldPokey = mPokey; |
| 1033 | int cumulative = 0; |
| 1034 | boolean oldAwakeOnSet = mPokeAwakeOnSet; |
| 1035 | boolean awakeOnSet = false; |
| 1036 | for (PokeLock p: mPokeLocks.values()) { |
| 1037 | cumulative |= p.pokey; |
| 1038 | if (p.awakeOnSet) { |
| 1039 | awakeOnSet = true; |
| 1040 | } |
| 1041 | } |
| 1042 | mPokey = cumulative; |
| 1043 | mPokeAwakeOnSet = awakeOnSet; |
| 1044 | |
| 1045 | int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1046 | int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1047 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | if (oldCumulativeTimeout != newCumulativeTimeout) { |
| 1049 | setScreenOffTimeoutsLocked(); |
| 1050 | // reset the countdown timer, but use the existing nextState so it doesn't |
| 1051 | // change anything |
| 1052 | setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState); |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | private static String lockType(int type) |
| 1058 | { |
| 1059 | switch (type) |
| 1060 | { |
| 1061 | case PowerManager.FULL_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1062 | return "FULL_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1064 | return "SCREEN_BRIGHT_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1066 | return "SCREEN_DIM_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | case PowerManager.PARTIAL_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1068 | return "PARTIAL_WAKE_LOCK "; |
| 1069 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 1070 | return "PROXIMITY_SCREEN_OFF_WAKE_LOCK"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | default: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1072 | return "??? "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | private static String dumpPowerState(int state) { |
| 1077 | return (((state & KEYBOARD_BRIGHT_BIT) != 0) |
| 1078 | ? "KEYBOARD_BRIGHT_BIT " : "") |
| 1079 | + (((state & SCREEN_BRIGHT_BIT) != 0) |
| 1080 | ? "SCREEN_BRIGHT_BIT " : "") |
| 1081 | + (((state & SCREEN_ON_BIT) != 0) |
| 1082 | ? "SCREEN_ON_BIT " : "") |
| 1083 | + (((state & BATTERY_LOW_BIT) != 0) |
| 1084 | ? "BATTERY_LOW_BIT " : ""); |
| 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1089 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1090 | != PackageManager.PERMISSION_GRANTED) { |
| 1091 | pw.println("Permission Denial: can't dump PowerManager from from pid=" |
| 1092 | + Binder.getCallingPid() |
| 1093 | + ", uid=" + Binder.getCallingUid()); |
| 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | long now = SystemClock.uptimeMillis(); |
| 1098 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1099 | synchronized (mLocks) { |
| 1100 | pw.println("Power Manager State:"); |
| 1101 | pw.println(" mIsPowered=" + mIsPowered |
| 1102 | + " mPowerState=" + mPowerState |
| 1103 | + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime) |
| 1104 | + " ms"); |
| 1105 | pw.println(" mPartialCount=" + mPartialCount); |
| 1106 | pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState)); |
| 1107 | pw.println(" mUserState=" + dumpPowerState(mUserState)); |
| 1108 | pw.println(" mPowerState=" + dumpPowerState(mPowerState)); |
| 1109 | pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState())); |
| 1110 | pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now |
| 1111 | + " " + ((mNextTimeout-now)/1000) + "s from now"); |
| 1112 | pw.println(" mDimScreen=" + mDimScreen |
| 1113 | + " mStayOnConditions=" + mStayOnConditions); |
| 1114 | pw.println(" mScreenOffReason=" + mScreenOffReason |
| 1115 | + " mUserState=" + mUserState); |
| 1116 | pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1] |
| 1117 | + ',' + mBroadcastQueue[2] + "}"); |
| 1118 | pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1] |
| 1119 | + ',' + mBroadcastWhy[2] + "}"); |
| 1120 | pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet); |
| 1121 | pw.println(" mKeyboardVisible=" + mKeyboardVisible |
| 1122 | + " mUserActivityAllowed=" + mUserActivityAllowed); |
| 1123 | pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay |
| 1124 | + " mScreenOffDelay=" + mScreenOffDelay); |
| 1125 | pw.println(" mPreventScreenOn=" + mPreventScreenOn |
| 1126 | + " mScreenBrightnessOverride=" + mScreenBrightnessOverride |
| 1127 | + " mButtonBrightnessOverride=" + mButtonBrightnessOverride); |
| 1128 | pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting |
| 1129 | + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout); |
| 1130 | pw.println(" mLastScreenOnTime=" + mLastScreenOnTime); |
| 1131 | pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock); |
| 1132 | pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock); |
| 1133 | pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock); |
| 1134 | pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock); |
| 1135 | pw.println(" mProximityPartialLock=" + mProximityPartialLock); |
| 1136 | pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount); |
| 1137 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1138 | pw.println(" mProximitySensorActive=" + mProximitySensorActive); |
| 1139 | pw.println(" mProximityPendingValue=" + mProximityPendingValue); |
| 1140 | pw.println(" mLastProximityEventTime=" + mLastProximityEventTime); |
| 1141 | pw.println(" mLightSensorEnabled=" + mLightSensorEnabled); |
| 1142 | pw.println(" mLightSensorValue=" + mLightSensorValue |
| 1143 | + " mLightSensorPendingValue=" + mLightSensorPendingValue); |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1144 | pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease |
| 1145 | + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease); |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1146 | pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness |
| 1147 | + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness |
| 1148 | + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness); |
| 1149 | pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness); |
| 1150 | pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 1151 | mScreenBrightness.dump(pw, " mScreenBrightness: "); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1152 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1153 | int N = mLocks.size(); |
| 1154 | pw.println(); |
| 1155 | pw.println("mLocks.size=" + N + ":"); |
| 1156 | for (int i=0; i<N; i++) { |
| 1157 | WakeLock wl = mLocks.get(i); |
| 1158 | String type = lockType(wl.flags & LOCK_MASK); |
| 1159 | String acquireCausesWakeup = ""; |
| 1160 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 1161 | acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP "; |
| 1162 | } |
| 1163 | String activated = ""; |
| 1164 | if (wl.activated) { |
| 1165 | activated = " activated"; |
| 1166 | } |
| 1167 | pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 1168 | + activated + " (minState=" + wl.minState + ", uid=" + wl.uid |
| 1169 | + ", pid=" + wl.pid + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1171 | |
| 1172 | pw.println(); |
| 1173 | pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":"); |
| 1174 | for (PokeLock p: mPokeLocks.values()) { |
| 1175 | pw.println(" poke lock '" + p.tag + "':" |
Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 1176 | + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0 |
| 1177 | ? " POKE_LOCK_IGNORE_TOUCH_EVENTS" : "") |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1178 | + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0 |
| 1179 | ? " POKE_LOCK_SHORT_TIMEOUT" : "") |
| 1180 | + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0 |
| 1181 | ? " POKE_LOCK_MEDIUM_TIMEOUT" : "")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1183 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1184 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1188 | private void setTimeoutLocked(long now, int nextState) { |
| 1189 | setTimeoutLocked(now, -1, nextState); |
| 1190 | } |
| 1191 | |
| 1192 | // If they gave a timeoutOverride it is the number of seconds |
| 1193 | // to screen-off. Figure out where in the countdown cycle we |
| 1194 | // should jump to. |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1195 | private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) { |
| 1196 | long timeoutOverride = originalTimeoutOverride; |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1197 | if (mBootCompleted) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1198 | synchronized (mLocks) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1199 | long when = 0; |
| 1200 | if (timeoutOverride <= 0) { |
| 1201 | switch (nextState) |
| 1202 | { |
| 1203 | case SCREEN_BRIGHT: |
| 1204 | when = now + mKeylightDelay; |
| 1205 | break; |
| 1206 | case SCREEN_DIM: |
| 1207 | if (mDimDelay >= 0) { |
| 1208 | when = now + mDimDelay; |
Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1209 | break; |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1210 | } else { |
| 1211 | Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim"); |
| 1212 | } |
| 1213 | case SCREEN_OFF: |
| 1214 | synchronized (mLocks) { |
| 1215 | when = now + mScreenOffDelay; |
| 1216 | } |
| 1217 | break; |
Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1218 | default: |
| 1219 | when = now; |
| 1220 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | } |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1222 | } else { |
| 1223 | override: { |
| 1224 | if (timeoutOverride <= mScreenOffDelay) { |
| 1225 | when = now + timeoutOverride; |
| 1226 | nextState = SCREEN_OFF; |
| 1227 | break override; |
| 1228 | } |
| 1229 | timeoutOverride -= mScreenOffDelay; |
| 1230 | |
| 1231 | if (mDimDelay >= 0) { |
| 1232 | if (timeoutOverride <= mDimDelay) { |
| 1233 | when = now + timeoutOverride; |
| 1234 | nextState = SCREEN_DIM; |
| 1235 | break override; |
| 1236 | } |
| 1237 | timeoutOverride -= mDimDelay; |
| 1238 | } |
| 1239 | |
| 1240 | when = now + timeoutOverride; |
| 1241 | nextState = SCREEN_BRIGHT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | } |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1243 | } |
| 1244 | if (mSpew) { |
| 1245 | Slog.d(TAG, "setTimeoutLocked now=" + now |
| 1246 | + " timeoutOverride=" + timeoutOverride |
| 1247 | + " nextState=" + nextState + " when=" + when); |
| 1248 | } |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1249 | |
| 1250 | mHandler.removeCallbacks(mTimeoutTask); |
| 1251 | mTimeoutTask.nextState = nextState; |
| 1252 | mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0 |
| 1253 | ? (originalTimeoutOverride - timeoutOverride) |
| 1254 | : -1; |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1255 | mHandler.postAtTime(mTimeoutTask, when); |
| 1256 | mNextTimeout = when; // for debugging |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | private void cancelTimerLocked() |
| 1262 | { |
| 1263 | mHandler.removeCallbacks(mTimeoutTask); |
| 1264 | mTimeoutTask.nextState = -1; |
| 1265 | } |
| 1266 | |
| 1267 | private class TimeoutTask implements Runnable |
| 1268 | { |
| 1269 | int nextState; // access should be synchronized on mLocks |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1270 | long remainingTimeoutOverride; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | public void run() |
| 1272 | { |
| 1273 | synchronized (mLocks) { |
| 1274 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1275 | Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | if (nextState == -1) { |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | mUserState = this.nextState; |
| 1283 | setPowerState(this.nextState | mWakeLockState); |
| 1284 | |
| 1285 | long now = SystemClock.uptimeMillis(); |
| 1286 | |
| 1287 | switch (this.nextState) |
| 1288 | { |
| 1289 | case SCREEN_BRIGHT: |
| 1290 | if (mDimDelay >= 0) { |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1291 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | break; |
| 1293 | } |
| 1294 | case SCREEN_DIM: |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1295 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | private void sendNotificationLocked(boolean on, int why) |
| 1303 | { |
Joe Onorato | 64c62ba | 2009-03-24 20:13:57 -0700 | [diff] [blame] | 1304 | if (!on) { |
| 1305 | mStillNeedSleepNotification = false; |
| 1306 | } |
| 1307 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1308 | // Add to the queue. |
| 1309 | int index = 0; |
| 1310 | while (mBroadcastQueue[index] != -1) { |
| 1311 | index++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1313 | mBroadcastQueue[index] = on ? 1 : 0; |
| 1314 | mBroadcastWhy[index] = why; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1316 | // If we added it position 2, then there is a pair that can be stripped. |
| 1317 | // If we added it position 1 and we're turning the screen off, we can strip |
| 1318 | // the pair and do nothing, because the screen is already off, and therefore |
| 1319 | // keyguard has already been enabled. |
| 1320 | // However, if we added it at position 1 and we're turning it on, then position |
| 1321 | // 0 was to turn it off, and we can't strip that, because keyguard needs to come |
| 1322 | // on, so have to run the queue then. |
| 1323 | if (index == 2) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1324 | // While we're collapsing them, if it's going off, and the new reason |
| 1325 | // is more significant than the first, then use the new one. |
| 1326 | if (!on && mBroadcastWhy[0] > why) { |
| 1327 | mBroadcastWhy[0] = why; |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1328 | } |
| 1329 | mBroadcastQueue[0] = on ? 1 : 0; |
| 1330 | mBroadcastQueue[1] = -1; |
| 1331 | mBroadcastQueue[2] = -1; |
Mike Lockwood | 9c90a37 | 2010-04-13 15:40:27 -0400 | [diff] [blame] | 1332 | mBroadcastWakeLock.release(); |
| 1333 | mBroadcastWakeLock.release(); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1334 | index = 0; |
| 1335 | } |
| 1336 | if (index == 1 && !on) { |
| 1337 | mBroadcastQueue[0] = -1; |
| 1338 | mBroadcastQueue[1] = -1; |
| 1339 | index = -1; |
| 1340 | // The wake lock was being held, but we're not actually going to do any |
| 1341 | // broadcasts, so release the wake lock. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1342 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | mBroadcastWakeLock.release(); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 1346 | // The broadcast queue has changed; make sure the screen is on if it |
| 1347 | // is now possible for it to be. |
| 1348 | updateNativePowerStateLocked(); |
| 1349 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1350 | // Now send the message. |
| 1351 | if (index >= 0) { |
| 1352 | // Acquire the broadcast wake lock before changing the power |
| 1353 | // state. It will be release after the broadcast is sent. |
| 1354 | // We always increment the ref count for each notification in the queue |
| 1355 | // and always decrement when that notification is handled. |
| 1356 | mBroadcastWakeLock.acquire(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1357 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1358 | mHandler.post(mNotificationTask); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | private Runnable mNotificationTask = new Runnable() |
| 1363 | { |
| 1364 | public void run() |
| 1365 | { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1366 | while (true) { |
| 1367 | int value; |
| 1368 | int why; |
| 1369 | WindowManagerPolicy policy; |
| 1370 | synchronized (mLocks) { |
| 1371 | value = mBroadcastQueue[0]; |
| 1372 | why = mBroadcastWhy[0]; |
| 1373 | for (int i=0; i<2; i++) { |
| 1374 | mBroadcastQueue[i] = mBroadcastQueue[i+1]; |
| 1375 | mBroadcastWhy[i] = mBroadcastWhy[i+1]; |
| 1376 | } |
| 1377 | policy = getPolicyLocked(); |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 1378 | if (value == 0) { |
| 1379 | mBroadcastingScreenOff = true; |
| 1380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1382 | if (value == 1) { |
| 1383 | mScreenOnStart = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1384 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1385 | policy.screenTurnedOn(); |
| 1386 | try { |
| 1387 | ActivityManagerNative.getDefault().wakingUp(); |
| 1388 | } catch (RemoteException e) { |
| 1389 | // ignore it |
| 1390 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1392 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1393 | Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1394 | } |
| 1395 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1396 | mContext.sendOrderedBroadcast(mScreenOnIntent, null, |
| 1397 | mScreenOnBroadcastDone, mHandler, 0, null, null); |
| 1398 | } else { |
| 1399 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1400 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1401 | mBroadcastWakeLock.mCount); |
| 1402 | mBroadcastWakeLock.release(); |
| 1403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |
| 1405 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1406 | else if (value == 0) { |
| 1407 | mScreenOffStart = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1408 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1409 | policy.screenTurnedOff(why); |
| 1410 | try { |
| 1411 | ActivityManagerNative.getDefault().goingToSleep(); |
| 1412 | } catch (RemoteException e) { |
| 1413 | // ignore it. |
| 1414 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1415 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1416 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1417 | mContext.sendOrderedBroadcast(mScreenOffIntent, null, |
| 1418 | mScreenOffBroadcastDone, mHandler, 0, null, null); |
| 1419 | } else { |
| 1420 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1421 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1422 | mBroadcastWakeLock.mCount); |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 1423 | mBroadcastingScreenOff = false; |
| 1424 | updateNativePowerStateLocked(); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1425 | mBroadcastWakeLock.release(); |
| 1426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | } |
| 1428 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1429 | else { |
| 1430 | // If we're in this case, then this handler is running for a previous |
| 1431 | // paired transaction. mBroadcastWakeLock will already have been released. |
| 1432 | break; |
| 1433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | }; |
| 1437 | |
| 1438 | long mScreenOnStart; |
| 1439 | private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() { |
| 1440 | public void onReceive(Context context, Intent intent) { |
| 1441 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1442 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount); |
| 1444 | mBroadcastWakeLock.release(); |
| 1445 | } |
| 1446 | } |
| 1447 | }; |
| 1448 | |
| 1449 | long mScreenOffStart; |
| 1450 | private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() { |
| 1451 | public void onReceive(Context context, Intent intent) { |
| 1452 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1453 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount); |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 1455 | synchronized (mLocks) { |
| 1456 | mBroadcastingScreenOff = false; |
| 1457 | updateNativePowerStateLocked(); |
| 1458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | mBroadcastWakeLock.release(); |
| 1460 | } |
| 1461 | } |
| 1462 | }; |
| 1463 | |
| 1464 | void logPointerUpEvent() { |
| 1465 | if (LOG_TOUCH_DOWNS) { |
| 1466 | mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown; |
| 1467 | mLastTouchDown = 0; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | void logPointerDownEvent() { |
| 1472 | if (LOG_TOUCH_DOWNS) { |
| 1473 | // If we are not already timing a down/up sequence |
| 1474 | if (mLastTouchDown == 0) { |
| 1475 | mLastTouchDown = SystemClock.elapsedRealtime(); |
| 1476 | mTouchCycles++; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * Prevents the screen from turning on even if it *should* turn on due |
| 1483 | * to a subsequent full wake lock being acquired. |
| 1484 | * <p> |
| 1485 | * This is a temporary hack that allows an activity to "cover up" any |
| 1486 | * display glitches that happen during the activity's startup |
| 1487 | * sequence. (Specifically, this API was added to work around a |
| 1488 | * cosmetic bug in the "incoming call" sequence, where the lock screen |
| 1489 | * would flicker briefly before the incoming call UI became visible.) |
| 1490 | * TODO: There ought to be a more elegant way of doing this, |
| 1491 | * probably by having the PowerManager and ActivityManager |
| 1492 | * work together to let apps specify that the screen on/off |
| 1493 | * state should be synchronized with the Activity lifecycle. |
| 1494 | * <p> |
| 1495 | * Note that calling preventScreenOn(true) will NOT turn the screen |
| 1496 | * off if it's currently on. (This API only affects *future* |
| 1497 | * acquisitions of full wake locks.) |
| 1498 | * But calling preventScreenOn(false) WILL turn the screen on if |
| 1499 | * it's currently off because of a prior preventScreenOn(true) call. |
| 1500 | * <p> |
| 1501 | * Any call to preventScreenOn(true) MUST be followed promptly by a call |
| 1502 | * to preventScreenOn(false). In fact, if the preventScreenOn(false) |
| 1503 | * call doesn't occur within 5 seconds, we'll turn the screen back on |
| 1504 | * ourselves (and log a warning about it); this prevents a buggy app |
| 1505 | * from disabling the screen forever.) |
| 1506 | * <p> |
| 1507 | * TODO: this feature should really be controlled by a new type of poke |
| 1508 | * lock (rather than an IPowerManager call). |
| 1509 | */ |
| 1510 | public void preventScreenOn(boolean prevent) { |
| 1511 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1512 | |
| 1513 | synchronized (mLocks) { |
| 1514 | if (prevent) { |
| 1515 | // First of all, grab a partial wake lock to |
| 1516 | // make sure the CPU stays on during the entire |
| 1517 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1518 | mPreventScreenOnPartialLock.acquire(); |
| 1519 | |
| 1520 | // Post a forceReenableScreen() call (for 5 seconds in the |
| 1521 | // future) to make sure the matching preventScreenOn(false) call |
| 1522 | // has happened by then. |
| 1523 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1524 | mHandler.postDelayed(mForceReenableScreenTask, 5000); |
| 1525 | |
| 1526 | // Finally, set the flag that prevents the screen from turning on. |
| 1527 | // (Below, in setPowerState(), we'll check mPreventScreenOn and |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1528 | // we *won't* call setScreenStateLocked(true) if it's set.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | mPreventScreenOn = true; |
| 1530 | } else { |
| 1531 | // (Re)enable the screen. |
| 1532 | mPreventScreenOn = false; |
| 1533 | |
| 1534 | // We're "undoing" a the prior preventScreenOn(true) call, so we |
| 1535 | // no longer need the 5-second safeguard. |
| 1536 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1537 | |
| 1538 | // Forcibly turn on the screen if it's supposed to be on. (This |
| 1539 | // handles the case where the screen is currently off because of |
| 1540 | // a prior preventScreenOn(true) call.) |
Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 1541 | if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1543 | Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | "preventScreenOn: turning on after a prior preventScreenOn(true)!"); |
| 1545 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1546 | int err = setScreenStateLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | if (err != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1548 | Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | // Release the partial wake lock that we held during the |
| 1553 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1554 | mPreventScreenOnPartialLock.release(); |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | public void setScreenBrightnessOverride(int brightness) { |
| 1560 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1561 | |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1562 | if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | synchronized (mLocks) { |
| 1564 | if (mScreenBrightnessOverride != brightness) { |
| 1565 | mScreenBrightnessOverride = brightness; |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1566 | if (isScreenOn()) { |
| 1567 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1568 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1572 | |
| 1573 | public void setButtonBrightnessOverride(int brightness) { |
| 1574 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1575 | |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1576 | if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness); |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1577 | synchronized (mLocks) { |
| 1578 | if (mButtonBrightnessOverride != brightness) { |
| 1579 | mButtonBrightnessOverride = brightness; |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1580 | if (isScreenOn()) { |
| 1581 | updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT); |
| 1582 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | /** |
| 1588 | * Sanity-check that gets called 5 seconds after any call to |
| 1589 | * preventScreenOn(true). This ensures that the original call |
| 1590 | * is followed promptly by a call to preventScreenOn(false). |
| 1591 | */ |
| 1592 | private void forceReenableScreen() { |
| 1593 | // We shouldn't get here at all if mPreventScreenOn is false, since |
| 1594 | // we should have already removed any existing |
| 1595 | // mForceReenableScreenTask messages... |
| 1596 | if (!mPreventScreenOn) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1597 | Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | // Uh oh. It's been 5 seconds since a call to |
| 1602 | // preventScreenOn(true) and we haven't re-enabled the screen yet. |
| 1603 | // This means the app that called preventScreenOn(true) is either |
| 1604 | // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)), |
| 1605 | // or buggy (i.e. it forgot to call preventScreenOn(false), or |
| 1606 | // crashed before doing so.) |
| 1607 | |
| 1608 | // Log a warning, and forcibly turn the screen back on. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1609 | Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | + "Forcing the screen back on..."); |
| 1611 | preventScreenOn(false); |
| 1612 | } |
| 1613 | |
| 1614 | private Runnable mForceReenableScreenTask = new Runnable() { |
| 1615 | public void run() { |
| 1616 | forceReenableScreen(); |
| 1617 | } |
| 1618 | }; |
| 1619 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1620 | private int setScreenStateLocked(boolean on) { |
| 1621 | int err = Power.setScreenState(on); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1622 | if (err == 0) { |
| 1623 | mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0); |
| 1624 | if (mUseSoftwareAutoBrightness) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 1625 | enableLightSensorLocked(on); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1626 | if (!on) { |
| 1627 | // make sure button and key backlights are off too |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1628 | mButtonLight.turnOff(); |
| 1629 | mKeyboardLight.turnOff(); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1630 | // clear current value so we will update based on the new conditions |
| 1631 | // when the sensor is reenabled. |
| 1632 | mLightSensorValue = -1; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 1633 | // reset our highest light sensor value when the screen turns off |
| 1634 | mHighestLightSensorValue = -1; |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1635 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 1636 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1637 | } |
| 1638 | return err; |
| 1639 | } |
| 1640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | private void setPowerState(int state) |
| 1642 | { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1643 | setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1646 | private void setPowerState(int newState, boolean noChangeLights, int reason) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | { |
| 1648 | synchronized (mLocks) { |
| 1649 | int err; |
| 1650 | |
| 1651 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1652 | Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | + " newState=0x" + Integer.toHexString(newState) |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1654 | + " noChangeLights=" + noChangeLights |
| 1655 | + " reason=" + reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | if (noChangeLights) { |
| 1659 | newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK); |
| 1660 | } |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1661 | if (mProximitySensorActive) { |
| 1662 | // don't turn on the screen when the proximity sensor lock is held |
| 1663 | newState = (newState & ~SCREEN_BRIGHT); |
| 1664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | |
| 1666 | if (batteryIsLow()) { |
| 1667 | newState |= BATTERY_LOW_BIT; |
| 1668 | } else { |
| 1669 | newState &= ~BATTERY_LOW_BIT; |
| 1670 | } |
| 1671 | if (newState == mPowerState) { |
| 1672 | return; |
| 1673 | } |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 1674 | |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1675 | if (!mBootCompleted && !mUseSoftwareAutoBrightness) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | newState |= ALL_BRIGHT; |
| 1677 | } |
| 1678 | |
| 1679 | boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; |
| 1680 | boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; |
| 1681 | |
Mike Lockwood | 51b84496 | 2009-11-16 21:51:18 -0500 | [diff] [blame] | 1682 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1683 | Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | + " newState=" + newState + " noChangeLights=" + noChangeLights); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1685 | Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1687 | Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1689 | Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1691 | Slog.d(TAG, " oldScreenOn=" + oldScreenOn |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | + " newScreenOn=" + newScreenOn); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1693 | Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0)); |
| 1695 | } |
| 1696 | |
| 1697 | if (mPowerState != newState) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1698 | updateLightsLocked(newState, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK); |
| 1700 | } |
| 1701 | |
| 1702 | if (oldScreenOn != newScreenOn) { |
| 1703 | if (newScreenOn) { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1704 | // When the user presses the power button, we need to always send out the |
| 1705 | // notification that it's going to sleep so the keyguard goes on. But |
| 1706 | // we can't do that until the screen fades out, so we don't show the keyguard |
| 1707 | // too early. |
| 1708 | if (mStillNeedSleepNotification) { |
| 1709 | sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 1710 | } |
| 1711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | // Turn on the screen UNLESS there was a prior |
| 1713 | // preventScreenOn(true) request. (Note that the lifetime |
| 1714 | // of a single preventScreenOn() request is limited to 5 |
| 1715 | // seconds to prevent a buggy app from disabling the |
| 1716 | // screen forever; see forceReenableScreen().) |
| 1717 | boolean reallyTurnScreenOn = true; |
| 1718 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1719 | Slog.d(TAG, "- turning screen on... mPreventScreenOn = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | + mPreventScreenOn); |
| 1721 | } |
| 1722 | |
| 1723 | if (mPreventScreenOn) { |
| 1724 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1725 | Slog.d(TAG, "- PREVENTING screen from really turning on!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } |
| 1727 | reallyTurnScreenOn = false; |
| 1728 | } |
| 1729 | if (reallyTurnScreenOn) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1730 | err = setScreenStateLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | long identity = Binder.clearCallingIdentity(); |
| 1732 | try { |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1733 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | mBatteryStats.noteScreenOn(); |
| 1735 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1736 | Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1737 | } finally { |
| 1738 | Binder.restoreCallingIdentity(identity); |
| 1739 | } |
| 1740 | } else { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1741 | setScreenStateLocked(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1742 | // But continue as if we really did turn the screen on... |
| 1743 | err = 0; |
| 1744 | } |
| 1745 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | mLastTouchDown = 0; |
| 1747 | mTotalTouchDownTime = 0; |
| 1748 | mTouchCycles = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1749 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | mTotalTouchDownTime, mTouchCycles); |
| 1751 | if (err == 0) { |
| 1752 | mPowerState |= SCREEN_ON_BIT; |
| 1753 | sendNotificationLocked(true, -1); |
| 1754 | } |
| 1755 | } else { |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 1756 | // cancel light sensor task |
| 1757 | mHandler.removeCallbacks(mAutoBrightnessTask); |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1758 | mLightSensorPendingDecrease = false; |
| 1759 | mLightSensorPendingIncrease = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1761 | long identity = Binder.clearCallingIdentity(); |
| 1762 | try { |
| 1763 | mBatteryStats.noteScreenOff(); |
| 1764 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1765 | Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | } finally { |
| 1767 | Binder.restoreCallingIdentity(identity); |
| 1768 | } |
| 1769 | mPowerState &= ~SCREEN_ON_BIT; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1770 | mScreenOffReason = reason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | if (!mScreenBrightness.animating) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1772 | err = screenOffFinishedAnimatingLocked(reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | err = 0; |
| 1775 | mLastTouchDown = 0; |
| 1776 | } |
| 1777 | } |
| 1778 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1779 | |
| 1780 | updateNativePowerStateLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1783 | |
| 1784 | private void updateNativePowerStateLocked() { |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame^] | 1785 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 1786 | // Don't turn screen on if we are currently reporting a screen off. |
| 1787 | // This is to avoid letting the screen go on before things like the |
| 1788 | // lock screen have been displayed due to it going off. |
| 1789 | if (mBroadcastingScreenOff) { |
| 1790 | // Currently broadcasting that the screen is off. Don't |
| 1791 | // allow screen to go on until that is done. |
| 1792 | return; |
| 1793 | } |
| 1794 | for (int i=0; i<mBroadcastQueue.length; i++) { |
| 1795 | if (mBroadcastQueue[i] == 0) { |
| 1796 | // A screen off is currently enqueued. |
| 1797 | return; |
| 1798 | } |
| 1799 | } |
| 1800 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1801 | nativeSetPowerState( |
| 1802 | (mPowerState & SCREEN_ON_BIT) != 0, |
| 1803 | (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT); |
| 1804 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1805 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1806 | private int screenOffFinishedAnimatingLocked(int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | // I don't think we need to check the current state here because all of these |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1808 | // Power.setScreenState and sendNotificationLocked can both handle being |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | // called multiple times in the same state. -joeo |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1810 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, |
| 1811 | mTouchCycles); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | mLastTouchDown = 0; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1813 | int err = setScreenStateLocked(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | if (err == 0) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1815 | mScreenOffReason = reason; |
| 1816 | sendNotificationLocked(false, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | } |
| 1818 | return err; |
| 1819 | } |
| 1820 | |
| 1821 | private boolean batteryIsLow() { |
| 1822 | return (!mIsPowered && |
| 1823 | mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD); |
| 1824 | } |
| 1825 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1826 | private void updateLightsLocked(int newState, int forceState) { |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1827 | final int oldState = mPowerState; |
Joe Onorato | 60607a90 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1828 | if ((newState & SCREEN_ON_BIT) != 0) { |
| 1829 | // Only turn on the buttons or keyboard if the screen is also on. |
| 1830 | // We should never see the buttons on but not the screen. |
| 1831 | newState = applyButtonState(newState); |
| 1832 | newState = applyKeyboardState(newState); |
| 1833 | } |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1834 | final int realDifference = (newState ^ oldState); |
| 1835 | final int difference = realDifference | forceState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | if (difference == 0) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1837 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1839 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | int offMask = 0; |
| 1841 | int dimMask = 0; |
| 1842 | int onMask = 0; |
| 1843 | |
| 1844 | int preferredBrightness = getPreferredBrightness(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1845 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1847 | if ((newState & KEYBOARD_BRIGHT_BIT) == 0) { |
| 1848 | offMask |= KEYBOARD_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1849 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1850 | onMask |= KEYBOARD_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | if ((difference & BUTTON_BRIGHT_BIT) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1855 | if ((newState & BUTTON_BRIGHT_BIT) == 0) { |
| 1856 | offMask |= BUTTON_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1858 | onMask |= BUTTON_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1863 | int nominalCurrentValue = -1; |
| 1864 | // If there was an actual difference in the light state, then |
| 1865 | // figure out the "ideal" current value based on the previous |
| 1866 | // state. Otherwise, this is a change due to the brightness |
| 1867 | // override, so we want to animate from whatever the current |
| 1868 | // value is. |
| 1869 | if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| 1870 | switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { |
| 1871 | case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: |
| 1872 | nominalCurrentValue = preferredBrightness; |
| 1873 | break; |
| 1874 | case SCREEN_ON_BIT: |
| 1875 | nominalCurrentValue = Power.BRIGHTNESS_DIM; |
| 1876 | break; |
| 1877 | case 0: |
| 1878 | nominalCurrentValue = Power.BRIGHTNESS_OFF; |
| 1879 | break; |
| 1880 | case SCREEN_BRIGHT_BIT: |
| 1881 | default: |
| 1882 | // not possible |
| 1883 | nominalCurrentValue = (int)mScreenBrightness.curValue; |
| 1884 | break; |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1885 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1886 | } |
| 1887 | int brightness = preferredBrightness; |
| 1888 | int steps = ANIM_STEPS; |
| 1889 | if ((newState & SCREEN_BRIGHT_BIT) == 0) { |
| 1890 | // dim or turn off backlight, depending on if the screen is on |
| 1891 | // the scale is because the brightness ramp isn't linear and this biases |
| 1892 | // it so the later parts take longer. |
| 1893 | final float scale = 1.5f; |
| 1894 | float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness); |
| 1895 | if (ratio > 1.0f) ratio = 1.0f; |
| 1896 | if ((newState & SCREEN_ON_BIT) == 0) { |
| 1897 | if ((oldState & SCREEN_BRIGHT_BIT) != 0) { |
| 1898 | // was bright |
| 1899 | steps = ANIM_STEPS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1900 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1901 | // was dim |
| 1902 | steps = (int)(ANIM_STEPS*ratio*scale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1904 | brightness = Power.BRIGHTNESS_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1906 | if ((oldState & SCREEN_ON_BIT) != 0) { |
| 1907 | // was bright |
| 1908 | steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale); |
| 1909 | } else { |
| 1910 | // was dim |
| 1911 | steps = (int)(ANIM_STEPS*ratio); |
| 1912 | } |
| 1913 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 1914 | // If the "stay on while plugged in" option is |
| 1915 | // turned on, then the screen will often not |
| 1916 | // automatically turn off while plugged in. To |
| 1917 | // still have a sense of when it is inactive, we |
| 1918 | // will then count going dim as turning off. |
| 1919 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1920 | } |
| 1921 | brightness = Power.BRIGHTNESS_DIM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | } |
| 1923 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1924 | long identity = Binder.clearCallingIdentity(); |
| 1925 | try { |
| 1926 | mBatteryStats.noteScreenBrightness(brightness); |
| 1927 | } catch (RemoteException e) { |
| 1928 | // Nothing interesting to do. |
| 1929 | } finally { |
| 1930 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1931 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1932 | mScreenBrightness.setTargetLocked(brightness, steps, |
| 1933 | INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1934 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1935 | |
Joe Onorato | 60607a90 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1936 | if (mSpew) { |
| 1937 | Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask) |
| 1938 | + " dimMask=0x" + Integer.toHexString(dimMask) |
| 1939 | + " onMask=0x" + Integer.toHexString(onMask) |
| 1940 | + " difference=0x" + Integer.toHexString(difference) |
| 1941 | + " realDifference=0x" + Integer.toHexString(realDifference) |
| 1942 | + " forceState=0x" + Integer.toHexString(forceState) |
| 1943 | ); |
| 1944 | } |
| 1945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1946 | if (offMask != 0) { |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 1947 | if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1948 | setLightBrightness(offMask, Power.BRIGHTNESS_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1949 | } |
| 1950 | if (dimMask != 0) { |
| 1951 | int brightness = Power.BRIGHTNESS_DIM; |
| 1952 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 1953 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 1954 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 1955 | } |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 1956 | if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1957 | setLightBrightness(dimMask, brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1958 | } |
| 1959 | if (onMask != 0) { |
| 1960 | int brightness = getPreferredBrightness(); |
| 1961 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 1962 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 1963 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 1964 | } |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 1965 | if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1966 | setLightBrightness(onMask, brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1970 | private void setLightBrightness(int mask, int value) { |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 1971 | int brightnessMode = (mAutoBrightessEnabled |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1972 | ? LightsService.BRIGHTNESS_MODE_SENSOR |
| 1973 | : LightsService.BRIGHTNESS_MODE_USER); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1974 | if ((mask & SCREEN_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1975 | mLcdLight.setBrightness(value, brightnessMode); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1976 | } |
| 1977 | if ((mask & BUTTON_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1978 | mButtonLight.setBrightness(value); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1979 | } |
| 1980 | if ((mask & KEYBOARD_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1981 | mKeyboardLight.setBrightness(value); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1982 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1985 | class BrightnessState implements Runnable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1986 | final int mask; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | boolean initialized; |
| 1989 | int targetValue; |
| 1990 | float curValue; |
| 1991 | float delta; |
| 1992 | boolean animating; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1993 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | BrightnessState(int m) { |
| 1995 | mask = m; |
| 1996 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1997 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | public void dump(PrintWriter pw, String prefix) { |
| 1999 | pw.println(prefix + "animating=" + animating |
| 2000 | + " targetValue=" + targetValue |
| 2001 | + " curValue=" + curValue |
| 2002 | + " delta=" + delta); |
| 2003 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2004 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2005 | void setTargetLocked(int target, int stepsToTarget, int initialValue, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2006 | int nominalCurrentValue) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | if (!initialized) { |
| 2008 | initialized = true; |
| 2009 | curValue = (float)initialValue; |
Dianne Hackborn | aa80b60 | 2009-10-09 17:38:26 -0700 | [diff] [blame] | 2010 | } else if (targetValue == target) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2011 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2012 | } |
| 2013 | targetValue = target; |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2014 | delta = (targetValue - |
| 2015 | (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue)) |
| 2016 | / stepsToTarget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | if (mSpew) { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2018 | String noticeMe = nominalCurrentValue == curValue ? "" : " ******************"; |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2019 | Slog.i(TAG, "setTargetLocked mask=" + mask + " curValue=" + curValue |
| 2020 | + " target=" + target + " targetValue=" + targetValue + " delta=" + delta |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2021 | + " nominalCurrentValue=" + nominalCurrentValue |
| 2022 | + noticeMe); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | } |
| 2024 | animating = true; |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2025 | |
| 2026 | if (mSpew) { |
| 2027 | Slog.i(TAG, "scheduling light animator"); |
| 2028 | } |
| 2029 | mScreenOffHandler.removeCallbacks(this); |
| 2030 | mScreenOffHandler.post(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2032 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | boolean stepLocked() { |
| 2034 | if (!animating) return false; |
| 2035 | if (false && mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2036 | Slog.i(TAG, "Step target " + mask + ": cur=" + curValue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | + " target=" + targetValue + " delta=" + delta); |
| 2038 | } |
| 2039 | curValue += delta; |
| 2040 | int curIntValue = (int)curValue; |
| 2041 | boolean more = true; |
| 2042 | if (delta == 0) { |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2043 | curValue = curIntValue = targetValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | more = false; |
| 2045 | } else if (delta > 0) { |
| 2046 | if (curIntValue >= targetValue) { |
| 2047 | curValue = curIntValue = targetValue; |
| 2048 | more = false; |
| 2049 | } |
| 2050 | } else { |
| 2051 | if (curIntValue <= targetValue) { |
| 2052 | curValue = curIntValue = targetValue; |
| 2053 | more = false; |
| 2054 | } |
| 2055 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2056 | if (mSpew) Slog.d(TAG, "Animating curIntValue=" + curIntValue + ": " + mask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2057 | setLightBrightness(mask, curIntValue); |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2058 | finishAnimationLocked(more, curIntValue); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2059 | return more; |
| 2060 | } |
| 2061 | |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2062 | void jumpToTargetLocked() { |
| 2063 | if (mSpew) Slog.d(TAG, "jumpToTargetLocked targetValue=" + targetValue + ": " + mask); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2064 | setLightBrightness(mask, targetValue); |
| 2065 | final int tv = targetValue; |
| 2066 | curValue = tv; |
| 2067 | targetValue = -1; |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2068 | finishAnimationLocked(false, tv); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2071 | private void finishAnimationLocked(boolean more, int curIntValue) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | animating = more; |
| 2073 | if (!more) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2074 | if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2075 | screenOffFinishedAnimatingLocked(mScreenOffReason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | } |
| 2077 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2079 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2080 | public void run() { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2081 | if (mAnimateScreenLights) { |
| 2082 | synchronized (mLocks) { |
| 2083 | long now = SystemClock.uptimeMillis(); |
| 2084 | boolean more = mScreenBrightness.stepLocked(); |
| 2085 | if (more) { |
| 2086 | mScreenOffHandler.postAtTime(this, now+(1000/60)); |
| 2087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2089 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2090 | synchronized (mLocks) { |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2091 | // we're turning off |
| 2092 | final boolean animate = animating && targetValue == Power.BRIGHTNESS_OFF; |
| 2093 | if (animate) { |
| 2094 | // It's pretty scary to hold mLocks for this long, and we should |
| 2095 | // redesign this, but it works for now. |
| 2096 | nativeStartSurfaceFlingerAnimation( |
| 2097 | mScreenOffReason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 2098 | ? 0 : mAnimationSetting); |
| 2099 | } |
| 2100 | mScreenBrightness.jumpToTargetLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2101 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | private int getPreferredBrightness() { |
| 2107 | try { |
| 2108 | if (mScreenBrightnessOverride >= 0) { |
| 2109 | return mScreenBrightnessOverride; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2110 | } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness |
Mike Lockwood | 27c6dd7 | 2009-11-04 08:57:07 -0500 | [diff] [blame] | 2111 | && mAutoBrightessEnabled) { |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2112 | return mLightSensorScreenBrightness; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2113 | } |
| 2114 | final int brightness = Settings.System.getInt(mContext.getContentResolver(), |
| 2115 | SCREEN_BRIGHTNESS); |
| 2116 | // Don't let applications turn the screen all the way off |
| 2117 | return Math.max(brightness, Power.BRIGHTNESS_DIM); |
| 2118 | } catch (SettingNotFoundException snfe) { |
| 2119 | return Power.BRIGHTNESS_ON; |
| 2120 | } |
| 2121 | } |
| 2122 | |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2123 | private int applyButtonState(int state) { |
| 2124 | int brightness = -1; |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2125 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2126 | // do not override brightness if the battery is low |
| 2127 | return state; |
| 2128 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2129 | if (mButtonBrightnessOverride >= 0) { |
| 2130 | brightness = mButtonBrightnessOverride; |
| 2131 | } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2132 | brightness = mLightSensorButtonBrightness; |
| 2133 | } |
| 2134 | if (brightness > 0) { |
| 2135 | return state | BUTTON_BRIGHT_BIT; |
| 2136 | } else if (brightness == 0) { |
| 2137 | return state & ~BUTTON_BRIGHT_BIT; |
| 2138 | } else { |
| 2139 | return state; |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | private int applyKeyboardState(int state) { |
| 2144 | int brightness = -1; |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2145 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2146 | // do not override brightness if the battery is low |
| 2147 | return state; |
| 2148 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2149 | if (!mKeyboardVisible) { |
| 2150 | brightness = 0; |
| 2151 | } else if (mButtonBrightnessOverride >= 0) { |
| 2152 | brightness = mButtonBrightnessOverride; |
| 2153 | } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2154 | brightness = mLightSensorKeyboardBrightness; |
| 2155 | } |
| 2156 | if (brightness > 0) { |
| 2157 | return state | KEYBOARD_BRIGHT_BIT; |
| 2158 | } else if (brightness == 0) { |
| 2159 | return state & ~KEYBOARD_BRIGHT_BIT; |
| 2160 | } else { |
| 2161 | return state; |
| 2162 | } |
| 2163 | } |
| 2164 | |
Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2165 | public boolean isScreenOn() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | synchronized (mLocks) { |
| 2167 | return (mPowerState & SCREEN_ON_BIT) != 0; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2171 | boolean isScreenBright() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2172 | synchronized (mLocks) { |
| 2173 | return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT; |
| 2174 | } |
| 2175 | } |
| 2176 | |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2177 | private boolean isScreenTurningOffLocked() { |
| 2178 | return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0); |
| 2179 | } |
| 2180 | |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2181 | private boolean shouldLog(long time) { |
| 2182 | synchronized (mLocks) { |
| 2183 | if (time > (mWarningSpewThrottleTime + (60*60*1000))) { |
| 2184 | mWarningSpewThrottleTime = time; |
| 2185 | mWarningSpewThrottleCount = 0; |
| 2186 | return true; |
| 2187 | } else if (mWarningSpewThrottleCount < 30) { |
| 2188 | mWarningSpewThrottleCount++; |
| 2189 | return true; |
| 2190 | } else { |
| 2191 | return false; |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2196 | private void forceUserActivityLocked() { |
Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 2197 | if (isScreenTurningOffLocked()) { |
| 2198 | // cancel animation so userActivity will succeed |
| 2199 | mScreenBrightness.animating = false; |
| 2200 | } |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2201 | boolean savedActivityAllowed = mUserActivityAllowed; |
| 2202 | mUserActivityAllowed = true; |
| 2203 | userActivity(SystemClock.uptimeMillis(), false); |
| 2204 | mUserActivityAllowed = savedActivityAllowed; |
| 2205 | } |
| 2206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2207 | public void userActivityWithForce(long time, boolean noChangeLights, boolean force) { |
| 2208 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2209 | userActivity(time, -1, noChangeLights, OTHER_EVENT, force); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | public void userActivity(long time, boolean noChangeLights) { |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2213 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER) |
| 2214 | != PackageManager.PERMISSION_GRANTED) { |
| 2215 | if (shouldLog(time)) { |
| 2216 | Slog.w(TAG, "Caller does not have DEVICE_POWER permission. pid=" |
| 2217 | + Binder.getCallingPid() + " uid=" + Binder.getCallingUid()); |
| 2218 | } |
| 2219 | return; |
| 2220 | } |
| 2221 | |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2222 | userActivity(time, -1, noChangeLights, OTHER_EVENT, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | public void userActivity(long time, boolean noChangeLights, int eventType) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2226 | userActivity(time, -1, noChangeLights, eventType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2230 | userActivity(time, -1, noChangeLights, eventType, force); |
| 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | * Reset the user activity timeout to now + timeout. This overrides whatever else is going |
| 2235 | * on with user activity. Don't use this function. |
| 2236 | */ |
| 2237 | public void clearUserActivityTimeout(long now, long timeout) { |
| 2238 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2239 | Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now"); |
| 2240 | userActivity(now, timeout, false, OTHER_EVENT, false); |
| 2241 | } |
| 2242 | |
| 2243 | private void userActivity(long time, long timeoutOverride, boolean noChangeLights, |
| 2244 | int eventType, boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2245 | |
Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 2246 | if (((mPokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0) && (eventType == TOUCH_EVENT)) { |
Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2247 | if (false) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2248 | Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey)); |
Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2249 | } |
| 2250 | return; |
| 2251 | } |
| 2252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | synchronized (mLocks) { |
| 2254 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2255 | Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2256 | + " mUserActivityAllowed=" + mUserActivityAllowed |
| 2257 | + " mUserState=0x" + Integer.toHexString(mUserState) |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2258 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState) |
| 2259 | + " mProximitySensorActive=" + mProximitySensorActive |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 2260 | + " timeoutOverride=" + timeoutOverride |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2261 | + " force=" + force); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | } |
Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2263 | // ignore user activity if we are in the process of turning off the screen |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2264 | if (isScreenTurningOffLocked()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2265 | Slog.d(TAG, "ignoring user activity while turning off screen"); |
Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2266 | return; |
| 2267 | } |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 2268 | // Disable proximity sensor if if user presses power key while we are in the |
| 2269 | // "waiting for proximity sensor to go negative" state. |
| 2270 | if (mProximitySensorActive && mProximityWakeLockCount == 0) { |
| 2271 | mProximitySensorActive = false; |
| 2272 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2273 | if (mLastEventTime <= time || force) { |
| 2274 | mLastEventTime = time; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2275 | if ((mUserActivityAllowed && !mProximitySensorActive) || force) { |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2276 | // Only turn on button backlights if a button was pressed |
| 2277 | // and auto brightness is disabled |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2278 | if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2279 | mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 2280 | } else { |
| 2281 | // don't clear button/keyboard backlights when the screen is touched. |
| 2282 | mUserState |= SCREEN_BRIGHT; |
| 2283 | } |
| 2284 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2285 | int uid = Binder.getCallingUid(); |
| 2286 | long ident = Binder.clearCallingIdentity(); |
| 2287 | try { |
| 2288 | mBatteryStats.noteUserActivity(uid, eventType); |
| 2289 | } catch (RemoteException e) { |
| 2290 | // Ignore |
| 2291 | } finally { |
| 2292 | Binder.restoreCallingIdentity(ident); |
| 2293 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2294 | |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2295 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2296 | setPowerState(mUserState | mWakeLockState, noChangeLights, |
| 2297 | WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2298 | setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | } |
Mike Lockwood | ef73162 | 2010-01-27 17:51:34 -0500 | [diff] [blame] | 2302 | |
| 2303 | if (mPolicy != null) { |
| 2304 | mPolicy.userActivity(); |
| 2305 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2306 | } |
| 2307 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2308 | private int getAutoBrightnessValue(int sensorValue, int[] values) { |
| 2309 | try { |
| 2310 | int i; |
| 2311 | for (i = 0; i < mAutoBrightnessLevels.length; i++) { |
| 2312 | if (sensorValue < mAutoBrightnessLevels[i]) { |
| 2313 | break; |
| 2314 | } |
| 2315 | } |
| 2316 | return values[i]; |
| 2317 | } catch (Exception e) { |
| 2318 | // guard against null pointer or index out of bounds errors |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2319 | Slog.e(TAG, "getAutoBrightnessValue", e); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2320 | return 255; |
| 2321 | } |
| 2322 | } |
| 2323 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2324 | private Runnable mProximityTask = new Runnable() { |
| 2325 | public void run() { |
| 2326 | synchronized (mLocks) { |
| 2327 | if (mProximityPendingValue != -1) { |
| 2328 | proximityChangedLocked(mProximityPendingValue == 1); |
| 2329 | mProximityPendingValue = -1; |
| 2330 | } |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2331 | if (mProximityPartialLock.isHeld()) { |
| 2332 | mProximityPartialLock.release(); |
| 2333 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | }; |
| 2337 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2338 | private Runnable mAutoBrightnessTask = new Runnable() { |
| 2339 | public void run() { |
Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2340 | synchronized (mLocks) { |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 2341 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 2342 | int value = (int)mLightSensorPendingValue; |
| 2343 | mLightSensorPendingDecrease = false; |
| 2344 | mLightSensorPendingIncrease = false; |
Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2345 | lightSensorChangedLocked(value); |
| 2346 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | }; |
| 2350 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2351 | private void dockStateChanged(int state) { |
| 2352 | synchronized (mLocks) { |
| 2353 | mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 2354 | if (mIsDocked) { |
| 2355 | mHighestLightSensorValue = -1; |
| 2356 | } |
| 2357 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2358 | // force lights recalculation |
| 2359 | int value = (int)mLightSensorValue; |
| 2360 | mLightSensorValue = -1; |
| 2361 | lightSensorChangedLocked(value); |
| 2362 | } |
| 2363 | } |
| 2364 | } |
| 2365 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2366 | private void lightSensorChangedLocked(int value) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2367 | if (mDebugLightSensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2368 | Slog.d(TAG, "lightSensorChangedLocked " + value); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2369 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2370 | |
Joe Onorato | 06eb33a | 2010-10-25 14:09:21 -0700 | [diff] [blame] | 2371 | // Don't do anything if the screen is off. |
| 2372 | if ((mPowerState & SCREEN_ON_BIT) == 0) { |
| 2373 | if (mDebugLightSensor) { |
| 2374 | Slog.d(TAG, "dropping lightSensorChangedLocked because screen is off"); |
| 2375 | } |
| 2376 | return; |
| 2377 | } |
| 2378 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2379 | // do not allow light sensor value to decrease |
| 2380 | if (mHighestLightSensorValue < value) { |
| 2381 | mHighestLightSensorValue = value; |
| 2382 | } |
| 2383 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2384 | if (mLightSensorValue != value) { |
| 2385 | mLightSensorValue = value; |
| 2386 | if ((mPowerState & BATTERY_LOW_BIT) == 0) { |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2387 | // use maximum light sensor value seen since screen went on for LCD to avoid flicker |
| 2388 | // we only do this if we are undocked, since lighting should be stable when |
| 2389 | // stationary in a dock. |
| 2390 | int lcdValue = getAutoBrightnessValue( |
| 2391 | (mIsDocked ? value : mHighestLightSensorValue), |
| 2392 | mLcdBacklightValues); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2393 | int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues); |
Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2394 | int keyboardValue; |
| 2395 | if (mKeyboardVisible) { |
| 2396 | keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues); |
| 2397 | } else { |
| 2398 | keyboardValue = 0; |
| 2399 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2400 | mLightSensorScreenBrightness = lcdValue; |
| 2401 | mLightSensorButtonBrightness = buttonValue; |
| 2402 | mLightSensorKeyboardBrightness = keyboardValue; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2403 | |
| 2404 | if (mDebugLightSensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2405 | Slog.d(TAG, "lcdValue " + lcdValue); |
| 2406 | Slog.d(TAG, "buttonValue " + buttonValue); |
| 2407 | Slog.d(TAG, "keyboardValue " + keyboardValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2410 | if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2411 | mScreenBrightness.setTargetLocked(lcdValue, AUTOBRIGHTNESS_ANIM_STEPS, |
| 2412 | INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2413 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2414 | if (mButtonBrightnessOverride < 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2415 | mButtonLight.setBrightness(buttonValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2416 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2417 | if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2418 | mKeyboardLight.setBrightness(keyboardValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2422 | } |
| 2423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2424 | /** |
| 2425 | * The user requested that we go to sleep (probably with the power button). |
| 2426 | * This overrides all wake locks that are held. |
| 2427 | */ |
| 2428 | public void goToSleep(long time) |
| 2429 | { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2430 | goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * The user requested that we go to sleep (probably with the power button). |
| 2435 | * This overrides all wake locks that are held. |
| 2436 | */ |
| 2437 | public void goToSleepWithReason(long time, int reason) |
| 2438 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2440 | synchronized (mLocks) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2441 | goToSleepLocked(time, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2442 | } |
| 2443 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2445 | /** |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2446 | * Reboot the device immediately, passing 'reason' (may be null) |
| 2447 | * to the underlying __reboot system call. Should not return. |
| 2448 | */ |
| 2449 | public void reboot(String reason) |
| 2450 | { |
| 2451 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
San Mehat | 14e69af | 2010-01-06 14:58:18 -0800 | [diff] [blame] | 2452 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2453 | if (mHandler == null || !ActivityManagerNative.isSystemReady()) { |
| 2454 | throw new IllegalStateException("Too early to call reboot()"); |
| 2455 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2456 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2457 | final String finalReason = reason; |
| 2458 | Runnable runnable = new Runnable() { |
| 2459 | public void run() { |
| 2460 | synchronized (this) { |
| 2461 | ShutdownThread.reboot(mContext, finalReason, false); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2462 | } |
| 2463 | |
San Mehat | 1e51279 | 2010-01-07 10:40:29 -0800 | [diff] [blame] | 2464 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2465 | }; |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2466 | // ShutdownThread must run on a looper capable of displaying the UI. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2467 | mHandler.post(runnable); |
| 2468 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2469 | // PowerManager.reboot() is documented not to return so just wait for the inevitable. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2470 | synchronized (runnable) { |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2471 | while (true) { |
| 2472 | try { |
| 2473 | runnable.wait(); |
| 2474 | } catch (InterruptedException e) { |
| 2475 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2476 | } |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2480 | /** |
| 2481 | * Crash the runtime (causing a complete restart of the Android framework). |
| 2482 | * Requires REBOOT permission. Mostly for testing. Should not return. |
| 2483 | */ |
| 2484 | public void crash(final String message) |
| 2485 | { |
| 2486 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 2487 | Thread t = new Thread("PowerManagerService.crash()") { |
| 2488 | public void run() { throw new RuntimeException(message); } |
| 2489 | }; |
| 2490 | try { |
| 2491 | t.start(); |
| 2492 | t.join(); |
| 2493 | } catch (InterruptedException e) { |
| 2494 | Log.wtf(TAG, e); |
| 2495 | } |
| 2496 | } |
| 2497 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2498 | private void goToSleepLocked(long time, int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2499 | |
| 2500 | if (mLastEventTime <= time) { |
| 2501 | mLastEventTime = time; |
| 2502 | // cancel all of the wake locks |
| 2503 | mWakeLockState = SCREEN_OFF; |
| 2504 | int N = mLocks.size(); |
| 2505 | int numCleared = 0; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2506 | boolean proxLock = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2507 | for (int i=0; i<N; i++) { |
| 2508 | WakeLock wl = mLocks.get(i); |
| 2509 | if (isScreenLock(wl.flags)) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2510 | if (((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) |
| 2511 | && reason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) { |
| 2512 | proxLock = true; |
| 2513 | } else { |
| 2514 | mLocks.get(i).activated = false; |
| 2515 | numCleared++; |
| 2516 | } |
| 2517 | } |
| 2518 | } |
| 2519 | if (!proxLock) { |
| 2520 | mProxIgnoredBecauseScreenTurnedOff = true; |
| 2521 | if (mDebugProximitySensor) { |
| 2522 | Slog.d(TAG, "setting mProxIgnoredBecauseScreenTurnedOff"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2523 | } |
| 2524 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2525 | EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2526 | mStillNeedSleepNotification = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | mUserState = SCREEN_OFF; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2528 | setPowerState(SCREEN_OFF, false, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2529 | cancelTimerLocked(); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | public long timeSinceScreenOn() { |
| 2534 | synchronized (mLocks) { |
| 2535 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2536 | return 0; |
| 2537 | } |
| 2538 | return SystemClock.elapsedRealtime() - mScreenOffTime; |
| 2539 | } |
| 2540 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2541 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | public void setKeyboardVisibility(boolean visible) { |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2543 | synchronized (mLocks) { |
| 2544 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2545 | Slog.d(TAG, "setKeyboardVisibility: " + visible); |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2546 | } |
Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2547 | if (mKeyboardVisible != visible) { |
| 2548 | mKeyboardVisible = visible; |
| 2549 | // don't signal user activity if the screen is off; other code |
| 2550 | // will take care of turning on due to a true change to the lid |
| 2551 | // switch and synchronized with the lock screen. |
| 2552 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2553 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2554 | // force recompute of backlight values |
| 2555 | if (mLightSensorValue >= 0) { |
| 2556 | int value = (int)mLightSensorValue; |
| 2557 | mLightSensorValue = -1; |
| 2558 | lightSensorChangedLocked(value); |
| 2559 | } |
| 2560 | } |
Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2561 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2562 | } |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2563 | } |
| 2564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | /** |
| 2568 | * When the keyguard is up, it manages the power state, and userActivity doesn't do anything. |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2569 | * When disabling user activity we also reset user power state so the keyguard can reset its |
| 2570 | * short screen timeout when keyguard is unhidden. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2571 | */ |
| 2572 | public void enableUserActivity(boolean enabled) { |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2573 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2574 | Slog.d(TAG, "enableUserActivity " + enabled); |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | synchronized (mLocks) { |
| 2577 | mUserActivityAllowed = enabled; |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2578 | if (!enabled) { |
| 2579 | // cancel timeout and clear mUserState so the keyguard can set a short timeout |
| 2580 | setTimeoutLocked(SystemClock.uptimeMillis(), 0); |
| 2581 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2585 | private void setScreenBrightnessMode(int mode) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2586 | synchronized (mLocks) { |
| 2587 | boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC); |
| 2588 | if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) { |
| 2589 | mAutoBrightessEnabled = enabled; |
| 2590 | // This will get us a new value |
| 2591 | enableLightSensorLocked(mAutoBrightessEnabled && isScreenOn()); |
Mike Lockwood | 2d155d2 | 2009-10-27 09:32:30 -0400 | [diff] [blame] | 2592 | } |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2596 | /** Sets the screen off timeouts: |
| 2597 | * mKeylightDelay |
| 2598 | * mDimDelay |
| 2599 | * mScreenOffDelay |
| 2600 | * */ |
| 2601 | private void setScreenOffTimeoutsLocked() { |
| 2602 | if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) { |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2603 | mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | mDimDelay = -1; |
| 2605 | mScreenOffDelay = 0; |
| 2606 | } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) { |
| 2607 | mKeylightDelay = MEDIUM_KEYLIGHT_DELAY; |
| 2608 | mDimDelay = -1; |
| 2609 | mScreenOffDelay = 0; |
| 2610 | } else { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2611 | int totalDelay = mScreenOffTimeoutSetting; |
| 2612 | if (totalDelay > mMaximumScreenOffTimeout) { |
| 2613 | totalDelay = mMaximumScreenOffTimeout; |
| 2614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | mKeylightDelay = LONG_KEYLIGHT_DELAY; |
| 2616 | if (totalDelay < 0) { |
Jim Miller | bc4603b | 2010-08-30 21:21:34 -0700 | [diff] [blame] | 2617 | // negative number means stay on as long as possible. |
| 2618 | mScreenOffDelay = mMaximumScreenOffTimeout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | } else if (mKeylightDelay < totalDelay) { |
| 2620 | // subtract the time that the keylight delay. This will give us the |
| 2621 | // remainder of the time that we need to sleep to get the accurate |
| 2622 | // screen off timeout. |
| 2623 | mScreenOffDelay = totalDelay - mKeylightDelay; |
| 2624 | } else { |
| 2625 | mScreenOffDelay = 0; |
| 2626 | } |
| 2627 | if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) { |
| 2628 | mDimDelay = mScreenOffDelay - LONG_DIM_TIME; |
| 2629 | mScreenOffDelay = LONG_DIM_TIME; |
| 2630 | } else { |
| 2631 | mDimDelay = -1; |
| 2632 | } |
| 2633 | } |
| 2634 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2635 | Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2636 | + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay |
| 2637 | + " mDimScreen=" + mDimScreen); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | /** |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2642 | * Refreshes cached secure settings. Called once on startup, and |
| 2643 | * on subsequent changes to secure settings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2644 | */ |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2645 | private void updateSettingsValues() { |
| 2646 | mShortKeylightDelay = Settings.Secure.getInt( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2647 | mContext.getContentResolver(), |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2648 | Settings.Secure.SHORT_KEYLIGHT_DELAY_MS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2649 | SHORT_KEYLIGHT_DELAY_DEFAULT); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2650 | // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | private class LockList extends ArrayList<WakeLock> |
| 2654 | { |
| 2655 | void addLock(WakeLock wl) |
| 2656 | { |
| 2657 | int index = getIndex(wl.binder); |
| 2658 | if (index < 0) { |
| 2659 | this.add(wl); |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | WakeLock removeLock(IBinder binder) |
| 2664 | { |
| 2665 | int index = getIndex(binder); |
| 2666 | if (index >= 0) { |
| 2667 | return this.remove(index); |
| 2668 | } else { |
| 2669 | return null; |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | int getIndex(IBinder binder) |
| 2674 | { |
| 2675 | int N = this.size(); |
| 2676 | for (int i=0; i<N; i++) { |
| 2677 | if (this.get(i).binder == binder) { |
| 2678 | return i; |
| 2679 | } |
| 2680 | } |
| 2681 | return -1; |
| 2682 | } |
| 2683 | |
| 2684 | int gatherState() |
| 2685 | { |
| 2686 | int result = 0; |
| 2687 | int N = this.size(); |
| 2688 | for (int i=0; i<N; i++) { |
| 2689 | WakeLock wl = this.get(i); |
| 2690 | if (wl.activated) { |
| 2691 | if (isScreenLock(wl.flags)) { |
| 2692 | result |= wl.minState; |
| 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | return result; |
| 2697 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2698 | |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2699 | int reactivateScreenLocksLocked() |
| 2700 | { |
| 2701 | int result = 0; |
| 2702 | int N = this.size(); |
| 2703 | for (int i=0; i<N; i++) { |
| 2704 | WakeLock wl = this.get(i); |
| 2705 | if (isScreenLock(wl.flags)) { |
| 2706 | wl.activated = true; |
| 2707 | result |= wl.minState; |
| 2708 | } |
| 2709 | } |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2710 | if (mDebugProximitySensor) { |
| 2711 | Slog.d(TAG, "reactivateScreenLocksLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 2712 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2713 | } |
| 2714 | mProxIgnoredBecauseScreenTurnedOff = false; |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2715 | return result; |
| 2716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2717 | } |
| 2718 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 2719 | public void setPolicy(WindowManagerPolicy p) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | synchronized (mLocks) { |
| 2721 | mPolicy = p; |
| 2722 | mLocks.notifyAll(); |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | WindowManagerPolicy getPolicyLocked() { |
| 2727 | while (mPolicy == null || !mDoneBooting) { |
| 2728 | try { |
| 2729 | mLocks.wait(); |
| 2730 | } catch (InterruptedException e) { |
| 2731 | // Ignore |
| 2732 | } |
| 2733 | } |
| 2734 | return mPolicy; |
| 2735 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2736 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | void systemReady() { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2738 | mSensorManager = new SensorManager(mHandlerThread.getLooper()); |
| 2739 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 2740 | // don't bother with the light sensor if auto brightness is handled in hardware |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 2741 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2742 | mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2743 | } |
| 2744 | |
Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2745 | // wait until sensors are enabled before turning on screen. |
| 2746 | // some devices will not activate the light sensor properly on boot |
| 2747 | // unless we do this. |
| 2748 | if (mUseSoftwareAutoBrightness) { |
| 2749 | // turn the screen on |
| 2750 | setPowerState(SCREEN_BRIGHT); |
| 2751 | } else { |
| 2752 | // turn everything on |
| 2753 | setPowerState(ALL_BRIGHT); |
| 2754 | } |
| 2755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | synchronized (mLocks) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2757 | Slog.d(TAG, "system ready!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2758 | mDoneBooting = true; |
Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2759 | |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2760 | enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled); |
| 2761 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2762 | long identity = Binder.clearCallingIdentity(); |
| 2763 | try { |
| 2764 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
| 2765 | mBatteryStats.noteScreenOn(); |
| 2766 | } catch (RemoteException e) { |
| 2767 | // Nothing interesting to do. |
| 2768 | } finally { |
| 2769 | Binder.restoreCallingIdentity(identity); |
| 2770 | } |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | void bootCompleted() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2775 | Slog.d(TAG, "bootCompleted"); |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2776 | synchronized (mLocks) { |
| 2777 | mBootCompleted = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2778 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2779 | updateWakeLockLocked(); |
| 2780 | mLocks.notifyAll(); |
| 2781 | } |
| 2782 | } |
| 2783 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2784 | // for watchdog |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | public void monitor() { |
| 2786 | synchronized (mLocks) { } |
| 2787 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2788 | |
| 2789 | public int getSupportedWakeLockFlags() { |
| 2790 | int result = PowerManager.PARTIAL_WAKE_LOCK |
| 2791 | | PowerManager.FULL_WAKE_LOCK |
| 2792 | | PowerManager.SCREEN_DIM_WAKE_LOCK; |
| 2793 | |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2794 | if (mProximitySensor != null) { |
| 2795 | result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| 2796 | } |
| 2797 | |
| 2798 | return result; |
| 2799 | } |
| 2800 | |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2801 | public void setBacklightBrightness(int brightness) { |
| 2802 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2803 | // Don't let applications turn the screen all the way off |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2804 | synchronized (mLocks) { |
| 2805 | brightness = Math.max(brightness, Power.BRIGHTNESS_DIM); |
| 2806 | mLcdLight.setBrightness(brightness); |
| 2807 | mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0); |
| 2808 | mButtonLight.setBrightness(brightness); |
| 2809 | long identity = Binder.clearCallingIdentity(); |
| 2810 | try { |
| 2811 | mBatteryStats.noteScreenBrightness(brightness); |
| 2812 | } catch (RemoteException e) { |
| 2813 | Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e); |
| 2814 | } finally { |
| 2815 | Binder.restoreCallingIdentity(identity); |
| 2816 | } |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2817 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2818 | // update our animation state |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2819 | synchronized (mLocks) { |
| 2820 | mScreenBrightness.targetValue = brightness; |
| 2821 | mScreenBrightness.jumpToTargetLocked(); |
| 2822 | } |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 2826 | public void setAttentionLight(boolean on, int color) { |
| 2827 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2828 | mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0); |
Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 2829 | } |
| 2830 | |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2831 | private void enableProximityLockLocked() { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2832 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2833 | Slog.d(TAG, "enableProximityLockLocked"); |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2834 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2835 | if (!mProximitySensorEnabled) { |
| 2836 | // clear calling identity so sensor manager battery stats are accurate |
| 2837 | long identity = Binder.clearCallingIdentity(); |
| 2838 | try { |
| 2839 | mSensorManager.registerListener(mProximityListener, mProximitySensor, |
| 2840 | SensorManager.SENSOR_DELAY_NORMAL); |
| 2841 | mProximitySensorEnabled = true; |
| 2842 | } finally { |
| 2843 | Binder.restoreCallingIdentity(identity); |
| 2844 | } |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2845 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | private void disableProximityLockLocked() { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2849 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2850 | Slog.d(TAG, "disableProximityLockLocked"); |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2851 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2852 | if (mProximitySensorEnabled) { |
| 2853 | // clear calling identity so sensor manager battery stats are accurate |
| 2854 | long identity = Binder.clearCallingIdentity(); |
| 2855 | try { |
| 2856 | mSensorManager.unregisterListener(mProximityListener); |
| 2857 | mHandler.removeCallbacks(mProximityTask); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2858 | if (mProximityPartialLock.isHeld()) { |
| 2859 | mProximityPartialLock.release(); |
| 2860 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2861 | mProximitySensorEnabled = false; |
| 2862 | } finally { |
| 2863 | Binder.restoreCallingIdentity(identity); |
| 2864 | } |
| 2865 | if (mProximitySensorActive) { |
| 2866 | mProximitySensorActive = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2867 | if (mDebugProximitySensor) { |
| 2868 | Slog.d(TAG, "disableProximityLockLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 2869 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2870 | } |
| 2871 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 2872 | forceUserActivityLocked(); |
| 2873 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2874 | } |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2875 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2878 | private void proximityChangedLocked(boolean active) { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2879 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2880 | Slog.d(TAG, "proximityChangedLocked, active: " + active); |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2881 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2882 | if (!mProximitySensorEnabled) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2883 | Slog.d(TAG, "Ignoring proximity change after sensor is disabled"); |
Mike Lockwood | 0d72f7e | 2009-11-05 20:53:00 -0500 | [diff] [blame] | 2884 | return; |
| 2885 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2886 | if (active) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2887 | if (mDebugProximitySensor) { |
| 2888 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 2889 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2890 | } |
| 2891 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 2892 | goToSleepLocked(SystemClock.uptimeMillis(), |
| 2893 | WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR); |
| 2894 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2895 | mProximitySensorActive = true; |
| 2896 | } else { |
| 2897 | // proximity sensor negative events trigger as user activity. |
| 2898 | // temporarily set mUserActivityAllowed to true so this will work |
| 2899 | // even when the keyguard is on. |
| 2900 | mProximitySensorActive = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2901 | if (mDebugProximitySensor) { |
| 2902 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 2903 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2904 | } |
| 2905 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 2906 | forceUserActivityLocked(); |
| 2907 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2908 | |
| 2909 | if (mProximityWakeLockCount == 0) { |
| 2910 | // disable sensor if we have no listeners left after proximity negative |
| 2911 | disableProximityLockLocked(); |
| 2912 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2913 | } |
| 2914 | } |
| 2915 | |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2916 | private void enableLightSensorLocked(boolean enable) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2917 | if (mDebugLightSensor) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2918 | Slog.d(TAG, "enableLightSensorLocked enable=" + enable |
| 2919 | + " mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 2920 | } |
| 2921 | if (!mAutoBrightessEnabled) { |
| 2922 | enable = false; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2923 | } |
| 2924 | if (mSensorManager != null && mLightSensorEnabled != enable) { |
| 2925 | mLightSensorEnabled = enable; |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2926 | // clear calling identity so sensor manager battery stats are accurate |
| 2927 | long identity = Binder.clearCallingIdentity(); |
| 2928 | try { |
| 2929 | if (enable) { |
| 2930 | mSensorManager.registerListener(mLightListener, mLightSensor, |
| 2931 | SensorManager.SENSOR_DELAY_NORMAL); |
| 2932 | } else { |
| 2933 | mSensorManager.unregisterListener(mLightListener); |
| 2934 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 2935 | } |
| 2936 | } finally { |
| 2937 | Binder.restoreCallingIdentity(identity); |
Mike Lockwood | 06952d9 | 2009-08-13 16:05:38 -0400 | [diff] [blame] | 2938 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2942 | SensorEventListener mProximityListener = new SensorEventListener() { |
| 2943 | public void onSensorChanged(SensorEvent event) { |
Mike Lockwood | ba8eb1e | 2009-11-08 19:31:18 -0500 | [diff] [blame] | 2944 | long milliseconds = SystemClock.elapsedRealtime(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2945 | synchronized (mLocks) { |
| 2946 | float distance = event.values[0]; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2947 | long timeSinceLastEvent = milliseconds - mLastProximityEventTime; |
| 2948 | mLastProximityEventTime = milliseconds; |
| 2949 | mHandler.removeCallbacks(mProximityTask); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2950 | boolean proximityTaskQueued = false; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2951 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2952 | // compare against getMaximumRange to support sensors that only return 0 or 1 |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2953 | boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && |
| 2954 | distance < mProximitySensor.getMaximumRange()); |
| 2955 | |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2956 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2957 | Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2958 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2959 | if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) { |
| 2960 | // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing |
| 2961 | mProximityPendingValue = (active ? 1 : 0); |
| 2962 | mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2963 | proximityTaskQueued = true; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2964 | } else { |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2965 | // process the value immediately |
| 2966 | mProximityPendingValue = -1; |
| 2967 | proximityChangedLocked(active); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2968 | } |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2969 | |
| 2970 | // update mProximityPartialLock state |
| 2971 | boolean held = mProximityPartialLock.isHeld(); |
| 2972 | if (!held && proximityTaskQueued) { |
| 2973 | // hold wakelock until mProximityTask runs |
| 2974 | mProximityPartialLock.acquire(); |
| 2975 | } else if (held && !proximityTaskQueued) { |
| 2976 | mProximityPartialLock.release(); |
| 2977 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 2982 | // ignore |
| 2983 | } |
| 2984 | }; |
| 2985 | |
| 2986 | SensorEventListener mLightListener = new SensorEventListener() { |
| 2987 | public void onSensorChanged(SensorEvent event) { |
| 2988 | synchronized (mLocks) { |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2989 | // ignore light sensor while screen is turning off |
| 2990 | if (isScreenTurningOffLocked()) { |
| 2991 | return; |
| 2992 | } |
| 2993 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2994 | int value = (int)event.values[0]; |
Mike Lockwood | ba8eb1e | 2009-11-08 19:31:18 -0500 | [diff] [blame] | 2995 | long milliseconds = SystemClock.elapsedRealtime(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2996 | if (mDebugLightSensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2997 | Slog.d(TAG, "onSensorChanged: light value: " + value); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2998 | } |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 2999 | if (mLightSensorValue == -1 || |
| 3000 | milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) { |
| 3001 | // process the value immediately if screen has just turned on |
| 3002 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3003 | mLightSensorPendingDecrease = false; |
| 3004 | mLightSensorPendingIncrease = false; |
| 3005 | lightSensorChangedLocked(value); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 3006 | } else { |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 3007 | if ((value > mLightSensorValue && mLightSensorPendingDecrease) || |
| 3008 | (value < mLightSensorValue && mLightSensorPendingIncrease) || |
| 3009 | (value == mLightSensorValue) || |
| 3010 | (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) { |
| 3011 | // delay processing to debounce the sensor |
| 3012 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3013 | mLightSensorPendingDecrease = (value < mLightSensorValue); |
| 3014 | mLightSensorPendingIncrease = (value > mLightSensorValue); |
| 3015 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 3016 | mLightSensorPendingValue = value; |
| 3017 | mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY); |
| 3018 | } |
| 3019 | } else { |
| 3020 | mLightSensorPendingValue = value; |
| 3021 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 3022 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 3027 | // ignore |
| 3028 | } |
| 3029 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3030 | } |