blob: 7974951e4e83d7c6b67ba9cb719862563b063730 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import com.android.internal.app.IBatteryStats;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080020import com.android.internal.app.ShutdownThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.server.am.BatteryStatsService;
22
23import android.app.ActivityManagerNative;
24import android.app.IActivityManager;
25import android.content.BroadcastReceiver;
26import android.content.ContentQueryMap;
27import android.content.ContentResolver;
Amith Yamasani8b619832010-09-22 16:11:59 -070028import android.content.ContentValues;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070033import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080034import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.database.Cursor;
Mike Lockwoodbc706a02009-07-27 13:50:57 -070036import android.hardware.Sensor;
37import android.hardware.SensorEvent;
38import android.hardware.SensorEventListener;
39import android.hardware.SensorManager;
Amith Yamasani8b619832010-09-22 16:11:59 -070040import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.BatteryStats;
42import android.os.Binder;
43import android.os.Handler;
44import android.os.HandlerThread;
45import android.os.IBinder;
46import android.os.IPowerManager;
47import android.os.LocalPowerManager;
48import android.os.Power;
49import android.os.PowerManager;
50import android.os.Process;
51import android.os.RemoteException;
52import android.os.SystemClock;
Mike Lockwood3a74bd32011-08-12 13:55:22 -070053import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070054import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.provider.Settings;
56import android.util.EventLog;
57import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.WindowManagerPolicy;
60import static android.provider.Settings.System.DIM_SCREEN;
61import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
Dan Murphy951764b2009-08-27 14:59:03 -050062import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -080063import static android.provider.Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ;
Mike Lockwooddc3494e2009-10-14 21:17:09 -070064import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
66import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
Joe Onorato609695d2010-10-14 14:57:49 -070067import static android.provider.Settings.System.WINDOW_ANIMATION_SCALE;
68import static android.provider.Settings.System.TRANSITION_ANIMATION_SCALE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70import java.io.FileDescriptor;
71import java.io.PrintWriter;
72import java.util.ArrayList;
73import java.util.HashMap;
74import java.util.Observable;
75import java.util.Observer;
76
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080077public class PowerManagerService extends IPowerManager.Stub
Mike Lockwood8738e0c2009-10-04 08:44:47 -040078 implements LocalPowerManager, Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 private static final String TAG = "PowerManagerService";
81 static final String PARTIAL_NAME = "PowerManagerService";
82
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -070083 static final boolean DEBUG_SCREEN_ON = false;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final boolean LOG_PARTIAL_WL = false;
86
87 // Indicates whether touch-down cycles should be logged as part of the
88 // LOG_POWER_SCREEN_STATE log events
89 private static final boolean LOG_TOUCH_DOWNS = true;
90
91 private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK
92 | PowerManager.SCREEN_DIM_WAKE_LOCK
93 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Mike Lockwoodbc706a02009-07-27 13:50:57 -070094 | PowerManager.FULL_WAKE_LOCK
95 | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 // time since last state: time since last event:
Doug Zongker43866e02010-01-07 12:09:54 -080098 // The short keylight delay comes from secure settings; this is the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
100 private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec
101 private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec
102 private static final int LONG_DIM_TIME = 7000; // t+N-5 sec
103
Mathias Agopian47f1fe52011-11-08 17:18:41 -0800104 // How long to wait to debounce light sensor changes in milliseconds
Mike Lockwood9b8136922009-11-06 15:53:59 -0500105 private static final int LIGHT_SENSOR_DELAY = 2000;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700106
Mathias Agopian47f1fe52011-11-08 17:18:41 -0800107 // light sensor events rate in microseconds
108 private static final int LIGHT_SENSOR_RATE = 1000000;
109
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800110 // Expansion of range of light values when applying scale from light
111 // sensor brightness setting, in the [0..255] brightness range.
112 private static final int LIGHT_SENSOR_RANGE_EXPANSION = 20;
113
114 // Scaling factor of the light sensor brightness setting when applying
115 // it to the final brightness.
116 private static final int LIGHT_SENSOR_OFFSET_SCALE = 8;
117
Mathias Agopian47f1fe52011-11-08 17:18:41 -0800118 // For debouncing the proximity sensor in milliseconds
Mike Lockwood20f87d72009-11-05 16:08:51 -0500119 private static final int PROXIMITY_SENSOR_DELAY = 1000;
120
Mike Lockwoodd20ea362009-09-15 00:13:38 -0400121 // trigger proximity if distance is less than 5 cm
122 private static final float PROXIMITY_THRESHOLD = 5.0f;
123
Doug Zongker43866e02010-01-07 12:09:54 -0800124 // Cached secure settings; see updateSettingsValues()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
126
Amith Yamasani8b619832010-09-22 16:11:59 -0700127 // Default timeout for screen off, if not found in settings database = 15 seconds.
128 private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15000;
129
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800130 // Screen brightness should always have a value, but just in case...
131 private static final int DEFAULT_SCREEN_BRIGHTNESS = 192;
132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 // flags for setPowerState
134 private static final int SCREEN_ON_BIT = 0x00000001;
135 private static final int SCREEN_BRIGHT_BIT = 0x00000002;
136 private static final int BUTTON_BRIGHT_BIT = 0x00000004;
137 private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
138 private static final int BATTERY_LOW_BIT = 0x00000010;
139
140 // values for setPowerState
141
142 // SCREEN_OFF == everything off
143 private static final int SCREEN_OFF = 0x00000000;
144
145 // SCREEN_DIM == screen on, screen backlight dim
146 private static final int SCREEN_DIM = SCREEN_ON_BIT;
147
148 // SCREEN_BRIGHT == screen on, screen backlight bright
149 private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
150
151 // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
152 private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
153
154 // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
155 private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
156
157 // used for noChangeLights in setPowerState()
158 private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
159
Joe Onoratob08a1af2010-10-11 19:28:58 -0700160 boolean mAnimateScreenLights = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int ANIM_STEPS = 60/4;
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400163 // Slower animation for autobrightness changes
164 static final int AUTOBRIGHTNESS_ANIM_STEPS = 60;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800165 // Number of steps when performing a more immediate brightness change.
166 static final int IMMEDIATE_ANIM_STEPS = 4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 // These magic numbers are the initial state of the LEDs at boot. Ideally
169 // we should read them from the driver, but our current hardware returns 0
170 // for the initial value. Oops!
171 static final int INITIAL_SCREEN_BRIGHTNESS = 255;
172 static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF;
173 static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private final int MY_UID;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700176 private final int MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 private boolean mDoneBooting = false;
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500179 private boolean mBootCompleted = false;
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700180 private boolean mHeadless = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 private int mStayOnConditions = 0;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500182 private final int[] mBroadcastQueue = new int[] { -1, -1, -1 };
183 private final int[] mBroadcastWhy = new int[3];
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700184 private boolean mPreparingForScreenOn = false;
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -0700185 private boolean mSkippedScreenOn = false;
186 private boolean mInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 private int mPartialCount = 0;
188 private int mPowerState;
Mike Lockwood435eb642009-12-03 08:40:18 -0500189 // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER,
190 // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
191 private int mScreenOffReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 private int mUserState;
193 private boolean mKeyboardVisible = false;
194 private boolean mUserActivityAllowed = true;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500195 private int mProximityWakeLockCount = 0;
196 private boolean mProximitySensorEnabled = false;
Mike Lockwood36fc3022009-08-25 16:49:06 -0700197 private boolean mProximitySensorActive = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -0500198 private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active
199 private long mLastProximityEventTime;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800200 private int mScreenOffTimeoutSetting;
201 private int mMaximumScreenOffTimeout = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private int mKeylightDelay;
203 private int mDimDelay;
204 private int mScreenOffDelay;
205 private int mWakeLockState;
206 private long mLastEventTime = 0;
207 private long mScreenOffTime;
208 private volatile WindowManagerPolicy mPolicy;
209 private final LockList mLocks = new LockList();
210 private Intent mScreenOffIntent;
211 private Intent mScreenOnIntent;
Mike Lockwood3a322132009-11-24 00:30:52 -0500212 private LightsService mLightsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 private Context mContext;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500214 private LightsService.Light mLcdLight;
215 private LightsService.Light mButtonLight;
216 private LightsService.Light mKeyboardLight;
217 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private UnsynchronizedWakeLock mBroadcastWakeLock;
219 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
220 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
221 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500222 private UnsynchronizedWakeLock mProximityPartialLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 private HandlerThread mHandlerThread;
Joe Onoratob08a1af2010-10-11 19:28:58 -0700224 private HandlerThread mScreenOffThread;
225 private Handler mScreenOffHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 private Handler mHandler;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500227 private final TimeoutTask mTimeoutTask = new TimeoutTask();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 private final BrightnessState mScreenBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700229 = new BrightnessState(SCREEN_BRIGHT_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -0700230 private boolean mStillNeedSleepNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 private boolean mIsPowered = false;
232 private IActivityManager mActivityService;
233 private IBatteryStats mBatteryStats;
234 private BatteryService mBatteryService;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700235 private SensorManager mSensorManager;
236 private Sensor mProximitySensor;
Mike Lockwood8738e0c2009-10-04 08:44:47 -0400237 private Sensor mLightSensor;
238 private boolean mLightSensorEnabled;
239 private float mLightSensorValue = -1;
Joe Onorato8274a0e2010-10-05 17:38:09 -0400240 private boolean mProxIgnoredBecauseScreenTurnedOff = false;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500241 private int mHighestLightSensorValue = -1;
Jim Rodovichd102fea2010-09-02 12:30:49 -0500242 private boolean mLightSensorPendingDecrease = false;
243 private boolean mLightSensorPendingIncrease = false;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700244 private float mLightSensorPendingValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800245 private float mLightSensorAdjustSetting = 0;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500246 private int mLightSensorScreenBrightness = -1;
247 private int mLightSensorButtonBrightness = -1;
248 private int mLightSensorKeyboardBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 private boolean mDimScreen = true;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500250 private boolean mIsDocked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 private long mNextTimeout;
252 private volatile int mPokey = 0;
253 private volatile boolean mPokeAwakeOnSet = false;
254 private volatile boolean mInitComplete = false;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500255 private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500256 // mLastScreenOnTime is the time the screen was last turned on
257 private long mLastScreenOnTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 private boolean mPreventScreenOn;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800259 private int mScreenBrightnessSetting = DEFAULT_SCREEN_BRIGHTNESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 private int mScreenBrightnessOverride = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500261 private int mButtonBrightnessOverride = -1;
Mike Lockwoodeb6456b2011-09-13 15:24:02 -0400262 private int mScreenBrightnessDim;
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400263 private boolean mUseSoftwareAutoBrightness;
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700264 private boolean mAutoBrightessEnabled;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700265 private int[] mAutoBrightnessLevels;
266 private int[] mLcdBacklightValues;
267 private int[] mButtonBacklightValues;
268 private int[] mKeyboardBacklightValues;
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500269 private int mLightSensorWarmupTime;
Joe Onorato6d747652010-10-11 15:15:31 -0700270 boolean mUnplugTurnsOnScreen;
Joe Onorato4b9f62d2010-10-11 13:41:35 -0700271 private int mWarningSpewThrottleCount;
272 private long mWarningSpewThrottleTime;
Joe Onorato609695d2010-10-14 14:57:49 -0700273 private int mAnimationSetting = ANIM_SETTING_OFF;
274
275 // Must match with the ISurfaceComposer constants in C++.
276 private static final int ANIM_SETTING_ON = 0x01;
277 private static final int ANIM_SETTING_OFF = 0x10;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
279 // Used when logging number and duration of touch-down cycles
280 private long mTotalTouchDownTime;
281 private long mLastTouchDown;
282 private int mTouchCycles;
283
284 // could be either static or controllable at runtime
285 private static final boolean mSpew = false;
Joe Onorato8274a0e2010-10-05 17:38:09 -0400286 private static final boolean mDebugProximitySensor = (false || mSpew);
Mike Lockwoodae92eb32011-10-25 10:11:46 -0400287 private static final boolean mDebugLightSensor = (false || mSpew);
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700288
289 private native void nativeInit();
290 private native void nativeSetPowerState(boolean screenOn, boolean screenBright);
Joe Onorato609695d2010-10-14 14:57:49 -0700291 private native void nativeStartSurfaceFlingerAnimation(int mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 /*
294 static PrintStream mLog;
295 static {
296 try {
297 mLog = new PrintStream("/data/power.log");
298 }
299 catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800300 android.util.Slog.e(TAG, "Life is hard", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 }
302 }
303 static class Log {
304 static void d(String tag, String s) {
305 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800306 android.util.Slog.d(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 static void i(String tag, String s) {
309 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800310 android.util.Slog.i(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 static void w(String tag, String s) {
313 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800314 android.util.Slog.w(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 static void e(String tag, String s) {
317 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800318 android.util.Slog.e(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320 }
321 */
322
323 /**
324 * This class works around a deadlock between the lock in PowerManager.WakeLock
325 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
326 * mToken object so it can be accessed from any thread, but it calls into here
327 * with its lock held. This class is essentially a reimplementation of
328 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
329 * only call it with our own locks held.
330 */
331 private class UnsynchronizedWakeLock {
332 int mFlags;
333 String mTag;
334 IBinder mToken;
335 int mCount = 0;
336 boolean mRefCounted;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500337 boolean mHeld;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
339 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
340 mFlags = flags;
341 mTag = tag;
342 mToken = new Binder();
343 mRefCounted = refCounted;
344 }
345
346 public void acquire() {
347 if (!mRefCounted || mCount++ == 0) {
348 long ident = Binder.clearCallingIdentity();
349 try {
350 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700351 MY_UID, MY_PID, mTag, null);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500352 mHeld = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 } finally {
354 Binder.restoreCallingIdentity(ident);
355 }
356 }
357 }
358
359 public void release() {
360 if (!mRefCounted || --mCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500361 PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500362 mHeld = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 if (mCount < 0) {
365 throw new RuntimeException("WakeLock under-locked " + mTag);
366 }
367 }
368
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500369 public boolean isHeld()
370 {
371 return mHeld;
372 }
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public String toString() {
375 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500376 + " mCount=" + mCount + " mHeld=" + mHeld + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379
380 private final class BatteryReceiver extends BroadcastReceiver {
381 @Override
382 public void onReceive(Context context, Intent intent) {
383 synchronized (mLocks) {
384 boolean wasPowered = mIsPowered;
385 mIsPowered = mBatteryService.isPowered();
386
387 if (mIsPowered != wasPowered) {
388 // update mStayOnWhilePluggedIn wake lock
389 updateWakeLockLocked();
390
391 // treat plugging and unplugging the devices as a user activity.
392 // users find it disconcerting when they unplug the device
393 // and it shuts off right away.
Mike Lockwood84a89342010-03-01 21:28:58 -0500394 // to avoid turning on the screen when unplugging, we only trigger
395 // user activity when screen was already on.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 // temporarily set mUserActivityAllowed to true so this will work
397 // even when the keyguard is on.
Joe Onorato6d747652010-10-11 15:15:31 -0700398 // However, you can also set config_unplugTurnsOnScreen to have it
399 // turn on. Some devices want this because they don't have a
400 // charging LED.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 synchronized (mLocks) {
Joe Onorato6d747652010-10-11 15:15:31 -0700402 if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0 ||
403 mUnplugTurnsOnScreen) {
Mike Lockwood84a89342010-03-01 21:28:58 -0500404 forceUserActivityLocked();
405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 }
408 }
409 }
410 }
411
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500412 private final class BootCompletedReceiver extends BroadcastReceiver {
413 @Override
414 public void onReceive(Context context, Intent intent) {
415 bootCompleted();
416 }
417 }
418
Mike Lockwoodb2865412010-02-02 22:40:33 -0500419 private final class DockReceiver extends BroadcastReceiver {
420 @Override
421 public void onReceive(Context context, Intent intent) {
422 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
423 Intent.EXTRA_DOCK_STATE_UNDOCKED);
424 dockStateChanged(state);
425 }
426 }
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
429 * Set the setting that determines whether the device stays on when plugged in.
430 * The argument is a bit string, with each bit specifying a power source that,
431 * when the device is connected to that source, causes the device to stay on.
432 * See {@link android.os.BatteryManager} for the list of power sources that
433 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
434 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
435 * @param val an {@code int} containing the bits that specify which power sources
436 * should cause the device to stay on.
437 */
438 public void setStayOnSetting(int val) {
439 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
440 Settings.System.putInt(mContext.getContentResolver(),
441 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
442 }
443
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800444 public void setMaximumScreenOffTimeount(int timeMs) {
445 mContext.enforceCallingOrSelfPermission(
446 android.Manifest.permission.WRITE_SECURE_SETTINGS, null);
447 synchronized (mLocks) {
448 mMaximumScreenOffTimeout = timeMs;
449 // recalculate everything
450 setScreenOffTimeoutsLocked();
451 }
452 }
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 private class SettingsObserver implements Observer {
Amith Yamasani8b619832010-09-22 16:11:59 -0700455 private int getInt(String name, int defValue) {
456 ContentValues values = mSettings.getValues(name);
457 Integer iVal = values != null ? values.getAsInteger(Settings.System.VALUE) : null;
458 return iVal != null ? iVal : defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460
Joe Onorato609695d2010-10-14 14:57:49 -0700461 private float getFloat(String name, float defValue) {
462 ContentValues values = mSettings.getValues(name);
463 Float fVal = values != null ? values.getAsFloat(Settings.System.VALUE) : null;
464 return fVal != null ? fVal : defValue;
465 }
466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public void update(Observable o, Object arg) {
468 synchronized (mLocks) {
Amith Yamasani8b619832010-09-22 16:11:59 -0700469 // STAY_ON_WHILE_PLUGGED_IN, default to when plugged into AC
470 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN,
471 BatteryManager.BATTERY_PLUGGED_AC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 updateWakeLockLocked();
473
Amith Yamasani8b619832010-09-22 16:11:59 -0700474 // SCREEN_OFF_TIMEOUT, default to 15 seconds
475 mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Joe Onorato609695d2010-10-14 14:57:49 -0700477 // DIM_SCREEN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 //mDimScreen = getInt(DIM_SCREEN) != 0;
479
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800480 mScreenBrightnessSetting = getInt(SCREEN_BRIGHTNESS, DEFAULT_SCREEN_BRIGHTNESS);
481 mLightSensorAdjustSetting = getFloat(SCREEN_AUTO_BRIGHTNESS_ADJ, 0);
482
Amith Yamasani8b619832010-09-22 16:11:59 -0700483 // SCREEN_BRIGHTNESS_MODE, default to manual
484 setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE,
485 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL));
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 // recalculate everything
488 setScreenOffTimeoutsLocked();
Joe Onorato609695d2010-10-14 14:57:49 -0700489
490 final float windowScale = getFloat(WINDOW_ANIMATION_SCALE, 1.0f);
491 final float transitionScale = getFloat(TRANSITION_ANIMATION_SCALE, 1.0f);
492 mAnimationSetting = 0;
493 if (windowScale > 0.5f) {
494 mAnimationSetting |= ANIM_SETTING_OFF;
495 }
496 if (transitionScale > 0.5f) {
497 // Uncomment this if you want the screen-on animation.
498 // mAnimationSetting |= ANIM_SETTING_ON;
499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501 }
502 }
503
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700504 PowerManagerService() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 // Hack to get our uid... should have a func for this.
506 long token = Binder.clearCallingIdentity();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700507 MY_UID = Process.myUid();
508 MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 Binder.restoreCallingIdentity(token);
510
511 // XXX remove this when the kernel doesn't timeout wake locks
512 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
513
514 // assume nothing is on yet
515 mUserState = mPowerState = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 // Add ourself to the Watchdog monitors.
518 Watchdog.getInstance().addMonitor(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520
521 private ContentQueryMap mSettings;
522
Mike Lockwood3a322132009-11-24 00:30:52 -0500523 void init(Context context, LightsService lights, IActivityManager activity,
The Android Open Source Project10592532009-03-18 17:39:46 -0700524 BatteryService battery) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500525 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 mContext = context;
527 mActivityService = activity;
528 mBatteryStats = BatteryStatsService.getService();
529 mBatteryService = battery;
530
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500531 mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT);
532 mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS);
533 mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD);
534 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700535 mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500536
Joe Onoratob08a1af2010-10-11 19:28:58 -0700537 nativeInit();
538 synchronized (mLocks) {
539 updateNativePowerStateLocked();
540 }
541
542 mInitComplete = false;
543 mScreenOffThread = new HandlerThread("PowerManagerService.mScreenOffThread") {
544 @Override
545 protected void onLooperPrepared() {
546 mScreenOffHandler = new Handler();
547 synchronized (mScreenOffThread) {
548 mInitComplete = true;
549 mScreenOffThread.notifyAll();
550 }
551 }
552 };
553 mScreenOffThread.start();
554
555 synchronized (mScreenOffThread) {
556 while (!mInitComplete) {
557 try {
558 mScreenOffThread.wait();
559 } catch (InterruptedException e) {
560 // Ignore
561 }
562 }
563 }
564
565 mInitComplete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 mHandlerThread = new HandlerThread("PowerManagerService") {
567 @Override
568 protected void onLooperPrepared() {
569 super.onLooperPrepared();
570 initInThread();
571 }
572 };
573 mHandlerThread.start();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 synchronized (mHandlerThread) {
576 while (!mInitComplete) {
577 try {
578 mHandlerThread.wait();
579 } catch (InterruptedException e) {
580 // Ignore
581 }
582 }
583 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700584
585 nativeInit();
586 synchronized (mLocks) {
587 updateNativePowerStateLocked();
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -0700588 // We make sure to start out with the screen on due to user activity.
589 // (They did just boot their device, after all.)
590 forceUserActivityLocked();
Dianne Hackborn40011092011-09-22 13:37:48 -0700591 mInitialized = true;
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 void initInThread() {
596 mHandler = new Handler();
597
598 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700599 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
601 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
602 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
603 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
604 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
605 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500606 mProximityPartialLock = new UnsynchronizedWakeLock(
607 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
609 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
610 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
611 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
612 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
613
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700614 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400615
Joe Onoratob08a1af2010-10-11 19:28:58 -0700616 mAnimateScreenLights = resources.getBoolean(
617 com.android.internal.R.bool.config_animateScreenLights);
618
Joe Onorato6d747652010-10-11 15:15:31 -0700619 mUnplugTurnsOnScreen = resources.getBoolean(
620 com.android.internal.R.bool.config_unplugTurnsOnScreen);
621
Mike Lockwoodeb6456b2011-09-13 15:24:02 -0400622 mScreenBrightnessDim = resources.getInteger(
623 com.android.internal.R.integer.config_screenBrightnessDim);
624
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400625 // read settings for auto-brightness
626 mUseSoftwareAutoBrightness = resources.getBoolean(
627 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400628 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700629 mAutoBrightnessLevels = resources.getIntArray(
630 com.android.internal.R.array.config_autoBrightnessLevels);
631 mLcdBacklightValues = resources.getIntArray(
632 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
633 mButtonBacklightValues = resources.getIntArray(
634 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
635 mKeyboardBacklightValues = resources.getIntArray(
636 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500637 mLightSensorWarmupTime = resources.getInteger(
638 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700639 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700640
641 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
643 "(" + Settings.System.NAME + "=?) or ("
644 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700645 + Settings.System.NAME + "=?) or ("
Joe Onorato609695d2010-10-14 14:57:49 -0700646 + Settings.System.NAME + "=?) or ("
647 + Settings.System.NAME + "=?) or ("
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800648 + Settings.System.NAME + "=?) or ("
649 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 + Settings.System.NAME + "=?)",
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800651 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, SCREEN_BRIGHTNESS,
652 SCREEN_BRIGHTNESS_MODE, SCREEN_AUTO_BRIGHTNESS_ADJ,
653 WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 null);
655 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
656 SettingsObserver settingsObserver = new SettingsObserver();
657 mSettings.addObserver(settingsObserver);
658
659 // pretend that the settings changed so we will get their initial state
660 settingsObserver.update(mSettings, null);
661
662 // register for the battery changed notifications
663 IntentFilter filter = new IntentFilter();
664 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
665 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500666 filter = new IntentFilter();
667 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
668 mContext.registerReceiver(new BootCompletedReceiver(), filter);
Mike Lockwoodb2865412010-02-02 22:40:33 -0500669 filter = new IntentFilter();
670 filter.addAction(Intent.ACTION_DOCK_EVENT);
671 mContext.registerReceiver(new DockReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
Doug Zongker43866e02010-01-07 12:09:54 -0800673 // Listen for secure settings changes
674 mContext.getContentResolver().registerContentObserver(
675 Settings.Secure.CONTENT_URI, true,
676 new ContentObserver(new Handler()) {
677 public void onChange(boolean selfChange) {
678 updateSettingsValues();
679 }
680 });
681 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 synchronized (mHandlerThread) {
684 mInitComplete = true;
685 mHandlerThread.notifyAll();
686 }
687 }
688
689 private class WakeLock implements IBinder.DeathRecipient
690 {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700691 WakeLock(int f, IBinder b, String t, int u, int p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 super();
693 flags = f;
694 binder = b;
695 tag = t;
696 uid = u == MY_UID ? Process.SYSTEM_UID : u;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700697 pid = p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (u != MY_UID || (
699 !"KEEP_SCREEN_ON_FLAG".equals(tag)
700 && !"KeyInputQueue".equals(tag))) {
701 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
702 ? BatteryStats.WAKE_TYPE_PARTIAL
703 : BatteryStats.WAKE_TYPE_FULL;
704 } else {
705 monitorType = -1;
706 }
707 try {
708 b.linkToDeath(this, 0);
709 } catch (RemoteException e) {
710 binderDied();
711 }
712 }
713 public void binderDied() {
714 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500715 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
718 final int flags;
719 final IBinder binder;
720 final String tag;
721 final int uid;
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400722 final int pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 final int monitorType;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700724 WorkSource ws;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 boolean activated = true;
726 int minState;
727 }
728
729 private void updateWakeLockLocked() {
730 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
731 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
732 mStayOnWhilePluggedInScreenDimLock.acquire();
733 mStayOnWhilePluggedInPartialLock.acquire();
734 } else {
735 mStayOnWhilePluggedInScreenDimLock.release();
736 mStayOnWhilePluggedInPartialLock.release();
737 }
738 }
739
740 private boolean isScreenLock(int flags)
741 {
742 int n = flags & LOCK_MASK;
743 return n == PowerManager.FULL_WAKE_LOCK
744 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Joe Onorato8274a0e2010-10-05 17:38:09 -0400745 || n == PowerManager.SCREEN_DIM_WAKE_LOCK
746 || n == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700749 void enforceWakeSourcePermission(int uid, int pid) {
750 if (uid == Process.myUid()) {
751 return;
752 }
753 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
754 pid, uid, null);
755 }
756
757 public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 int uid = Binder.getCallingUid();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700759 int pid = Binder.getCallingPid();
Michael Chane96440f2009-05-06 10:27:36 -0700760 if (uid != Process.myUid()) {
761 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
762 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700763 if (ws != null) {
764 enforceWakeSourcePermission(uid, pid);
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 long ident = Binder.clearCallingIdentity();
767 try {
768 synchronized (mLocks) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700769 acquireWakeLockLocked(flags, lock, uid, pid, tag, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 } finally {
772 Binder.restoreCallingIdentity(ident);
773 }
774 }
775
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700776 void noteStartWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700777 if (wl.monitorType >= 0) {
778 long origId = Binder.clearCallingIdentity();
779 try {
780 if (ws != null) {
781 mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
782 wl.monitorType);
783 } else {
784 mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
785 }
786 } catch (RemoteException e) {
787 // Ignore
788 } finally {
789 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700790 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700791 }
792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700794 void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700795 if (wl.monitorType >= 0) {
796 long origId = Binder.clearCallingIdentity();
797 try {
798 if (ws != null) {
799 mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
800 wl.monitorType);
801 } else {
802 mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
803 }
804 } catch (RemoteException e) {
805 // Ignore
806 } finally {
807 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700808 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700809 }
810 }
811
812 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
813 WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800815 Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700818 if (ws != null && ws.size() == 0) {
819 ws = null;
820 }
821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 int index = mLocks.getIndex(lock);
823 WakeLock wl;
824 boolean newlock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700825 boolean diffsource;
826 WorkSource oldsource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 if (index < 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700828 wl = new WakeLock(flags, lock, tag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 switch (wl.flags & LOCK_MASK)
830 {
831 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500832 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400833 wl.minState = SCREEN_BRIGHT;
834 } else {
835 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 break;
838 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
839 wl.minState = SCREEN_BRIGHT;
840 break;
841 case PowerManager.SCREEN_DIM_WAKE_LOCK:
842 wl.minState = SCREEN_DIM;
843 break;
844 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700845 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 break;
847 default:
848 // just log and bail. we're in the server, so don't
849 // throw an exception.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800850 Slog.e(TAG, "bad wakelock type for lock '" + tag + "' "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 + " flags=" + flags);
852 return;
853 }
854 mLocks.addLock(wl);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700855 if (ws != null) {
856 wl.ws = new WorkSource(ws);
857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 newlock = true;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700859 diffsource = false;
860 oldsource = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 } else {
862 wl = mLocks.get(index);
863 newlock = false;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700864 oldsource = wl.ws;
865 if (oldsource != null) {
866 if (ws == null) {
867 wl.ws = null;
868 diffsource = true;
869 } else {
870 diffsource = oldsource.diff(ws);
871 }
872 } else if (ws != null) {
873 diffsource = true;
874 } else {
875 diffsource = false;
876 }
877 if (diffsource) {
878 wl.ws = new WorkSource(ws);
879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 if (isScreenLock(flags)) {
882 // if this causes a wakeup, we reactivate all of the locks and
883 // set it to whatever they want. otherwise, we modulate that
884 // by the current state so we never turn it more on than
885 // it already is.
Joe Onorato8274a0e2010-10-05 17:38:09 -0400886 if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
887 mProximityWakeLockCount++;
888 if (mProximityWakeLockCount == 1) {
889 enableProximityLockLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 } else {
Joe Onorato8274a0e2010-10-05 17:38:09 -0400892 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
893 int oldWakeLockState = mWakeLockState;
894 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwooddb97f602011-09-02 11:59:08 -0400895
896 // Disable proximity sensor if if user presses power key while we are in the
897 // "waiting for proximity sensor to go negative" state.
898 if ((mWakeLockState & SCREEN_ON_BIT) != 0
899 && mProximitySensorActive && mProximityWakeLockCount == 0) {
900 mProximitySensorActive = false;
901 }
902
Joe Onorato8274a0e2010-10-05 17:38:09 -0400903 if (mSpew) {
904 Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
905 + " mWakeLockState=0x"
906 + Integer.toHexString(mWakeLockState)
907 + " previous wakeLockState=0x"
908 + Integer.toHexString(oldWakeLockState));
909 }
910 } else {
911 if (mSpew) {
912 Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
913 + " mLocks.gatherState()=0x"
914 + Integer.toHexString(mLocks.gatherState())
915 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
916 }
917 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Joe Onorato8274a0e2010-10-05 17:38:09 -0400919 setPowerState(mWakeLockState | mUserState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
922 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
923 if (newlock) {
924 mPartialCount++;
925 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800926 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 }
929 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700932 if (diffsource) {
933 // If the lock sources have changed, need to first release the
934 // old ones.
935 noteStopWakeLocked(wl, oldsource);
936 }
937 if (newlock || diffsource) {
938 noteStartWakeLocked(wl, ws);
939 }
940 }
941
942 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
943 int uid = Binder.getCallingUid();
944 int pid = Binder.getCallingPid();
945 if (ws != null && ws.size() == 0) {
946 ws = null;
947 }
948 if (ws != null) {
949 enforceWakeSourcePermission(uid, pid);
950 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700951 synchronized (mLocks) {
952 int index = mLocks.getIndex(lock);
953 if (index < 0) {
954 throw new IllegalArgumentException("Wake lock not active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700956 WakeLock wl = mLocks.get(index);
957 WorkSource oldsource = wl.ws;
958 wl.ws = ws != null ? new WorkSource(ws) : null;
959 noteStopWakeLocked(wl, oldsource);
960 noteStartWakeLocked(wl, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 }
963
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500964 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700965 int uid = Binder.getCallingUid();
966 if (uid != Process.myUid()) {
967 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969
970 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500971 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
973 }
974
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500975 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 WakeLock wl = mLocks.removeLock(lock);
977 if (wl == null) {
978 return;
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 Slog.d(TAG, "releaseWakeLock flags=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
984 }
985
986 if (isScreenLock(wl.flags)) {
Joe Onorato8274a0e2010-10-05 17:38:09 -0400987 if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
988 mProximityWakeLockCount--;
989 if (mProximityWakeLockCount == 0) {
990 if (mProximitySensorActive &&
991 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
992 // wait for proximity sensor to go negative before disabling sensor
993 if (mDebugProximitySensor) {
994 Slog.d(TAG, "waiting for proximity sensor to go negative");
995 }
996 } else {
997 disableProximityLockLocked();
998 }
999 }
1000 } else {
1001 mWakeLockState = mLocks.gatherState();
1002 // goes in the middle to reduce flicker
1003 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
1004 userActivity(SystemClock.uptimeMillis(), -1, false, OTHER_EVENT, false);
1005 }
1006 setPowerState(mWakeLockState | mUserState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
1010 mPartialCount--;
1011 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001012 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 Power.releaseWakeLock(PARTIAL_NAME);
1014 }
1015 }
1016 // Unlink the lock from the binder.
1017 wl.binder.unlinkToDeath(wl, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018
Dianne Hackborn70be1672010-09-14 11:13:03 -07001019 noteStopWakeLocked(wl, wl.ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 private class PokeLock implements IBinder.DeathRecipient
1023 {
1024 PokeLock(int p, IBinder b, String t) {
1025 super();
1026 this.pokey = p;
1027 this.binder = b;
1028 this.tag = t;
1029 try {
1030 b.linkToDeath(this, 0);
1031 } catch (RemoteException e) {
1032 binderDied();
1033 }
1034 }
1035 public void binderDied() {
1036 setPokeLock(0, this.binder, this.tag);
1037 }
1038 int pokey;
1039 IBinder binder;
1040 String tag;
1041 boolean awakeOnSet;
1042 }
1043
1044 public void setPokeLock(int pokey, IBinder token, String tag) {
1045 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1046 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001047 Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 return;
1049 }
1050
1051 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
1052 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
1053 + " and POKE_LOCK_MEDIUM_TIMEOUT");
1054 }
1055
1056 synchronized (mLocks) {
1057 if (pokey != 0) {
1058 PokeLock p = mPokeLocks.get(token);
1059 int oldPokey = 0;
1060 if (p != null) {
1061 oldPokey = p.pokey;
1062 p.pokey = pokey;
1063 } else {
1064 p = new PokeLock(pokey, token, tag);
1065 mPokeLocks.put(token, p);
1066 }
1067 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
1068 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
1069 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
1070 p.awakeOnSet = true;
1071 }
1072 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001073 PokeLock rLock = mPokeLocks.remove(token);
1074 if (rLock != null) {
1075 token.unlinkToDeath(rLock, 0);
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078
1079 int oldPokey = mPokey;
1080 int cumulative = 0;
1081 boolean oldAwakeOnSet = mPokeAwakeOnSet;
1082 boolean awakeOnSet = false;
1083 for (PokeLock p: mPokeLocks.values()) {
1084 cumulative |= p.pokey;
1085 if (p.awakeOnSet) {
1086 awakeOnSet = true;
1087 }
1088 }
1089 mPokey = cumulative;
1090 mPokeAwakeOnSet = awakeOnSet;
1091
1092 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
1093 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 if (oldCumulativeTimeout != newCumulativeTimeout) {
1096 setScreenOffTimeoutsLocked();
1097 // reset the countdown timer, but use the existing nextState so it doesn't
1098 // change anything
1099 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
1100 }
1101 }
1102 }
1103
1104 private static String lockType(int type)
1105 {
1106 switch (type)
1107 {
1108 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001109 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001111 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001113 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001115 return "PARTIAL_WAKE_LOCK ";
1116 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
1117 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 default:
David Brown251faa62009-08-02 22:04:36 -07001119 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
1122
1123 private static String dumpPowerState(int state) {
1124 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
1125 ? "KEYBOARD_BRIGHT_BIT " : "")
1126 + (((state & SCREEN_BRIGHT_BIT) != 0)
1127 ? "SCREEN_BRIGHT_BIT " : "")
1128 + (((state & SCREEN_ON_BIT) != 0)
1129 ? "SCREEN_ON_BIT " : "")
1130 + (((state & BATTERY_LOW_BIT) != 0)
1131 ? "BATTERY_LOW_BIT " : "");
1132 }
1133
1134 @Override
1135 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1136 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1137 != PackageManager.PERMISSION_GRANTED) {
1138 pw.println("Permission Denial: can't dump PowerManager from from pid="
1139 + Binder.getCallingPid()
1140 + ", uid=" + Binder.getCallingUid());
1141 return;
1142 }
1143
1144 long now = SystemClock.uptimeMillis();
1145
Mike Lockwoodca44df82010-02-25 13:48:49 -05001146 synchronized (mLocks) {
1147 pw.println("Power Manager State:");
1148 pw.println(" mIsPowered=" + mIsPowered
1149 + " mPowerState=" + mPowerState
1150 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
1151 + " ms");
1152 pw.println(" mPartialCount=" + mPartialCount);
1153 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
1154 pw.println(" mUserState=" + dumpPowerState(mUserState));
1155 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
1156 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
1157 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
1158 + " " + ((mNextTimeout-now)/1000) + "s from now");
1159 pw.println(" mDimScreen=" + mDimScreen
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001160 + " mStayOnConditions=" + mStayOnConditions
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001161 + " mPreparingForScreenOn=" + mPreparingForScreenOn
1162 + " mSkippedScreenOn=" + mSkippedScreenOn);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001163 pw.println(" mScreenOffReason=" + mScreenOffReason
1164 + " mUserState=" + mUserState);
1165 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
1166 + ',' + mBroadcastQueue[2] + "}");
1167 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
1168 + ',' + mBroadcastWhy[2] + "}");
1169 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
1170 pw.println(" mKeyboardVisible=" + mKeyboardVisible
1171 + " mUserActivityAllowed=" + mUserActivityAllowed);
1172 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
1173 + " mScreenOffDelay=" + mScreenOffDelay);
1174 pw.println(" mPreventScreenOn=" + mPreventScreenOn
1175 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
1176 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
1177 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
1178 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
1179 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
1180 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
1181 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
1182 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
1183 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
1184 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
1185 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
1186 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1187 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
1188 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
1189 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08001190 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled
1191 + " mLightSensorAdjustSetting=" + mLightSensorAdjustSetting);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001192 pw.println(" mLightSensorValue=" + mLightSensorValue
1193 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
Jim Rodovichd102fea2010-09-02 12:30:49 -05001194 pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease
1195 + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001196 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
1197 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
1198 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
1199 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
1200 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
1201 mScreenBrightness.dump(pw, " mScreenBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
Mike Lockwoodca44df82010-02-25 13:48:49 -05001203 int N = mLocks.size();
1204 pw.println();
1205 pw.println("mLocks.size=" + N + ":");
1206 for (int i=0; i<N; i++) {
1207 WakeLock wl = mLocks.get(i);
1208 String type = lockType(wl.flags & LOCK_MASK);
1209 String acquireCausesWakeup = "";
1210 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
1211 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
1212 }
1213 String activated = "";
1214 if (wl.activated) {
1215 activated = " activated";
1216 }
1217 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001218 + activated + " (minState=" + wl.minState + ", uid=" + wl.uid
1219 + ", pid=" + wl.pid + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
Mike Lockwoodca44df82010-02-25 13:48:49 -05001221
1222 pw.println();
1223 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
1224 for (PokeLock p: mPokeLocks.values()) {
1225 pw.println(" poke lock '" + p.tag + "':"
Joe Onorato1a542c72010-11-08 09:48:20 -08001226 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0
1227 ? " POKE_LOCK_IGNORE_TOUCH_EVENTS" : "")
Mike Lockwoodca44df82010-02-25 13:48:49 -05001228 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1229 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1230 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1231 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
Mike Lockwoodca44df82010-02-25 13:48:49 -05001234 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237
Joe Onorato7999bff2010-07-24 11:50:05 -04001238 private void setTimeoutLocked(long now, int nextState) {
1239 setTimeoutLocked(now, -1, nextState);
1240 }
1241
1242 // If they gave a timeoutOverride it is the number of seconds
1243 // to screen-off. Figure out where in the countdown cycle we
1244 // should jump to.
Joe Onorato797e6882010-08-26 14:46:01 -04001245 private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
1246 long timeoutOverride = originalTimeoutOverride;
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001247 if (mBootCompleted) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001248 synchronized (mLocks) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001249 long when = 0;
1250 if (timeoutOverride <= 0) {
1251 switch (nextState)
1252 {
1253 case SCREEN_BRIGHT:
1254 when = now + mKeylightDelay;
1255 break;
1256 case SCREEN_DIM:
1257 if (mDimDelay >= 0) {
1258 when = now + mDimDelay;
Andreas Huber84047bc2010-07-27 16:49:10 -07001259 break;
Joe Onorato7999bff2010-07-24 11:50:05 -04001260 } else {
1261 Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1262 }
1263 case SCREEN_OFF:
1264 synchronized (mLocks) {
1265 when = now + mScreenOffDelay;
1266 }
1267 break;
Andreas Huber84047bc2010-07-27 16:49:10 -07001268 default:
1269 when = now;
1270 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001272 } else {
1273 override: {
1274 if (timeoutOverride <= mScreenOffDelay) {
1275 when = now + timeoutOverride;
1276 nextState = SCREEN_OFF;
1277 break override;
1278 }
1279 timeoutOverride -= mScreenOffDelay;
1280
1281 if (mDimDelay >= 0) {
1282 if (timeoutOverride <= mDimDelay) {
1283 when = now + timeoutOverride;
1284 nextState = SCREEN_DIM;
1285 break override;
1286 }
1287 timeoutOverride -= mDimDelay;
1288 }
1289
1290 when = now + timeoutOverride;
1291 nextState = SCREEN_BRIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001293 }
1294 if (mSpew) {
1295 Slog.d(TAG, "setTimeoutLocked now=" + now
1296 + " timeoutOverride=" + timeoutOverride
1297 + " nextState=" + nextState + " when=" + when);
1298 }
Joe Onorato797e6882010-08-26 14:46:01 -04001299
1300 mHandler.removeCallbacks(mTimeoutTask);
1301 mTimeoutTask.nextState = nextState;
1302 mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
1303 ? (originalTimeoutOverride - timeoutOverride)
1304 : -1;
Joe Onorato7999bff2010-07-24 11:50:05 -04001305 mHandler.postAtTime(mTimeoutTask, when);
1306 mNextTimeout = when; // for debugging
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
1309 }
1310
1311 private void cancelTimerLocked()
1312 {
1313 mHandler.removeCallbacks(mTimeoutTask);
1314 mTimeoutTask.nextState = -1;
1315 }
1316
1317 private class TimeoutTask implements Runnable
1318 {
1319 int nextState; // access should be synchronized on mLocks
Joe Onorato797e6882010-08-26 14:46:01 -04001320 long remainingTimeoutOverride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 public void run()
1322 {
1323 synchronized (mLocks) {
1324 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001325 Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
1328 if (nextState == -1) {
1329 return;
1330 }
1331
1332 mUserState = this.nextState;
1333 setPowerState(this.nextState | mWakeLockState);
1334
1335 long now = SystemClock.uptimeMillis();
1336
1337 switch (this.nextState)
1338 {
1339 case SCREEN_BRIGHT:
1340 if (mDimDelay >= 0) {
Joe Onorato797e6882010-08-26 14:46:01 -04001341 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 break;
1343 }
1344 case SCREEN_DIM:
Joe Onorato797e6882010-08-26 14:46:01 -04001345 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 break;
1347 }
1348 }
1349 }
1350 }
1351
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001352 private void sendNotificationLocked(boolean on, int why) {
1353 if (!mInitialized) {
1354 // No notifications sent until first initialization is done.
1355 // This is so that when we are moving from our initial state
1356 // which looks like the screen was off to it being on, we do not
1357 // go through the process of waiting for the higher-level user
1358 // space to be ready before turning up the display brightness.
1359 // (And also do not send needless broadcasts about the screen.)
1360 return;
1361 }
Dianne Hackborn40011092011-09-22 13:37:48 -07001362
1363 if (DEBUG_SCREEN_ON) {
1364 RuntimeException here = new RuntimeException("here");
1365 here.fillInStackTrace();
1366 Slog.i(TAG, "sendNotificationLocked: " + on, here);
1367 }
1368
Joe Onorato64c62ba2009-03-24 20:13:57 -07001369 if (!on) {
1370 mStillNeedSleepNotification = false;
1371 }
1372
Joe Onorato128e7292009-03-24 18:41:31 -07001373 // Add to the queue.
1374 int index = 0;
1375 while (mBroadcastQueue[index] != -1) {
1376 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
Joe Onorato128e7292009-03-24 18:41:31 -07001378 mBroadcastQueue[index] = on ? 1 : 0;
1379 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
Joe Onorato128e7292009-03-24 18:41:31 -07001381 // If we added it position 2, then there is a pair that can be stripped.
1382 // If we added it position 1 and we're turning the screen off, we can strip
1383 // the pair and do nothing, because the screen is already off, and therefore
1384 // keyguard has already been enabled.
1385 // However, if we added it at position 1 and we're turning it on, then position
1386 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1387 // on, so have to run the queue then.
1388 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001389 // While we're collapsing them, if it's going off, and the new reason
1390 // is more significant than the first, then use the new one.
1391 if (!on && mBroadcastWhy[0] > why) {
1392 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001393 }
1394 mBroadcastQueue[0] = on ? 1 : 0;
1395 mBroadcastQueue[1] = -1;
1396 mBroadcastQueue[2] = -1;
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001397 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
Mike Lockwood9c90a372010-04-13 15:40:27 -04001398 mBroadcastWakeLock.release();
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001399 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
Mike Lockwood9c90a372010-04-13 15:40:27 -04001400 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001401 index = 0;
1402 }
1403 if (index == 1 && !on) {
1404 mBroadcastQueue[0] = -1;
1405 mBroadcastQueue[1] = -1;
1406 index = -1;
1407 // The wake lock was being held, but we're not actually going to do any
1408 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001411 }
1412
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001413 // The broadcast queue has changed; make sure the screen is on if it
1414 // is now possible for it to be.
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001415 if (mSkippedScreenOn) {
1416 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1417 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001418
Joe Onorato128e7292009-03-24 18:41:31 -07001419 // Now send the message.
1420 if (index >= 0) {
1421 // Acquire the broadcast wake lock before changing the power
1422 // state. It will be release after the broadcast is sent.
1423 // We always increment the ref count for each notification in the queue
1424 // and always decrement when that notification is handled.
1425 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001427 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429 }
1430
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001431 private WindowManagerPolicy.ScreenOnListener mScreenOnListener =
1432 new WindowManagerPolicy.ScreenOnListener() {
1433 @Override public void onScreenOn() {
1434 synchronized (mLocks) {
1435 if (mPreparingForScreenOn) {
1436 mPreparingForScreenOn = false;
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001437 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001438 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP,
1439 4, mBroadcastWakeLock.mCount);
1440 mBroadcastWakeLock.release();
1441 }
1442 }
1443 }
1444 };
1445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 private Runnable mNotificationTask = new Runnable()
1447 {
1448 public void run()
1449 {
Joe Onorato128e7292009-03-24 18:41:31 -07001450 while (true) {
1451 int value;
1452 int why;
1453 WindowManagerPolicy policy;
1454 synchronized (mLocks) {
1455 value = mBroadcastQueue[0];
1456 why = mBroadcastWhy[0];
1457 for (int i=0; i<2; i++) {
1458 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1459 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1460 }
1461 policy = getPolicyLocked();
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001462 if (value == 1 && !mPreparingForScreenOn) {
1463 mPreparingForScreenOn = true;
1464 mBroadcastWakeLock.acquire();
1465 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND,
1466 mBroadcastWakeLock.mCount);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Joe Onorato128e7292009-03-24 18:41:31 -07001469 if (value == 1) {
1470 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001472 policy.screenTurningOn(mScreenOnListener);
Joe Onorato128e7292009-03-24 18:41:31 -07001473 try {
1474 ActivityManagerNative.getDefault().wakingUp();
1475 } catch (RemoteException e) {
1476 // ignore it
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478
Joe Onorato128e7292009-03-24 18:41:31 -07001479 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001480 Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
Joe Onorato128e7292009-03-24 18:41:31 -07001481 }
1482 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1483 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1484 mScreenOnBroadcastDone, mHandler, 0, null, null);
1485 } else {
1486 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001487 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001488 mBroadcastWakeLock.mCount);
1489 mBroadcastWakeLock.release();
1490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 }
Joe Onorato128e7292009-03-24 18:41:31 -07001493 else if (value == 0) {
1494 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Joe Onorato128e7292009-03-24 18:41:31 -07001496 policy.screenTurnedOff(why);
1497 try {
1498 ActivityManagerNative.getDefault().goingToSleep();
1499 } catch (RemoteException e) {
1500 // ignore it.
1501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502
Joe Onorato128e7292009-03-24 18:41:31 -07001503 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1504 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1505 mScreenOffBroadcastDone, mHandler, 0, null, null);
1506 } else {
1507 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001509 mBroadcastWakeLock.mCount);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001510 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -07001511 mBroadcastWakeLock.release();
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514 }
Joe Onorato128e7292009-03-24 18:41:31 -07001515 else {
1516 // If we're in this case, then this handler is running for a previous
1517 // paired transaction. mBroadcastWakeLock will already have been released.
1518 break;
1519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521 }
1522 };
1523
1524 long mScreenOnStart;
1525 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1526 public void onReceive(Context context, Intent intent) {
1527 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1530 mBroadcastWakeLock.release();
1531 }
1532 }
1533 };
1534
1535 long mScreenOffStart;
1536 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1537 public void onReceive(Context context, Intent intent) {
1538 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1541 mBroadcastWakeLock.release();
1542 }
1543 }
1544 };
1545
1546 void logPointerUpEvent() {
1547 if (LOG_TOUCH_DOWNS) {
1548 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1549 mLastTouchDown = 0;
1550 }
1551 }
1552
1553 void logPointerDownEvent() {
1554 if (LOG_TOUCH_DOWNS) {
1555 // If we are not already timing a down/up sequence
1556 if (mLastTouchDown == 0) {
1557 mLastTouchDown = SystemClock.elapsedRealtime();
1558 mTouchCycles++;
1559 }
1560 }
1561 }
1562
1563 /**
1564 * Prevents the screen from turning on even if it *should* turn on due
1565 * to a subsequent full wake lock being acquired.
1566 * <p>
1567 * This is a temporary hack that allows an activity to "cover up" any
1568 * display glitches that happen during the activity's startup
1569 * sequence. (Specifically, this API was added to work around a
1570 * cosmetic bug in the "incoming call" sequence, where the lock screen
1571 * would flicker briefly before the incoming call UI became visible.)
1572 * TODO: There ought to be a more elegant way of doing this,
1573 * probably by having the PowerManager and ActivityManager
1574 * work together to let apps specify that the screen on/off
1575 * state should be synchronized with the Activity lifecycle.
1576 * <p>
1577 * Note that calling preventScreenOn(true) will NOT turn the screen
1578 * off if it's currently on. (This API only affects *future*
1579 * acquisitions of full wake locks.)
1580 * But calling preventScreenOn(false) WILL turn the screen on if
1581 * it's currently off because of a prior preventScreenOn(true) call.
1582 * <p>
1583 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1584 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1585 * call doesn't occur within 5 seconds, we'll turn the screen back on
1586 * ourselves (and log a warning about it); this prevents a buggy app
1587 * from disabling the screen forever.)
1588 * <p>
1589 * TODO: this feature should really be controlled by a new type of poke
1590 * lock (rather than an IPowerManager call).
1591 */
1592 public void preventScreenOn(boolean prevent) {
1593 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1594
1595 synchronized (mLocks) {
1596 if (prevent) {
1597 // First of all, grab a partial wake lock to
1598 // make sure the CPU stays on during the entire
1599 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1600 mPreventScreenOnPartialLock.acquire();
1601
1602 // Post a forceReenableScreen() call (for 5 seconds in the
1603 // future) to make sure the matching preventScreenOn(false) call
1604 // has happened by then.
1605 mHandler.removeCallbacks(mForceReenableScreenTask);
1606 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1607
1608 // Finally, set the flag that prevents the screen from turning on.
1609 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001610 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 mPreventScreenOn = true;
1612 } else {
1613 // (Re)enable the screen.
1614 mPreventScreenOn = false;
1615
1616 // We're "undoing" a the prior preventScreenOn(true) call, so we
1617 // no longer need the 5-second safeguard.
1618 mHandler.removeCallbacks(mForceReenableScreenTask);
1619
1620 // Forcibly turn on the screen if it's supposed to be on. (This
1621 // handles the case where the screen is currently off because of
1622 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001623 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001625 Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1627 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001628 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if (err != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001630 Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
1632 }
1633
1634 // Release the partial wake lock that we held during the
1635 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1636 mPreventScreenOnPartialLock.release();
1637 }
1638 }
1639 }
1640
1641 public void setScreenBrightnessOverride(int brightness) {
1642 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1643
Mike Lockwoodf527c712010-06-10 14:12:33 -04001644 if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 synchronized (mLocks) {
1646 if (mScreenBrightnessOverride != brightness) {
1647 mScreenBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001648 if (isScreenOn()) {
1649 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 }
1653 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001654
1655 public void setButtonBrightnessOverride(int brightness) {
1656 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1657
Mike Lockwoodf527c712010-06-10 14:12:33 -04001658 if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness);
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001659 synchronized (mLocks) {
1660 if (mButtonBrightnessOverride != brightness) {
1661 mButtonBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001662 if (isScreenOn()) {
1663 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1664 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001665 }
1666 }
1667 }
1668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 /**
1670 * Sanity-check that gets called 5 seconds after any call to
1671 * preventScreenOn(true). This ensures that the original call
1672 * is followed promptly by a call to preventScreenOn(false).
1673 */
1674 private void forceReenableScreen() {
1675 // We shouldn't get here at all if mPreventScreenOn is false, since
1676 // we should have already removed any existing
1677 // mForceReenableScreenTask messages...
1678 if (!mPreventScreenOn) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001679 Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 return;
1681 }
1682
1683 // Uh oh. It's been 5 seconds since a call to
1684 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1685 // This means the app that called preventScreenOn(true) is either
1686 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1687 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1688 // crashed before doing so.)
1689
1690 // Log a warning, and forcibly turn the screen back on.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001691 Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 + "Forcing the screen back on...");
1693 preventScreenOn(false);
1694 }
1695
1696 private Runnable mForceReenableScreenTask = new Runnable() {
1697 public void run() {
1698 forceReenableScreen();
1699 }
1700 };
1701
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001702 private int setScreenStateLocked(boolean on) {
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001703 if (DEBUG_SCREEN_ON) {
1704 RuntimeException e = new RuntimeException("here");
1705 e.fillInStackTrace();
1706 Slog.i(TAG, "Set screen state: " + on, e);
1707 }
Dianne Hackborn474fd742011-10-10 18:40:22 -07001708 if (on) {
1709 if ((mPowerState & SCREEN_ON_BIT) == 0 || mSkippedScreenOn) {
1710 // If we are turning the screen state on, but the screen
1711 // light is currently off, then make sure that we set the
1712 // light at this point to 0. This is the case where we are
1713 // turning on the screen and waiting for the UI to be drawn
1714 // before showing it to the user. We want the light off
1715 // until it is ready to be shown to the user, not it using
1716 // whatever the last value it had.
Dianne Hackborn81de8b92011-11-28 16:54:31 -08001717 if (DEBUG_SCREEN_ON) {
1718 Slog.i(TAG, "Forcing brightness 0: mPowerState=0x"
1719 + Integer.toHexString(mPowerState)
1720 + " mSkippedScreenOn=" + mSkippedScreenOn);
1721 }
Dianne Hackborn474fd742011-10-10 18:40:22 -07001722 mScreenBrightness.forceValueLocked(Power.BRIGHTNESS_OFF);
1723 }
1724 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001725 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001726 if (err == 0) {
1727 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1728 if (mUseSoftwareAutoBrightness) {
Joe Onoratod28f7532010-11-06 12:56:53 -07001729 enableLightSensorLocked(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001730 if (!on) {
1731 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001732 mButtonLight.turnOff();
1733 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001734 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001735 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001736 }
1737 return err;
1738 }
1739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 private void setPowerState(int state)
1741 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001742 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744
Mike Lockwood435eb642009-12-03 08:40:18 -05001745 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 {
1747 synchronized (mLocks) {
1748 int err;
1749
1750 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001751 Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001753 + " noChangeLights=" + noChangeLights
1754 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
1756
1757 if (noChangeLights) {
1758 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1759 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001760 if (mProximitySensorActive) {
1761 // don't turn on the screen when the proximity sensor lock is held
1762 newState = (newState & ~SCREEN_BRIGHT);
1763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
1765 if (batteryIsLow()) {
1766 newState |= BATTERY_LOW_BIT;
1767 } else {
1768 newState &= ~BATTERY_LOW_BIT;
1769 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001770 if (newState == mPowerState && mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 return;
1772 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001773
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001774 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 newState |= ALL_BRIGHT;
1776 }
1777
1778 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1779 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1780
Mike Lockwood51b844962009-11-16 21:51:18 -05001781 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001782 Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 + " newState=" + newState + " noChangeLights=" + noChangeLights);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001784 Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001786 Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001788 Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001790 Slog.d(TAG, " oldScreenOn=" + oldScreenOn
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 + " newScreenOn=" + newScreenOn);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001792 Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1794 }
1795
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001796 final boolean stateChanged = mPowerState != newState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797
1798 if (oldScreenOn != newScreenOn) {
1799 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001800 // When the user presses the power button, we need to always send out the
1801 // notification that it's going to sleep so the keyguard goes on. But
1802 // we can't do that until the screen fades out, so we don't show the keyguard
1803 // too early.
1804 if (mStillNeedSleepNotification) {
1805 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1806 }
1807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 // Turn on the screen UNLESS there was a prior
1809 // preventScreenOn(true) request. (Note that the lifetime
1810 // of a single preventScreenOn() request is limited to 5
1811 // seconds to prevent a buggy app from disabling the
1812 // screen forever; see forceReenableScreen().)
1813 boolean reallyTurnScreenOn = true;
1814 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001815 Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 + mPreventScreenOn);
1817 }
1818
1819 if (mPreventScreenOn) {
1820 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001821 Slog.d(TAG, "- PREVENTING screen from really turning on!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 reallyTurnScreenOn = false;
1824 }
1825 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001826 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 long identity = Binder.clearCallingIdentity();
1828 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001829 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 mBatteryStats.noteScreenOn();
1831 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001832 Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 } finally {
1834 Binder.restoreCallingIdentity(identity);
1835 }
1836 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001837 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 // But continue as if we really did turn the screen on...
1839 err = 0;
1840 }
1841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 mLastTouchDown = 0;
1843 mTotalTouchDownTime = 0;
1844 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 mTotalTouchDownTime, mTouchCycles);
1847 if (err == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 sendNotificationLocked(true, -1);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001849 // Update the lights *after* taking care of turning the
1850 // screen on, so we do this after our notifications are
1851 // enqueued and thus will delay turning on the screen light
1852 // until the windows are correctly displayed.
1853 if (stateChanged) {
1854 updateLightsLocked(newState, 0);
1855 }
1856 mPowerState |= SCREEN_ON_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 } else {
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001860 // Update the lights *before* taking care of turning the
1861 // screen off, so we can initiate any animations that are desired.
1862 if (stateChanged) {
1863 updateLightsLocked(newState, 0);
1864 }
1865
Mike Lockwood497087e32009-11-08 18:33:03 -05001866 // cancel light sensor task
1867 mHandler.removeCallbacks(mAutoBrightnessTask);
Jim Rodovichd102fea2010-09-02 12:30:49 -05001868 mLightSensorPendingDecrease = false;
1869 mLightSensorPendingIncrease = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 mScreenOffTime = SystemClock.elapsedRealtime();
1871 long identity = Binder.clearCallingIdentity();
1872 try {
1873 mBatteryStats.noteScreenOff();
1874 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001875 Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 } finally {
1877 Binder.restoreCallingIdentity(identity);
1878 }
1879 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001880 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001882 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 err = 0;
1885 mLastTouchDown = 0;
1886 }
1887 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001888 } else if (stateChanged) {
1889 // Screen on/off didn't change, but lights may have.
1890 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001892
1893 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1894
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001895 updateNativePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001898
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001899 private void updateNativePowerStateLocked() {
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001900 if (!mHeadless) {
1901 nativeSetPowerState(
1902 (mPowerState & SCREEN_ON_BIT) != 0,
1903 (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
1904 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001906
Mike Lockwood435eb642009-12-03 08:40:18 -05001907 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 // called multiple times in the same state. -joeo
Joe Onoratob08a1af2010-10-11 19:28:58 -07001911 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime,
1912 mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001914 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001916 mScreenOffReason = reason;
1917 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 return err;
1920 }
1921
1922 private boolean batteryIsLow() {
1923 return (!mIsPowered &&
1924 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1925 }
1926
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001927 private boolean shouldDeferScreenOnLocked() {
1928 if (mPreparingForScreenOn) {
1929 // Currently waiting for confirmation from the policy that it
1930 // is okay to turn on the screen. Don't allow the screen to go
1931 // on until that is done.
1932 if (DEBUG_SCREEN_ON) Slog.i(TAG,
1933 "updateLights: delaying screen on due to mPreparingForScreenOn");
1934 return true;
1935 } else {
1936 // If there is a screen-on command in the notification queue, we
1937 // can't turn the screen on until it has been processed (and we
1938 // have set mPreparingForScreenOn) or it has been dropped.
1939 for (int i=0; i<mBroadcastQueue.length; i++) {
1940 if (mBroadcastQueue[i] == 1) {
1941 if (DEBUG_SCREEN_ON) Slog.i(TAG,
1942 "updateLights: delaying screen on due to notification queue");
1943 return true;
1944 }
1945 }
1946 }
1947 return false;
1948 }
1949
The Android Open Source Project10592532009-03-18 17:39:46 -07001950 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001951 final int oldState = mPowerState;
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001952
1953 // If the screen is not currently on, we will want to delay actually
1954 // turning the lights on if we are still getting the UI put up.
1955 if ((oldState&SCREEN_ON_BIT) == 0 || mSkippedScreenOn) {
1956 // Don't turn screen on until we know we are really ready to.
1957 // This is to avoid letting the screen go on before things like the
1958 // lock screen have been displayed.
1959 if ((mSkippedScreenOn=shouldDeferScreenOnLocked())) {
1960 newState &= ~(SCREEN_ON_BIT|SCREEN_BRIGHT_BIT);
1961 }
1962 }
1963
Joe Onorato60607a902010-10-23 14:49:30 -07001964 if ((newState & SCREEN_ON_BIT) != 0) {
1965 // Only turn on the buttons or keyboard if the screen is also on.
1966 // We should never see the buttons on but not the screen.
1967 newState = applyButtonState(newState);
1968 newState = applyKeyboardState(newState);
1969 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001970 final int realDifference = (newState ^ oldState);
1971 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001973 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 int offMask = 0;
1977 int dimMask = 0;
1978 int onMask = 0;
1979
1980 int preferredBrightness = getPreferredBrightness();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001983 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1984 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001986 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988 }
1989
1990 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001991 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1992 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001994 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 }
1997
1998 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001999 int nominalCurrentValue = -1;
2000 // If there was an actual difference in the light state, then
2001 // figure out the "ideal" current value based on the previous
2002 // state. Otherwise, this is a change due to the brightness
2003 // override, so we want to animate from whatever the current
2004 // value is.
2005 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
2006 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
2007 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
2008 nominalCurrentValue = preferredBrightness;
2009 break;
2010 case SCREEN_ON_BIT:
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002011 nominalCurrentValue = mScreenBrightnessDim;
Joe Onoratob08a1af2010-10-11 19:28:58 -07002012 break;
2013 case 0:
2014 nominalCurrentValue = Power.BRIGHTNESS_OFF;
2015 break;
2016 case SCREEN_BRIGHT_BIT:
2017 default:
2018 // not possible
2019 nominalCurrentValue = (int)mScreenBrightness.curValue;
2020 break;
Joe Onorato128e7292009-03-24 18:41:31 -07002021 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002022 }
2023 int brightness = preferredBrightness;
2024 int steps = ANIM_STEPS;
2025 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
2026 // dim or turn off backlight, depending on if the screen is on
2027 // the scale is because the brightness ramp isn't linear and this biases
2028 // it so the later parts take longer.
2029 final float scale = 1.5f;
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002030 float ratio = (((float)mScreenBrightnessDim)/preferredBrightness);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002031 if (ratio > 1.0f) ratio = 1.0f;
2032 if ((newState & SCREEN_ON_BIT) == 0) {
2033 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
2034 // was bright
2035 steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002037 // was dim
2038 steps = (int)(ANIM_STEPS*ratio*scale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002040 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002042 if ((oldState & SCREEN_ON_BIT) != 0) {
2043 // was bright
2044 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
2045 } else {
2046 // was dim
2047 steps = (int)(ANIM_STEPS*ratio);
2048 }
2049 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
2050 // If the "stay on while plugged in" option is
2051 // turned on, then the screen will often not
2052 // automatically turn off while plugged in. To
2053 // still have a sense of when it is inactive, we
2054 // will then count going dim as turning off.
2055 mScreenOffTime = SystemClock.elapsedRealtime();
2056 }
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002057 brightness = mScreenBrightnessDim;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002060 long identity = Binder.clearCallingIdentity();
2061 try {
2062 mBatteryStats.noteScreenBrightness(brightness);
2063 } catch (RemoteException e) {
2064 // Nothing interesting to do.
2065 } finally {
2066 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002068 if (!mSkippedScreenOn) {
2069 mScreenBrightness.setTargetLocked(brightness, steps,
2070 INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
2071 if (DEBUG_SCREEN_ON) {
2072 RuntimeException e = new RuntimeException("here");
2073 e.fillInStackTrace();
2074 Slog.i(TAG, "Setting screen brightness: " + brightness, e);
2075 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07002076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002078
Joe Onorato60607a902010-10-23 14:49:30 -07002079 if (mSpew) {
2080 Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask)
2081 + " dimMask=0x" + Integer.toHexString(dimMask)
2082 + " onMask=0x" + Integer.toHexString(onMask)
2083 + " difference=0x" + Integer.toHexString(difference)
2084 + " realDifference=0x" + Integer.toHexString(realDifference)
2085 + " forceState=0x" + Integer.toHexString(forceState)
2086 );
2087 }
2088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 if (offMask != 0) {
Mike Lockwood48358bd2010-04-17 22:29:20 -04002090 if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002091 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093 if (dimMask != 0) {
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002094 int brightness = mScreenBrightnessDim;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 if ((newState & BATTERY_LOW_BIT) != 0 &&
2096 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
2097 brightness = Power.BRIGHTNESS_LOW_BATTERY;
2098 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04002099 if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002100 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
2102 if (onMask != 0) {
2103 int brightness = getPreferredBrightness();
2104 if ((newState & BATTERY_LOW_BIT) != 0 &&
2105 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
2106 brightness = Power.BRIGHTNESS_LOW_BATTERY;
2107 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04002108 if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002109 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112
The Android Open Source Project10592532009-03-18 17:39:46 -07002113 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002114 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002115 ? LightsService.BRIGHTNESS_MODE_SENSOR
2116 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07002117 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002118 if (DEBUG_SCREEN_ON) {
2119 RuntimeException e = new RuntimeException("here");
2120 e.fillInStackTrace();
2121 Slog.i(TAG, "Set LCD brightness: " + value, e);
2122 }
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002123 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07002124 }
2125 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002126 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07002127 }
2128 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002129 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07002130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
Joe Onoratob08a1af2010-10-11 19:28:58 -07002133 class BrightnessState implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 boolean initialized;
2137 int targetValue;
2138 float curValue;
2139 float delta;
2140 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 BrightnessState(int m) {
2143 mask = m;
2144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 public void dump(PrintWriter pw, String prefix) {
2147 pw.println(prefix + "animating=" + animating
2148 + " targetValue=" + targetValue
2149 + " curValue=" + curValue
2150 + " delta=" + delta);
2151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
Dianne Hackborn474fd742011-10-10 18:40:22 -07002153 void forceValueLocked(int value) {
2154 targetValue = -1;
2155 curValue = value;
2156 setLightBrightness(mask, value);
2157 if (animating) {
2158 finishAnimationLocked(false, value);
2159 }
2160 }
2161
Joe Onoratob08a1af2010-10-11 19:28:58 -07002162 void setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07002163 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 if (!initialized) {
2165 initialized = true;
2166 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07002167 } else if (targetValue == target) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002168 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07002171 delta = (targetValue -
2172 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
2173 / stepsToTarget;
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002174 if (mSpew || DEBUG_SCREEN_ON) {
Joe Onorato128e7292009-03-24 18:41:31 -07002175 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
Joe Onorato3d3db602010-10-18 16:08:16 -04002176 Slog.i(TAG, "setTargetLocked mask=" + mask + " curValue=" + curValue
2177 + " target=" + target + " targetValue=" + targetValue + " delta=" + delta
Joe Onorato128e7292009-03-24 18:41:31 -07002178 + " nominalCurrentValue=" + nominalCurrentValue
2179 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181 animating = true;
Joe Onoratob08a1af2010-10-11 19:28:58 -07002182
2183 if (mSpew) {
2184 Slog.i(TAG, "scheduling light animator");
2185 }
2186 mScreenOffHandler.removeCallbacks(this);
2187 mScreenOffHandler.post(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 boolean stepLocked() {
2191 if (!animating) return false;
2192 if (false && mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002193 Slog.i(TAG, "Step target " + mask + ": cur=" + curValue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 + " target=" + targetValue + " delta=" + delta);
2195 }
2196 curValue += delta;
2197 int curIntValue = (int)curValue;
2198 boolean more = true;
2199 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07002200 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 more = false;
2202 } else if (delta > 0) {
2203 if (curIntValue >= targetValue) {
2204 curValue = curIntValue = targetValue;
2205 more = false;
2206 }
2207 } else {
2208 if (curIntValue <= targetValue) {
2209 curValue = curIntValue = targetValue;
2210 more = false;
2211 }
2212 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002213 if (mSpew) Slog.d(TAG, "Animating curIntValue=" + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002214 setLightBrightness(mask, curIntValue);
Joe Onorato3d3db602010-10-18 16:08:16 -04002215 finishAnimationLocked(more, curIntValue);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002216 return more;
2217 }
2218
Joe Onorato3d3db602010-10-18 16:08:16 -04002219 void jumpToTargetLocked() {
2220 if (mSpew) Slog.d(TAG, "jumpToTargetLocked targetValue=" + targetValue + ": " + mask);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002221 setLightBrightness(mask, targetValue);
2222 final int tv = targetValue;
2223 curValue = tv;
2224 targetValue = -1;
Joe Onorato3d3db602010-10-18 16:08:16 -04002225 finishAnimationLocked(false, tv);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002226 }
2227
Joe Onorato3d3db602010-10-18 16:08:16 -04002228 private void finishAnimationLocked(boolean more, int curIntValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 animating = more;
2230 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002231 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002232 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 public void run() {
Dianne Hackborne02c88a2011-10-28 13:58:15 -07002238 synchronized (mLocks) {
2239 // we're turning off
2240 final boolean turningOff = animating && targetValue == Power.BRIGHTNESS_OFF;
2241 if (mAnimateScreenLights || !turningOff) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002242 long now = SystemClock.uptimeMillis();
2243 boolean more = mScreenBrightness.stepLocked();
2244 if (more) {
2245 mScreenOffHandler.postAtTime(this, now+(1000/60));
2246 }
Dianne Hackborne02c88a2011-10-28 13:58:15 -07002247 } else {
2248 // It's pretty scary to hold mLocks for this long, and we should
2249 // redesign this, but it works for now.
2250 nativeStartSurfaceFlingerAnimation(
2251 mScreenOffReason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
2252 ? 0 : mAnimationSetting);
Joe Onorato3d3db602010-10-18 16:08:16 -04002253 mScreenBrightness.jumpToTargetLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
2256 }
2257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 private int getPreferredBrightness() {
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002260 if (mScreenBrightnessOverride >= 0) {
2261 return mScreenBrightnessOverride;
2262 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
2263 && mAutoBrightessEnabled) {
2264 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002266 final int brightness = mScreenBrightnessSetting;
2267 // Don't let applications turn the screen all the way off
2268 return Math.max(brightness, mScreenBrightnessDim);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 }
2270
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002271 private int applyButtonState(int state) {
2272 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002273 if ((state & BATTERY_LOW_BIT) != 0) {
2274 // do not override brightness if the battery is low
2275 return state;
2276 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002277 if (mButtonBrightnessOverride >= 0) {
2278 brightness = mButtonBrightnessOverride;
2279 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
2280 brightness = mLightSensorButtonBrightness;
2281 }
2282 if (brightness > 0) {
2283 return state | BUTTON_BRIGHT_BIT;
2284 } else if (brightness == 0) {
2285 return state & ~BUTTON_BRIGHT_BIT;
2286 } else {
2287 return state;
2288 }
2289 }
2290
2291 private int applyKeyboardState(int state) {
2292 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002293 if ((state & BATTERY_LOW_BIT) != 0) {
2294 // do not override brightness if the battery is low
2295 return state;
2296 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002297 if (!mKeyboardVisible) {
2298 brightness = 0;
2299 } else if (mButtonBrightnessOverride >= 0) {
2300 brightness = mButtonBrightnessOverride;
2301 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
2302 brightness = mLightSensorKeyboardBrightness;
2303 }
2304 if (brightness > 0) {
2305 return state | KEYBOARD_BRIGHT_BIT;
2306 } else if (brightness == 0) {
2307 return state & ~KEYBOARD_BRIGHT_BIT;
2308 } else {
2309 return state;
2310 }
2311 }
2312
Charles Mendis322591c2009-10-29 11:06:59 -07002313 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 synchronized (mLocks) {
2315 return (mPowerState & SCREEN_ON_BIT) != 0;
2316 }
2317 }
2318
Charles Mendis322591c2009-10-29 11:06:59 -07002319 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 synchronized (mLocks) {
2321 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
2322 }
2323 }
2324
Mike Lockwood497087e32009-11-08 18:33:03 -05002325 private boolean isScreenTurningOffLocked() {
2326 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
2327 }
2328
Joe Onorato4b9f62d2010-10-11 13:41:35 -07002329 private boolean shouldLog(long time) {
2330 synchronized (mLocks) {
2331 if (time > (mWarningSpewThrottleTime + (60*60*1000))) {
2332 mWarningSpewThrottleTime = time;
2333 mWarningSpewThrottleCount = 0;
2334 return true;
2335 } else if (mWarningSpewThrottleCount < 30) {
2336 mWarningSpewThrottleCount++;
2337 return true;
2338 } else {
2339 return false;
2340 }
2341 }
2342 }
2343
Mike Lockwood200b30b2009-09-20 00:23:59 -04002344 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05002345 if (isScreenTurningOffLocked()) {
2346 // cancel animation so userActivity will succeed
2347 mScreenBrightness.animating = false;
2348 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002349 boolean savedActivityAllowed = mUserActivityAllowed;
2350 mUserActivityAllowed = true;
2351 userActivity(SystemClock.uptimeMillis(), false);
2352 mUserActivityAllowed = savedActivityAllowed;
2353 }
2354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
2356 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Joe Onorato7999bff2010-07-24 11:50:05 -04002357 userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359
2360 public void userActivity(long time, boolean noChangeLights) {
Joe Onorato4b9f62d2010-10-11 13:41:35 -07002361 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
2362 != PackageManager.PERMISSION_GRANTED) {
2363 if (shouldLog(time)) {
2364 Slog.w(TAG, "Caller does not have DEVICE_POWER permission. pid="
2365 + Binder.getCallingPid() + " uid=" + Binder.getCallingUid());
2366 }
2367 return;
2368 }
2369
Joe Onorato7999bff2010-07-24 11:50:05 -04002370 userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372
2373 public void userActivity(long time, boolean noChangeLights, int eventType) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002374 userActivity(time, -1, noChangeLights, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376
2377 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002378 userActivity(time, -1, noChangeLights, eventType, force);
2379 }
2380
2381 /*
2382 * Reset the user activity timeout to now + timeout. This overrides whatever else is going
2383 * on with user activity. Don't use this function.
2384 */
2385 public void clearUserActivityTimeout(long now, long timeout) {
2386 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2387 Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2388 userActivity(now, timeout, false, OTHER_EVENT, false);
2389 }
2390
2391 private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2392 int eventType, boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393
Joe Onorato1a542c72010-11-08 09:48:20 -08002394 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0) && (eventType == TOUCH_EVENT)) {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002395 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002396 Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002397 }
2398 return;
2399 }
2400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 synchronized (mLocks) {
2402 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002403 Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 + " mUserActivityAllowed=" + mUserActivityAllowed
2405 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07002406 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
2407 + " mProximitySensorActive=" + mProximitySensorActive
Joe Onorato797e6882010-08-26 14:46:01 -04002408 + " timeoutOverride=" + timeoutOverride
Mike Lockwood36fc3022009-08-25 16:49:06 -07002409 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Mike Lockwood05067122009-10-27 23:07:25 -04002411 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05002412 if (isScreenTurningOffLocked()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002413 Slog.d(TAG, "ignoring user activity while turning off screen");
Mike Lockwood05067122009-10-27 23:07:25 -04002414 return;
2415 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002416 // Disable proximity sensor if if user presses power key while we are in the
2417 // "waiting for proximity sensor to go negative" state.
2418 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
2419 mProximitySensorActive = false;
2420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 if (mLastEventTime <= time || force) {
2422 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002423 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002424 // Only turn on button backlights if a button was pressed
2425 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05002426 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
2428 } else {
2429 // don't clear button/keyboard backlights when the screen is touched.
2430 mUserState |= SCREEN_BRIGHT;
2431 }
2432
Dianne Hackborn617f8772009-03-31 15:04:46 -07002433 int uid = Binder.getCallingUid();
2434 long ident = Binder.clearCallingIdentity();
2435 try {
2436 mBatteryStats.noteUserActivity(uid, eventType);
2437 } catch (RemoteException e) {
2438 // Ignore
2439 } finally {
2440 Binder.restoreCallingIdentity(ident);
2441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002442
Michael Chane96440f2009-05-06 10:27:36 -07002443 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002444 setPowerState(mUserState | mWakeLockState, noChangeLights,
2445 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
Joe Onorato7999bff2010-07-24 11:50:05 -04002446 setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 }
2449 }
Mike Lockwoodef731622010-01-27 17:51:34 -05002450
2451 if (mPolicy != null) {
2452 mPolicy.userActivity();
2453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
2455
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002456 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2457 try {
2458 int i;
2459 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2460 if (sensorValue < mAutoBrightnessLevels[i]) {
2461 break;
2462 }
2463 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002464 // This is the range of brightness values that we can use.
2465 final int minval = values[0];
2466 final int maxval = values[mAutoBrightnessLevels.length];
2467 // This is the range we will be scaling. We put some padding
2468 // at the low and high end to give the adjustment a little better
2469 // impact on the actual observed value.
2470 final int range = (maxval-minval) + LIGHT_SENSOR_RANGE_EXPANSION;
2471 // This is the desired brightness value from 0.0 to 1.0.
2472 float valf = ((values[i]-minval+(LIGHT_SENSOR_RANGE_EXPANSION/2))/(float)range);
2473 // Apply a scaling to the value based on the adjustment.
2474 if (mLightSensorAdjustSetting > 0 && mLightSensorAdjustSetting <= 1) {
2475 float adj = (float)Math.sqrt(1.0f-mLightSensorAdjustSetting);
2476 if (adj <= .00001) {
2477 valf = 1;
2478 } else {
2479 valf /= adj;
2480 }
2481 } else if (mLightSensorAdjustSetting < 0 && mLightSensorAdjustSetting >= -1) {
2482 float adj = (float)Math.sqrt(1.0f+mLightSensorAdjustSetting);
2483 valf *= adj;
2484 }
2485 // Apply an additional offset to the value based on the adjustment.
2486 valf += mLightSensorAdjustSetting/LIGHT_SENSOR_OFFSET_SCALE;
2487 // Convert the 0.0-1.0 value back to a brightness integer.
2488 int val = (int)((valf*range)+minval) - (LIGHT_SENSOR_RANGE_EXPANSION/2);
2489 if (val < minval) val = minval;
2490 else if (val > maxval) val = maxval;
2491 return val;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002492 } catch (Exception e) {
2493 // guard against null pointer or index out of bounds errors
Joe Onorato8a9b2202010-02-26 18:56:32 -08002494 Slog.e(TAG, "getAutoBrightnessValue", e);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002495 return 255;
2496 }
2497 }
2498
Mike Lockwood20f87d72009-11-05 16:08:51 -05002499 private Runnable mProximityTask = new Runnable() {
2500 public void run() {
2501 synchronized (mLocks) {
2502 if (mProximityPendingValue != -1) {
2503 proximityChangedLocked(mProximityPendingValue == 1);
2504 mProximityPendingValue = -1;
2505 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002506 if (mProximityPartialLock.isHeld()) {
2507 mProximityPartialLock.release();
2508 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002509 }
2510 }
2511 };
2512
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002513 private Runnable mAutoBrightnessTask = new Runnable() {
2514 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002515 synchronized (mLocks) {
Jim Rodovichd102fea2010-09-02 12:30:49 -05002516 if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
2517 int value = (int)mLightSensorPendingValue;
2518 mLightSensorPendingDecrease = false;
2519 mLightSensorPendingIncrease = false;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002520 lightSensorChangedLocked(value, false);
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002521 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002522 }
2523 }
2524 };
2525
Mike Lockwoodb2865412010-02-02 22:40:33 -05002526 private void dockStateChanged(int state) {
2527 synchronized (mLocks) {
2528 mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2529 if (mIsDocked) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04002530 // allow brightness to decrease when docked
Mike Lockwoodb2865412010-02-02 22:40:33 -05002531 mHighestLightSensorValue = -1;
2532 }
2533 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2534 // force lights recalculation
2535 int value = (int)mLightSensorValue;
2536 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002537 lightSensorChangedLocked(value, false);
Mike Lockwoodb2865412010-02-02 22:40:33 -05002538 }
2539 }
2540 }
2541
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002542 private void lightSensorChangedLocked(int value, boolean immediate) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002543 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002544 Slog.d(TAG, "lightSensorChangedLocked " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002545 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002546
Joe Onorato06eb33a2010-10-25 14:09:21 -07002547 // Don't do anything if the screen is off.
2548 if ((mPowerState & SCREEN_ON_BIT) == 0) {
2549 if (mDebugLightSensor) {
2550 Slog.d(TAG, "dropping lightSensorChangedLocked because screen is off");
2551 }
2552 return;
2553 }
2554
Mike Lockwoodb2865412010-02-02 22:40:33 -05002555 // do not allow light sensor value to decrease
2556 if (mHighestLightSensorValue < value) {
2557 mHighestLightSensorValue = value;
2558 }
2559
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002560 if (mLightSensorValue != value) {
2561 mLightSensorValue = value;
2562 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
Mike Lockwoodb2865412010-02-02 22:40:33 -05002563 // use maximum light sensor value seen since screen went on for LCD to avoid flicker
2564 // we only do this if we are undocked, since lighting should be stable when
2565 // stationary in a dock.
2566 int lcdValue = getAutoBrightnessValue(
2567 (mIsDocked ? value : mHighestLightSensorValue),
2568 mLcdBacklightValues);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002569 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002570 int keyboardValue;
2571 if (mKeyboardVisible) {
2572 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2573 } else {
2574 keyboardValue = 0;
2575 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002576 mLightSensorScreenBrightness = lcdValue;
2577 mLightSensorButtonBrightness = buttonValue;
2578 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002579
2580 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002581 Slog.d(TAG, "lcdValue " + lcdValue);
2582 Slog.d(TAG, "buttonValue " + buttonValue);
2583 Slog.d(TAG, "keyboardValue " + keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002584 }
2585
Mike Lockwood4984e732009-11-01 08:16:33 -05002586 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002587 if (!mSkippedScreenOn) {
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002588 mScreenBrightness.setTargetLocked(lcdValue,
2589 immediate ? IMMEDIATE_ANIM_STEPS : AUTOBRIGHTNESS_ANIM_STEPS,
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002590 INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue);
2591 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002592 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002593 if (mButtonBrightnessOverride < 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002594 mButtonLight.setBrightness(buttonValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002595 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002596 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002597 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002598 }
2599 }
2600 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002601 }
2602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 /**
2604 * The user requested that we go to sleep (probably with the power button).
2605 * This overrides all wake locks that are held.
2606 */
2607 public void goToSleep(long time)
2608 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002609 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2610 }
2611
2612 /**
2613 * The user requested that we go to sleep (probably with the power button).
2614 * This overrides all wake locks that are held.
2615 */
2616 public void goToSleepWithReason(long time, int reason)
2617 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2619 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002620 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
2622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002625 * Reboot the device immediately, passing 'reason' (may be null)
2626 * to the underlying __reboot system call. Should not return.
2627 */
2628 public void reboot(String reason)
2629 {
2630 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002631
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002632 if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
2633 throw new IllegalStateException("Too early to call reboot()");
2634 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002635
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002636 final String finalReason = reason;
2637 Runnable runnable = new Runnable() {
2638 public void run() {
2639 synchronized (this) {
2640 ShutdownThread.reboot(mContext, finalReason, false);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002641 }
2642
San Mehat1e512792010-01-07 10:40:29 -08002643 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002644 };
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002645 // ShutdownThread must run on a looper capable of displaying the UI.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002646 mHandler.post(runnable);
2647
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002648 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002649 synchronized (runnable) {
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002650 while (true) {
2651 try {
2652 runnable.wait();
2653 } catch (InterruptedException e) {
2654 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002655 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002656 }
2657 }
2658
Dan Egnor60d87622009-12-16 16:32:58 -08002659 /**
2660 * Crash the runtime (causing a complete restart of the Android framework).
2661 * Requires REBOOT permission. Mostly for testing. Should not return.
2662 */
2663 public void crash(final String message)
2664 {
2665 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2666 Thread t = new Thread("PowerManagerService.crash()") {
2667 public void run() { throw new RuntimeException(message); }
2668 };
2669 try {
2670 t.start();
2671 t.join();
2672 } catch (InterruptedException e) {
2673 Log.wtf(TAG, e);
2674 }
2675 }
2676
Mike Lockwood435eb642009-12-03 08:40:18 -05002677 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
2679 if (mLastEventTime <= time) {
2680 mLastEventTime = time;
2681 // cancel all of the wake locks
2682 mWakeLockState = SCREEN_OFF;
2683 int N = mLocks.size();
2684 int numCleared = 0;
Joe Onorato8274a0e2010-10-05 17:38:09 -04002685 boolean proxLock = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 for (int i=0; i<N; i++) {
2687 WakeLock wl = mLocks.get(i);
2688 if (isScreenLock(wl.flags)) {
Joe Onorato8274a0e2010-10-05 17:38:09 -04002689 if (((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
2690 && reason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
2691 proxLock = true;
2692 } else {
2693 mLocks.get(i).activated = false;
2694 numCleared++;
2695 }
2696 }
2697 }
2698 if (!proxLock) {
2699 mProxIgnoredBecauseScreenTurnedOff = true;
2700 if (mDebugProximitySensor) {
2701 Slog.d(TAG, "setting mProxIgnoredBecauseScreenTurnedOff");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
2703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002704 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002705 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002707 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 cancelTimerLocked();
2709 }
2710 }
2711
2712 public long timeSinceScreenOn() {
2713 synchronized (mLocks) {
2714 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2715 return 0;
2716 }
2717 return SystemClock.elapsedRealtime() - mScreenOffTime;
2718 }
2719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002722 synchronized (mLocks) {
2723 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002724 Slog.d(TAG, "setKeyboardVisibility: " + visible);
Mike Lockwooda625b382009-09-12 17:36:03 -07002725 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002726 if (mKeyboardVisible != visible) {
2727 mKeyboardVisible = visible;
2728 // don't signal user activity if the screen is off; other code
2729 // will take care of turning on due to a true change to the lid
2730 // switch and synchronized with the lock screen.
2731 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002732 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002733 // force recompute of backlight values
2734 if (mLightSensorValue >= 0) {
2735 int value = (int)mLightSensorValue;
2736 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002737 lightSensorChangedLocked(value, false);
Mike Lockwooddf024922009-10-29 21:29:15 -04002738 }
2739 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002740 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2741 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002742 }
2743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745
2746 /**
2747 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002748 * When disabling user activity we also reset user power state so the keyguard can reset its
2749 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 */
2751 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002752 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002753 Slog.d(TAG, "enableUserActivity " + enabled);
Mike Lockwood50c548d2009-11-09 16:02:06 -05002754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 synchronized (mLocks) {
2756 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002757 if (!enabled) {
2758 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2759 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 }
2762 }
2763
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002764 private void setScreenBrightnessMode(int mode) {
Joe Onoratod28f7532010-11-06 12:56:53 -07002765 synchronized (mLocks) {
2766 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
2767 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
2768 mAutoBrightessEnabled = enabled;
2769 // This will get us a new value
2770 enableLightSensorLocked(mAutoBrightessEnabled && isScreenOn());
Mike Lockwood2d155d22009-10-27 09:32:30 -04002771 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002772 }
2773 }
2774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 /** Sets the screen off timeouts:
2776 * mKeylightDelay
2777 * mDimDelay
2778 * mScreenOffDelay
2779 * */
2780 private void setScreenOffTimeoutsLocked() {
2781 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002782 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 mDimDelay = -1;
2784 mScreenOffDelay = 0;
2785 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2786 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2787 mDimDelay = -1;
2788 mScreenOffDelay = 0;
2789 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002790 int totalDelay = mScreenOffTimeoutSetting;
2791 if (totalDelay > mMaximumScreenOffTimeout) {
2792 totalDelay = mMaximumScreenOffTimeout;
2793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2795 if (totalDelay < 0) {
Jim Millerbc4603b2010-08-30 21:21:34 -07002796 // negative number means stay on as long as possible.
2797 mScreenOffDelay = mMaximumScreenOffTimeout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 } else if (mKeylightDelay < totalDelay) {
2799 // subtract the time that the keylight delay. This will give us the
2800 // remainder of the time that we need to sleep to get the accurate
2801 // screen off timeout.
2802 mScreenOffDelay = totalDelay - mKeylightDelay;
2803 } else {
2804 mScreenOffDelay = 0;
2805 }
2806 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2807 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2808 mScreenOffDelay = LONG_DIM_TIME;
2809 } else {
2810 mDimDelay = -1;
2811 }
2812 }
2813 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002814 Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2816 + " mDimScreen=" + mDimScreen);
2817 }
2818 }
2819
2820 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002821 * Refreshes cached secure settings. Called once on startup, and
2822 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 */
Doug Zongker43866e02010-01-07 12:09:54 -08002824 private void updateSettingsValues() {
2825 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002827 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 SHORT_KEYLIGHT_DELAY_DEFAULT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002829 // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831
2832 private class LockList extends ArrayList<WakeLock>
2833 {
2834 void addLock(WakeLock wl)
2835 {
2836 int index = getIndex(wl.binder);
2837 if (index < 0) {
2838 this.add(wl);
2839 }
2840 }
2841
2842 WakeLock removeLock(IBinder binder)
2843 {
2844 int index = getIndex(binder);
2845 if (index >= 0) {
2846 return this.remove(index);
2847 } else {
2848 return null;
2849 }
2850 }
2851
2852 int getIndex(IBinder binder)
2853 {
2854 int N = this.size();
2855 for (int i=0; i<N; i++) {
2856 if (this.get(i).binder == binder) {
2857 return i;
2858 }
2859 }
2860 return -1;
2861 }
2862
2863 int gatherState()
2864 {
2865 int result = 0;
2866 int N = this.size();
2867 for (int i=0; i<N; i++) {
2868 WakeLock wl = this.get(i);
2869 if (wl.activated) {
2870 if (isScreenLock(wl.flags)) {
2871 result |= wl.minState;
2872 }
2873 }
2874 }
2875 return result;
2876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002877
Michael Chane96440f2009-05-06 10:27:36 -07002878 int reactivateScreenLocksLocked()
2879 {
2880 int result = 0;
2881 int N = this.size();
2882 for (int i=0; i<N; i++) {
2883 WakeLock wl = this.get(i);
2884 if (isScreenLock(wl.flags)) {
2885 wl.activated = true;
2886 result |= wl.minState;
2887 }
2888 }
Joe Onorato8274a0e2010-10-05 17:38:09 -04002889 if (mDebugProximitySensor) {
2890 Slog.d(TAG, "reactivateScreenLocksLocked mProxIgnoredBecauseScreenTurnedOff="
2891 + mProxIgnoredBecauseScreenTurnedOff);
2892 }
2893 mProxIgnoredBecauseScreenTurnedOff = false;
Michael Chane96440f2009-05-06 10:27:36 -07002894 return result;
2895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
2897
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08002898 public void setPolicy(WindowManagerPolicy p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 synchronized (mLocks) {
2900 mPolicy = p;
2901 mLocks.notifyAll();
2902 }
2903 }
2904
2905 WindowManagerPolicy getPolicyLocked() {
2906 while (mPolicy == null || !mDoneBooting) {
2907 try {
2908 mLocks.wait();
2909 } catch (InterruptedException e) {
2910 // Ignore
2911 }
2912 }
2913 return mPolicy;
2914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002917 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2918 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2919 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002920 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002921 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002922 }
2923
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002924 // wait until sensors are enabled before turning on screen.
2925 // some devices will not activate the light sensor properly on boot
2926 // unless we do this.
2927 if (mUseSoftwareAutoBrightness) {
2928 // turn the screen on
2929 setPowerState(SCREEN_BRIGHT);
2930 } else {
2931 // turn everything on
2932 setPowerState(ALL_BRIGHT);
2933 }
2934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 synchronized (mLocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002936 Slog.d(TAG, "system ready!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 mDoneBooting = true;
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002938
Joe Onoratod28f7532010-11-06 12:56:53 -07002939 enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled);
2940
Dianne Hackborn617f8772009-03-31 15:04:46 -07002941 long identity = Binder.clearCallingIdentity();
2942 try {
2943 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2944 mBatteryStats.noteScreenOn();
2945 } catch (RemoteException e) {
2946 // Nothing interesting to do.
2947 } finally {
2948 Binder.restoreCallingIdentity(identity);
2949 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002950 }
2951 }
2952
2953 void bootCompleted() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002954 Slog.d(TAG, "bootCompleted");
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002955 synchronized (mLocks) {
2956 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2958 updateWakeLockLocked();
2959 mLocks.notifyAll();
2960 }
2961 }
2962
Joe Onoratob08a1af2010-10-11 19:28:58 -07002963 // for watchdog
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 public void monitor() {
2965 synchronized (mLocks) { }
2966 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002967
2968 public int getSupportedWakeLockFlags() {
2969 int result = PowerManager.PARTIAL_WAKE_LOCK
2970 | PowerManager.FULL_WAKE_LOCK
2971 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2972
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002973 if (mProximitySensor != null) {
2974 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2975 }
2976
2977 return result;
2978 }
2979
Mike Lockwood237a2992009-09-15 14:42:16 -04002980 public void setBacklightBrightness(int brightness) {
2981 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2982 // Don't let applications turn the screen all the way off
Joe Onoratob08a1af2010-10-11 19:28:58 -07002983 synchronized (mLocks) {
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002984 brightness = Math.max(brightness, mScreenBrightnessDim);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002985 mLcdLight.setBrightness(brightness);
2986 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2987 mButtonLight.setBrightness(brightness);
2988 long identity = Binder.clearCallingIdentity();
2989 try {
2990 mBatteryStats.noteScreenBrightness(brightness);
2991 } catch (RemoteException e) {
2992 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
2993 } finally {
2994 Binder.restoreCallingIdentity(identity);
2995 }
Mike Lockwood237a2992009-09-15 14:42:16 -04002996
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002997 mScreenBrightness.targetValue = brightness;
2998 mScreenBrightness.jumpToTargetLocked();
2999 }
3000 }
3001
3002 public void setAutoBrightnessAdjustment(float adj) {
3003 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
3004 synchronized (mLocks) {
3005 mLightSensorAdjustSetting = adj;
3006 if (mSensorManager != null && mLightSensorEnabled) {
3007 // clear calling identity so sensor manager battery stats are accurate
3008 long identity = Binder.clearCallingIdentity();
3009 try {
3010 // force recompute of backlight values
3011 if (mLightSensorValue >= 0) {
3012 int value = (int)mLightSensorValue;
3013 mLightSensorValue = -1;
3014 handleLightSensorValue(value, true);
3015 }
3016 } finally {
3017 Binder.restoreCallingIdentity(identity);
3018 }
Joe Onorato3d3db602010-10-18 16:08:16 -04003019 }
Mike Lockwood237a2992009-09-15 14:42:16 -04003020 }
3021 }
3022
Mike Lockwoodb11832d2009-11-25 15:25:55 -05003023 public void setAttentionLight(boolean on, int color) {
3024 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05003025 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05003026 }
3027
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003028 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003029 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003030 Slog.d(TAG, "enableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07003031 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003032 if (!mProximitySensorEnabled) {
3033 // clear calling identity so sensor manager battery stats are accurate
3034 long identity = Binder.clearCallingIdentity();
3035 try {
3036 mSensorManager.registerListener(mProximityListener, mProximitySensor,
3037 SensorManager.SENSOR_DELAY_NORMAL);
3038 mProximitySensorEnabled = true;
3039 } finally {
3040 Binder.restoreCallingIdentity(identity);
3041 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003042 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003043 }
3044
3045 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003046 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003047 Slog.d(TAG, "disableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07003048 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003049 if (mProximitySensorEnabled) {
3050 // clear calling identity so sensor manager battery stats are accurate
3051 long identity = Binder.clearCallingIdentity();
3052 try {
3053 mSensorManager.unregisterListener(mProximityListener);
3054 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003055 if (mProximityPartialLock.isHeld()) {
3056 mProximityPartialLock.release();
3057 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003058 mProximitySensorEnabled = false;
3059 } finally {
3060 Binder.restoreCallingIdentity(identity);
3061 }
3062 if (mProximitySensorActive) {
3063 mProximitySensorActive = false;
Joe Onorato8274a0e2010-10-05 17:38:09 -04003064 if (mDebugProximitySensor) {
3065 Slog.d(TAG, "disableProximityLockLocked mProxIgnoredBecauseScreenTurnedOff="
3066 + mProxIgnoredBecauseScreenTurnedOff);
3067 }
3068 if (!mProxIgnoredBecauseScreenTurnedOff) {
3069 forceUserActivityLocked();
3070 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003071 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04003072 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003073 }
3074
Mike Lockwood20f87d72009-11-05 16:08:51 -05003075 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003076 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003077 Slog.d(TAG, "proximityChangedLocked, active: " + active);
Mike Lockwood20f87d72009-11-05 16:08:51 -05003078 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003079 if (!mProximitySensorEnabled) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003080 Slog.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05003081 return;
3082 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003083 if (active) {
Joe Onorato8274a0e2010-10-05 17:38:09 -04003084 if (mDebugProximitySensor) {
3085 Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff="
3086 + mProxIgnoredBecauseScreenTurnedOff);
3087 }
3088 if (!mProxIgnoredBecauseScreenTurnedOff) {
3089 goToSleepLocked(SystemClock.uptimeMillis(),
3090 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
3091 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003092 mProximitySensorActive = true;
3093 } else {
3094 // proximity sensor negative events trigger as user activity.
3095 // temporarily set mUserActivityAllowed to true so this will work
3096 // even when the keyguard is on.
3097 mProximitySensorActive = false;
Joe Onorato8274a0e2010-10-05 17:38:09 -04003098 if (mDebugProximitySensor) {
3099 Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff="
3100 + mProxIgnoredBecauseScreenTurnedOff);
3101 }
3102 if (!mProxIgnoredBecauseScreenTurnedOff) {
3103 forceUserActivityLocked();
3104 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003105
3106 if (mProximityWakeLockCount == 0) {
3107 // disable sensor if we have no listeners left after proximity negative
3108 disableProximityLockLocked();
3109 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003110 }
3111 }
3112
Joe Onoratod28f7532010-11-06 12:56:53 -07003113 private void enableLightSensorLocked(boolean enable) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003114 if (mDebugLightSensor) {
Joe Onoratod28f7532010-11-06 12:56:53 -07003115 Slog.d(TAG, "enableLightSensorLocked enable=" + enable
3116 + " mAutoBrightessEnabled=" + mAutoBrightessEnabled);
3117 }
3118 if (!mAutoBrightessEnabled) {
3119 enable = false;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003120 }
3121 if (mSensorManager != null && mLightSensorEnabled != enable) {
3122 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003123 // clear calling identity so sensor manager battery stats are accurate
3124 long identity = Binder.clearCallingIdentity();
3125 try {
3126 if (enable) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003127 // reset our highest value when reenabling
3128 mHighestLightSensorValue = -1;
3129 // force recompute of backlight values
3130 if (mLightSensorValue >= 0) {
3131 int value = (int)mLightSensorValue;
3132 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003133 handleLightSensorValue(value, true);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003134 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003135 mSensorManager.registerListener(mLightListener, mLightSensor,
Mathias Agopian47f1fe52011-11-08 17:18:41 -08003136 LIGHT_SENSOR_RATE);
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003137 } else {
3138 mSensorManager.unregisterListener(mLightListener);
3139 mHandler.removeCallbacks(mAutoBrightnessTask);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003140 mLightSensorPendingDecrease = false;
3141 mLightSensorPendingIncrease = false;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003142 }
3143 } finally {
3144 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04003145 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003146 }
3147 }
3148
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003149 SensorEventListener mProximityListener = new SensorEventListener() {
3150 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05003151 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003152 synchronized (mLocks) {
3153 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05003154 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
3155 mLastProximityEventTime = milliseconds;
3156 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003157 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05003158
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003159 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05003160 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
3161 distance < mProximitySensor.getMaximumRange());
3162
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003163 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003164 Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active);
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003165 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003166 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
3167 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
3168 mProximityPendingValue = (active ? 1 : 0);
3169 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003170 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003171 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05003172 // process the value immediately
3173 mProximityPendingValue = -1;
3174 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003175 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003176
3177 // update mProximityPartialLock state
3178 boolean held = mProximityPartialLock.isHeld();
3179 if (!held && proximityTaskQueued) {
3180 // hold wakelock until mProximityTask runs
3181 mProximityPartialLock.acquire();
3182 } else if (held && !proximityTaskQueued) {
3183 mProximityPartialLock.release();
3184 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003185 }
3186 }
3187
3188 public void onAccuracyChanged(Sensor sensor, int accuracy) {
3189 // ignore
3190 }
3191 };
3192
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003193 private void handleLightSensorValue(int value, boolean immediate) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003194 long milliseconds = SystemClock.elapsedRealtime();
3195 if (mLightSensorValue == -1 ||
3196 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
3197 // process the value immediately if screen has just turned on
3198 mHandler.removeCallbacks(mAutoBrightnessTask);
3199 mLightSensorPendingDecrease = false;
3200 mLightSensorPendingIncrease = false;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003201 lightSensorChangedLocked(value, immediate);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003202 } else {
3203 if ((value > mLightSensorValue && mLightSensorPendingDecrease) ||
3204 (value < mLightSensorValue && mLightSensorPendingIncrease) ||
3205 (value == mLightSensorValue) ||
3206 (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) {
3207 // delay processing to debounce the sensor
3208 mHandler.removeCallbacks(mAutoBrightnessTask);
3209 mLightSensorPendingDecrease = (value < mLightSensorValue);
3210 mLightSensorPendingIncrease = (value > mLightSensorValue);
3211 if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
3212 mLightSensorPendingValue = value;
3213 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
3214 }
3215 } else {
3216 mLightSensorPendingValue = value;
3217 }
3218 }
3219 }
3220
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003221 SensorEventListener mLightListener = new SensorEventListener() {
3222 public void onSensorChanged(SensorEvent event) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003223 if (mDebugLightSensor) {
3224 Slog.d(TAG, "onSensorChanged: light value: " + event.values[0]);
3225 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003226 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05003227 // ignore light sensor while screen is turning off
3228 if (isScreenTurningOffLocked()) {
3229 return;
3230 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003231 handleLightSensorValue((int)event.values[0], false);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003232 }
3233 }
3234
3235 public void onAccuracyChanged(Sensor sensor, int accuracy) {
3236 // ignore
3237 }
3238 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239}