blob: 7b4372fec8dad0ec25967bad57ef982613bf2a39 [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();
Todd Poynor94d00242012-01-11 20:14:34 -0800586 Power.powerInitNative();
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700587 synchronized (mLocks) {
588 updateNativePowerStateLocked();
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -0700589 // We make sure to start out with the screen on due to user activity.
590 // (They did just boot their device, after all.)
591 forceUserActivityLocked();
Dianne Hackborn40011092011-09-22 13:37:48 -0700592 mInitialized = true;
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 void initInThread() {
597 mHandler = new Handler();
598
599 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700600 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
602 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
603 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
604 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
605 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
606 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500607 mProximityPartialLock = new UnsynchronizedWakeLock(
608 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
611 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
612 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
613 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
614
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700615 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400616
Joe Onoratob08a1af2010-10-11 19:28:58 -0700617 mAnimateScreenLights = resources.getBoolean(
618 com.android.internal.R.bool.config_animateScreenLights);
619
Joe Onorato6d747652010-10-11 15:15:31 -0700620 mUnplugTurnsOnScreen = resources.getBoolean(
621 com.android.internal.R.bool.config_unplugTurnsOnScreen);
622
Mike Lockwoodeb6456b2011-09-13 15:24:02 -0400623 mScreenBrightnessDim = resources.getInteger(
624 com.android.internal.R.integer.config_screenBrightnessDim);
625
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400626 // read settings for auto-brightness
627 mUseSoftwareAutoBrightness = resources.getBoolean(
628 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400629 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700630 mAutoBrightnessLevels = resources.getIntArray(
631 com.android.internal.R.array.config_autoBrightnessLevels);
632 mLcdBacklightValues = resources.getIntArray(
633 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
634 mButtonBacklightValues = resources.getIntArray(
635 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
636 mKeyboardBacklightValues = resources.getIntArray(
637 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500638 mLightSensorWarmupTime = resources.getInteger(
639 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700640 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700641
642 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
644 "(" + Settings.System.NAME + "=?) or ("
645 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700646 + Settings.System.NAME + "=?) or ("
Joe Onorato609695d2010-10-14 14:57:49 -0700647 + Settings.System.NAME + "=?) or ("
648 + Settings.System.NAME + "=?) or ("
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800649 + Settings.System.NAME + "=?) or ("
650 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 + Settings.System.NAME + "=?)",
Dianne Hackbornd9ea4682012-01-20 18:36:40 -0800652 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, SCREEN_BRIGHTNESS,
653 SCREEN_BRIGHTNESS_MODE, SCREEN_AUTO_BRIGHTNESS_ADJ,
654 WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 null);
656 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
657 SettingsObserver settingsObserver = new SettingsObserver();
658 mSettings.addObserver(settingsObserver);
659
660 // pretend that the settings changed so we will get their initial state
661 settingsObserver.update(mSettings, null);
662
663 // register for the battery changed notifications
664 IntentFilter filter = new IntentFilter();
665 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
666 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500667 filter = new IntentFilter();
668 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
669 mContext.registerReceiver(new BootCompletedReceiver(), filter);
Mike Lockwoodb2865412010-02-02 22:40:33 -0500670 filter = new IntentFilter();
671 filter.addAction(Intent.ACTION_DOCK_EVENT);
672 mContext.registerReceiver(new DockReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
Doug Zongker43866e02010-01-07 12:09:54 -0800674 // Listen for secure settings changes
675 mContext.getContentResolver().registerContentObserver(
676 Settings.Secure.CONTENT_URI, true,
677 new ContentObserver(new Handler()) {
678 public void onChange(boolean selfChange) {
679 updateSettingsValues();
680 }
681 });
682 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 synchronized (mHandlerThread) {
685 mInitComplete = true;
686 mHandlerThread.notifyAll();
687 }
688 }
689
690 private class WakeLock implements IBinder.DeathRecipient
691 {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700692 WakeLock(int f, IBinder b, String t, int u, int p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 super();
694 flags = f;
695 binder = b;
696 tag = t;
697 uid = u == MY_UID ? Process.SYSTEM_UID : u;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700698 pid = p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (u != MY_UID || (
700 !"KEEP_SCREEN_ON_FLAG".equals(tag)
701 && !"KeyInputQueue".equals(tag))) {
702 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
703 ? BatteryStats.WAKE_TYPE_PARTIAL
704 : BatteryStats.WAKE_TYPE_FULL;
705 } else {
706 monitorType = -1;
707 }
708 try {
709 b.linkToDeath(this, 0);
710 } catch (RemoteException e) {
711 binderDied();
712 }
713 }
714 public void binderDied() {
715 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500716 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 }
719 final int flags;
720 final IBinder binder;
721 final String tag;
722 final int uid;
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400723 final int pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 final int monitorType;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700725 WorkSource ws;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 boolean activated = true;
727 int minState;
728 }
729
730 private void updateWakeLockLocked() {
731 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
732 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
733 mStayOnWhilePluggedInScreenDimLock.acquire();
734 mStayOnWhilePluggedInPartialLock.acquire();
735 } else {
736 mStayOnWhilePluggedInScreenDimLock.release();
737 mStayOnWhilePluggedInPartialLock.release();
738 }
739 }
740
741 private boolean isScreenLock(int flags)
742 {
743 int n = flags & LOCK_MASK;
744 return n == PowerManager.FULL_WAKE_LOCK
745 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Joe Onorato8274a0e2010-10-05 17:38:09 -0400746 || n == PowerManager.SCREEN_DIM_WAKE_LOCK
747 || n == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700750 void enforceWakeSourcePermission(int uid, int pid) {
751 if (uid == Process.myUid()) {
752 return;
753 }
754 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
755 pid, uid, null);
756 }
757
758 public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 int uid = Binder.getCallingUid();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700760 int pid = Binder.getCallingPid();
Michael Chane96440f2009-05-06 10:27:36 -0700761 if (uid != Process.myUid()) {
762 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
763 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700764 if (ws != null) {
765 enforceWakeSourcePermission(uid, pid);
766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 long ident = Binder.clearCallingIdentity();
768 try {
769 synchronized (mLocks) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700770 acquireWakeLockLocked(flags, lock, uid, pid, tag, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772 } finally {
773 Binder.restoreCallingIdentity(ident);
774 }
775 }
776
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700777 void noteStartWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700778 if (wl.monitorType >= 0) {
779 long origId = Binder.clearCallingIdentity();
780 try {
781 if (ws != null) {
782 mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
783 wl.monitorType);
784 } else {
785 mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
786 }
787 } catch (RemoteException e) {
788 // Ignore
789 } finally {
790 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700791 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700792 }
793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700795 void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700796 if (wl.monitorType >= 0) {
797 long origId = Binder.clearCallingIdentity();
798 try {
799 if (ws != null) {
800 mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
801 wl.monitorType);
802 } else {
803 mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
804 }
805 } catch (RemoteException e) {
806 // Ignore
807 } finally {
808 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700809 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700810 }
811 }
812
813 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
814 WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700819 if (ws != null && ws.size() == 0) {
820 ws = null;
821 }
822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 int index = mLocks.getIndex(lock);
824 WakeLock wl;
825 boolean newlock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700826 boolean diffsource;
827 WorkSource oldsource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 if (index < 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700829 wl = new WakeLock(flags, lock, tag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 switch (wl.flags & LOCK_MASK)
831 {
832 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500833 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400834 wl.minState = SCREEN_BRIGHT;
835 } else {
836 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 break;
839 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
840 wl.minState = SCREEN_BRIGHT;
841 break;
842 case PowerManager.SCREEN_DIM_WAKE_LOCK:
843 wl.minState = SCREEN_DIM;
844 break;
845 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700846 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 break;
848 default:
849 // just log and bail. we're in the server, so don't
850 // throw an exception.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800851 Slog.e(TAG, "bad wakelock type for lock '" + tag + "' "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 + " flags=" + flags);
853 return;
854 }
855 mLocks.addLock(wl);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700856 if (ws != null) {
857 wl.ws = new WorkSource(ws);
858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 newlock = true;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700860 diffsource = false;
861 oldsource = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 } else {
863 wl = mLocks.get(index);
864 newlock = false;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700865 oldsource = wl.ws;
866 if (oldsource != null) {
867 if (ws == null) {
868 wl.ws = null;
869 diffsource = true;
870 } else {
871 diffsource = oldsource.diff(ws);
872 }
873 } else if (ws != null) {
874 diffsource = true;
875 } else {
876 diffsource = false;
877 }
878 if (diffsource) {
879 wl.ws = new WorkSource(ws);
880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882 if (isScreenLock(flags)) {
883 // if this causes a wakeup, we reactivate all of the locks and
884 // set it to whatever they want. otherwise, we modulate that
885 // by the current state so we never turn it more on than
886 // it already is.
Joe Onorato8274a0e2010-10-05 17:38:09 -0400887 if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
888 mProximityWakeLockCount++;
889 if (mProximityWakeLockCount == 1) {
890 enableProximityLockLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 } else {
Joe Onorato8274a0e2010-10-05 17:38:09 -0400893 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
894 int oldWakeLockState = mWakeLockState;
895 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwooddb97f602011-09-02 11:59:08 -0400896
897 // Disable proximity sensor if if user presses power key while we are in the
898 // "waiting for proximity sensor to go negative" state.
899 if ((mWakeLockState & SCREEN_ON_BIT) != 0
900 && mProximitySensorActive && mProximityWakeLockCount == 0) {
901 mProximitySensorActive = false;
902 }
903
Joe Onorato8274a0e2010-10-05 17:38:09 -0400904 if (mSpew) {
905 Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
906 + " mWakeLockState=0x"
907 + Integer.toHexString(mWakeLockState)
908 + " previous wakeLockState=0x"
909 + Integer.toHexString(oldWakeLockState));
910 }
911 } else {
912 if (mSpew) {
913 Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
914 + " mLocks.gatherState()=0x"
915 + Integer.toHexString(mLocks.gatherState())
916 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
917 }
918 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Joe Onorato8274a0e2010-10-05 17:38:09 -0400920 setPowerState(mWakeLockState | mUserState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
923 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
924 if (newlock) {
925 mPartialCount++;
926 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929 }
930 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700933 if (diffsource) {
934 // If the lock sources have changed, need to first release the
935 // old ones.
936 noteStopWakeLocked(wl, oldsource);
937 }
938 if (newlock || diffsource) {
939 noteStartWakeLocked(wl, ws);
940 }
941 }
942
943 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
944 int uid = Binder.getCallingUid();
945 int pid = Binder.getCallingPid();
946 if (ws != null && ws.size() == 0) {
947 ws = null;
948 }
949 if (ws != null) {
950 enforceWakeSourcePermission(uid, pid);
951 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700952 synchronized (mLocks) {
953 int index = mLocks.getIndex(lock);
954 if (index < 0) {
955 throw new IllegalArgumentException("Wake lock not active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700957 WakeLock wl = mLocks.get(index);
958 WorkSource oldsource = wl.ws;
959 wl.ws = ws != null ? new WorkSource(ws) : null;
960 noteStopWakeLocked(wl, oldsource);
961 noteStartWakeLocked(wl, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 }
964
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500965 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700966 int uid = Binder.getCallingUid();
967 if (uid != Process.myUid()) {
968 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
971 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500972 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974 }
975
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500976 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 WakeLock wl = mLocks.removeLock(lock);
978 if (wl == null) {
979 return;
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800983 Slog.d(TAG, "releaseWakeLock flags=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
985 }
986
987 if (isScreenLock(wl.flags)) {
Joe Onorato8274a0e2010-10-05 17:38:09 -0400988 if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
989 mProximityWakeLockCount--;
990 if (mProximityWakeLockCount == 0) {
991 if (mProximitySensorActive &&
992 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
993 // wait for proximity sensor to go negative before disabling sensor
994 if (mDebugProximitySensor) {
995 Slog.d(TAG, "waiting for proximity sensor to go negative");
996 }
997 } else {
998 disableProximityLockLocked();
999 }
1000 }
1001 } else {
1002 mWakeLockState = mLocks.gatherState();
1003 // goes in the middle to reduce flicker
1004 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
1005 userActivity(SystemClock.uptimeMillis(), -1, false, OTHER_EVENT, false);
1006 }
1007 setPowerState(mWakeLockState | mUserState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
1011 mPartialCount--;
1012 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001013 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 Power.releaseWakeLock(PARTIAL_NAME);
1015 }
1016 }
1017 // Unlink the lock from the binder.
1018 wl.binder.unlinkToDeath(wl, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Dianne Hackborn70be1672010-09-14 11:13:03 -07001020 noteStopWakeLocked(wl, wl.ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 private class PokeLock implements IBinder.DeathRecipient
1024 {
1025 PokeLock(int p, IBinder b, String t) {
1026 super();
1027 this.pokey = p;
1028 this.binder = b;
1029 this.tag = t;
1030 try {
1031 b.linkToDeath(this, 0);
1032 } catch (RemoteException e) {
1033 binderDied();
1034 }
1035 }
1036 public void binderDied() {
1037 setPokeLock(0, this.binder, this.tag);
1038 }
1039 int pokey;
1040 IBinder binder;
1041 String tag;
1042 boolean awakeOnSet;
1043 }
1044
1045 public void setPokeLock(int pokey, IBinder token, String tag) {
1046 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1047 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 return;
1050 }
1051
1052 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
1053 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
1054 + " and POKE_LOCK_MEDIUM_TIMEOUT");
1055 }
1056
1057 synchronized (mLocks) {
1058 if (pokey != 0) {
1059 PokeLock p = mPokeLocks.get(token);
1060 int oldPokey = 0;
1061 if (p != null) {
1062 oldPokey = p.pokey;
1063 p.pokey = pokey;
1064 } else {
1065 p = new PokeLock(pokey, token, tag);
1066 mPokeLocks.put(token, p);
1067 }
1068 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
1069 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
1070 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
1071 p.awakeOnSet = true;
1072 }
1073 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07001074 PokeLock rLock = mPokeLocks.remove(token);
1075 if (rLock != null) {
1076 token.unlinkToDeath(rLock, 0);
1077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079
1080 int oldPokey = mPokey;
1081 int cumulative = 0;
1082 boolean oldAwakeOnSet = mPokeAwakeOnSet;
1083 boolean awakeOnSet = false;
1084 for (PokeLock p: mPokeLocks.values()) {
1085 cumulative |= p.pokey;
1086 if (p.awakeOnSet) {
1087 awakeOnSet = true;
1088 }
1089 }
1090 mPokey = cumulative;
1091 mPokeAwakeOnSet = awakeOnSet;
1092
1093 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
1094 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (oldCumulativeTimeout != newCumulativeTimeout) {
1097 setScreenOffTimeoutsLocked();
1098 // reset the countdown timer, but use the existing nextState so it doesn't
1099 // change anything
1100 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
1101 }
1102 }
1103 }
1104
1105 private static String lockType(int type)
1106 {
1107 switch (type)
1108 {
1109 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001110 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001112 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001114 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -07001116 return "PARTIAL_WAKE_LOCK ";
1117 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
1118 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 default:
David Brown251faa62009-08-02 22:04:36 -07001120 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 }
1123
1124 private static String dumpPowerState(int state) {
1125 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
1126 ? "KEYBOARD_BRIGHT_BIT " : "")
1127 + (((state & SCREEN_BRIGHT_BIT) != 0)
1128 ? "SCREEN_BRIGHT_BIT " : "")
1129 + (((state & SCREEN_ON_BIT) != 0)
1130 ? "SCREEN_ON_BIT " : "")
1131 + (((state & BATTERY_LOW_BIT) != 0)
1132 ? "BATTERY_LOW_BIT " : "");
1133 }
1134
1135 @Override
1136 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1137 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1138 != PackageManager.PERMISSION_GRANTED) {
1139 pw.println("Permission Denial: can't dump PowerManager from from pid="
1140 + Binder.getCallingPid()
1141 + ", uid=" + Binder.getCallingUid());
1142 return;
1143 }
1144
1145 long now = SystemClock.uptimeMillis();
1146
Mike Lockwoodca44df82010-02-25 13:48:49 -05001147 synchronized (mLocks) {
1148 pw.println("Power Manager State:");
1149 pw.println(" mIsPowered=" + mIsPowered
1150 + " mPowerState=" + mPowerState
1151 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
1152 + " ms");
1153 pw.println(" mPartialCount=" + mPartialCount);
1154 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
1155 pw.println(" mUserState=" + dumpPowerState(mUserState));
1156 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
1157 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
1158 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
1159 + " " + ((mNextTimeout-now)/1000) + "s from now");
1160 pw.println(" mDimScreen=" + mDimScreen
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001161 + " mStayOnConditions=" + mStayOnConditions
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001162 + " mPreparingForScreenOn=" + mPreparingForScreenOn
1163 + " mSkippedScreenOn=" + mSkippedScreenOn);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001164 pw.println(" mScreenOffReason=" + mScreenOffReason
1165 + " mUserState=" + mUserState);
1166 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
1167 + ',' + mBroadcastQueue[2] + "}");
1168 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
1169 + ',' + mBroadcastWhy[2] + "}");
1170 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
1171 pw.println(" mKeyboardVisible=" + mKeyboardVisible
1172 + " mUserActivityAllowed=" + mUserActivityAllowed);
1173 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
1174 + " mScreenOffDelay=" + mScreenOffDelay);
1175 pw.println(" mPreventScreenOn=" + mPreventScreenOn
1176 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
1177 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
1178 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
1179 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
1180 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
1181 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
1182 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
1183 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
1184 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
1185 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
1186 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
1187 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1188 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
1189 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
1190 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08001191 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled
1192 + " mLightSensorAdjustSetting=" + mLightSensorAdjustSetting);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001193 pw.println(" mLightSensorValue=" + mLightSensorValue
1194 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
Jim Rodovichd102fea2010-09-02 12:30:49 -05001195 pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease
1196 + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease);
Mike Lockwoodca44df82010-02-25 13:48:49 -05001197 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
1198 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
1199 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
1200 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
1201 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
1202 mScreenBrightness.dump(pw, " mScreenBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
Mike Lockwoodca44df82010-02-25 13:48:49 -05001204 int N = mLocks.size();
1205 pw.println();
1206 pw.println("mLocks.size=" + N + ":");
1207 for (int i=0; i<N; i++) {
1208 WakeLock wl = mLocks.get(i);
1209 String type = lockType(wl.flags & LOCK_MASK);
1210 String acquireCausesWakeup = "";
1211 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
1212 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
1213 }
1214 String activated = "";
1215 if (wl.activated) {
1216 activated = " activated";
1217 }
1218 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001219 + activated + " (minState=" + wl.minState + ", uid=" + wl.uid
1220 + ", pid=" + wl.pid + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
Mike Lockwoodca44df82010-02-25 13:48:49 -05001222
1223 pw.println();
1224 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
1225 for (PokeLock p: mPokeLocks.values()) {
1226 pw.println(" poke lock '" + p.tag + "':"
Joe Onorato1a542c72010-11-08 09:48:20 -08001227 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0
1228 ? " POKE_LOCK_IGNORE_TOUCH_EVENTS" : "")
Mike Lockwoodca44df82010-02-25 13:48:49 -05001229 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1230 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1231 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1232 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
Mike Lockwoodca44df82010-02-25 13:48:49 -05001235 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238
Joe Onorato7999bff2010-07-24 11:50:05 -04001239 private void setTimeoutLocked(long now, int nextState) {
1240 setTimeoutLocked(now, -1, nextState);
1241 }
1242
1243 // If they gave a timeoutOverride it is the number of seconds
1244 // to screen-off. Figure out where in the countdown cycle we
1245 // should jump to.
Joe Onorato797e6882010-08-26 14:46:01 -04001246 private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
1247 long timeoutOverride = originalTimeoutOverride;
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001248 if (mBootCompleted) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001249 synchronized (mLocks) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001250 long when = 0;
1251 if (timeoutOverride <= 0) {
1252 switch (nextState)
1253 {
1254 case SCREEN_BRIGHT:
1255 when = now + mKeylightDelay;
1256 break;
1257 case SCREEN_DIM:
1258 if (mDimDelay >= 0) {
1259 when = now + mDimDelay;
Andreas Huber84047bc2010-07-27 16:49:10 -07001260 break;
Joe Onorato7999bff2010-07-24 11:50:05 -04001261 } else {
1262 Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1263 }
1264 case SCREEN_OFF:
1265 synchronized (mLocks) {
1266 when = now + mScreenOffDelay;
1267 }
1268 break;
Andreas Huber84047bc2010-07-27 16:49:10 -07001269 default:
1270 when = now;
1271 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001273 } else {
1274 override: {
1275 if (timeoutOverride <= mScreenOffDelay) {
1276 when = now + timeoutOverride;
1277 nextState = SCREEN_OFF;
1278 break override;
1279 }
1280 timeoutOverride -= mScreenOffDelay;
1281
1282 if (mDimDelay >= 0) {
1283 if (timeoutOverride <= mDimDelay) {
1284 when = now + timeoutOverride;
1285 nextState = SCREEN_DIM;
1286 break override;
1287 }
1288 timeoutOverride -= mDimDelay;
1289 }
1290
1291 when = now + timeoutOverride;
1292 nextState = SCREEN_BRIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001294 }
1295 if (mSpew) {
1296 Slog.d(TAG, "setTimeoutLocked now=" + now
1297 + " timeoutOverride=" + timeoutOverride
1298 + " nextState=" + nextState + " when=" + when);
1299 }
Joe Onorato797e6882010-08-26 14:46:01 -04001300
1301 mHandler.removeCallbacks(mTimeoutTask);
1302 mTimeoutTask.nextState = nextState;
1303 mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
1304 ? (originalTimeoutOverride - timeoutOverride)
1305 : -1;
Joe Onorato7999bff2010-07-24 11:50:05 -04001306 mHandler.postAtTime(mTimeoutTask, when);
1307 mNextTimeout = when; // for debugging
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 }
1311
1312 private void cancelTimerLocked()
1313 {
1314 mHandler.removeCallbacks(mTimeoutTask);
1315 mTimeoutTask.nextState = -1;
1316 }
1317
1318 private class TimeoutTask implements Runnable
1319 {
1320 int nextState; // access should be synchronized on mLocks
Joe Onorato797e6882010-08-26 14:46:01 -04001321 long remainingTimeoutOverride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 public void run()
1323 {
1324 synchronized (mLocks) {
1325 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001326 Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328
1329 if (nextState == -1) {
1330 return;
1331 }
1332
1333 mUserState = this.nextState;
1334 setPowerState(this.nextState | mWakeLockState);
1335
1336 long now = SystemClock.uptimeMillis();
1337
1338 switch (this.nextState)
1339 {
1340 case SCREEN_BRIGHT:
1341 if (mDimDelay >= 0) {
Joe Onorato797e6882010-08-26 14:46:01 -04001342 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 break;
1344 }
1345 case SCREEN_DIM:
Joe Onorato797e6882010-08-26 14:46:01 -04001346 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 break;
1348 }
1349 }
1350 }
1351 }
1352
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001353 private void sendNotificationLocked(boolean on, int why) {
1354 if (!mInitialized) {
1355 // No notifications sent until first initialization is done.
1356 // This is so that when we are moving from our initial state
1357 // which looks like the screen was off to it being on, we do not
1358 // go through the process of waiting for the higher-level user
1359 // space to be ready before turning up the display brightness.
1360 // (And also do not send needless broadcasts about the screen.)
1361 return;
1362 }
Dianne Hackborn40011092011-09-22 13:37:48 -07001363
1364 if (DEBUG_SCREEN_ON) {
1365 RuntimeException here = new RuntimeException("here");
1366 here.fillInStackTrace();
1367 Slog.i(TAG, "sendNotificationLocked: " + on, here);
1368 }
1369
Joe Onorato64c62ba2009-03-24 20:13:57 -07001370 if (!on) {
1371 mStillNeedSleepNotification = false;
1372 }
1373
Joe Onorato128e7292009-03-24 18:41:31 -07001374 // Add to the queue.
1375 int index = 0;
1376 while (mBroadcastQueue[index] != -1) {
1377 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
Joe Onorato128e7292009-03-24 18:41:31 -07001379 mBroadcastQueue[index] = on ? 1 : 0;
1380 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
Joe Onorato128e7292009-03-24 18:41:31 -07001382 // If we added it position 2, then there is a pair that can be stripped.
1383 // If we added it position 1 and we're turning the screen off, we can strip
1384 // the pair and do nothing, because the screen is already off, and therefore
1385 // keyguard has already been enabled.
1386 // However, if we added it at position 1 and we're turning it on, then position
1387 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1388 // on, so have to run the queue then.
1389 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001390 // While we're collapsing them, if it's going off, and the new reason
1391 // is more significant than the first, then use the new one.
1392 if (!on && mBroadcastWhy[0] > why) {
1393 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001394 }
1395 mBroadcastQueue[0] = on ? 1 : 0;
1396 mBroadcastQueue[1] = -1;
1397 mBroadcastQueue[2] = -1;
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001398 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
Mike Lockwood9c90a372010-04-13 15:40:27 -04001399 mBroadcastWakeLock.release();
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001400 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
Mike Lockwood9c90a372010-04-13 15:40:27 -04001401 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001402 index = 0;
1403 }
1404 if (index == 1 && !on) {
1405 mBroadcastQueue[0] = -1;
1406 mBroadcastQueue[1] = -1;
1407 index = -1;
1408 // The wake lock was being held, but we're not actually going to do any
1409 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001412 }
1413
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001414 // The broadcast queue has changed; make sure the screen is on if it
1415 // is now possible for it to be.
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001416 if (mSkippedScreenOn) {
1417 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1418 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001419
Joe Onorato128e7292009-03-24 18:41:31 -07001420 // Now send the message.
1421 if (index >= 0) {
1422 // Acquire the broadcast wake lock before changing the power
1423 // state. It will be release after the broadcast is sent.
1424 // We always increment the ref count for each notification in the queue
1425 // and always decrement when that notification is handled.
1426 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001428 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 }
1431
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001432 private WindowManagerPolicy.ScreenOnListener mScreenOnListener =
1433 new WindowManagerPolicy.ScreenOnListener() {
1434 @Override public void onScreenOn() {
1435 synchronized (mLocks) {
1436 if (mPreparingForScreenOn) {
1437 mPreparingForScreenOn = false;
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001438 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001439 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP,
1440 4, mBroadcastWakeLock.mCount);
1441 mBroadcastWakeLock.release();
1442 }
1443 }
1444 }
1445 };
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 private Runnable mNotificationTask = new Runnable()
1448 {
1449 public void run()
1450 {
Joe Onorato128e7292009-03-24 18:41:31 -07001451 while (true) {
1452 int value;
1453 int why;
1454 WindowManagerPolicy policy;
1455 synchronized (mLocks) {
1456 value = mBroadcastQueue[0];
1457 why = mBroadcastWhy[0];
1458 for (int i=0; i<2; i++) {
1459 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1460 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1461 }
1462 policy = getPolicyLocked();
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001463 if (value == 1 && !mPreparingForScreenOn) {
1464 mPreparingForScreenOn = true;
1465 mBroadcastWakeLock.acquire();
1466 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND,
1467 mBroadcastWakeLock.mCount);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
Joe Onorato128e7292009-03-24 18:41:31 -07001470 if (value == 1) {
1471 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001472
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001473 policy.screenTurningOn(mScreenOnListener);
Joe Onorato128e7292009-03-24 18:41:31 -07001474 try {
1475 ActivityManagerNative.getDefault().wakingUp();
1476 } catch (RemoteException e) {
1477 // ignore it
1478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479
Joe Onorato128e7292009-03-24 18:41:31 -07001480 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001481 Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
Joe Onorato128e7292009-03-24 18:41:31 -07001482 }
1483 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1484 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1485 mScreenOnBroadcastDone, mHandler, 0, null, null);
1486 } else {
1487 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001489 mBroadcastWakeLock.mCount);
1490 mBroadcastWakeLock.release();
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
1493 }
Joe Onorato128e7292009-03-24 18:41:31 -07001494 else if (value == 0) {
1495 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
Joe Onorato128e7292009-03-24 18:41:31 -07001497 policy.screenTurnedOff(why);
1498 try {
1499 ActivityManagerNative.getDefault().goingToSleep();
1500 } catch (RemoteException e) {
1501 // ignore it.
1502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503
Joe Onorato128e7292009-03-24 18:41:31 -07001504 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1505 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1506 mScreenOffBroadcastDone, mHandler, 0, null, null);
1507 } else {
1508 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001509 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001510 mBroadcastWakeLock.mCount);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001511 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -07001512 mBroadcastWakeLock.release();
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 }
Joe Onorato128e7292009-03-24 18:41:31 -07001516 else {
1517 // If we're in this case, then this handler is running for a previous
1518 // paired transaction. mBroadcastWakeLock will already have been released.
1519 break;
1520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522 }
1523 };
1524
1525 long mScreenOnStart;
1526 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1527 public void onReceive(Context context, Intent intent) {
1528 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1531 mBroadcastWakeLock.release();
1532 }
1533 }
1534 };
1535
1536 long mScreenOffStart;
1537 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1538 public void onReceive(Context context, Intent intent) {
1539 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001540 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1542 mBroadcastWakeLock.release();
1543 }
1544 }
1545 };
1546
1547 void logPointerUpEvent() {
1548 if (LOG_TOUCH_DOWNS) {
1549 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1550 mLastTouchDown = 0;
1551 }
1552 }
1553
1554 void logPointerDownEvent() {
1555 if (LOG_TOUCH_DOWNS) {
1556 // If we are not already timing a down/up sequence
1557 if (mLastTouchDown == 0) {
1558 mLastTouchDown = SystemClock.elapsedRealtime();
1559 mTouchCycles++;
1560 }
1561 }
1562 }
1563
1564 /**
1565 * Prevents the screen from turning on even if it *should* turn on due
1566 * to a subsequent full wake lock being acquired.
1567 * <p>
1568 * This is a temporary hack that allows an activity to "cover up" any
1569 * display glitches that happen during the activity's startup
1570 * sequence. (Specifically, this API was added to work around a
1571 * cosmetic bug in the "incoming call" sequence, where the lock screen
1572 * would flicker briefly before the incoming call UI became visible.)
1573 * TODO: There ought to be a more elegant way of doing this,
1574 * probably by having the PowerManager and ActivityManager
1575 * work together to let apps specify that the screen on/off
1576 * state should be synchronized with the Activity lifecycle.
1577 * <p>
1578 * Note that calling preventScreenOn(true) will NOT turn the screen
1579 * off if it's currently on. (This API only affects *future*
1580 * acquisitions of full wake locks.)
1581 * But calling preventScreenOn(false) WILL turn the screen on if
1582 * it's currently off because of a prior preventScreenOn(true) call.
1583 * <p>
1584 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1585 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1586 * call doesn't occur within 5 seconds, we'll turn the screen back on
1587 * ourselves (and log a warning about it); this prevents a buggy app
1588 * from disabling the screen forever.)
1589 * <p>
1590 * TODO: this feature should really be controlled by a new type of poke
1591 * lock (rather than an IPowerManager call).
1592 */
1593 public void preventScreenOn(boolean prevent) {
1594 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1595
1596 synchronized (mLocks) {
1597 if (prevent) {
1598 // First of all, grab a partial wake lock to
1599 // make sure the CPU stays on during the entire
1600 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1601 mPreventScreenOnPartialLock.acquire();
1602
1603 // Post a forceReenableScreen() call (for 5 seconds in the
1604 // future) to make sure the matching preventScreenOn(false) call
1605 // has happened by then.
1606 mHandler.removeCallbacks(mForceReenableScreenTask);
1607 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1608
1609 // Finally, set the flag that prevents the screen from turning on.
1610 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001611 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 mPreventScreenOn = true;
1613 } else {
1614 // (Re)enable the screen.
1615 mPreventScreenOn = false;
1616
1617 // We're "undoing" a the prior preventScreenOn(true) call, so we
1618 // no longer need the 5-second safeguard.
1619 mHandler.removeCallbacks(mForceReenableScreenTask);
1620
1621 // Forcibly turn on the screen if it's supposed to be on. (This
1622 // handles the case where the screen is currently off because of
1623 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001624 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1628 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001629 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (err != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001631 Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 }
1634
1635 // Release the partial wake lock that we held during the
1636 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1637 mPreventScreenOnPartialLock.release();
1638 }
1639 }
1640 }
1641
1642 public void setScreenBrightnessOverride(int brightness) {
1643 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1644
Mike Lockwoodf527c712010-06-10 14:12:33 -04001645 if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 synchronized (mLocks) {
1647 if (mScreenBrightnessOverride != brightness) {
1648 mScreenBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001649 if (isScreenOn()) {
1650 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653 }
1654 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001655
1656 public void setButtonBrightnessOverride(int brightness) {
1657 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1658
Mike Lockwoodf527c712010-06-10 14:12:33 -04001659 if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness);
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001660 synchronized (mLocks) {
1661 if (mButtonBrightnessOverride != brightness) {
1662 mButtonBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001663 if (isScreenOn()) {
1664 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1665 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001666 }
1667 }
1668 }
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 /**
1671 * Sanity-check that gets called 5 seconds after any call to
1672 * preventScreenOn(true). This ensures that the original call
1673 * is followed promptly by a call to preventScreenOn(false).
1674 */
1675 private void forceReenableScreen() {
1676 // We shouldn't get here at all if mPreventScreenOn is false, since
1677 // we should have already removed any existing
1678 // mForceReenableScreenTask messages...
1679 if (!mPreventScreenOn) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001680 Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return;
1682 }
1683
1684 // Uh oh. It's been 5 seconds since a call to
1685 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1686 // This means the app that called preventScreenOn(true) is either
1687 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1688 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1689 // crashed before doing so.)
1690
1691 // Log a warning, and forcibly turn the screen back on.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 + "Forcing the screen back on...");
1694 preventScreenOn(false);
1695 }
1696
1697 private Runnable mForceReenableScreenTask = new Runnable() {
1698 public void run() {
1699 forceReenableScreen();
1700 }
1701 };
1702
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001703 private int setScreenStateLocked(boolean on) {
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001704 if (DEBUG_SCREEN_ON) {
1705 RuntimeException e = new RuntimeException("here");
1706 e.fillInStackTrace();
1707 Slog.i(TAG, "Set screen state: " + on, e);
1708 }
Dianne Hackborn474fd742011-10-10 18:40:22 -07001709 if (on) {
1710 if ((mPowerState & SCREEN_ON_BIT) == 0 || mSkippedScreenOn) {
1711 // If we are turning the screen state on, but the screen
1712 // light is currently off, then make sure that we set the
1713 // light at this point to 0. This is the case where we are
1714 // turning on the screen and waiting for the UI to be drawn
1715 // before showing it to the user. We want the light off
1716 // until it is ready to be shown to the user, not it using
1717 // whatever the last value it had.
Dianne Hackborn81de8b92011-11-28 16:54:31 -08001718 if (DEBUG_SCREEN_ON) {
1719 Slog.i(TAG, "Forcing brightness 0: mPowerState=0x"
1720 + Integer.toHexString(mPowerState)
1721 + " mSkippedScreenOn=" + mSkippedScreenOn);
1722 }
Dianne Hackborn474fd742011-10-10 18:40:22 -07001723 mScreenBrightness.forceValueLocked(Power.BRIGHTNESS_OFF);
1724 }
1725 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001726 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001727 if (err == 0) {
1728 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1729 if (mUseSoftwareAutoBrightness) {
Joe Onoratod28f7532010-11-06 12:56:53 -07001730 enableLightSensorLocked(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001731 if (!on) {
1732 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001733 mButtonLight.turnOff();
1734 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001735 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001736 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001737 }
1738 return err;
1739 }
1740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 private void setPowerState(int state)
1742 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001743 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745
Mike Lockwood435eb642009-12-03 08:40:18 -05001746 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 {
1748 synchronized (mLocks) {
1749 int err;
1750
1751 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001752 Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001754 + " noChangeLights=" + noChangeLights
1755 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757
1758 if (noChangeLights) {
1759 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1760 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001761 if (mProximitySensorActive) {
1762 // don't turn on the screen when the proximity sensor lock is held
1763 newState = (newState & ~SCREEN_BRIGHT);
1764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765
1766 if (batteryIsLow()) {
1767 newState |= BATTERY_LOW_BIT;
1768 } else {
1769 newState &= ~BATTERY_LOW_BIT;
1770 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001771 if (newState == mPowerState && mInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 return;
1773 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001774
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001775 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 newState |= ALL_BRIGHT;
1777 }
1778
1779 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1780 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1781
Mike Lockwood51b844962009-11-16 21:51:18 -05001782 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001783 Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 + " newState=" + newState + " noChangeLights=" + noChangeLights);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001787 Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001789 Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001791 Slog.d(TAG, " oldScreenOn=" + oldScreenOn
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 + " newScreenOn=" + newScreenOn);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001793 Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1795 }
1796
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001797 final boolean stateChanged = mPowerState != newState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798
1799 if (oldScreenOn != newScreenOn) {
1800 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001801 // When the user presses the power button, we need to always send out the
1802 // notification that it's going to sleep so the keyguard goes on. But
1803 // we can't do that until the screen fades out, so we don't show the keyguard
1804 // too early.
1805 if (mStillNeedSleepNotification) {
1806 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1807 }
1808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 // Turn on the screen UNLESS there was a prior
1810 // preventScreenOn(true) request. (Note that the lifetime
1811 // of a single preventScreenOn() request is limited to 5
1812 // seconds to prevent a buggy app from disabling the
1813 // screen forever; see forceReenableScreen().)
1814 boolean reallyTurnScreenOn = true;
1815 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001816 Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 + mPreventScreenOn);
1818 }
1819
1820 if (mPreventScreenOn) {
1821 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001822 Slog.d(TAG, "- PREVENTING screen from really turning on!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
1824 reallyTurnScreenOn = false;
1825 }
1826 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001827 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 long identity = Binder.clearCallingIdentity();
1829 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001830 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 mBatteryStats.noteScreenOn();
1832 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001833 Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 } finally {
1835 Binder.restoreCallingIdentity(identity);
1836 }
1837 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001838 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 // But continue as if we really did turn the screen on...
1840 err = 0;
1841 }
1842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 mLastTouchDown = 0;
1844 mTotalTouchDownTime = 0;
1845 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 mTotalTouchDownTime, mTouchCycles);
1848 if (err == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 sendNotificationLocked(true, -1);
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001850 // Update the lights *after* taking care of turning the
1851 // screen on, so we do this after our notifications are
1852 // enqueued and thus will delay turning on the screen light
1853 // until the windows are correctly displayed.
1854 if (stateChanged) {
1855 updateLightsLocked(newState, 0);
1856 }
1857 mPowerState |= SCREEN_ON_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 } else {
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001861 // Update the lights *before* taking care of turning the
1862 // screen off, so we can initiate any animations that are desired.
1863 if (stateChanged) {
1864 updateLightsLocked(newState, 0);
1865 }
1866
Mike Lockwood497087e32009-11-08 18:33:03 -05001867 // cancel light sensor task
1868 mHandler.removeCallbacks(mAutoBrightnessTask);
Jim Rodovichd102fea2010-09-02 12:30:49 -05001869 mLightSensorPendingDecrease = false;
1870 mLightSensorPendingIncrease = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 mScreenOffTime = SystemClock.elapsedRealtime();
1872 long identity = Binder.clearCallingIdentity();
1873 try {
1874 mBatteryStats.noteScreenOff();
1875 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001876 Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 } finally {
1878 Binder.restoreCallingIdentity(identity);
1879 }
1880 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001881 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001883 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 err = 0;
1886 mLastTouchDown = 0;
1887 }
1888 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001889 } else if (stateChanged) {
1890 // Screen on/off didn't change, but lights may have.
1891 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001893
1894 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1895
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001896 updateNativePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 }
1898 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001899
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001900 private void updateNativePowerStateLocked() {
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001901 if (!mHeadless) {
1902 nativeSetPowerState(
1903 (mPowerState & SCREEN_ON_BIT) != 0,
1904 (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
1905 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001907
Mike Lockwood435eb642009-12-03 08:40:18 -05001908 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 // called multiple times in the same state. -joeo
Joe Onoratob08a1af2010-10-11 19:28:58 -07001912 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime,
1913 mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001915 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001917 mScreenOffReason = reason;
1918 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
1920 return err;
1921 }
1922
1923 private boolean batteryIsLow() {
1924 return (!mIsPowered &&
1925 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1926 }
1927
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001928 private boolean shouldDeferScreenOnLocked() {
1929 if (mPreparingForScreenOn) {
1930 // Currently waiting for confirmation from the policy that it
1931 // is okay to turn on the screen. Don't allow the screen to go
1932 // on until that is done.
1933 if (DEBUG_SCREEN_ON) Slog.i(TAG,
1934 "updateLights: delaying screen on due to mPreparingForScreenOn");
1935 return true;
1936 } else {
1937 // If there is a screen-on command in the notification queue, we
1938 // can't turn the screen on until it has been processed (and we
1939 // have set mPreparingForScreenOn) or it has been dropped.
1940 for (int i=0; i<mBroadcastQueue.length; i++) {
1941 if (mBroadcastQueue[i] == 1) {
1942 if (DEBUG_SCREEN_ON) Slog.i(TAG,
1943 "updateLights: delaying screen on due to notification queue");
1944 return true;
1945 }
1946 }
1947 }
1948 return false;
1949 }
1950
The Android Open Source Project10592532009-03-18 17:39:46 -07001951 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001952 final int oldState = mPowerState;
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07001953
1954 // If the screen is not currently on, we will want to delay actually
1955 // turning the lights on if we are still getting the UI put up.
1956 if ((oldState&SCREEN_ON_BIT) == 0 || mSkippedScreenOn) {
1957 // Don't turn screen on until we know we are really ready to.
1958 // This is to avoid letting the screen go on before things like the
1959 // lock screen have been displayed.
1960 if ((mSkippedScreenOn=shouldDeferScreenOnLocked())) {
1961 newState &= ~(SCREEN_ON_BIT|SCREEN_BRIGHT_BIT);
1962 }
1963 }
1964
Joe Onorato60607a902010-10-23 14:49:30 -07001965 if ((newState & SCREEN_ON_BIT) != 0) {
1966 // Only turn on the buttons or keyboard if the screen is also on.
1967 // We should never see the buttons on but not the screen.
1968 newState = applyButtonState(newState);
1969 newState = applyKeyboardState(newState);
1970 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001971 final int realDifference = (newState ^ oldState);
1972 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001974 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 int offMask = 0;
1978 int dimMask = 0;
1979 int onMask = 0;
1980
1981 int preferredBrightness = getPreferredBrightness();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001984 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1985 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001987 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
1989 }
1990
1991 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001992 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1993 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07001995 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997 }
1998
1999 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002000 int nominalCurrentValue = -1;
2001 // If there was an actual difference in the light state, then
2002 // figure out the "ideal" current value based on the previous
2003 // state. Otherwise, this is a change due to the brightness
2004 // override, so we want to animate from whatever the current
2005 // value is.
2006 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
2007 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
2008 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
2009 nominalCurrentValue = preferredBrightness;
2010 break;
2011 case SCREEN_ON_BIT:
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002012 nominalCurrentValue = mScreenBrightnessDim;
Joe Onoratob08a1af2010-10-11 19:28:58 -07002013 break;
2014 case 0:
2015 nominalCurrentValue = Power.BRIGHTNESS_OFF;
2016 break;
2017 case SCREEN_BRIGHT_BIT:
2018 default:
2019 // not possible
2020 nominalCurrentValue = (int)mScreenBrightness.curValue;
2021 break;
Joe Onorato128e7292009-03-24 18:41:31 -07002022 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002023 }
2024 int brightness = preferredBrightness;
2025 int steps = ANIM_STEPS;
2026 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
2027 // dim or turn off backlight, depending on if the screen is on
2028 // the scale is because the brightness ramp isn't linear and this biases
2029 // it so the later parts take longer.
2030 final float scale = 1.5f;
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002031 float ratio = (((float)mScreenBrightnessDim)/preferredBrightness);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002032 if (ratio > 1.0f) ratio = 1.0f;
2033 if ((newState & SCREEN_ON_BIT) == 0) {
2034 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
2035 // was bright
2036 steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002038 // was dim
2039 steps = (int)(ANIM_STEPS*ratio*scale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002041 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } else {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002043 if ((oldState & SCREEN_ON_BIT) != 0) {
2044 // was bright
2045 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
2046 } else {
2047 // was dim
2048 steps = (int)(ANIM_STEPS*ratio);
2049 }
2050 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
2051 // If the "stay on while plugged in" option is
2052 // turned on, then the screen will often not
2053 // automatically turn off while plugged in. To
2054 // still have a sense of when it is inactive, we
2055 // will then count going dim as turning off.
2056 mScreenOffTime = SystemClock.elapsedRealtime();
2057 }
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002058 brightness = mScreenBrightnessDim;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
2060 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002061 long identity = Binder.clearCallingIdentity();
2062 try {
2063 mBatteryStats.noteScreenBrightness(brightness);
2064 } catch (RemoteException e) {
2065 // Nothing interesting to do.
2066 } finally {
2067 Binder.restoreCallingIdentity(identity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002069 if (!mSkippedScreenOn) {
2070 mScreenBrightness.setTargetLocked(brightness, steps,
2071 INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue);
2072 if (DEBUG_SCREEN_ON) {
2073 RuntimeException e = new RuntimeException("here");
2074 e.fillInStackTrace();
2075 Slog.i(TAG, "Setting screen brightness: " + brightness, e);
2076 }
Dianne Hackbornbeae3bd2011-09-21 10:55:12 -07002077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002079
Joe Onorato60607a902010-10-23 14:49:30 -07002080 if (mSpew) {
2081 Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask)
2082 + " dimMask=0x" + Integer.toHexString(dimMask)
2083 + " onMask=0x" + Integer.toHexString(onMask)
2084 + " difference=0x" + Integer.toHexString(difference)
2085 + " realDifference=0x" + Integer.toHexString(realDifference)
2086 + " forceState=0x" + Integer.toHexString(forceState)
2087 );
2088 }
2089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 if (offMask != 0) {
Mike Lockwood48358bd2010-04-17 22:29:20 -04002091 if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002092 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 if (dimMask != 0) {
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002095 int brightness = mScreenBrightnessDim;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 if ((newState & BATTERY_LOW_BIT) != 0 &&
2097 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
2098 brightness = Power.BRIGHTNESS_LOW_BATTERY;
2099 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04002100 if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002101 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103 if (onMask != 0) {
2104 int brightness = getPreferredBrightness();
2105 if ((newState & BATTERY_LOW_BIT) != 0 &&
2106 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
2107 brightness = Power.BRIGHTNESS_LOW_BATTERY;
2108 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04002109 if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002110 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113
The Android Open Source Project10592532009-03-18 17:39:46 -07002114 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002115 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002116 ? LightsService.BRIGHTNESS_MODE_SENSOR
2117 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07002118 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002119 if (DEBUG_SCREEN_ON) {
2120 RuntimeException e = new RuntimeException("here");
2121 e.fillInStackTrace();
2122 Slog.i(TAG, "Set LCD brightness: " + value, e);
2123 }
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002124 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07002125 }
2126 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002127 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07002128 }
2129 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002130 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07002131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
2133
Joe Onoratob08a1af2010-10-11 19:28:58 -07002134 class BrightnessState implements Runnable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 boolean initialized;
2138 int targetValue;
2139 float curValue;
2140 float delta;
2141 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 BrightnessState(int m) {
2144 mask = m;
2145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 public void dump(PrintWriter pw, String prefix) {
2148 pw.println(prefix + "animating=" + animating
2149 + " targetValue=" + targetValue
2150 + " curValue=" + curValue
2151 + " delta=" + delta);
2152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
Dianne Hackborn474fd742011-10-10 18:40:22 -07002154 void forceValueLocked(int value) {
2155 targetValue = -1;
2156 curValue = value;
2157 setLightBrightness(mask, value);
2158 if (animating) {
2159 finishAnimationLocked(false, value);
2160 }
2161 }
2162
Joe Onoratob08a1af2010-10-11 19:28:58 -07002163 void setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07002164 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 if (!initialized) {
2166 initialized = true;
2167 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07002168 } else if (targetValue == target) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002169 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07002172 delta = (targetValue -
2173 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
2174 / stepsToTarget;
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002175 if (mSpew || DEBUG_SCREEN_ON) {
Joe Onorato128e7292009-03-24 18:41:31 -07002176 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
Joe Onorato3d3db602010-10-18 16:08:16 -04002177 Slog.i(TAG, "setTargetLocked mask=" + mask + " curValue=" + curValue
2178 + " target=" + target + " targetValue=" + targetValue + " delta=" + delta
Joe Onorato128e7292009-03-24 18:41:31 -07002179 + " nominalCurrentValue=" + nominalCurrentValue
2180 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 animating = true;
Joe Onoratob08a1af2010-10-11 19:28:58 -07002183
2184 if (mSpew) {
2185 Slog.i(TAG, "scheduling light animator");
2186 }
2187 mScreenOffHandler.removeCallbacks(this);
2188 mScreenOffHandler.post(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 boolean stepLocked() {
2192 if (!animating) return false;
2193 if (false && mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002194 Slog.i(TAG, "Step target " + mask + ": cur=" + curValue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 + " target=" + targetValue + " delta=" + delta);
2196 }
2197 curValue += delta;
2198 int curIntValue = (int)curValue;
2199 boolean more = true;
2200 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07002201 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 more = false;
2203 } else if (delta > 0) {
2204 if (curIntValue >= targetValue) {
2205 curValue = curIntValue = targetValue;
2206 more = false;
2207 }
2208 } else {
2209 if (curIntValue <= targetValue) {
2210 curValue = curIntValue = targetValue;
2211 more = false;
2212 }
2213 }
Joe Onoratob08a1af2010-10-11 19:28:58 -07002214 if (mSpew) Slog.d(TAG, "Animating curIntValue=" + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07002215 setLightBrightness(mask, curIntValue);
Joe Onorato3d3db602010-10-18 16:08:16 -04002216 finishAnimationLocked(more, curIntValue);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002217 return more;
2218 }
2219
Joe Onorato3d3db602010-10-18 16:08:16 -04002220 void jumpToTargetLocked() {
2221 if (mSpew) Slog.d(TAG, "jumpToTargetLocked targetValue=" + targetValue + ": " + mask);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002222 setLightBrightness(mask, targetValue);
2223 final int tv = targetValue;
2224 curValue = tv;
2225 targetValue = -1;
Joe Onorato3d3db602010-10-18 16:08:16 -04002226 finishAnimationLocked(false, tv);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002227 }
2228
Joe Onorato3d3db602010-10-18 16:08:16 -04002229 private void finishAnimationLocked(boolean more, int curIntValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 animating = more;
2231 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002232 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002233 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 public void run() {
Dianne Hackborne02c88a2011-10-28 13:58:15 -07002239 synchronized (mLocks) {
2240 // we're turning off
2241 final boolean turningOff = animating && targetValue == Power.BRIGHTNESS_OFF;
2242 if (mAnimateScreenLights || !turningOff) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002243 long now = SystemClock.uptimeMillis();
2244 boolean more = mScreenBrightness.stepLocked();
2245 if (more) {
2246 mScreenOffHandler.postAtTime(this, now+(1000/60));
2247 }
Dianne Hackborne02c88a2011-10-28 13:58:15 -07002248 } else {
Mike Lockwood491f40d2011-10-31 10:30:29 -04002249 if (!mHeadless) {
2250 // It's pretty scary to hold mLocks for this long, and we should
2251 // redesign this, but it works for now.
2252 nativeStartSurfaceFlingerAnimation(
2253 mScreenOffReason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
2254 ? 0 : mAnimationSetting);
2255 }
Joe Onorato3d3db602010-10-18 16:08:16 -04002256 mScreenBrightness.jumpToTargetLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
2259 }
2260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 private int getPreferredBrightness() {
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002263 if (mScreenBrightnessOverride >= 0) {
2264 return mScreenBrightnessOverride;
2265 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
2266 && mAutoBrightessEnabled) {
2267 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002269 final int brightness = mScreenBrightnessSetting;
2270 // Don't let applications turn the screen all the way off
2271 return Math.max(brightness, mScreenBrightnessDim);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 }
2273
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002274 private int applyButtonState(int state) {
2275 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002276 if ((state & BATTERY_LOW_BIT) != 0) {
2277 // do not override brightness if the battery is low
2278 return state;
2279 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002280 if (mButtonBrightnessOverride >= 0) {
2281 brightness = mButtonBrightnessOverride;
2282 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
2283 brightness = mLightSensorButtonBrightness;
2284 }
2285 if (brightness > 0) {
2286 return state | BUTTON_BRIGHT_BIT;
2287 } else if (brightness == 0) {
2288 return state & ~BUTTON_BRIGHT_BIT;
2289 } else {
2290 return state;
2291 }
2292 }
2293
2294 private int applyKeyboardState(int state) {
2295 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002296 if ((state & BATTERY_LOW_BIT) != 0) {
2297 // do not override brightness if the battery is low
2298 return state;
2299 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002300 if (!mKeyboardVisible) {
2301 brightness = 0;
2302 } else if (mButtonBrightnessOverride >= 0) {
2303 brightness = mButtonBrightnessOverride;
2304 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
2305 brightness = mLightSensorKeyboardBrightness;
2306 }
2307 if (brightness > 0) {
2308 return state | KEYBOARD_BRIGHT_BIT;
2309 } else if (brightness == 0) {
2310 return state & ~KEYBOARD_BRIGHT_BIT;
2311 } else {
2312 return state;
2313 }
2314 }
2315
Charles Mendis322591c2009-10-29 11:06:59 -07002316 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 synchronized (mLocks) {
2318 return (mPowerState & SCREEN_ON_BIT) != 0;
2319 }
2320 }
2321
Charles Mendis322591c2009-10-29 11:06:59 -07002322 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 synchronized (mLocks) {
2324 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
2325 }
2326 }
2327
Mike Lockwood497087e32009-11-08 18:33:03 -05002328 private boolean isScreenTurningOffLocked() {
2329 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
2330 }
2331
Joe Onorato4b9f62d2010-10-11 13:41:35 -07002332 private boolean shouldLog(long time) {
2333 synchronized (mLocks) {
2334 if (time > (mWarningSpewThrottleTime + (60*60*1000))) {
2335 mWarningSpewThrottleTime = time;
2336 mWarningSpewThrottleCount = 0;
2337 return true;
2338 } else if (mWarningSpewThrottleCount < 30) {
2339 mWarningSpewThrottleCount++;
2340 return true;
2341 } else {
2342 return false;
2343 }
2344 }
2345 }
2346
Mike Lockwood200b30b2009-09-20 00:23:59 -04002347 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05002348 if (isScreenTurningOffLocked()) {
2349 // cancel animation so userActivity will succeed
2350 mScreenBrightness.animating = false;
2351 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002352 boolean savedActivityAllowed = mUserActivityAllowed;
2353 mUserActivityAllowed = true;
2354 userActivity(SystemClock.uptimeMillis(), false);
2355 mUserActivityAllowed = savedActivityAllowed;
2356 }
2357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
2359 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Joe Onorato7999bff2010-07-24 11:50:05 -04002360 userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 }
2362
2363 public void userActivity(long time, boolean noChangeLights) {
Joe Onorato4b9f62d2010-10-11 13:41:35 -07002364 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
2365 != PackageManager.PERMISSION_GRANTED) {
2366 if (shouldLog(time)) {
2367 Slog.w(TAG, "Caller does not have DEVICE_POWER permission. pid="
2368 + Binder.getCallingPid() + " uid=" + Binder.getCallingUid());
2369 }
2370 return;
2371 }
2372
Joe Onorato7999bff2010-07-24 11:50:05 -04002373 userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375
2376 public void userActivity(long time, boolean noChangeLights, int eventType) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002377 userActivity(time, -1, noChangeLights, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379
2380 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002381 userActivity(time, -1, noChangeLights, eventType, force);
2382 }
2383
2384 /*
2385 * Reset the user activity timeout to now + timeout. This overrides whatever else is going
2386 * on with user activity. Don't use this function.
2387 */
2388 public void clearUserActivityTimeout(long now, long timeout) {
2389 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2390 Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2391 userActivity(now, timeout, false, OTHER_EVENT, false);
2392 }
2393
2394 private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2395 int eventType, boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396
Joe Onorato1a542c72010-11-08 09:48:20 -08002397 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0) && (eventType == TOUCH_EVENT)) {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002398 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002399 Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002400 }
2401 return;
2402 }
2403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 synchronized (mLocks) {
2405 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002406 Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 + " mUserActivityAllowed=" + mUserActivityAllowed
2408 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07002409 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
2410 + " mProximitySensorActive=" + mProximitySensorActive
Joe Onorato797e6882010-08-26 14:46:01 -04002411 + " timeoutOverride=" + timeoutOverride
Mike Lockwood36fc3022009-08-25 16:49:06 -07002412 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
Mike Lockwood05067122009-10-27 23:07:25 -04002414 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05002415 if (isScreenTurningOffLocked()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002416 Slog.d(TAG, "ignoring user activity while turning off screen");
Mike Lockwood05067122009-10-27 23:07:25 -04002417 return;
2418 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002419 // Disable proximity sensor if if user presses power key while we are in the
2420 // "waiting for proximity sensor to go negative" state.
2421 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
2422 mProximitySensorActive = false;
2423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (mLastEventTime <= time || force) {
2425 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002426 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002427 // Only turn on button backlights if a button was pressed
2428 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05002429 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
2431 } else {
2432 // don't clear button/keyboard backlights when the screen is touched.
2433 mUserState |= SCREEN_BRIGHT;
2434 }
2435
Dianne Hackborn617f8772009-03-31 15:04:46 -07002436 int uid = Binder.getCallingUid();
2437 long ident = Binder.clearCallingIdentity();
2438 try {
2439 mBatteryStats.noteUserActivity(uid, eventType);
2440 } catch (RemoteException e) {
2441 // Ignore
2442 } finally {
2443 Binder.restoreCallingIdentity(ident);
2444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002445
Michael Chane96440f2009-05-06 10:27:36 -07002446 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002447 setPowerState(mUserState | mWakeLockState, noChangeLights,
2448 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
Joe Onorato7999bff2010-07-24 11:50:05 -04002449 setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451 }
2452 }
Mike Lockwoodef731622010-01-27 17:51:34 -05002453
2454 if (mPolicy != null) {
2455 mPolicy.userActivity();
2456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
2458
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002459 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2460 try {
2461 int i;
2462 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2463 if (sensorValue < mAutoBrightnessLevels[i]) {
2464 break;
2465 }
2466 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002467 // This is the range of brightness values that we can use.
2468 final int minval = values[0];
2469 final int maxval = values[mAutoBrightnessLevels.length];
2470 // This is the range we will be scaling. We put some padding
2471 // at the low and high end to give the adjustment a little better
2472 // impact on the actual observed value.
2473 final int range = (maxval-minval) + LIGHT_SENSOR_RANGE_EXPANSION;
2474 // This is the desired brightness value from 0.0 to 1.0.
2475 float valf = ((values[i]-minval+(LIGHT_SENSOR_RANGE_EXPANSION/2))/(float)range);
2476 // Apply a scaling to the value based on the adjustment.
2477 if (mLightSensorAdjustSetting > 0 && mLightSensorAdjustSetting <= 1) {
2478 float adj = (float)Math.sqrt(1.0f-mLightSensorAdjustSetting);
2479 if (adj <= .00001) {
2480 valf = 1;
2481 } else {
2482 valf /= adj;
2483 }
2484 } else if (mLightSensorAdjustSetting < 0 && mLightSensorAdjustSetting >= -1) {
2485 float adj = (float)Math.sqrt(1.0f+mLightSensorAdjustSetting);
2486 valf *= adj;
2487 }
2488 // Apply an additional offset to the value based on the adjustment.
2489 valf += mLightSensorAdjustSetting/LIGHT_SENSOR_OFFSET_SCALE;
2490 // Convert the 0.0-1.0 value back to a brightness integer.
2491 int val = (int)((valf*range)+minval) - (LIGHT_SENSOR_RANGE_EXPANSION/2);
2492 if (val < minval) val = minval;
2493 else if (val > maxval) val = maxval;
2494 return val;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002495 } catch (Exception e) {
2496 // guard against null pointer or index out of bounds errors
Joe Onorato8a9b2202010-02-26 18:56:32 -08002497 Slog.e(TAG, "getAutoBrightnessValue", e);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002498 return 255;
2499 }
2500 }
2501
Mike Lockwood20f87d72009-11-05 16:08:51 -05002502 private Runnable mProximityTask = new Runnable() {
2503 public void run() {
2504 synchronized (mLocks) {
2505 if (mProximityPendingValue != -1) {
2506 proximityChangedLocked(mProximityPendingValue == 1);
2507 mProximityPendingValue = -1;
2508 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002509 if (mProximityPartialLock.isHeld()) {
2510 mProximityPartialLock.release();
2511 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002512 }
2513 }
2514 };
2515
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002516 private Runnable mAutoBrightnessTask = new Runnable() {
2517 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002518 synchronized (mLocks) {
Jim Rodovichd102fea2010-09-02 12:30:49 -05002519 if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
2520 int value = (int)mLightSensorPendingValue;
2521 mLightSensorPendingDecrease = false;
2522 mLightSensorPendingIncrease = false;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002523 lightSensorChangedLocked(value, false);
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002524 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002525 }
2526 }
2527 };
2528
Mike Lockwoodb2865412010-02-02 22:40:33 -05002529 private void dockStateChanged(int state) {
2530 synchronized (mLocks) {
2531 mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2532 if (mIsDocked) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04002533 // allow brightness to decrease when docked
Mike Lockwoodb2865412010-02-02 22:40:33 -05002534 mHighestLightSensorValue = -1;
2535 }
2536 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2537 // force lights recalculation
2538 int value = (int)mLightSensorValue;
2539 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002540 lightSensorChangedLocked(value, false);
Mike Lockwoodb2865412010-02-02 22:40:33 -05002541 }
2542 }
2543 }
2544
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002545 private void lightSensorChangedLocked(int value, boolean immediate) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002546 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002547 Slog.d(TAG, "lightSensorChangedLocked " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002548 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002549
Joe Onorato06eb33a2010-10-25 14:09:21 -07002550 // Don't do anything if the screen is off.
2551 if ((mPowerState & SCREEN_ON_BIT) == 0) {
2552 if (mDebugLightSensor) {
2553 Slog.d(TAG, "dropping lightSensorChangedLocked because screen is off");
2554 }
2555 return;
2556 }
2557
Mike Lockwoodb2865412010-02-02 22:40:33 -05002558 // do not allow light sensor value to decrease
2559 if (mHighestLightSensorValue < value) {
2560 mHighestLightSensorValue = value;
2561 }
2562
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002563 if (mLightSensorValue != value) {
2564 mLightSensorValue = value;
2565 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
Mike Lockwoodb2865412010-02-02 22:40:33 -05002566 // use maximum light sensor value seen since screen went on for LCD to avoid flicker
2567 // we only do this if we are undocked, since lighting should be stable when
2568 // stationary in a dock.
2569 int lcdValue = getAutoBrightnessValue(
2570 (mIsDocked ? value : mHighestLightSensorValue),
2571 mLcdBacklightValues);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002572 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002573 int keyboardValue;
2574 if (mKeyboardVisible) {
2575 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2576 } else {
2577 keyboardValue = 0;
2578 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002579 mLightSensorScreenBrightness = lcdValue;
2580 mLightSensorButtonBrightness = buttonValue;
2581 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002582
2583 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002584 Slog.d(TAG, "lcdValue " + lcdValue);
2585 Slog.d(TAG, "buttonValue " + buttonValue);
2586 Slog.d(TAG, "keyboardValue " + keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002587 }
2588
Mike Lockwood4984e732009-11-01 08:16:33 -05002589 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002590 if (!mSkippedScreenOn) {
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002591 mScreenBrightness.setTargetLocked(lcdValue,
2592 immediate ? IMMEDIATE_ANIM_STEPS : AUTOBRIGHTNESS_ANIM_STEPS,
Dianne Hackborn81de8b92011-11-28 16:54:31 -08002593 INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue);
2594 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002595 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002596 if (mButtonBrightnessOverride < 0) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002597 mButtonLight.setBrightness(buttonValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002598 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002599 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
Joe Onoratob08a1af2010-10-11 19:28:58 -07002600 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002601 }
2602 }
2603 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002604 }
2605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 /**
2607 * The user requested that we go to sleep (probably with the power button).
2608 * This overrides all wake locks that are held.
2609 */
2610 public void goToSleep(long time)
2611 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002612 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2613 }
2614
2615 /**
2616 * The user requested that we go to sleep (probably with the power button).
2617 * This overrides all wake locks that are held.
2618 */
2619 public void goToSleepWithReason(long time, int reason)
2620 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2622 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002623 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 }
2625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002628 * Reboot the device immediately, passing 'reason' (may be null)
2629 * to the underlying __reboot system call. Should not return.
2630 */
2631 public void reboot(String reason)
2632 {
2633 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002634
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002635 if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
2636 throw new IllegalStateException("Too early to call reboot()");
2637 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002638
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002639 final String finalReason = reason;
2640 Runnable runnable = new Runnable() {
2641 public void run() {
2642 synchronized (this) {
2643 ShutdownThread.reboot(mContext, finalReason, false);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002644 }
2645
San Mehat1e512792010-01-07 10:40:29 -08002646 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002647 };
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002648 // ShutdownThread must run on a looper capable of displaying the UI.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002649 mHandler.post(runnable);
2650
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002651 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002652 synchronized (runnable) {
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002653 while (true) {
2654 try {
2655 runnable.wait();
2656 } catch (InterruptedException e) {
2657 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002658 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002659 }
2660 }
2661
Dan Egnor60d87622009-12-16 16:32:58 -08002662 /**
2663 * Crash the runtime (causing a complete restart of the Android framework).
2664 * Requires REBOOT permission. Mostly for testing. Should not return.
2665 */
2666 public void crash(final String message)
2667 {
2668 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2669 Thread t = new Thread("PowerManagerService.crash()") {
2670 public void run() { throw new RuntimeException(message); }
2671 };
2672 try {
2673 t.start();
2674 t.join();
2675 } catch (InterruptedException e) {
2676 Log.wtf(TAG, e);
2677 }
2678 }
2679
Mike Lockwood435eb642009-12-03 08:40:18 -05002680 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681
2682 if (mLastEventTime <= time) {
2683 mLastEventTime = time;
2684 // cancel all of the wake locks
2685 mWakeLockState = SCREEN_OFF;
2686 int N = mLocks.size();
2687 int numCleared = 0;
Joe Onorato8274a0e2010-10-05 17:38:09 -04002688 boolean proxLock = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 for (int i=0; i<N; i++) {
2690 WakeLock wl = mLocks.get(i);
2691 if (isScreenLock(wl.flags)) {
Joe Onorato8274a0e2010-10-05 17:38:09 -04002692 if (((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
2693 && reason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
2694 proxLock = true;
2695 } else {
2696 mLocks.get(i).activated = false;
2697 numCleared++;
2698 }
2699 }
2700 }
2701 if (!proxLock) {
2702 mProxIgnoredBecauseScreenTurnedOff = true;
2703 if (mDebugProximitySensor) {
2704 Slog.d(TAG, "setting mProxIgnoredBecauseScreenTurnedOff");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
2706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002707 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002708 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002710 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 cancelTimerLocked();
2712 }
2713 }
2714
2715 public long timeSinceScreenOn() {
2716 synchronized (mLocks) {
2717 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2718 return 0;
2719 }
2720 return SystemClock.elapsedRealtime() - mScreenOffTime;
2721 }
2722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002725 synchronized (mLocks) {
2726 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002727 Slog.d(TAG, "setKeyboardVisibility: " + visible);
Mike Lockwooda625b382009-09-12 17:36:03 -07002728 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002729 if (mKeyboardVisible != visible) {
2730 mKeyboardVisible = visible;
2731 // don't signal user activity if the screen is off; other code
2732 // will take care of turning on due to a true change to the lid
2733 // switch and synchronized with the lock screen.
2734 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002735 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002736 // force recompute of backlight values
2737 if (mLightSensorValue >= 0) {
2738 int value = (int)mLightSensorValue;
2739 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08002740 lightSensorChangedLocked(value, false);
Mike Lockwooddf024922009-10-29 21:29:15 -04002741 }
2742 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002743 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2744 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002745 }
2746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 }
2748
2749 /**
2750 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002751 * When disabling user activity we also reset user power state so the keyguard can reset its
2752 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 */
2754 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002755 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002756 Slog.d(TAG, "enableUserActivity " + enabled);
Mike Lockwood50c548d2009-11-09 16:02:06 -05002757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 synchronized (mLocks) {
2759 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002760 if (!enabled) {
2761 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2762 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
2765 }
2766
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002767 private void setScreenBrightnessMode(int mode) {
Joe Onoratod28f7532010-11-06 12:56:53 -07002768 synchronized (mLocks) {
2769 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
2770 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
2771 mAutoBrightessEnabled = enabled;
2772 // This will get us a new value
2773 enableLightSensorLocked(mAutoBrightessEnabled && isScreenOn());
Mike Lockwood2d155d22009-10-27 09:32:30 -04002774 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002775 }
2776 }
2777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 /** Sets the screen off timeouts:
2779 * mKeylightDelay
2780 * mDimDelay
2781 * mScreenOffDelay
2782 * */
2783 private void setScreenOffTimeoutsLocked() {
2784 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002785 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 mDimDelay = -1;
2787 mScreenOffDelay = 0;
2788 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2789 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2790 mDimDelay = -1;
2791 mScreenOffDelay = 0;
2792 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002793 int totalDelay = mScreenOffTimeoutSetting;
2794 if (totalDelay > mMaximumScreenOffTimeout) {
2795 totalDelay = mMaximumScreenOffTimeout;
2796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2798 if (totalDelay < 0) {
Jim Millerbc4603b2010-08-30 21:21:34 -07002799 // negative number means stay on as long as possible.
2800 mScreenOffDelay = mMaximumScreenOffTimeout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 } else if (mKeylightDelay < totalDelay) {
2802 // subtract the time that the keylight delay. This will give us the
2803 // remainder of the time that we need to sleep to get the accurate
2804 // screen off timeout.
2805 mScreenOffDelay = totalDelay - mKeylightDelay;
2806 } else {
2807 mScreenOffDelay = 0;
2808 }
2809 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2810 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2811 mScreenOffDelay = LONG_DIM_TIME;
2812 } else {
2813 mDimDelay = -1;
2814 }
2815 }
2816 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002817 Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2819 + " mDimScreen=" + mDimScreen);
2820 }
2821 }
2822
2823 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002824 * Refreshes cached secure settings. Called once on startup, and
2825 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 */
Doug Zongker43866e02010-01-07 12:09:54 -08002827 private void updateSettingsValues() {
2828 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002830 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 SHORT_KEYLIGHT_DELAY_DEFAULT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002832 // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
2834
2835 private class LockList extends ArrayList<WakeLock>
2836 {
2837 void addLock(WakeLock wl)
2838 {
2839 int index = getIndex(wl.binder);
2840 if (index < 0) {
2841 this.add(wl);
2842 }
2843 }
2844
2845 WakeLock removeLock(IBinder binder)
2846 {
2847 int index = getIndex(binder);
2848 if (index >= 0) {
2849 return this.remove(index);
2850 } else {
2851 return null;
2852 }
2853 }
2854
2855 int getIndex(IBinder binder)
2856 {
2857 int N = this.size();
2858 for (int i=0; i<N; i++) {
2859 if (this.get(i).binder == binder) {
2860 return i;
2861 }
2862 }
2863 return -1;
2864 }
2865
2866 int gatherState()
2867 {
2868 int result = 0;
2869 int N = this.size();
2870 for (int i=0; i<N; i++) {
2871 WakeLock wl = this.get(i);
2872 if (wl.activated) {
2873 if (isScreenLock(wl.flags)) {
2874 result |= wl.minState;
2875 }
2876 }
2877 }
2878 return result;
2879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002880
Michael Chane96440f2009-05-06 10:27:36 -07002881 int reactivateScreenLocksLocked()
2882 {
2883 int result = 0;
2884 int N = this.size();
2885 for (int i=0; i<N; i++) {
2886 WakeLock wl = this.get(i);
2887 if (isScreenLock(wl.flags)) {
2888 wl.activated = true;
2889 result |= wl.minState;
2890 }
2891 }
Joe Onorato8274a0e2010-10-05 17:38:09 -04002892 if (mDebugProximitySensor) {
2893 Slog.d(TAG, "reactivateScreenLocksLocked mProxIgnoredBecauseScreenTurnedOff="
2894 + mProxIgnoredBecauseScreenTurnedOff);
2895 }
2896 mProxIgnoredBecauseScreenTurnedOff = false;
Michael Chane96440f2009-05-06 10:27:36 -07002897 return result;
2898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
2900
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08002901 public void setPolicy(WindowManagerPolicy p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 synchronized (mLocks) {
2903 mPolicy = p;
2904 mLocks.notifyAll();
2905 }
2906 }
2907
2908 WindowManagerPolicy getPolicyLocked() {
2909 while (mPolicy == null || !mDoneBooting) {
2910 try {
2911 mLocks.wait();
2912 } catch (InterruptedException e) {
2913 // Ignore
2914 }
2915 }
2916 return mPolicy;
2917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002920 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2921 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2922 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002923 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002924 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002925 }
2926
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002927 // wait until sensors are enabled before turning on screen.
2928 // some devices will not activate the light sensor properly on boot
2929 // unless we do this.
2930 if (mUseSoftwareAutoBrightness) {
2931 // turn the screen on
2932 setPowerState(SCREEN_BRIGHT);
2933 } else {
2934 // turn everything on
2935 setPowerState(ALL_BRIGHT);
2936 }
2937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 synchronized (mLocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002939 Slog.d(TAG, "system ready!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 mDoneBooting = true;
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002941
Joe Onoratod28f7532010-11-06 12:56:53 -07002942 enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled);
2943
Dianne Hackborn617f8772009-03-31 15:04:46 -07002944 long identity = Binder.clearCallingIdentity();
2945 try {
2946 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2947 mBatteryStats.noteScreenOn();
2948 } catch (RemoteException e) {
2949 // Nothing interesting to do.
2950 } finally {
2951 Binder.restoreCallingIdentity(identity);
2952 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002953 }
2954 }
2955
2956 void bootCompleted() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002957 Slog.d(TAG, "bootCompleted");
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002958 synchronized (mLocks) {
2959 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2961 updateWakeLockLocked();
2962 mLocks.notifyAll();
2963 }
2964 }
2965
Joe Onoratob08a1af2010-10-11 19:28:58 -07002966 // for watchdog
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 public void monitor() {
2968 synchronized (mLocks) { }
2969 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002970
2971 public int getSupportedWakeLockFlags() {
2972 int result = PowerManager.PARTIAL_WAKE_LOCK
2973 | PowerManager.FULL_WAKE_LOCK
2974 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2975
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002976 if (mProximitySensor != null) {
2977 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2978 }
2979
2980 return result;
2981 }
2982
Mike Lockwood237a2992009-09-15 14:42:16 -04002983 public void setBacklightBrightness(int brightness) {
2984 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2985 // Don't let applications turn the screen all the way off
Joe Onoratob08a1af2010-10-11 19:28:58 -07002986 synchronized (mLocks) {
Mike Lockwoodeb6456b2011-09-13 15:24:02 -04002987 brightness = Math.max(brightness, mScreenBrightnessDim);
Joe Onoratob08a1af2010-10-11 19:28:58 -07002988 mLcdLight.setBrightness(brightness);
2989 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2990 mButtonLight.setBrightness(brightness);
2991 long identity = Binder.clearCallingIdentity();
2992 try {
2993 mBatteryStats.noteScreenBrightness(brightness);
2994 } catch (RemoteException e) {
2995 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
2996 } finally {
2997 Binder.restoreCallingIdentity(identity);
2998 }
Mike Lockwood237a2992009-09-15 14:42:16 -04002999
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003000 mScreenBrightness.targetValue = brightness;
3001 mScreenBrightness.jumpToTargetLocked();
3002 }
3003 }
3004
3005 public void setAutoBrightnessAdjustment(float adj) {
3006 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
3007 synchronized (mLocks) {
3008 mLightSensorAdjustSetting = adj;
3009 if (mSensorManager != null && mLightSensorEnabled) {
3010 // clear calling identity so sensor manager battery stats are accurate
3011 long identity = Binder.clearCallingIdentity();
3012 try {
3013 // force recompute of backlight values
3014 if (mLightSensorValue >= 0) {
3015 int value = (int)mLightSensorValue;
3016 mLightSensorValue = -1;
3017 handleLightSensorValue(value, true);
3018 }
3019 } finally {
3020 Binder.restoreCallingIdentity(identity);
3021 }
Joe Onorato3d3db602010-10-18 16:08:16 -04003022 }
Mike Lockwood237a2992009-09-15 14:42:16 -04003023 }
3024 }
3025
Mike Lockwoodb11832d2009-11-25 15:25:55 -05003026 public void setAttentionLight(boolean on, int color) {
3027 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05003028 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05003029 }
3030
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003031 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003032 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003033 Slog.d(TAG, "enableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07003034 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003035 if (!mProximitySensorEnabled) {
3036 // clear calling identity so sensor manager battery stats are accurate
3037 long identity = Binder.clearCallingIdentity();
3038 try {
3039 mSensorManager.registerListener(mProximityListener, mProximitySensor,
3040 SensorManager.SENSOR_DELAY_NORMAL);
3041 mProximitySensorEnabled = true;
3042 } finally {
3043 Binder.restoreCallingIdentity(identity);
3044 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003045 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003046 }
3047
3048 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003049 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003050 Slog.d(TAG, "disableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07003051 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003052 if (mProximitySensorEnabled) {
3053 // clear calling identity so sensor manager battery stats are accurate
3054 long identity = Binder.clearCallingIdentity();
3055 try {
3056 mSensorManager.unregisterListener(mProximityListener);
3057 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003058 if (mProximityPartialLock.isHeld()) {
3059 mProximityPartialLock.release();
3060 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003061 mProximitySensorEnabled = false;
3062 } finally {
3063 Binder.restoreCallingIdentity(identity);
3064 }
3065 if (mProximitySensorActive) {
3066 mProximitySensorActive = false;
Joe Onorato8274a0e2010-10-05 17:38:09 -04003067 if (mDebugProximitySensor) {
3068 Slog.d(TAG, "disableProximityLockLocked mProxIgnoredBecauseScreenTurnedOff="
3069 + mProxIgnoredBecauseScreenTurnedOff);
3070 }
3071 if (!mProxIgnoredBecauseScreenTurnedOff) {
3072 forceUserActivityLocked();
3073 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003074 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04003075 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003076 }
3077
Mike Lockwood20f87d72009-11-05 16:08:51 -05003078 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003079 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003080 Slog.d(TAG, "proximityChangedLocked, active: " + active);
Mike Lockwood20f87d72009-11-05 16:08:51 -05003081 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003082 if (!mProximitySensorEnabled) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003083 Slog.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05003084 return;
3085 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003086 if (active) {
Joe Onorato8274a0e2010-10-05 17:38:09 -04003087 if (mDebugProximitySensor) {
3088 Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff="
3089 + mProxIgnoredBecauseScreenTurnedOff);
3090 }
3091 if (!mProxIgnoredBecauseScreenTurnedOff) {
3092 goToSleepLocked(SystemClock.uptimeMillis(),
3093 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
3094 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003095 mProximitySensorActive = true;
3096 } else {
3097 // proximity sensor negative events trigger as user activity.
3098 // temporarily set mUserActivityAllowed to true so this will work
3099 // even when the keyguard is on.
3100 mProximitySensorActive = false;
Joe Onorato8274a0e2010-10-05 17:38:09 -04003101 if (mDebugProximitySensor) {
3102 Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff="
3103 + mProxIgnoredBecauseScreenTurnedOff);
3104 }
3105 if (!mProxIgnoredBecauseScreenTurnedOff) {
3106 forceUserActivityLocked();
3107 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003108
3109 if (mProximityWakeLockCount == 0) {
3110 // disable sensor if we have no listeners left after proximity negative
3111 disableProximityLockLocked();
3112 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003113 }
3114 }
3115
Joe Onoratod28f7532010-11-06 12:56:53 -07003116 private void enableLightSensorLocked(boolean enable) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003117 if (mDebugLightSensor) {
Joe Onoratod28f7532010-11-06 12:56:53 -07003118 Slog.d(TAG, "enableLightSensorLocked enable=" + enable
3119 + " mAutoBrightessEnabled=" + mAutoBrightessEnabled);
3120 }
3121 if (!mAutoBrightessEnabled) {
3122 enable = false;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003123 }
3124 if (mSensorManager != null && mLightSensorEnabled != enable) {
3125 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003126 // clear calling identity so sensor manager battery stats are accurate
3127 long identity = Binder.clearCallingIdentity();
3128 try {
3129 if (enable) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003130 // reset our highest value when reenabling
3131 mHighestLightSensorValue = -1;
3132 // force recompute of backlight values
3133 if (mLightSensorValue >= 0) {
3134 int value = (int)mLightSensorValue;
3135 mLightSensorValue = -1;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003136 handleLightSensorValue(value, true);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003137 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003138 mSensorManager.registerListener(mLightListener, mLightSensor,
Mathias Agopian47f1fe52011-11-08 17:18:41 -08003139 LIGHT_SENSOR_RATE);
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003140 } else {
3141 mSensorManager.unregisterListener(mLightListener);
3142 mHandler.removeCallbacks(mAutoBrightnessTask);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003143 mLightSensorPendingDecrease = false;
3144 mLightSensorPendingIncrease = false;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04003145 }
3146 } finally {
3147 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04003148 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07003149 }
3150 }
3151
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003152 SensorEventListener mProximityListener = new SensorEventListener() {
3153 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05003154 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003155 synchronized (mLocks) {
3156 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05003157 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
3158 mLastProximityEventTime = milliseconds;
3159 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003160 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05003161
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003162 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05003163 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
3164 distance < mProximitySensor.getMaximumRange());
3165
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003166 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003167 Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active);
Mike Lockwoodee2b0942009-11-09 14:09:02 -05003168 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05003169 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
3170 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
3171 mProximityPendingValue = (active ? 1 : 0);
3172 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003173 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003174 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05003175 // process the value immediately
3176 mProximityPendingValue = -1;
3177 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003178 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05003179
3180 // update mProximityPartialLock state
3181 boolean held = mProximityPartialLock.isHeld();
3182 if (!held && proximityTaskQueued) {
3183 // hold wakelock until mProximityTask runs
3184 mProximityPartialLock.acquire();
3185 } else if (held && !proximityTaskQueued) {
3186 mProximityPartialLock.release();
3187 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003188 }
3189 }
3190
3191 public void onAccuracyChanged(Sensor sensor, int accuracy) {
3192 // ignore
3193 }
3194 };
3195
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003196 private void handleLightSensorValue(int value, boolean immediate) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003197 long milliseconds = SystemClock.elapsedRealtime();
3198 if (mLightSensorValue == -1 ||
3199 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
3200 // process the value immediately if screen has just turned on
3201 mHandler.removeCallbacks(mAutoBrightnessTask);
3202 mLightSensorPendingDecrease = false;
3203 mLightSensorPendingIncrease = false;
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003204 lightSensorChangedLocked(value, immediate);
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003205 } else {
3206 if ((value > mLightSensorValue && mLightSensorPendingDecrease) ||
3207 (value < mLightSensorValue && mLightSensorPendingIncrease) ||
3208 (value == mLightSensorValue) ||
3209 (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) {
3210 // delay processing to debounce the sensor
3211 mHandler.removeCallbacks(mAutoBrightnessTask);
3212 mLightSensorPendingDecrease = (value < mLightSensorValue);
3213 mLightSensorPendingIncrease = (value > mLightSensorValue);
3214 if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
3215 mLightSensorPendingValue = value;
3216 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
3217 }
3218 } else {
3219 mLightSensorPendingValue = value;
3220 }
3221 }
3222 }
3223
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003224 SensorEventListener mLightListener = new SensorEventListener() {
3225 public void onSensorChanged(SensorEvent event) {
Mike Lockwood5dca30a2011-10-13 16:29:29 -04003226 if (mDebugLightSensor) {
3227 Slog.d(TAG, "onSensorChanged: light value: " + event.values[0]);
3228 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003229 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05003230 // ignore light sensor while screen is turning off
3231 if (isScreenTurningOffLocked()) {
3232 return;
3233 }
Dianne Hackbornd9ea4682012-01-20 18:36:40 -08003234 handleLightSensorValue((int)event.values[0], false);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04003235 }
3236 }
3237
3238 public void onAccuracyChanged(Sensor sensor, int accuracy) {
3239 // ignore
3240 }
3241 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242}