blob: bf6996c7943275fcc2c61946e6eefacce24aa13c [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;
20import com.android.server.am.BatteryStatsService;
21
22import android.app.ActivityManagerNative;
23import android.app.IActivityManager;
24import android.content.BroadcastReceiver;
25import android.content.ContentQueryMap;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070031import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080032import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.database.Cursor;
Mike Lockwoodbc706a02009-07-27 13:50:57 -070034import android.hardware.Sensor;
35import android.hardware.SensorEvent;
36import android.hardware.SensorEventListener;
37import android.hardware.SensorManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.BatteryStats;
39import android.os.Binder;
Doug Zongker43866e02010-01-07 12:09:54 -080040import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Handler;
42import android.os.HandlerThread;
43import android.os.IBinder;
Doug Zongker43866e02010-01-07 12:09:54 -080044import android.os.IMountService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.IPowerManager;
46import android.os.LocalPowerManager;
47import android.os.Power;
48import android.os.PowerManager;
49import android.os.Process;
50import android.os.RemoteException;
San Mehat14e69af2010-01-06 14:58:18 -080051import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.SystemClock;
53import android.provider.Settings.SettingNotFoundException;
54import android.provider.Settings;
55import android.util.EventLog;
56import android.util.Log;
57import android.view.WindowManagerPolicy;
58import static android.provider.Settings.System.DIM_SCREEN;
59import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
Dan Murphy951764b2009-08-27 14:59:03 -050060import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
Mike Lockwooddc3494e2009-10-14 21:17:09 -070061import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
63import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
64
65import java.io.FileDescriptor;
Doug Zongker50a21f42009-11-19 12:49:53 -080066import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.PrintWriter;
68import java.util.ArrayList;
69import java.util.HashMap;
70import java.util.Observable;
71import java.util.Observer;
72
Mike Lockwoodbc706a02009-07-27 13:50:57 -070073class PowerManagerService extends IPowerManager.Stub
Mike Lockwood8738e0c2009-10-04 08:44:47 -040074 implements LocalPowerManager, Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 private static final String TAG = "PowerManagerService";
77 static final String PARTIAL_NAME = "PowerManagerService";
78
79 private static final boolean LOG_PARTIAL_WL = false;
80
81 // Indicates whether touch-down cycles should be logged as part of the
82 // LOG_POWER_SCREEN_STATE log events
83 private static final boolean LOG_TOUCH_DOWNS = true;
84
85 private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK
86 | PowerManager.SCREEN_DIM_WAKE_LOCK
87 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Mike Lockwoodbc706a02009-07-27 13:50:57 -070088 | PowerManager.FULL_WAKE_LOCK
89 | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 // time since last state: time since last event:
Doug Zongker43866e02010-01-07 12:09:54 -080092 // The short keylight delay comes from secure settings; this is the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
94 private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec
95 private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec
96 private static final int LONG_DIM_TIME = 7000; // t+N-5 sec
97
Mike Lockwoodd7786b42009-10-15 17:09:16 -070098 // How long to wait to debounce light sensor changes.
Mike Lockwood9b8136922009-11-06 15:53:59 -050099 private static final int LIGHT_SENSOR_DELAY = 2000;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700100
Mike Lockwood20f87d72009-11-05 16:08:51 -0500101 // For debouncing the proximity sensor.
102 private static final int PROXIMITY_SENSOR_DELAY = 1000;
103
Mike Lockwoodd20ea362009-09-15 00:13:38 -0400104 // trigger proximity if distance is less than 5 cm
105 private static final float PROXIMITY_THRESHOLD = 5.0f;
106
Doug Zongker43866e02010-01-07 12:09:54 -0800107 // Cached secure settings; see updateSettingsValues()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
109
110 // flags for setPowerState
111 private static final int SCREEN_ON_BIT = 0x00000001;
112 private static final int SCREEN_BRIGHT_BIT = 0x00000002;
113 private static final int BUTTON_BRIGHT_BIT = 0x00000004;
114 private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
115 private static final int BATTERY_LOW_BIT = 0x00000010;
116
117 // values for setPowerState
118
119 // SCREEN_OFF == everything off
120 private static final int SCREEN_OFF = 0x00000000;
121
122 // SCREEN_DIM == screen on, screen backlight dim
123 private static final int SCREEN_DIM = SCREEN_ON_BIT;
124
125 // SCREEN_BRIGHT == screen on, screen backlight bright
126 private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
127
128 // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
129 private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
130
131 // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
132 private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
133
134 // used for noChangeLights in setPowerState()
135 private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
136
137 static final boolean ANIMATE_SCREEN_LIGHTS = true;
138 static final boolean ANIMATE_BUTTON_LIGHTS = false;
139 static final boolean ANIMATE_KEYBOARD_LIGHTS = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 static final int ANIM_STEPS = 60/4;
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400142 // Slower animation for autobrightness changes
143 static final int AUTOBRIGHTNESS_ANIM_STEPS = 60;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145 // These magic numbers are the initial state of the LEDs at boot. Ideally
146 // we should read them from the driver, but our current hardware returns 0
147 // for the initial value. Oops!
148 static final int INITIAL_SCREEN_BRIGHTNESS = 255;
149 static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF;
150 static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private final int MY_UID;
153
154 private boolean mDoneBooting = false;
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500155 private boolean mBootCompleted = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 private int mStayOnConditions = 0;
Joe Onorato128e7292009-03-24 18:41:31 -0700157 private int[] mBroadcastQueue = new int[] { -1, -1, -1 };
158 private int[] mBroadcastWhy = new int[3];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private int mPartialCount = 0;
160 private int mPowerState;
Mike Lockwood435eb642009-12-03 08:40:18 -0500161 // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER,
162 // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
163 private int mScreenOffReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private int mUserState;
165 private boolean mKeyboardVisible = false;
166 private boolean mUserActivityAllowed = true;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500167 private int mProximityWakeLockCount = 0;
168 private boolean mProximitySensorEnabled = false;
Mike Lockwood36fc3022009-08-25 16:49:06 -0700169 private boolean mProximitySensorActive = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -0500170 private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active
171 private long mLastProximityEventTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 private int mTotalDelaySetting;
173 private int mKeylightDelay;
174 private int mDimDelay;
175 private int mScreenOffDelay;
176 private int mWakeLockState;
177 private long mLastEventTime = 0;
178 private long mScreenOffTime;
179 private volatile WindowManagerPolicy mPolicy;
180 private final LockList mLocks = new LockList();
181 private Intent mScreenOffIntent;
182 private Intent mScreenOnIntent;
Mike Lockwood3a322132009-11-24 00:30:52 -0500183 private LightsService mLightsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private Context mContext;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500185 private LightsService.Light mLcdLight;
186 private LightsService.Light mButtonLight;
187 private LightsService.Light mKeyboardLight;
188 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 private UnsynchronizedWakeLock mBroadcastWakeLock;
190 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
191 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
192 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500193 private UnsynchronizedWakeLock mProximityPartialLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 private HandlerThread mHandlerThread;
195 private Handler mHandler;
196 private TimeoutTask mTimeoutTask = new TimeoutTask();
197 private LightAnimator mLightAnimator = new LightAnimator();
198 private final BrightnessState mScreenBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700199 = new BrightnessState(SCREEN_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 private final BrightnessState mKeyboardBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700201 = new BrightnessState(KEYBOARD_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private final BrightnessState mButtonBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700203 = new BrightnessState(BUTTON_BRIGHT_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -0700204 private boolean mStillNeedSleepNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 private boolean mIsPowered = false;
206 private IActivityManager mActivityService;
207 private IBatteryStats mBatteryStats;
208 private BatteryService mBatteryService;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700209 private SensorManager mSensorManager;
210 private Sensor mProximitySensor;
Mike Lockwood8738e0c2009-10-04 08:44:47 -0400211 private Sensor mLightSensor;
212 private boolean mLightSensorEnabled;
213 private float mLightSensorValue = -1;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700214 private float mLightSensorPendingValue = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500215 private int mLightSensorScreenBrightness = -1;
216 private int mLightSensorButtonBrightness = -1;
217 private int mLightSensorKeyboardBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private boolean mDimScreen = true;
219 private long mNextTimeout;
220 private volatile int mPokey = 0;
221 private volatile boolean mPokeAwakeOnSet = false;
222 private volatile boolean mInitComplete = false;
223 private HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500224 // mLastScreenOnTime is the time the screen was last turned on
225 private long mLastScreenOnTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 private boolean mPreventScreenOn;
227 private int mScreenBrightnessOverride = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500228 private int mButtonBrightnessOverride = -1;
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400229 private boolean mUseSoftwareAutoBrightness;
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700230 private boolean mAutoBrightessEnabled;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700231 private int[] mAutoBrightnessLevels;
232 private int[] mLcdBacklightValues;
233 private int[] mButtonBacklightValues;
234 private int[] mKeyboardBacklightValues;
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500235 private int mLightSensorWarmupTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 // Used when logging number and duration of touch-down cycles
238 private long mTotalTouchDownTime;
239 private long mLastTouchDown;
240 private int mTouchCycles;
241
242 // could be either static or controllable at runtime
243 private static final boolean mSpew = false;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500244 private static final boolean mDebugProximitySensor = (true || mSpew);
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400245 private static final boolean mDebugLightSensor = (false || mSpew);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
247 /*
248 static PrintStream mLog;
249 static {
250 try {
251 mLog = new PrintStream("/data/power.log");
252 }
253 catch (FileNotFoundException e) {
254 android.util.Log.e(TAG, "Life is hard", e);
255 }
256 }
257 static class Log {
258 static void d(String tag, String s) {
259 mLog.println(s);
260 android.util.Log.d(tag, s);
261 }
262 static void i(String tag, String s) {
263 mLog.println(s);
264 android.util.Log.i(tag, s);
265 }
266 static void w(String tag, String s) {
267 mLog.println(s);
268 android.util.Log.w(tag, s);
269 }
270 static void e(String tag, String s) {
271 mLog.println(s);
272 android.util.Log.e(tag, s);
273 }
274 }
275 */
276
277 /**
278 * This class works around a deadlock between the lock in PowerManager.WakeLock
279 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
280 * mToken object so it can be accessed from any thread, but it calls into here
281 * with its lock held. This class is essentially a reimplementation of
282 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
283 * only call it with our own locks held.
284 */
285 private class UnsynchronizedWakeLock {
286 int mFlags;
287 String mTag;
288 IBinder mToken;
289 int mCount = 0;
290 boolean mRefCounted;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500291 boolean mHeld;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
294 mFlags = flags;
295 mTag = tag;
296 mToken = new Binder();
297 mRefCounted = refCounted;
298 }
299
300 public void acquire() {
301 if (!mRefCounted || mCount++ == 0) {
302 long ident = Binder.clearCallingIdentity();
303 try {
304 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
305 MY_UID, mTag);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500306 mHeld = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 } finally {
308 Binder.restoreCallingIdentity(ident);
309 }
310 }
311 }
312
313 public void release() {
314 if (!mRefCounted || --mCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500315 PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500316 mHeld = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 if (mCount < 0) {
319 throw new RuntimeException("WakeLock under-locked " + mTag);
320 }
321 }
322
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500323 public boolean isHeld()
324 {
325 return mHeld;
326 }
327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public String toString() {
329 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500330 + " mCount=" + mCount + " mHeld=" + mHeld + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332 }
333
334 private final class BatteryReceiver extends BroadcastReceiver {
335 @Override
336 public void onReceive(Context context, Intent intent) {
337 synchronized (mLocks) {
338 boolean wasPowered = mIsPowered;
339 mIsPowered = mBatteryService.isPowered();
340
341 if (mIsPowered != wasPowered) {
342 // update mStayOnWhilePluggedIn wake lock
343 updateWakeLockLocked();
344
345 // treat plugging and unplugging the devices as a user activity.
346 // users find it disconcerting when they unplug the device
347 // and it shuts off right away.
348 // temporarily set mUserActivityAllowed to true so this will work
349 // even when the keyguard is on.
350 synchronized (mLocks) {
Mike Lockwood200b30b2009-09-20 00:23:59 -0400351 forceUserActivityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353 }
354 }
355 }
356 }
357
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500358 private final class BootCompletedReceiver extends BroadcastReceiver {
359 @Override
360 public void onReceive(Context context, Intent intent) {
361 bootCompleted();
362 }
363 }
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Set the setting that determines whether the device stays on when plugged in.
367 * The argument is a bit string, with each bit specifying a power source that,
368 * when the device is connected to that source, causes the device to stay on.
369 * See {@link android.os.BatteryManager} for the list of power sources that
370 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
371 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
372 * @param val an {@code int} containing the bits that specify which power sources
373 * should cause the device to stay on.
374 */
375 public void setStayOnSetting(int val) {
376 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
377 Settings.System.putInt(mContext.getContentResolver(),
378 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
379 }
380
381 private class SettingsObserver implements Observer {
382 private int getInt(String name) {
383 return mSettings.getValues(name).getAsInteger(Settings.System.VALUE);
384 }
385
386 public void update(Observable o, Object arg) {
387 synchronized (mLocks) {
388 // STAY_ON_WHILE_PLUGGED_IN
389 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN);
390 updateWakeLockLocked();
391
392 // SCREEN_OFF_TIMEOUT
393 mTotalDelaySetting = getInt(SCREEN_OFF_TIMEOUT);
394
395 // DIM_SCREEN
396 //mDimScreen = getInt(DIM_SCREEN) != 0;
397
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700398 // SCREEN_BRIGHTNESS_MODE
399 setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE));
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 // recalculate everything
402 setScreenOffTimeoutsLocked();
403 }
404 }
405 }
406
407 PowerManagerService()
408 {
409 // Hack to get our uid... should have a func for this.
410 long token = Binder.clearCallingIdentity();
411 MY_UID = Binder.getCallingUid();
412 Binder.restoreCallingIdentity(token);
413
414 // XXX remove this when the kernel doesn't timeout wake locks
415 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
416
417 // assume nothing is on yet
418 mUserState = mPowerState = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 // Add ourself to the Watchdog monitors.
421 Watchdog.getInstance().addMonitor(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
424 private ContentQueryMap mSettings;
425
Mike Lockwood3a322132009-11-24 00:30:52 -0500426 void init(Context context, LightsService lights, IActivityManager activity,
The Android Open Source Project10592532009-03-18 17:39:46 -0700427 BatteryService battery) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500428 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 mContext = context;
430 mActivityService = activity;
431 mBatteryStats = BatteryStatsService.getService();
432 mBatteryService = battery;
433
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500434 mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT);
435 mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS);
436 mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD);
437 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 mHandlerThread = new HandlerThread("PowerManagerService") {
440 @Override
441 protected void onLooperPrepared() {
442 super.onLooperPrepared();
443 initInThread();
444 }
445 };
446 mHandlerThread.start();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 synchronized (mHandlerThread) {
449 while (!mInitComplete) {
450 try {
451 mHandlerThread.wait();
452 } catch (InterruptedException e) {
453 // Ignore
454 }
455 }
456 }
457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 void initInThread() {
460 mHandler = new Handler();
461
462 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700463 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
465 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
466 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
467 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
468 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
469 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500470 mProximityPartialLock = new UnsynchronizedWakeLock(
471 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
473 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
474 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
475 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
476 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
477
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700478 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400479
480 // read settings for auto-brightness
481 mUseSoftwareAutoBrightness = resources.getBoolean(
482 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400483 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700484 mAutoBrightnessLevels = resources.getIntArray(
485 com.android.internal.R.array.config_autoBrightnessLevels);
486 mLcdBacklightValues = resources.getIntArray(
487 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
488 mButtonBacklightValues = resources.getIntArray(
489 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
490 mKeyboardBacklightValues = resources.getIntArray(
491 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500492 mLightSensorWarmupTime = resources.getInteger(
493 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700494 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700495
496 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
498 "(" + Settings.System.NAME + "=?) or ("
499 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700500 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 + Settings.System.NAME + "=?)",
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700502 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN,
503 SCREEN_BRIGHTNESS_MODE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 null);
505 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
506 SettingsObserver settingsObserver = new SettingsObserver();
507 mSettings.addObserver(settingsObserver);
508
509 // pretend that the settings changed so we will get their initial state
510 settingsObserver.update(mSettings, null);
511
512 // register for the battery changed notifications
513 IntentFilter filter = new IntentFilter();
514 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
515 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500516 filter = new IntentFilter();
517 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
518 mContext.registerReceiver(new BootCompletedReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519
Doug Zongker43866e02010-01-07 12:09:54 -0800520 // Listen for secure settings changes
521 mContext.getContentResolver().registerContentObserver(
522 Settings.Secure.CONTENT_URI, true,
523 new ContentObserver(new Handler()) {
524 public void onChange(boolean selfChange) {
525 updateSettingsValues();
526 }
527 });
528 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
Mike Lockwood4984e732009-11-01 08:16:33 -0500530 if (mUseSoftwareAutoBrightness) {
Mike Lockwood6c97fca2009-10-20 08:10:00 -0400531 // turn the screen on
532 setPowerState(SCREEN_BRIGHT);
533 } else {
534 // turn everything on
535 setPowerState(ALL_BRIGHT);
536 }
Dan Murphy951764b2009-08-27 14:59:03 -0500537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 synchronized (mHandlerThread) {
539 mInitComplete = true;
540 mHandlerThread.notifyAll();
541 }
542 }
543
544 private class WakeLock implements IBinder.DeathRecipient
545 {
546 WakeLock(int f, IBinder b, String t, int u) {
547 super();
548 flags = f;
549 binder = b;
550 tag = t;
551 uid = u == MY_UID ? Process.SYSTEM_UID : u;
552 if (u != MY_UID || (
553 !"KEEP_SCREEN_ON_FLAG".equals(tag)
554 && !"KeyInputQueue".equals(tag))) {
555 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
556 ? BatteryStats.WAKE_TYPE_PARTIAL
557 : BatteryStats.WAKE_TYPE_FULL;
558 } else {
559 monitorType = -1;
560 }
561 try {
562 b.linkToDeath(this, 0);
563 } catch (RemoteException e) {
564 binderDied();
565 }
566 }
567 public void binderDied() {
568 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500569 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 }
572 final int flags;
573 final IBinder binder;
574 final String tag;
575 final int uid;
576 final int monitorType;
577 boolean activated = true;
578 int minState;
579 }
580
581 private void updateWakeLockLocked() {
582 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
583 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
584 mStayOnWhilePluggedInScreenDimLock.acquire();
585 mStayOnWhilePluggedInPartialLock.acquire();
586 } else {
587 mStayOnWhilePluggedInScreenDimLock.release();
588 mStayOnWhilePluggedInPartialLock.release();
589 }
590 }
591
592 private boolean isScreenLock(int flags)
593 {
594 int n = flags & LOCK_MASK;
595 return n == PowerManager.FULL_WAKE_LOCK
596 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
597 || n == PowerManager.SCREEN_DIM_WAKE_LOCK;
598 }
599
600 public void acquireWakeLock(int flags, IBinder lock, String tag) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 int uid = Binder.getCallingUid();
Michael Chane96440f2009-05-06 10:27:36 -0700602 if (uid != Process.myUid()) {
603 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 long ident = Binder.clearCallingIdentity();
606 try {
607 synchronized (mLocks) {
608 acquireWakeLockLocked(flags, lock, uid, tag);
609 }
610 } finally {
611 Binder.restoreCallingIdentity(ident);
612 }
613 }
614
615 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, String tag) {
616 int acquireUid = -1;
617 String acquireName = null;
618 int acquireType = -1;
619
620 if (mSpew) {
621 Log.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
622 }
623
624 int index = mLocks.getIndex(lock);
625 WakeLock wl;
626 boolean newlock;
627 if (index < 0) {
628 wl = new WakeLock(flags, lock, tag, uid);
629 switch (wl.flags & LOCK_MASK)
630 {
631 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500632 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400633 wl.minState = SCREEN_BRIGHT;
634 } else {
635 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 break;
638 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
639 wl.minState = SCREEN_BRIGHT;
640 break;
641 case PowerManager.SCREEN_DIM_WAKE_LOCK:
642 wl.minState = SCREEN_DIM;
643 break;
644 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700645 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 break;
647 default:
648 // just log and bail. we're in the server, so don't
649 // throw an exception.
650 Log.e(TAG, "bad wakelock type for lock '" + tag + "' "
651 + " flags=" + flags);
652 return;
653 }
654 mLocks.addLock(wl);
655 newlock = true;
656 } else {
657 wl = mLocks.get(index);
658 newlock = false;
659 }
660 if (isScreenLock(flags)) {
661 // if this causes a wakeup, we reactivate all of the locks and
662 // set it to whatever they want. otherwise, we modulate that
663 // by the current state so we never turn it more on than
664 // it already is.
665 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
Michael Chane96440f2009-05-06 10:27:36 -0700666 int oldWakeLockState = mWakeLockState;
667 mWakeLockState = mLocks.reactivateScreenLocksLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 if (mSpew) {
669 Log.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
Michael Chane96440f2009-05-06 10:27:36 -0700670 + " mWakeLockState=0x"
671 + Integer.toHexString(mWakeLockState)
672 + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 } else {
675 if (mSpew) {
676 Log.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
677 + " mLocks.gatherState()=0x"
678 + Integer.toHexString(mLocks.gatherState())
679 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
680 }
681 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
682 }
683 setPowerState(mWakeLockState | mUserState);
684 }
685 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
686 if (newlock) {
687 mPartialCount++;
688 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800689 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691 }
692 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700693 } else if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500694 mProximityWakeLockCount++;
695 if (mProximityWakeLockCount == 1) {
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700696 enableProximityLockLocked();
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699 if (newlock) {
700 acquireUid = wl.uid;
701 acquireName = wl.tag;
702 acquireType = wl.monitorType;
703 }
704
705 if (acquireType >= 0) {
706 try {
707 mBatteryStats.noteStartWakelock(acquireUid, acquireName, acquireType);
708 } catch (RemoteException e) {
709 // Ignore
710 }
711 }
712 }
713
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500714 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700715 int uid = Binder.getCallingUid();
716 if (uid != Process.myUid()) {
717 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719
720 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500721 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 }
724
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500725 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 int releaseUid;
727 String releaseName;
728 int releaseType;
729
730 WakeLock wl = mLocks.removeLock(lock);
731 if (wl == null) {
732 return;
733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 if (mSpew) {
736 Log.d(TAG, "releaseWakeLock flags=0x"
737 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
738 }
739
740 if (isScreenLock(wl.flags)) {
741 mWakeLockState = mLocks.gatherState();
742 // goes in the middle to reduce flicker
743 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
744 userActivity(SystemClock.uptimeMillis(), false);
745 }
746 setPowerState(mWakeLockState | mUserState);
747 }
748 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
749 mPartialCount--;
750 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800751 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 Power.releaseWakeLock(PARTIAL_NAME);
753 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700754 } else if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500755 mProximityWakeLockCount--;
756 if (mProximityWakeLockCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500757 if (mProximitySensorActive &&
758 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500759 // wait for proximity sensor to go negative before disabling sensor
760 if (mDebugProximitySensor) {
761 Log.d(TAG, "waiting for proximity sensor to go negative");
762 }
763 } else {
764 disableProximityLockLocked();
765 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768 // Unlink the lock from the binder.
769 wl.binder.unlinkToDeath(wl, 0);
770 releaseUid = wl.uid;
771 releaseName = wl.tag;
772 releaseType = wl.monitorType;
773
774 if (releaseType >= 0) {
775 long origId = Binder.clearCallingIdentity();
776 try {
777 mBatteryStats.noteStopWakelock(releaseUid, releaseName, releaseType);
778 } catch (RemoteException e) {
779 // Ignore
780 } finally {
781 Binder.restoreCallingIdentity(origId);
782 }
783 }
784 }
785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 private class PokeLock implements IBinder.DeathRecipient
787 {
788 PokeLock(int p, IBinder b, String t) {
789 super();
790 this.pokey = p;
791 this.binder = b;
792 this.tag = t;
793 try {
794 b.linkToDeath(this, 0);
795 } catch (RemoteException e) {
796 binderDied();
797 }
798 }
799 public void binderDied() {
800 setPokeLock(0, this.binder, this.tag);
801 }
802 int pokey;
803 IBinder binder;
804 String tag;
805 boolean awakeOnSet;
806 }
807
808 public void setPokeLock(int pokey, IBinder token, String tag) {
809 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
810 if (token == null) {
811 Log.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
812 return;
813 }
814
815 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
816 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
817 + " and POKE_LOCK_MEDIUM_TIMEOUT");
818 }
819
820 synchronized (mLocks) {
821 if (pokey != 0) {
822 PokeLock p = mPokeLocks.get(token);
823 int oldPokey = 0;
824 if (p != null) {
825 oldPokey = p.pokey;
826 p.pokey = pokey;
827 } else {
828 p = new PokeLock(pokey, token, tag);
829 mPokeLocks.put(token, p);
830 }
831 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
832 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
833 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
834 p.awakeOnSet = true;
835 }
836 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700837 PokeLock rLock = mPokeLocks.remove(token);
838 if (rLock != null) {
839 token.unlinkToDeath(rLock, 0);
840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
843 int oldPokey = mPokey;
844 int cumulative = 0;
845 boolean oldAwakeOnSet = mPokeAwakeOnSet;
846 boolean awakeOnSet = false;
847 for (PokeLock p: mPokeLocks.values()) {
848 cumulative |= p.pokey;
849 if (p.awakeOnSet) {
850 awakeOnSet = true;
851 }
852 }
853 mPokey = cumulative;
854 mPokeAwakeOnSet = awakeOnSet;
855
856 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
857 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (oldCumulativeTimeout != newCumulativeTimeout) {
860 setScreenOffTimeoutsLocked();
861 // reset the countdown timer, but use the existing nextState so it doesn't
862 // change anything
863 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
864 }
865 }
866 }
867
868 private static String lockType(int type)
869 {
870 switch (type)
871 {
872 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700873 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700875 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700877 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700879 return "PARTIAL_WAKE_LOCK ";
880 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
881 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 default:
David Brown251faa62009-08-02 22:04:36 -0700883 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885 }
886
887 private static String dumpPowerState(int state) {
888 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
889 ? "KEYBOARD_BRIGHT_BIT " : "")
890 + (((state & SCREEN_BRIGHT_BIT) != 0)
891 ? "SCREEN_BRIGHT_BIT " : "")
892 + (((state & SCREEN_ON_BIT) != 0)
893 ? "SCREEN_ON_BIT " : "")
894 + (((state & BATTERY_LOW_BIT) != 0)
895 ? "BATTERY_LOW_BIT " : "");
896 }
897
898 @Override
899 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
900 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
901 != PackageManager.PERMISSION_GRANTED) {
902 pw.println("Permission Denial: can't dump PowerManager from from pid="
903 + Binder.getCallingPid()
904 + ", uid=" + Binder.getCallingUid());
905 return;
906 }
907
908 long now = SystemClock.uptimeMillis();
909
910 pw.println("Power Manager State:");
911 pw.println(" mIsPowered=" + mIsPowered
912 + " mPowerState=" + mPowerState
913 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
914 + " ms");
915 pw.println(" mPartialCount=" + mPartialCount);
916 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
917 pw.println(" mUserState=" + dumpPowerState(mUserState));
918 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
919 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
920 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
921 + " " + ((mNextTimeout-now)/1000) + "s from now");
922 pw.println(" mDimScreen=" + mDimScreen
923 + " mStayOnConditions=" + mStayOnConditions);
Mike Lockwood435eb642009-12-03 08:40:18 -0500924 pw.println(" mScreenOffReason=" + mScreenOffReason
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 + " mUserState=" + mUserState);
Joe Onorato128e7292009-03-24 18:41:31 -0700926 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
927 + ',' + mBroadcastQueue[2] + "}");
928 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
929 + ',' + mBroadcastWhy[2] + "}");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
931 pw.println(" mKeyboardVisible=" + mKeyboardVisible
932 + " mUserActivityAllowed=" + mUserActivityAllowed);
933 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
934 + " mScreenOffDelay=" + mScreenOffDelay);
935 pw.println(" mPreventScreenOn=" + mPreventScreenOn
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500936 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
937 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 pw.println(" mTotalDelaySetting=" + mTotalDelaySetting);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500939 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
941 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
942 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
943 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500944 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500945 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
946 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700947 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
Mike Lockwood20f87d72009-11-05 16:08:51 -0500948 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
949 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700950 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500951 pw.println(" mLightSensorValue=" + mLightSensorValue
952 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
953 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
954 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
955 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400956 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700957 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 mScreenBrightness.dump(pw, " mScreenBrightness: ");
959 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
960 mButtonBrightness.dump(pw, " mButtonBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 int N = mLocks.size();
963 pw.println();
964 pw.println("mLocks.size=" + N + ":");
965 for (int i=0; i<N; i++) {
966 WakeLock wl = mLocks.get(i);
967 String type = lockType(wl.flags & LOCK_MASK);
968 String acquireCausesWakeup = "";
969 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
970 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
971 }
972 String activated = "";
973 if (wl.activated) {
974 activated = " activated";
975 }
976 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
977 + activated + " (minState=" + wl.minState + ")");
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 pw.println();
981 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
982 for (PokeLock p: mPokeLocks.values()) {
983 pw.println(" poke lock '" + p.tag + "':"
984 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
985 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
Joe Onoratoe68ffcb2009-03-24 19:11:13 -0700986 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
987 ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
989 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
990 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
991 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
992 }
993
994 pw.println();
995 }
996
997 private void setTimeoutLocked(long now, int nextState)
998 {
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500999 if (mBootCompleted) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 mHandler.removeCallbacks(mTimeoutTask);
1001 mTimeoutTask.nextState = nextState;
1002 long when = now;
1003 switch (nextState)
1004 {
1005 case SCREEN_BRIGHT:
1006 when += mKeylightDelay;
1007 break;
1008 case SCREEN_DIM:
1009 if (mDimDelay >= 0) {
1010 when += mDimDelay;
1011 break;
1012 } else {
1013 Log.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1014 }
1015 case SCREEN_OFF:
1016 synchronized (mLocks) {
1017 when += mScreenOffDelay;
1018 }
1019 break;
1020 }
1021 if (mSpew) {
1022 Log.d(TAG, "setTimeoutLocked now=" + now + " nextState=" + nextState
1023 + " when=" + when);
1024 }
1025 mHandler.postAtTime(mTimeoutTask, when);
1026 mNextTimeout = when; // for debugging
1027 }
1028 }
1029
1030 private void cancelTimerLocked()
1031 {
1032 mHandler.removeCallbacks(mTimeoutTask);
1033 mTimeoutTask.nextState = -1;
1034 }
1035
1036 private class TimeoutTask implements Runnable
1037 {
1038 int nextState; // access should be synchronized on mLocks
1039 public void run()
1040 {
1041 synchronized (mLocks) {
1042 if (mSpew) {
1043 Log.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
1044 }
1045
1046 if (nextState == -1) {
1047 return;
1048 }
1049
1050 mUserState = this.nextState;
1051 setPowerState(this.nextState | mWakeLockState);
1052
1053 long now = SystemClock.uptimeMillis();
1054
1055 switch (this.nextState)
1056 {
1057 case SCREEN_BRIGHT:
1058 if (mDimDelay >= 0) {
1059 setTimeoutLocked(now, SCREEN_DIM);
1060 break;
1061 }
1062 case SCREEN_DIM:
1063 setTimeoutLocked(now, SCREEN_OFF);
1064 break;
1065 }
1066 }
1067 }
1068 }
1069
1070 private void sendNotificationLocked(boolean on, int why)
1071 {
Joe Onorato64c62ba2009-03-24 20:13:57 -07001072 if (!on) {
1073 mStillNeedSleepNotification = false;
1074 }
1075
Joe Onorato128e7292009-03-24 18:41:31 -07001076 // Add to the queue.
1077 int index = 0;
1078 while (mBroadcastQueue[index] != -1) {
1079 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
Joe Onorato128e7292009-03-24 18:41:31 -07001081 mBroadcastQueue[index] = on ? 1 : 0;
1082 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
Joe Onorato128e7292009-03-24 18:41:31 -07001084 // If we added it position 2, then there is a pair that can be stripped.
1085 // If we added it position 1 and we're turning the screen off, we can strip
1086 // the pair and do nothing, because the screen is already off, and therefore
1087 // keyguard has already been enabled.
1088 // However, if we added it at position 1 and we're turning it on, then position
1089 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1090 // on, so have to run the queue then.
1091 if (index == 2) {
1092 // Also, while we're collapsing them, if it's going to be an "off," and one
1093 // is off because of user, then use that, regardless of whether it's the first
1094 // or second one.
1095 if (!on && why == WindowManagerPolicy.OFF_BECAUSE_OF_USER) {
1096 mBroadcastWhy[0] = WindowManagerPolicy.OFF_BECAUSE_OF_USER;
1097 }
1098 mBroadcastQueue[0] = on ? 1 : 0;
1099 mBroadcastQueue[1] = -1;
1100 mBroadcastQueue[2] = -1;
1101 index = 0;
1102 }
1103 if (index == 1 && !on) {
1104 mBroadcastQueue[0] = -1;
1105 mBroadcastQueue[1] = -1;
1106 index = -1;
1107 // The wake lock was being held, but we're not actually going to do any
1108 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001109 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001111 }
1112
1113 // Now send the message.
1114 if (index >= 0) {
1115 // Acquire the broadcast wake lock before changing the power
1116 // state. It will be release after the broadcast is sent.
1117 // We always increment the ref count for each notification in the queue
1118 // and always decrement when that notification is handled.
1119 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001121 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 }
1124
1125 private Runnable mNotificationTask = new Runnable()
1126 {
1127 public void run()
1128 {
Joe Onorato128e7292009-03-24 18:41:31 -07001129 while (true) {
1130 int value;
1131 int why;
1132 WindowManagerPolicy policy;
1133 synchronized (mLocks) {
1134 value = mBroadcastQueue[0];
1135 why = mBroadcastWhy[0];
1136 for (int i=0; i<2; i++) {
1137 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1138 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1139 }
1140 policy = getPolicyLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
Joe Onorato128e7292009-03-24 18:41:31 -07001142 if (value == 1) {
1143 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
Joe Onorato128e7292009-03-24 18:41:31 -07001145 policy.screenTurnedOn();
1146 try {
1147 ActivityManagerNative.getDefault().wakingUp();
1148 } catch (RemoteException e) {
1149 // ignore it
1150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151
Joe Onorato128e7292009-03-24 18:41:31 -07001152 if (mSpew) {
1153 Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
1154 }
1155 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1156 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1157 mScreenOnBroadcastDone, mHandler, 0, null, null);
1158 } else {
1159 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001161 mBroadcastWakeLock.mCount);
1162 mBroadcastWakeLock.release();
1163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
Joe Onorato128e7292009-03-24 18:41:31 -07001166 else if (value == 0) {
1167 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
Joe Onorato128e7292009-03-24 18:41:31 -07001169 policy.screenTurnedOff(why);
1170 try {
1171 ActivityManagerNative.getDefault().goingToSleep();
1172 } catch (RemoteException e) {
1173 // ignore it.
1174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175
Joe Onorato128e7292009-03-24 18:41:31 -07001176 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1177 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1178 mScreenOffBroadcastDone, mHandler, 0, null, null);
1179 } else {
1180 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001182 mBroadcastWakeLock.mCount);
1183 mBroadcastWakeLock.release();
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
Joe Onorato128e7292009-03-24 18:41:31 -07001187 else {
1188 // If we're in this case, then this handler is running for a previous
1189 // paired transaction. mBroadcastWakeLock will already have been released.
1190 break;
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194 };
1195
1196 long mScreenOnStart;
1197 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1198 public void onReceive(Context context, Intent intent) {
1199 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1202 mBroadcastWakeLock.release();
1203 }
1204 }
1205 };
1206
1207 long mScreenOffStart;
1208 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1209 public void onReceive(Context context, Intent intent) {
1210 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1213 mBroadcastWakeLock.release();
1214 }
1215 }
1216 };
1217
1218 void logPointerUpEvent() {
1219 if (LOG_TOUCH_DOWNS) {
1220 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1221 mLastTouchDown = 0;
1222 }
1223 }
1224
1225 void logPointerDownEvent() {
1226 if (LOG_TOUCH_DOWNS) {
1227 // If we are not already timing a down/up sequence
1228 if (mLastTouchDown == 0) {
1229 mLastTouchDown = SystemClock.elapsedRealtime();
1230 mTouchCycles++;
1231 }
1232 }
1233 }
1234
1235 /**
1236 * Prevents the screen from turning on even if it *should* turn on due
1237 * to a subsequent full wake lock being acquired.
1238 * <p>
1239 * This is a temporary hack that allows an activity to "cover up" any
1240 * display glitches that happen during the activity's startup
1241 * sequence. (Specifically, this API was added to work around a
1242 * cosmetic bug in the "incoming call" sequence, where the lock screen
1243 * would flicker briefly before the incoming call UI became visible.)
1244 * TODO: There ought to be a more elegant way of doing this,
1245 * probably by having the PowerManager and ActivityManager
1246 * work together to let apps specify that the screen on/off
1247 * state should be synchronized with the Activity lifecycle.
1248 * <p>
1249 * Note that calling preventScreenOn(true) will NOT turn the screen
1250 * off if it's currently on. (This API only affects *future*
1251 * acquisitions of full wake locks.)
1252 * But calling preventScreenOn(false) WILL turn the screen on if
1253 * it's currently off because of a prior preventScreenOn(true) call.
1254 * <p>
1255 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1256 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1257 * call doesn't occur within 5 seconds, we'll turn the screen back on
1258 * ourselves (and log a warning about it); this prevents a buggy app
1259 * from disabling the screen forever.)
1260 * <p>
1261 * TODO: this feature should really be controlled by a new type of poke
1262 * lock (rather than an IPowerManager call).
1263 */
1264 public void preventScreenOn(boolean prevent) {
1265 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1266
1267 synchronized (mLocks) {
1268 if (prevent) {
1269 // First of all, grab a partial wake lock to
1270 // make sure the CPU stays on during the entire
1271 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1272 mPreventScreenOnPartialLock.acquire();
1273
1274 // Post a forceReenableScreen() call (for 5 seconds in the
1275 // future) to make sure the matching preventScreenOn(false) call
1276 // has happened by then.
1277 mHandler.removeCallbacks(mForceReenableScreenTask);
1278 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1279
1280 // Finally, set the flag that prevents the screen from turning on.
1281 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001282 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mPreventScreenOn = true;
1284 } else {
1285 // (Re)enable the screen.
1286 mPreventScreenOn = false;
1287
1288 // We're "undoing" a the prior preventScreenOn(true) call, so we
1289 // no longer need the 5-second safeguard.
1290 mHandler.removeCallbacks(mForceReenableScreenTask);
1291
1292 // Forcibly turn on the screen if it's supposed to be on. (This
1293 // handles the case where the screen is currently off because of
1294 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001295 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 if (mSpew) {
1297 Log.d(TAG,
1298 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1299 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001300 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 if (err != 0) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001302 Log.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304 }
1305
1306 // Release the partial wake lock that we held during the
1307 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1308 mPreventScreenOnPartialLock.release();
1309 }
1310 }
1311 }
1312
1313 public void setScreenBrightnessOverride(int brightness) {
1314 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1315
1316 synchronized (mLocks) {
1317 if (mScreenBrightnessOverride != brightness) {
1318 mScreenBrightnessOverride = brightness;
1319 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1320 }
1321 }
1322 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001323
1324 public void setButtonBrightnessOverride(int brightness) {
1325 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1326
1327 synchronized (mLocks) {
1328 if (mButtonBrightnessOverride != brightness) {
1329 mButtonBrightnessOverride = brightness;
1330 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1331 }
1332 }
1333 }
1334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * Sanity-check that gets called 5 seconds after any call to
1337 * preventScreenOn(true). This ensures that the original call
1338 * is followed promptly by a call to preventScreenOn(false).
1339 */
1340 private void forceReenableScreen() {
1341 // We shouldn't get here at all if mPreventScreenOn is false, since
1342 // we should have already removed any existing
1343 // mForceReenableScreenTask messages...
1344 if (!mPreventScreenOn) {
1345 Log.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
1346 return;
1347 }
1348
1349 // Uh oh. It's been 5 seconds since a call to
1350 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1351 // This means the app that called preventScreenOn(true) is either
1352 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1353 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1354 // crashed before doing so.)
1355
1356 // Log a warning, and forcibly turn the screen back on.
1357 Log.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
1358 + "Forcing the screen back on...");
1359 preventScreenOn(false);
1360 }
1361
1362 private Runnable mForceReenableScreenTask = new Runnable() {
1363 public void run() {
1364 forceReenableScreen();
1365 }
1366 };
1367
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001368 private int setScreenStateLocked(boolean on) {
1369 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001370 if (err == 0) {
1371 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1372 if (mUseSoftwareAutoBrightness) {
1373 enableLightSensor(on);
1374 if (!on) {
1375 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001376 mButtonLight.turnOff();
1377 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001378 // clear current value so we will update based on the new conditions
1379 // when the sensor is reenabled.
1380 mLightSensorValue = -1;
1381 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001382 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001383 }
1384 return err;
1385 }
1386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 private void setPowerState(int state)
1388 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001389 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391
Mike Lockwood435eb642009-12-03 08:40:18 -05001392 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 {
1394 synchronized (mLocks) {
1395 int err;
1396
1397 if (mSpew) {
1398 Log.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
1399 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001400 + " noChangeLights=" + noChangeLights
1401 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403
1404 if (noChangeLights) {
1405 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1406 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001407 if (mProximitySensorActive) {
1408 // don't turn on the screen when the proximity sensor lock is held
1409 newState = (newState & ~SCREEN_BRIGHT);
1410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411
1412 if (batteryIsLow()) {
1413 newState |= BATTERY_LOW_BIT;
1414 } else {
1415 newState &= ~BATTERY_LOW_BIT;
1416 }
1417 if (newState == mPowerState) {
1418 return;
1419 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001420
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001421 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 newState |= ALL_BRIGHT;
1423 }
1424
1425 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1426 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1427
Mike Lockwood51b84492009-11-16 21:51:18 -05001428 if (mSpew) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 Log.d(TAG, "setPowerState: mPowerState=" + mPowerState
1430 + " newState=" + newState + " noChangeLights=" + noChangeLights);
1431 Log.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
1432 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
1433 Log.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
1434 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
1435 Log.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
1436 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
1437 Log.d(TAG, " oldScreenOn=" + oldScreenOn
1438 + " newScreenOn=" + newScreenOn);
1439 Log.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
1440 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1441 }
1442
1443 if (mPowerState != newState) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001444 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1446 }
1447
1448 if (oldScreenOn != newScreenOn) {
1449 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001450 // When the user presses the power button, we need to always send out the
1451 // notification that it's going to sleep so the keyguard goes on. But
1452 // we can't do that until the screen fades out, so we don't show the keyguard
1453 // too early.
1454 if (mStillNeedSleepNotification) {
1455 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1456 }
1457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 // Turn on the screen UNLESS there was a prior
1459 // preventScreenOn(true) request. (Note that the lifetime
1460 // of a single preventScreenOn() request is limited to 5
1461 // seconds to prevent a buggy app from disabling the
1462 // screen forever; see forceReenableScreen().)
1463 boolean reallyTurnScreenOn = true;
1464 if (mSpew) {
1465 Log.d(TAG, "- turning screen on... mPreventScreenOn = "
1466 + mPreventScreenOn);
1467 }
1468
1469 if (mPreventScreenOn) {
1470 if (mSpew) {
1471 Log.d(TAG, "- PREVENTING screen from really turning on!");
1472 }
1473 reallyTurnScreenOn = false;
1474 }
1475 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001476 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 long identity = Binder.clearCallingIdentity();
1478 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001479 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 mBatteryStats.noteScreenOn();
1481 } catch (RemoteException e) {
1482 Log.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
1483 } finally {
1484 Binder.restoreCallingIdentity(identity);
1485 }
1486 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001487 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 // But continue as if we really did turn the screen on...
1489 err = 0;
1490 }
1491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 mLastTouchDown = 0;
1493 mTotalTouchDownTime = 0;
1494 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 mTotalTouchDownTime, mTouchCycles);
1497 if (err == 0) {
1498 mPowerState |= SCREEN_ON_BIT;
1499 sendNotificationLocked(true, -1);
1500 }
1501 } else {
Mike Lockwood497087e32009-11-08 18:33:03 -05001502 // cancel light sensor task
1503 mHandler.removeCallbacks(mAutoBrightnessTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 mScreenOffTime = SystemClock.elapsedRealtime();
1505 long identity = Binder.clearCallingIdentity();
1506 try {
1507 mBatteryStats.noteScreenOff();
1508 } catch (RemoteException e) {
1509 Log.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
1510 } finally {
1511 Binder.restoreCallingIdentity(identity);
1512 }
1513 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001514 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001516 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 err = 0;
1519 mLastTouchDown = 0;
1520 }
1521 }
1522 }
1523 }
1524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Mike Lockwood435eb642009-12-03 08:40:18 -05001526 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 // called multiple times in the same state. -joeo
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001532 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001534 mScreenOffReason = reason;
1535 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
1537 return err;
1538 }
1539
1540 private boolean batteryIsLow() {
1541 return (!mIsPowered &&
1542 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1543 }
1544
The Android Open Source Project10592532009-03-18 17:39:46 -07001545 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001546 final int oldState = mPowerState;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001547 newState = applyButtonState(newState);
1548 newState = applyKeyboardState(newState);
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001549 final int realDifference = (newState ^ oldState);
1550 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001552 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 int offMask = 0;
1556 int dimMask = 0;
1557 int onMask = 0;
1558
1559 int preferredBrightness = getPreferredBrightness();
1560 boolean startAnimation = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1563 if (ANIMATE_KEYBOARD_LIGHTS) {
1564 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1565 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001566 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001567 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001569 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001570 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
1571 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 startAnimation = true;
1574 } else {
1575 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001576 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001578 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
1580 }
1581 }
1582
1583 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1584 if (ANIMATE_BUTTON_LIGHTS) {
1585 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1586 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001587 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001588 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001590 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001591 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
1592 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594 startAnimation = true;
1595 } else {
1596 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001597 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001599 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 }
1601 }
1602 }
1603
1604 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1605 if (ANIMATE_SCREEN_LIGHTS) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001606 int nominalCurrentValue = -1;
1607 // If there was an actual difference in the light state, then
1608 // figure out the "ideal" current value based on the previous
1609 // state. Otherwise, this is a change due to the brightness
1610 // override, so we want to animate from whatever the current
1611 // value is.
1612 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1613 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
1614 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
1615 nominalCurrentValue = preferredBrightness;
1616 break;
1617 case SCREEN_ON_BIT:
1618 nominalCurrentValue = Power.BRIGHTNESS_DIM;
1619 break;
1620 case 0:
1621 nominalCurrentValue = Power.BRIGHTNESS_OFF;
1622 break;
1623 case SCREEN_BRIGHT_BIT:
1624 default:
1625 // not possible
1626 nominalCurrentValue = (int)mScreenBrightness.curValue;
1627 break;
1628 }
Joe Onorato128e7292009-03-24 18:41:31 -07001629 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001630 int brightness = preferredBrightness;
1631 int steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1633 // dim or turn off backlight, depending on if the screen is on
1634 // the scale is because the brightness ramp isn't linear and this biases
1635 // it so the later parts take longer.
1636 final float scale = 1.5f;
1637 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1638 if (ratio > 1.0f) ratio = 1.0f;
1639 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1641 // was bright
1642 steps = ANIM_STEPS;
1643 } else {
1644 // was dim
1645 steps = (int)(ANIM_STEPS*ratio*scale);
1646 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001647 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 if ((oldState & SCREEN_ON_BIT) != 0) {
1650 // was bright
1651 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1652 } else {
1653 // was dim
1654 steps = (int)(ANIM_STEPS*ratio);
1655 }
1656 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1657 // If the "stay on while plugged in" option is
1658 // turned on, then the screen will often not
1659 // automatically turn off while plugged in. To
1660 // still have a sense of when it is inactive, we
1661 // will then count going dim as turning off.
1662 mScreenOffTime = SystemClock.elapsedRealtime();
1663 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001664 brightness = Power.BRIGHTNESS_DIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001667 long identity = Binder.clearCallingIdentity();
1668 try {
1669 mBatteryStats.noteScreenBrightness(brightness);
1670 } catch (RemoteException e) {
1671 // Nothing interesting to do.
1672 } finally {
1673 Binder.restoreCallingIdentity(identity);
1674 }
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001675 if (mScreenBrightness.setTargetLocked(brightness,
1676 steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue)) {
1677 startAnimation = true;
1678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 } else {
1680 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1681 // dim or turn off backlight, depending on if the screen is on
1682 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001683 offMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001685 dimMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001688 onMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690 }
1691 }
1692
1693 if (startAnimation) {
1694 if (mSpew) {
1695 Log.i(TAG, "Scheduling light animator!");
1696 }
1697 mHandler.removeCallbacks(mLightAnimator);
1698 mHandler.post(mLightAnimator);
1699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (offMask != 0) {
1702 //Log.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001703 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705 if (dimMask != 0) {
1706 int brightness = Power.BRIGHTNESS_DIM;
1707 if ((newState & BATTERY_LOW_BIT) != 0 &&
1708 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1709 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1710 }
1711 //Log.i(TAG, "Setting brightess dim " + brightness + ": " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001712 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
1714 if (onMask != 0) {
1715 int brightness = getPreferredBrightness();
1716 if ((newState & BATTERY_LOW_BIT) != 0 &&
1717 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1718 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1719 }
1720 //Log.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001721 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724
The Android Open Source Project10592532009-03-18 17:39:46 -07001725 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05001726 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05001727 ? LightsService.BRIGHTNESS_MODE_SENSOR
1728 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07001729 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001730 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07001731 }
1732 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001733 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 }
1735 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001736 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739
1740 class BrightnessState {
1741 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 boolean initialized;
1744 int targetValue;
1745 float curValue;
1746 float delta;
1747 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 BrightnessState(int m) {
1750 mask = m;
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 public void dump(PrintWriter pw, String prefix) {
1754 pw.println(prefix + "animating=" + animating
1755 + " targetValue=" + targetValue
1756 + " curValue=" + curValue
1757 + " delta=" + delta);
1758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001759
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001760 boolean setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07001761 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 if (!initialized) {
1763 initialized = true;
1764 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001765 } else if (targetValue == target) {
1766 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001769 delta = (targetValue -
1770 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
1771 / stepsToTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 if (mSpew) {
Joe Onorato128e7292009-03-24 18:41:31 -07001773 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 Log.i(TAG, "Setting target " + mask + ": cur=" + curValue
Joe Onorato128e7292009-03-24 18:41:31 -07001775 + " target=" + targetValue + " delta=" + delta
1776 + " nominalCurrentValue=" + nominalCurrentValue
1777 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779 animating = true;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001780 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 boolean stepLocked() {
1784 if (!animating) return false;
1785 if (false && mSpew) {
1786 Log.i(TAG, "Step target " + mask + ": cur=" + curValue
1787 + " target=" + targetValue + " delta=" + delta);
1788 }
1789 curValue += delta;
1790 int curIntValue = (int)curValue;
1791 boolean more = true;
1792 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001793 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 more = false;
1795 } else if (delta > 0) {
1796 if (curIntValue >= targetValue) {
1797 curValue = curIntValue = targetValue;
1798 more = false;
1799 }
1800 } else {
1801 if (curIntValue <= targetValue) {
1802 curValue = curIntValue = targetValue;
1803 more = false;
1804 }
1805 }
1806 //Log.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001807 setLightBrightness(mask, curIntValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 animating = more;
1809 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001810 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001811 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 }
1814 return more;
1815 }
1816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 private class LightAnimator implements Runnable {
1819 public void run() {
1820 synchronized (mLocks) {
1821 long now = SystemClock.uptimeMillis();
1822 boolean more = mScreenBrightness.stepLocked();
1823 if (mKeyboardBrightness.stepLocked()) {
1824 more = true;
1825 }
1826 if (mButtonBrightness.stepLocked()) {
1827 more = true;
1828 }
1829 if (more) {
1830 mHandler.postAtTime(mLightAnimator, now+(1000/60));
1831 }
1832 }
1833 }
1834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 private int getPreferredBrightness() {
1837 try {
1838 if (mScreenBrightnessOverride >= 0) {
1839 return mScreenBrightnessOverride;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001840 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
Mike Lockwood27c6dd72009-11-04 08:57:07 -05001841 && mAutoBrightessEnabled) {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001842 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
1844 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
1845 SCREEN_BRIGHTNESS);
1846 // Don't let applications turn the screen all the way off
1847 return Math.max(brightness, Power.BRIGHTNESS_DIM);
1848 } catch (SettingNotFoundException snfe) {
1849 return Power.BRIGHTNESS_ON;
1850 }
1851 }
1852
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001853 private int applyButtonState(int state) {
1854 int brightness = -1;
1855 if (mButtonBrightnessOverride >= 0) {
1856 brightness = mButtonBrightnessOverride;
1857 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
1858 brightness = mLightSensorButtonBrightness;
1859 }
1860 if (brightness > 0) {
1861 return state | BUTTON_BRIGHT_BIT;
1862 } else if (brightness == 0) {
1863 return state & ~BUTTON_BRIGHT_BIT;
1864 } else {
1865 return state;
1866 }
1867 }
1868
1869 private int applyKeyboardState(int state) {
1870 int brightness = -1;
1871 if (!mKeyboardVisible) {
1872 brightness = 0;
1873 } else if (mButtonBrightnessOverride >= 0) {
1874 brightness = mButtonBrightnessOverride;
1875 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
1876 brightness = mLightSensorKeyboardBrightness;
1877 }
1878 if (brightness > 0) {
1879 return state | KEYBOARD_BRIGHT_BIT;
1880 } else if (brightness == 0) {
1881 return state & ~KEYBOARD_BRIGHT_BIT;
1882 } else {
1883 return state;
1884 }
1885 }
1886
Charles Mendis322591c2009-10-29 11:06:59 -07001887 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 synchronized (mLocks) {
1889 return (mPowerState & SCREEN_ON_BIT) != 0;
1890 }
1891 }
1892
Charles Mendis322591c2009-10-29 11:06:59 -07001893 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 synchronized (mLocks) {
1895 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
1896 }
1897 }
1898
Mike Lockwood497087e32009-11-08 18:33:03 -05001899 private boolean isScreenTurningOffLocked() {
1900 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
1901 }
1902
Mike Lockwood200b30b2009-09-20 00:23:59 -04001903 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05001904 if (isScreenTurningOffLocked()) {
1905 // cancel animation so userActivity will succeed
1906 mScreenBrightness.animating = false;
1907 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04001908 boolean savedActivityAllowed = mUserActivityAllowed;
1909 mUserActivityAllowed = true;
1910 userActivity(SystemClock.uptimeMillis(), false);
1911 mUserActivityAllowed = savedActivityAllowed;
1912 }
1913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
1915 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1916 userActivity(time, noChangeLights, OTHER_EVENT, force);
1917 }
1918
1919 public void userActivity(long time, boolean noChangeLights) {
1920 userActivity(time, noChangeLights, OTHER_EVENT, false);
1921 }
1922
1923 public void userActivity(long time, boolean noChangeLights, int eventType) {
1924 userActivity(time, noChangeLights, eventType, false);
1925 }
1926
1927 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
1928 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1929
1930 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001931 && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 if (false) {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001933 Log.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
1935 return;
1936 }
1937
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001938 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
1939 && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
1940 || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
1941 if (false) {
1942 Log.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
1943 }
1944 return;
1945 }
1946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (false) {
1948 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
1949 Log.d(TAG, "userActivity !!!");//, new RuntimeException());
1950 } else {
1951 Log.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
1952 }
1953 }
1954
1955 synchronized (mLocks) {
1956 if (mSpew) {
1957 Log.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
1958 + " mUserActivityAllowed=" + mUserActivityAllowed
1959 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07001960 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
1961 + " mProximitySensorActive=" + mProximitySensorActive
1962 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
Mike Lockwood05067122009-10-27 23:07:25 -04001964 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05001965 if (isScreenTurningOffLocked()) {
Mike Lockwood05067122009-10-27 23:07:25 -04001966 Log.d(TAG, "ignoring user activity while turning off screen");
1967 return;
1968 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05001969 // Disable proximity sensor if if user presses power key while we are in the
1970 // "waiting for proximity sensor to go negative" state.
1971 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
1972 mProximitySensorActive = false;
1973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 if (mLastEventTime <= time || force) {
1975 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07001976 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001977 // Only turn on button backlights if a button was pressed
1978 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05001979 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
1981 } else {
1982 // don't clear button/keyboard backlights when the screen is touched.
1983 mUserState |= SCREEN_BRIGHT;
1984 }
1985
Dianne Hackborn617f8772009-03-31 15:04:46 -07001986 int uid = Binder.getCallingUid();
1987 long ident = Binder.clearCallingIdentity();
1988 try {
1989 mBatteryStats.noteUserActivity(uid, eventType);
1990 } catch (RemoteException e) {
1991 // Ignore
1992 } finally {
1993 Binder.restoreCallingIdentity(ident);
1994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001995
Michael Chane96440f2009-05-06 10:27:36 -07001996 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05001997 setPowerState(mUserState | mWakeLockState, noChangeLights,
1998 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 setTimeoutLocked(time, SCREEN_BRIGHT);
2000 }
2001 }
2002 }
2003 }
2004
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002005 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2006 try {
2007 int i;
2008 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2009 if (sensorValue < mAutoBrightnessLevels[i]) {
2010 break;
2011 }
2012 }
2013 return values[i];
2014 } catch (Exception e) {
2015 // guard against null pointer or index out of bounds errors
2016 Log.e(TAG, "getAutoBrightnessValue", e);
2017 return 255;
2018 }
2019 }
2020
Mike Lockwood20f87d72009-11-05 16:08:51 -05002021 private Runnable mProximityTask = new Runnable() {
2022 public void run() {
2023 synchronized (mLocks) {
2024 if (mProximityPendingValue != -1) {
2025 proximityChangedLocked(mProximityPendingValue == 1);
2026 mProximityPendingValue = -1;
2027 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002028 if (mProximityPartialLock.isHeld()) {
2029 mProximityPartialLock.release();
2030 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002031 }
2032 }
2033 };
2034
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002035 private Runnable mAutoBrightnessTask = new Runnable() {
2036 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002037 synchronized (mLocks) {
2038 int value = (int)mLightSensorPendingValue;
2039 if (value >= 0) {
2040 mLightSensorPendingValue = -1;
2041 lightSensorChangedLocked(value);
2042 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002043 }
2044 }
2045 };
2046
2047 private void lightSensorChangedLocked(int value) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002048 if (mDebugLightSensor) {
2049 Log.d(TAG, "lightSensorChangedLocked " + value);
2050 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002051
2052 if (mLightSensorValue != value) {
2053 mLightSensorValue = value;
2054 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
2055 int lcdValue = getAutoBrightnessValue(value, mLcdBacklightValues);
2056 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002057 int keyboardValue;
2058 if (mKeyboardVisible) {
2059 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2060 } else {
2061 keyboardValue = 0;
2062 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002063 mLightSensorScreenBrightness = lcdValue;
2064 mLightSensorButtonBrightness = buttonValue;
2065 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002066
2067 if (mDebugLightSensor) {
2068 Log.d(TAG, "lcdValue " + lcdValue);
2069 Log.d(TAG, "buttonValue " + buttonValue);
2070 Log.d(TAG, "keyboardValue " + keyboardValue);
2071 }
2072
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002073 boolean startAnimation = false;
Mike Lockwood4984e732009-11-01 08:16:33 -05002074 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002075 if (ANIMATE_SCREEN_LIGHTS) {
2076 if (mScreenBrightness.setTargetLocked(lcdValue,
2077 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
2078 (int)mScreenBrightness.curValue)) {
2079 startAnimation = true;
2080 }
2081 } else {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002082 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002083 ? LightsService.BRIGHTNESS_MODE_SENSOR
2084 : LightsService.BRIGHTNESS_MODE_USER);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002085 mLcdLight.setBrightness(lcdValue, brightnessMode);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002086 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002087 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002088 if (mButtonBrightnessOverride < 0) {
2089 if (ANIMATE_BUTTON_LIGHTS) {
2090 if (mButtonBrightness.setTargetLocked(buttonValue,
2091 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2092 (int)mButtonBrightness.curValue)) {
2093 startAnimation = true;
2094 }
2095 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002096 mButtonLight.setBrightness(buttonValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002097 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002098 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002099 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
2100 if (ANIMATE_KEYBOARD_LIGHTS) {
2101 if (mKeyboardBrightness.setTargetLocked(keyboardValue,
2102 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2103 (int)mKeyboardBrightness.curValue)) {
2104 startAnimation = true;
2105 }
2106 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002107 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002108 }
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002109 }
2110 if (startAnimation) {
2111 if (mDebugLightSensor) {
2112 Log.i(TAG, "lightSensorChangedLocked scheduling light animator");
2113 }
2114 mHandler.removeCallbacks(mLightAnimator);
2115 mHandler.post(mLightAnimator);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002116 }
2117 }
2118 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002119 }
2120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 /**
2122 * The user requested that we go to sleep (probably with the power button).
2123 * This overrides all wake locks that are held.
2124 */
2125 public void goToSleep(long time)
2126 {
2127 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2128 synchronized (mLocks) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002129 goToSleepLocked(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002134 * Reboot the device immediately, passing 'reason' (may be null)
2135 * to the underlying __reboot system call. Should not return.
2136 */
2137 public void reboot(String reason)
2138 {
2139 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002140
San Mehat1e512792010-01-07 10:40:29 -08002141 /*
2142 * Manually shutdown the MountService to ensure media is
2143 * put into a safe state.
2144 */
2145 IMountService mSvc = IMountService.Stub.asInterface(
2146 ServiceManager.getService("mount"));
2147
2148 if (mSvc != null) {
2149 try {
2150 mSvc.shutdown();
2151 } catch (Exception e) {
2152 Log.e(TAG, "MountService shutdown failed", e);
2153 }
2154 } else {
2155 Log.w(TAG, "MountService unavailable for shutdown");
2156 }
San Mehat14e69af2010-01-06 14:58:18 -08002157
Doug Zongker50a21f42009-11-19 12:49:53 -08002158 try {
2159 Power.reboot(reason);
2160 } catch (IOException e) {
2161 Log.e(TAG, "reboot failed", e);
2162 }
2163 }
2164
Dan Egnor60d87622009-12-16 16:32:58 -08002165 /**
2166 * Crash the runtime (causing a complete restart of the Android framework).
2167 * Requires REBOOT permission. Mostly for testing. Should not return.
2168 */
2169 public void crash(final String message)
2170 {
2171 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2172 Thread t = new Thread("PowerManagerService.crash()") {
2173 public void run() { throw new RuntimeException(message); }
2174 };
2175 try {
2176 t.start();
2177 t.join();
2178 } catch (InterruptedException e) {
2179 Log.wtf(TAG, e);
2180 }
2181 }
2182
Mike Lockwood435eb642009-12-03 08:40:18 -05002183 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184
2185 if (mLastEventTime <= time) {
2186 mLastEventTime = time;
2187 // cancel all of the wake locks
2188 mWakeLockState = SCREEN_OFF;
2189 int N = mLocks.size();
2190 int numCleared = 0;
2191 for (int i=0; i<N; i++) {
2192 WakeLock wl = mLocks.get(i);
2193 if (isScreenLock(wl.flags)) {
2194 mLocks.get(i).activated = false;
2195 numCleared++;
2196 }
2197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002198 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002199 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002201 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 cancelTimerLocked();
2203 }
2204 }
2205
2206 public long timeSinceScreenOn() {
2207 synchronized (mLocks) {
2208 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2209 return 0;
2210 }
2211 return SystemClock.elapsedRealtime() - mScreenOffTime;
2212 }
2213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002216 synchronized (mLocks) {
2217 if (mSpew) {
2218 Log.d(TAG, "setKeyboardVisibility: " + visible);
2219 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002220 if (mKeyboardVisible != visible) {
2221 mKeyboardVisible = visible;
2222 // don't signal user activity if the screen is off; other code
2223 // will take care of turning on due to a true change to the lid
2224 // switch and synchronized with the lock screen.
2225 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002226 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002227 // force recompute of backlight values
2228 if (mLightSensorValue >= 0) {
2229 int value = (int)mLightSensorValue;
2230 mLightSensorValue = -1;
2231 lightSensorChangedLocked(value);
2232 }
2233 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002234 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2235 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002236 }
2237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239
2240 /**
2241 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002242 * When disabling user activity we also reset user power state so the keyguard can reset its
2243 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 */
2245 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002246 if (mSpew) {
2247 Log.d(TAG, "enableUserActivity " + enabled);
2248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 synchronized (mLocks) {
2250 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002251 if (!enabled) {
2252 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2253 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
2256 }
2257
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002258 private void setScreenBrightnessMode(int mode) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002259 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Mike Lockwoodf90ffcc2009-11-03 11:41:27 -05002260 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002261 mAutoBrightessEnabled = enabled;
Charles Mendis322591c2009-10-29 11:06:59 -07002262 if (isScreenOn()) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002263 // force recompute of backlight values
2264 if (mLightSensorValue >= 0) {
2265 int value = (int)mLightSensorValue;
2266 mLightSensorValue = -1;
2267 lightSensorChangedLocked(value);
2268 }
Mike Lockwood2d155d22009-10-27 09:32:30 -04002269 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002270 }
2271 }
2272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 /** Sets the screen off timeouts:
2274 * mKeylightDelay
2275 * mDimDelay
2276 * mScreenOffDelay
2277 * */
2278 private void setScreenOffTimeoutsLocked() {
2279 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002280 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 mDimDelay = -1;
2282 mScreenOffDelay = 0;
2283 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2284 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2285 mDimDelay = -1;
2286 mScreenOffDelay = 0;
2287 } else {
2288 int totalDelay = mTotalDelaySetting;
2289 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2290 if (totalDelay < 0) {
2291 mScreenOffDelay = Integer.MAX_VALUE;
2292 } else if (mKeylightDelay < totalDelay) {
2293 // subtract the time that the keylight delay. This will give us the
2294 // remainder of the time that we need to sleep to get the accurate
2295 // screen off timeout.
2296 mScreenOffDelay = totalDelay - mKeylightDelay;
2297 } else {
2298 mScreenOffDelay = 0;
2299 }
2300 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2301 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2302 mScreenOffDelay = LONG_DIM_TIME;
2303 } else {
2304 mDimDelay = -1;
2305 }
2306 }
2307 if (mSpew) {
2308 Log.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
2309 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2310 + " mDimScreen=" + mDimScreen);
2311 }
2312 }
2313
2314 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002315 * Refreshes cached secure settings. Called once on startup, and
2316 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 */
Doug Zongker43866e02010-01-07 12:09:54 -08002318 private void updateSettingsValues() {
2319 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002321 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 SHORT_KEYLIGHT_DELAY_DEFAULT);
Doug Zongker43866e02010-01-07 12:09:54 -08002323 // Log.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
2325
2326 private class LockList extends ArrayList<WakeLock>
2327 {
2328 void addLock(WakeLock wl)
2329 {
2330 int index = getIndex(wl.binder);
2331 if (index < 0) {
2332 this.add(wl);
2333 }
2334 }
2335
2336 WakeLock removeLock(IBinder binder)
2337 {
2338 int index = getIndex(binder);
2339 if (index >= 0) {
2340 return this.remove(index);
2341 } else {
2342 return null;
2343 }
2344 }
2345
2346 int getIndex(IBinder binder)
2347 {
2348 int N = this.size();
2349 for (int i=0; i<N; i++) {
2350 if (this.get(i).binder == binder) {
2351 return i;
2352 }
2353 }
2354 return -1;
2355 }
2356
2357 int gatherState()
2358 {
2359 int result = 0;
2360 int N = this.size();
2361 for (int i=0; i<N; i++) {
2362 WakeLock wl = this.get(i);
2363 if (wl.activated) {
2364 if (isScreenLock(wl.flags)) {
2365 result |= wl.minState;
2366 }
2367 }
2368 }
2369 return result;
2370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002371
Michael Chane96440f2009-05-06 10:27:36 -07002372 int reactivateScreenLocksLocked()
2373 {
2374 int result = 0;
2375 int N = this.size();
2376 for (int i=0; i<N; i++) {
2377 WakeLock wl = this.get(i);
2378 if (isScreenLock(wl.flags)) {
2379 wl.activated = true;
2380 result |= wl.minState;
2381 }
2382 }
2383 return result;
2384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386
2387 void setPolicy(WindowManagerPolicy p) {
2388 synchronized (mLocks) {
2389 mPolicy = p;
2390 mLocks.notifyAll();
2391 }
2392 }
2393
2394 WindowManagerPolicy getPolicyLocked() {
2395 while (mPolicy == null || !mDoneBooting) {
2396 try {
2397 mLocks.wait();
2398 } catch (InterruptedException e) {
2399 // Ignore
2400 }
2401 }
2402 return mPolicy;
2403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002406 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2407 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2408 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002409 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002410 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood4984e732009-11-01 08:16:33 -05002411 enableLightSensor(true);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002412 }
2413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 synchronized (mLocks) {
2415 Log.d(TAG, "system ready!");
2416 mDoneBooting = true;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002417 long identity = Binder.clearCallingIdentity();
2418 try {
2419 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2420 mBatteryStats.noteScreenOn();
2421 } catch (RemoteException e) {
2422 // Nothing interesting to do.
2423 } finally {
2424 Binder.restoreCallingIdentity(identity);
2425 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002426 }
2427 }
2428
2429 void bootCompleted() {
2430 Log.d(TAG, "bootCompleted");
2431 synchronized (mLocks) {
2432 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2434 updateWakeLockLocked();
2435 mLocks.notifyAll();
2436 }
2437 }
2438
2439 public void monitor() {
2440 synchronized (mLocks) { }
2441 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002442
2443 public int getSupportedWakeLockFlags() {
2444 int result = PowerManager.PARTIAL_WAKE_LOCK
2445 | PowerManager.FULL_WAKE_LOCK
2446 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2447
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002448 if (mProximitySensor != null) {
2449 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2450 }
2451
2452 return result;
2453 }
2454
Mike Lockwood237a2992009-09-15 14:42:16 -04002455 public void setBacklightBrightness(int brightness) {
2456 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2457 // Don't let applications turn the screen all the way off
2458 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002459 mLcdLight.setBrightness(brightness);
2460 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2461 mButtonLight.setBrightness(brightness);
Mike Lockwood237a2992009-09-15 14:42:16 -04002462 long identity = Binder.clearCallingIdentity();
2463 try {
2464 mBatteryStats.noteScreenBrightness(brightness);
2465 } catch (RemoteException e) {
2466 Log.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
2467 } finally {
2468 Binder.restoreCallingIdentity(identity);
2469 }
2470
2471 // update our animation state
2472 if (ANIMATE_SCREEN_LIGHTS) {
2473 mScreenBrightness.curValue = brightness;
2474 mScreenBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002475 mScreenBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002476 }
2477 if (ANIMATE_KEYBOARD_LIGHTS) {
2478 mKeyboardBrightness.curValue = brightness;
2479 mKeyboardBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002480 mKeyboardBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002481 }
2482 if (ANIMATE_BUTTON_LIGHTS) {
2483 mButtonBrightness.curValue = brightness;
2484 mButtonBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002485 mButtonBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002486 }
2487 }
2488
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002489 public void setAttentionLight(boolean on, int color) {
2490 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002491 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002492 }
2493
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002494 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002495 if (mDebugProximitySensor) {
Mike Lockwood36fc3022009-08-25 16:49:06 -07002496 Log.d(TAG, "enableProximityLockLocked");
2497 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002498 if (!mProximitySensorEnabled) {
2499 // clear calling identity so sensor manager battery stats are accurate
2500 long identity = Binder.clearCallingIdentity();
2501 try {
2502 mSensorManager.registerListener(mProximityListener, mProximitySensor,
2503 SensorManager.SENSOR_DELAY_NORMAL);
2504 mProximitySensorEnabled = true;
2505 } finally {
2506 Binder.restoreCallingIdentity(identity);
2507 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002508 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002509 }
2510
2511 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002512 if (mDebugProximitySensor) {
Mike Lockwood36fc3022009-08-25 16:49:06 -07002513 Log.d(TAG, "disableProximityLockLocked");
2514 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002515 if (mProximitySensorEnabled) {
2516 // clear calling identity so sensor manager battery stats are accurate
2517 long identity = Binder.clearCallingIdentity();
2518 try {
2519 mSensorManager.unregisterListener(mProximityListener);
2520 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002521 if (mProximityPartialLock.isHeld()) {
2522 mProximityPartialLock.release();
2523 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002524 mProximitySensorEnabled = false;
2525 } finally {
2526 Binder.restoreCallingIdentity(identity);
2527 }
2528 if (mProximitySensorActive) {
2529 mProximitySensorActive = false;
2530 forceUserActivityLocked();
2531 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002532 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002533 }
2534
Mike Lockwood20f87d72009-11-05 16:08:51 -05002535 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002536 if (mDebugProximitySensor) {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002537 Log.d(TAG, "proximityChangedLocked, active: " + active);
2538 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002539 if (!mProximitySensorEnabled) {
2540 Log.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05002541 return;
2542 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002543 if (active) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002544 goToSleepLocked(SystemClock.uptimeMillis(),
2545 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002546 mProximitySensorActive = true;
2547 } else {
2548 // proximity sensor negative events trigger as user activity.
2549 // temporarily set mUserActivityAllowed to true so this will work
2550 // even when the keyguard is on.
2551 mProximitySensorActive = false;
2552 forceUserActivityLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002553
2554 if (mProximityWakeLockCount == 0) {
2555 // disable sensor if we have no listeners left after proximity negative
2556 disableProximityLockLocked();
2557 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002558 }
2559 }
2560
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002561 private void enableLightSensor(boolean enable) {
2562 if (mDebugLightSensor) {
2563 Log.d(TAG, "enableLightSensor " + enable);
2564 }
2565 if (mSensorManager != null && mLightSensorEnabled != enable) {
2566 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002567 // clear calling identity so sensor manager battery stats are accurate
2568 long identity = Binder.clearCallingIdentity();
2569 try {
2570 if (enable) {
2571 mSensorManager.registerListener(mLightListener, mLightSensor,
2572 SensorManager.SENSOR_DELAY_NORMAL);
2573 } else {
2574 mSensorManager.unregisterListener(mLightListener);
2575 mHandler.removeCallbacks(mAutoBrightnessTask);
2576 }
2577 } finally {
2578 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04002579 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002580 }
2581 }
2582
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002583 SensorEventListener mProximityListener = new SensorEventListener() {
2584 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002585 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002586 synchronized (mLocks) {
2587 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05002588 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
2589 mLastProximityEventTime = milliseconds;
2590 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002591 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05002592
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002593 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05002594 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
2595 distance < mProximitySensor.getMaximumRange());
2596
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002597 if (mDebugProximitySensor) {
2598 Log.d(TAG, "mProximityListener.onSensorChanged active: " + active);
2599 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002600 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
2601 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
2602 mProximityPendingValue = (active ? 1 : 0);
2603 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002604 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002605 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002606 // process the value immediately
2607 mProximityPendingValue = -1;
2608 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002609 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002610
2611 // update mProximityPartialLock state
2612 boolean held = mProximityPartialLock.isHeld();
2613 if (!held && proximityTaskQueued) {
2614 // hold wakelock until mProximityTask runs
2615 mProximityPartialLock.acquire();
2616 } else if (held && !proximityTaskQueued) {
2617 mProximityPartialLock.release();
2618 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002619 }
2620 }
2621
2622 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2623 // ignore
2624 }
2625 };
2626
2627 SensorEventListener mLightListener = new SensorEventListener() {
2628 public void onSensorChanged(SensorEvent event) {
2629 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05002630 // ignore light sensor while screen is turning off
2631 if (isScreenTurningOffLocked()) {
2632 return;
2633 }
2634
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002635 int value = (int)event.values[0];
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002636 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002637 if (mDebugLightSensor) {
2638 Log.d(TAG, "onSensorChanged: light value: " + value);
2639 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002640 mHandler.removeCallbacks(mAutoBrightnessTask);
2641 if (mLightSensorValue != value) {
Mike Lockwood20ee6f22009-11-07 20:33:47 -05002642 if (mLightSensorValue == -1 ||
2643 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
2644 // process the value immediately if screen has just turned on
Mike Lockwood6c97fca2009-10-20 08:10:00 -04002645 lightSensorChangedLocked(value);
2646 } else {
2647 // delay processing to debounce the sensor
2648 mLightSensorPendingValue = value;
2649 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
2650 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002651 } else {
2652 mLightSensorPendingValue = -1;
2653 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002654 }
2655 }
2656
2657 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2658 // ignore
2659 }
2660 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661}