blob: cefd3124f80d21203ce47bcdc4349f739c8208f7 [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;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800172 private int mScreenOffTimeoutSetting;
173 private int mMaximumScreenOffTimeout = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private int mKeylightDelay;
175 private int mDimDelay;
176 private int mScreenOffDelay;
177 private int mWakeLockState;
178 private long mLastEventTime = 0;
179 private long mScreenOffTime;
180 private volatile WindowManagerPolicy mPolicy;
181 private final LockList mLocks = new LockList();
182 private Intent mScreenOffIntent;
183 private Intent mScreenOnIntent;
Mike Lockwood3a322132009-11-24 00:30:52 -0500184 private LightsService mLightsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private Context mContext;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500186 private LightsService.Light mLcdLight;
187 private LightsService.Light mButtonLight;
188 private LightsService.Light mKeyboardLight;
189 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 private UnsynchronizedWakeLock mBroadcastWakeLock;
191 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
192 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
193 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500194 private UnsynchronizedWakeLock mProximityPartialLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 private HandlerThread mHandlerThread;
196 private Handler mHandler;
197 private TimeoutTask mTimeoutTask = new TimeoutTask();
198 private LightAnimator mLightAnimator = new LightAnimator();
199 private final BrightnessState mScreenBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700200 = new BrightnessState(SCREEN_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private final BrightnessState mKeyboardBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700202 = new BrightnessState(KEYBOARD_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private final BrightnessState mButtonBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700204 = new BrightnessState(BUTTON_BRIGHT_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -0700205 private boolean mStillNeedSleepNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 private boolean mIsPowered = false;
207 private IActivityManager mActivityService;
208 private IBatteryStats mBatteryStats;
209 private BatteryService mBatteryService;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700210 private SensorManager mSensorManager;
211 private Sensor mProximitySensor;
Mike Lockwood8738e0c2009-10-04 08:44:47 -0400212 private Sensor mLightSensor;
213 private boolean mLightSensorEnabled;
214 private float mLightSensorValue = -1;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700215 private float mLightSensorPendingValue = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500216 private int mLightSensorScreenBrightness = -1;
217 private int mLightSensorButtonBrightness = -1;
218 private int mLightSensorKeyboardBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 private boolean mDimScreen = true;
220 private long mNextTimeout;
221 private volatile int mPokey = 0;
222 private volatile boolean mPokeAwakeOnSet = false;
223 private volatile boolean mInitComplete = false;
224 private HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500225 // mLastScreenOnTime is the time the screen was last turned on
226 private long mLastScreenOnTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 private boolean mPreventScreenOn;
228 private int mScreenBrightnessOverride = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500229 private int mButtonBrightnessOverride = -1;
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400230 private boolean mUseSoftwareAutoBrightness;
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700231 private boolean mAutoBrightessEnabled;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700232 private int[] mAutoBrightnessLevels;
233 private int[] mLcdBacklightValues;
234 private int[] mButtonBacklightValues;
235 private int[] mKeyboardBacklightValues;
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500236 private int mLightSensorWarmupTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
238 // Used when logging number and duration of touch-down cycles
239 private long mTotalTouchDownTime;
240 private long mLastTouchDown;
241 private int mTouchCycles;
242
243 // could be either static or controllable at runtime
244 private static final boolean mSpew = false;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500245 private static final boolean mDebugProximitySensor = (true || mSpew);
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400246 private static final boolean mDebugLightSensor = (false || mSpew);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 /*
249 static PrintStream mLog;
250 static {
251 try {
252 mLog = new PrintStream("/data/power.log");
253 }
254 catch (FileNotFoundException e) {
255 android.util.Log.e(TAG, "Life is hard", e);
256 }
257 }
258 static class Log {
259 static void d(String tag, String s) {
260 mLog.println(s);
261 android.util.Log.d(tag, s);
262 }
263 static void i(String tag, String s) {
264 mLog.println(s);
265 android.util.Log.i(tag, s);
266 }
267 static void w(String tag, String s) {
268 mLog.println(s);
269 android.util.Log.w(tag, s);
270 }
271 static void e(String tag, String s) {
272 mLog.println(s);
273 android.util.Log.e(tag, s);
274 }
275 }
276 */
277
278 /**
279 * This class works around a deadlock between the lock in PowerManager.WakeLock
280 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
281 * mToken object so it can be accessed from any thread, but it calls into here
282 * with its lock held. This class is essentially a reimplementation of
283 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
284 * only call it with our own locks held.
285 */
286 private class UnsynchronizedWakeLock {
287 int mFlags;
288 String mTag;
289 IBinder mToken;
290 int mCount = 0;
291 boolean mRefCounted;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500292 boolean mHeld;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
294 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
295 mFlags = flags;
296 mTag = tag;
297 mToken = new Binder();
298 mRefCounted = refCounted;
299 }
300
301 public void acquire() {
302 if (!mRefCounted || mCount++ == 0) {
303 long ident = Binder.clearCallingIdentity();
304 try {
305 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
306 MY_UID, mTag);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500307 mHeld = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 } finally {
309 Binder.restoreCallingIdentity(ident);
310 }
311 }
312 }
313
314 public void release() {
315 if (!mRefCounted || --mCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500316 PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500317 mHeld = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319 if (mCount < 0) {
320 throw new RuntimeException("WakeLock under-locked " + mTag);
321 }
322 }
323
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500324 public boolean isHeld()
325 {
326 return mHeld;
327 }
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 public String toString() {
330 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500331 + " mCount=" + mCount + " mHeld=" + mHeld + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
333 }
334
335 private final class BatteryReceiver extends BroadcastReceiver {
336 @Override
337 public void onReceive(Context context, Intent intent) {
338 synchronized (mLocks) {
339 boolean wasPowered = mIsPowered;
340 mIsPowered = mBatteryService.isPowered();
341
342 if (mIsPowered != wasPowered) {
343 // update mStayOnWhilePluggedIn wake lock
344 updateWakeLockLocked();
345
346 // treat plugging and unplugging the devices as a user activity.
347 // users find it disconcerting when they unplug the device
348 // and it shuts off right away.
349 // temporarily set mUserActivityAllowed to true so this will work
350 // even when the keyguard is on.
351 synchronized (mLocks) {
Mike Lockwood200b30b2009-09-20 00:23:59 -0400352 forceUserActivityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354 }
355 }
356 }
357 }
358
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500359 private final class BootCompletedReceiver extends BroadcastReceiver {
360 @Override
361 public void onReceive(Context context, Intent intent) {
362 bootCompleted();
363 }
364 }
365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * Set the setting that determines whether the device stays on when plugged in.
368 * The argument is a bit string, with each bit specifying a power source that,
369 * when the device is connected to that source, causes the device to stay on.
370 * See {@link android.os.BatteryManager} for the list of power sources that
371 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
372 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
373 * @param val an {@code int} containing the bits that specify which power sources
374 * should cause the device to stay on.
375 */
376 public void setStayOnSetting(int val) {
377 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
378 Settings.System.putInt(mContext.getContentResolver(),
379 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
380 }
381
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800382 public void setMaximumScreenOffTimeount(int timeMs) {
383 mContext.enforceCallingOrSelfPermission(
384 android.Manifest.permission.WRITE_SECURE_SETTINGS, null);
385 synchronized (mLocks) {
386 mMaximumScreenOffTimeout = timeMs;
387 // recalculate everything
388 setScreenOffTimeoutsLocked();
389 }
390 }
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 private class SettingsObserver implements Observer {
393 private int getInt(String name) {
394 return mSettings.getValues(name).getAsInteger(Settings.System.VALUE);
395 }
396
397 public void update(Observable o, Object arg) {
398 synchronized (mLocks) {
399 // STAY_ON_WHILE_PLUGGED_IN
400 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN);
401 updateWakeLockLocked();
402
403 // SCREEN_OFF_TIMEOUT
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800404 mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
406 // DIM_SCREEN
407 //mDimScreen = getInt(DIM_SCREEN) != 0;
408
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700409 // SCREEN_BRIGHTNESS_MODE
410 setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE));
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 // recalculate everything
413 setScreenOffTimeoutsLocked();
414 }
415 }
416 }
417
418 PowerManagerService()
419 {
420 // Hack to get our uid... should have a func for this.
421 long token = Binder.clearCallingIdentity();
422 MY_UID = Binder.getCallingUid();
423 Binder.restoreCallingIdentity(token);
424
425 // XXX remove this when the kernel doesn't timeout wake locks
426 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
427
428 // assume nothing is on yet
429 mUserState = mPowerState = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 // Add ourself to the Watchdog monitors.
432 Watchdog.getInstance().addMonitor(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
435 private ContentQueryMap mSettings;
436
Mike Lockwood3a322132009-11-24 00:30:52 -0500437 void init(Context context, LightsService lights, IActivityManager activity,
The Android Open Source Project10592532009-03-18 17:39:46 -0700438 BatteryService battery) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500439 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 mContext = context;
441 mActivityService = activity;
442 mBatteryStats = BatteryStatsService.getService();
443 mBatteryService = battery;
444
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500445 mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT);
446 mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS);
447 mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD);
448 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 mHandlerThread = new HandlerThread("PowerManagerService") {
451 @Override
452 protected void onLooperPrepared() {
453 super.onLooperPrepared();
454 initInThread();
455 }
456 };
457 mHandlerThread.start();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 synchronized (mHandlerThread) {
460 while (!mInitComplete) {
461 try {
462 mHandlerThread.wait();
463 } catch (InterruptedException e) {
464 // Ignore
465 }
466 }
467 }
468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 void initInThread() {
471 mHandler = new Handler();
472
473 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700474 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
476 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
477 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
478 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
479 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
480 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500481 mProximityPartialLock = new UnsynchronizedWakeLock(
482 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
485 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
486 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
487 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
488
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700489 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400490
491 // read settings for auto-brightness
492 mUseSoftwareAutoBrightness = resources.getBoolean(
493 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400494 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700495 mAutoBrightnessLevels = resources.getIntArray(
496 com.android.internal.R.array.config_autoBrightnessLevels);
497 mLcdBacklightValues = resources.getIntArray(
498 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
499 mButtonBacklightValues = resources.getIntArray(
500 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
501 mKeyboardBacklightValues = resources.getIntArray(
502 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500503 mLightSensorWarmupTime = resources.getInteger(
504 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700505 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700506
507 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
509 "(" + Settings.System.NAME + "=?) or ("
510 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700511 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 + Settings.System.NAME + "=?)",
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700513 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN,
514 SCREEN_BRIGHTNESS_MODE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 null);
516 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
517 SettingsObserver settingsObserver = new SettingsObserver();
518 mSettings.addObserver(settingsObserver);
519
520 // pretend that the settings changed so we will get their initial state
521 settingsObserver.update(mSettings, null);
522
523 // register for the battery changed notifications
524 IntentFilter filter = new IntentFilter();
525 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
526 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500527 filter = new IntentFilter();
528 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
529 mContext.registerReceiver(new BootCompletedReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Doug Zongker43866e02010-01-07 12:09:54 -0800531 // Listen for secure settings changes
532 mContext.getContentResolver().registerContentObserver(
533 Settings.Secure.CONTENT_URI, true,
534 new ContentObserver(new Handler()) {
535 public void onChange(boolean selfChange) {
536 updateSettingsValues();
537 }
538 });
539 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Mike Lockwood4984e732009-11-01 08:16:33 -0500541 if (mUseSoftwareAutoBrightness) {
Mike Lockwood6c97fca2009-10-20 08:10:00 -0400542 // turn the screen on
543 setPowerState(SCREEN_BRIGHT);
544 } else {
545 // turn everything on
546 setPowerState(ALL_BRIGHT);
547 }
Dan Murphy951764b2009-08-27 14:59:03 -0500548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 synchronized (mHandlerThread) {
550 mInitComplete = true;
551 mHandlerThread.notifyAll();
552 }
553 }
554
555 private class WakeLock implements IBinder.DeathRecipient
556 {
557 WakeLock(int f, IBinder b, String t, int u) {
558 super();
559 flags = f;
560 binder = b;
561 tag = t;
562 uid = u == MY_UID ? Process.SYSTEM_UID : u;
563 if (u != MY_UID || (
564 !"KEEP_SCREEN_ON_FLAG".equals(tag)
565 && !"KeyInputQueue".equals(tag))) {
566 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
567 ? BatteryStats.WAKE_TYPE_PARTIAL
568 : BatteryStats.WAKE_TYPE_FULL;
569 } else {
570 monitorType = -1;
571 }
572 try {
573 b.linkToDeath(this, 0);
574 } catch (RemoteException e) {
575 binderDied();
576 }
577 }
578 public void binderDied() {
579 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500580 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 }
583 final int flags;
584 final IBinder binder;
585 final String tag;
586 final int uid;
587 final int monitorType;
588 boolean activated = true;
589 int minState;
590 }
591
592 private void updateWakeLockLocked() {
593 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
594 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
595 mStayOnWhilePluggedInScreenDimLock.acquire();
596 mStayOnWhilePluggedInPartialLock.acquire();
597 } else {
598 mStayOnWhilePluggedInScreenDimLock.release();
599 mStayOnWhilePluggedInPartialLock.release();
600 }
601 }
602
603 private boolean isScreenLock(int flags)
604 {
605 int n = flags & LOCK_MASK;
606 return n == PowerManager.FULL_WAKE_LOCK
607 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
608 || n == PowerManager.SCREEN_DIM_WAKE_LOCK;
609 }
610
611 public void acquireWakeLock(int flags, IBinder lock, String tag) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 int uid = Binder.getCallingUid();
Michael Chane96440f2009-05-06 10:27:36 -0700613 if (uid != Process.myUid()) {
614 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 long ident = Binder.clearCallingIdentity();
617 try {
618 synchronized (mLocks) {
619 acquireWakeLockLocked(flags, lock, uid, tag);
620 }
621 } finally {
622 Binder.restoreCallingIdentity(ident);
623 }
624 }
625
626 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, String tag) {
627 int acquireUid = -1;
628 String acquireName = null;
629 int acquireType = -1;
630
631 if (mSpew) {
632 Log.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
633 }
634
635 int index = mLocks.getIndex(lock);
636 WakeLock wl;
637 boolean newlock;
638 if (index < 0) {
639 wl = new WakeLock(flags, lock, tag, uid);
640 switch (wl.flags & LOCK_MASK)
641 {
642 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500643 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400644 wl.minState = SCREEN_BRIGHT;
645 } else {
646 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 break;
649 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
650 wl.minState = SCREEN_BRIGHT;
651 break;
652 case PowerManager.SCREEN_DIM_WAKE_LOCK:
653 wl.minState = SCREEN_DIM;
654 break;
655 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700656 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 break;
658 default:
659 // just log and bail. we're in the server, so don't
660 // throw an exception.
661 Log.e(TAG, "bad wakelock type for lock '" + tag + "' "
662 + " flags=" + flags);
663 return;
664 }
665 mLocks.addLock(wl);
666 newlock = true;
667 } else {
668 wl = mLocks.get(index);
669 newlock = false;
670 }
671 if (isScreenLock(flags)) {
672 // if this causes a wakeup, we reactivate all of the locks and
673 // set it to whatever they want. otherwise, we modulate that
674 // by the current state so we never turn it more on than
675 // it already is.
676 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
Michael Chane96440f2009-05-06 10:27:36 -0700677 int oldWakeLockState = mWakeLockState;
678 mWakeLockState = mLocks.reactivateScreenLocksLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 if (mSpew) {
680 Log.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
Michael Chane96440f2009-05-06 10:27:36 -0700681 + " mWakeLockState=0x"
682 + Integer.toHexString(mWakeLockState)
683 + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 } else {
686 if (mSpew) {
687 Log.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
688 + " mLocks.gatherState()=0x"
689 + Integer.toHexString(mLocks.gatherState())
690 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
691 }
692 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
693 }
694 setPowerState(mWakeLockState | mUserState);
695 }
696 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
697 if (newlock) {
698 mPartialCount++;
699 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702 }
703 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700704 } else if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500705 mProximityWakeLockCount++;
706 if (mProximityWakeLockCount == 1) {
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700707 enableProximityLockLocked();
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710 if (newlock) {
711 acquireUid = wl.uid;
712 acquireName = wl.tag;
713 acquireType = wl.monitorType;
714 }
715
716 if (acquireType >= 0) {
717 try {
718 mBatteryStats.noteStartWakelock(acquireUid, acquireName, acquireType);
719 } catch (RemoteException e) {
720 // Ignore
721 }
722 }
723 }
724
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500725 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700726 int uid = Binder.getCallingUid();
727 if (uid != Process.myUid()) {
728 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730
731 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500732 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 }
735
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500736 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 int releaseUid;
738 String releaseName;
739 int releaseType;
740
741 WakeLock wl = mLocks.removeLock(lock);
742 if (wl == null) {
743 return;
744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 if (mSpew) {
747 Log.d(TAG, "releaseWakeLock flags=0x"
748 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
749 }
750
751 if (isScreenLock(wl.flags)) {
752 mWakeLockState = mLocks.gatherState();
753 // goes in the middle to reduce flicker
754 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
755 userActivity(SystemClock.uptimeMillis(), false);
756 }
757 setPowerState(mWakeLockState | mUserState);
758 }
759 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
760 mPartialCount--;
761 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 Power.releaseWakeLock(PARTIAL_NAME);
764 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700765 } else if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500766 mProximityWakeLockCount--;
767 if (mProximityWakeLockCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500768 if (mProximitySensorActive &&
769 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500770 // wait for proximity sensor to go negative before disabling sensor
771 if (mDebugProximitySensor) {
772 Log.d(TAG, "waiting for proximity sensor to go negative");
773 }
774 } else {
775 disableProximityLockLocked();
776 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
779 // Unlink the lock from the binder.
780 wl.binder.unlinkToDeath(wl, 0);
781 releaseUid = wl.uid;
782 releaseName = wl.tag;
783 releaseType = wl.monitorType;
784
785 if (releaseType >= 0) {
786 long origId = Binder.clearCallingIdentity();
787 try {
788 mBatteryStats.noteStopWakelock(releaseUid, releaseName, releaseType);
789 } catch (RemoteException e) {
790 // Ignore
791 } finally {
792 Binder.restoreCallingIdentity(origId);
793 }
794 }
795 }
796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 private class PokeLock implements IBinder.DeathRecipient
798 {
799 PokeLock(int p, IBinder b, String t) {
800 super();
801 this.pokey = p;
802 this.binder = b;
803 this.tag = t;
804 try {
805 b.linkToDeath(this, 0);
806 } catch (RemoteException e) {
807 binderDied();
808 }
809 }
810 public void binderDied() {
811 setPokeLock(0, this.binder, this.tag);
812 }
813 int pokey;
814 IBinder binder;
815 String tag;
816 boolean awakeOnSet;
817 }
818
819 public void setPokeLock(int pokey, IBinder token, String tag) {
820 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
821 if (token == null) {
822 Log.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
823 return;
824 }
825
826 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
827 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
828 + " and POKE_LOCK_MEDIUM_TIMEOUT");
829 }
830
831 synchronized (mLocks) {
832 if (pokey != 0) {
833 PokeLock p = mPokeLocks.get(token);
834 int oldPokey = 0;
835 if (p != null) {
836 oldPokey = p.pokey;
837 p.pokey = pokey;
838 } else {
839 p = new PokeLock(pokey, token, tag);
840 mPokeLocks.put(token, p);
841 }
842 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
843 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
844 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
845 p.awakeOnSet = true;
846 }
847 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700848 PokeLock rLock = mPokeLocks.remove(token);
849 if (rLock != null) {
850 token.unlinkToDeath(rLock, 0);
851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 }
853
854 int oldPokey = mPokey;
855 int cumulative = 0;
856 boolean oldAwakeOnSet = mPokeAwakeOnSet;
857 boolean awakeOnSet = false;
858 for (PokeLock p: mPokeLocks.values()) {
859 cumulative |= p.pokey;
860 if (p.awakeOnSet) {
861 awakeOnSet = true;
862 }
863 }
864 mPokey = cumulative;
865 mPokeAwakeOnSet = awakeOnSet;
866
867 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
868 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (oldCumulativeTimeout != newCumulativeTimeout) {
871 setScreenOffTimeoutsLocked();
872 // reset the countdown timer, but use the existing nextState so it doesn't
873 // change anything
874 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
875 }
876 }
877 }
878
879 private static String lockType(int type)
880 {
881 switch (type)
882 {
883 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700884 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700886 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700888 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700890 return "PARTIAL_WAKE_LOCK ";
891 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
892 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 default:
David Brown251faa62009-08-02 22:04:36 -0700894 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 }
896 }
897
898 private static String dumpPowerState(int state) {
899 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
900 ? "KEYBOARD_BRIGHT_BIT " : "")
901 + (((state & SCREEN_BRIGHT_BIT) != 0)
902 ? "SCREEN_BRIGHT_BIT " : "")
903 + (((state & SCREEN_ON_BIT) != 0)
904 ? "SCREEN_ON_BIT " : "")
905 + (((state & BATTERY_LOW_BIT) != 0)
906 ? "BATTERY_LOW_BIT " : "");
907 }
908
909 @Override
910 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
911 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
912 != PackageManager.PERMISSION_GRANTED) {
913 pw.println("Permission Denial: can't dump PowerManager from from pid="
914 + Binder.getCallingPid()
915 + ", uid=" + Binder.getCallingUid());
916 return;
917 }
918
919 long now = SystemClock.uptimeMillis();
920
921 pw.println("Power Manager State:");
922 pw.println(" mIsPowered=" + mIsPowered
923 + " mPowerState=" + mPowerState
924 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
925 + " ms");
926 pw.println(" mPartialCount=" + mPartialCount);
927 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
928 pw.println(" mUserState=" + dumpPowerState(mUserState));
929 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
930 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
931 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
932 + " " + ((mNextTimeout-now)/1000) + "s from now");
933 pw.println(" mDimScreen=" + mDimScreen
934 + " mStayOnConditions=" + mStayOnConditions);
Mike Lockwood435eb642009-12-03 08:40:18 -0500935 pw.println(" mScreenOffReason=" + mScreenOffReason
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 + " mUserState=" + mUserState);
Joe Onorato128e7292009-03-24 18:41:31 -0700937 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
938 + ',' + mBroadcastQueue[2] + "}");
939 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
940 + ',' + mBroadcastWhy[2] + "}");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
942 pw.println(" mKeyboardVisible=" + mKeyboardVisible
943 + " mUserActivityAllowed=" + mUserActivityAllowed);
944 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
945 + " mScreenOffDelay=" + mScreenOffDelay);
946 pw.println(" mPreventScreenOn=" + mPreventScreenOn
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500947 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
948 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800949 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
950 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500951 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
953 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
954 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
955 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500956 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500957 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
958 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700959 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
Mike Lockwood20f87d72009-11-05 16:08:51 -0500960 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
961 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700962 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500963 pw.println(" mLightSensorValue=" + mLightSensorValue
964 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
965 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
966 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
967 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400968 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700969 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 mScreenBrightness.dump(pw, " mScreenBrightness: ");
971 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
972 mButtonBrightness.dump(pw, " mButtonBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 int N = mLocks.size();
975 pw.println();
976 pw.println("mLocks.size=" + N + ":");
977 for (int i=0; i<N; i++) {
978 WakeLock wl = mLocks.get(i);
979 String type = lockType(wl.flags & LOCK_MASK);
980 String acquireCausesWakeup = "";
981 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
982 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
983 }
984 String activated = "";
985 if (wl.activated) {
986 activated = " activated";
987 }
988 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
989 + activated + " (minState=" + wl.minState + ")");
990 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 pw.println();
993 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
994 for (PokeLock p: mPokeLocks.values()) {
995 pw.println(" poke lock '" + p.tag + "':"
996 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
997 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
Joe Onoratoe68ffcb2009-03-24 19:11:13 -0700998 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
999 ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1001 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1002 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1003 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
1004 }
1005
1006 pw.println();
1007 }
1008
1009 private void setTimeoutLocked(long now, int nextState)
1010 {
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001011 if (mBootCompleted) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 mHandler.removeCallbacks(mTimeoutTask);
1013 mTimeoutTask.nextState = nextState;
1014 long when = now;
1015 switch (nextState)
1016 {
1017 case SCREEN_BRIGHT:
1018 when += mKeylightDelay;
1019 break;
1020 case SCREEN_DIM:
1021 if (mDimDelay >= 0) {
1022 when += mDimDelay;
1023 break;
1024 } else {
1025 Log.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1026 }
1027 case SCREEN_OFF:
1028 synchronized (mLocks) {
1029 when += mScreenOffDelay;
1030 }
1031 break;
1032 }
1033 if (mSpew) {
1034 Log.d(TAG, "setTimeoutLocked now=" + now + " nextState=" + nextState
1035 + " when=" + when);
1036 }
1037 mHandler.postAtTime(mTimeoutTask, when);
1038 mNextTimeout = when; // for debugging
1039 }
1040 }
1041
1042 private void cancelTimerLocked()
1043 {
1044 mHandler.removeCallbacks(mTimeoutTask);
1045 mTimeoutTask.nextState = -1;
1046 }
1047
1048 private class TimeoutTask implements Runnable
1049 {
1050 int nextState; // access should be synchronized on mLocks
1051 public void run()
1052 {
1053 synchronized (mLocks) {
1054 if (mSpew) {
1055 Log.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
1056 }
1057
1058 if (nextState == -1) {
1059 return;
1060 }
1061
1062 mUserState = this.nextState;
1063 setPowerState(this.nextState | mWakeLockState);
1064
1065 long now = SystemClock.uptimeMillis();
1066
1067 switch (this.nextState)
1068 {
1069 case SCREEN_BRIGHT:
1070 if (mDimDelay >= 0) {
1071 setTimeoutLocked(now, SCREEN_DIM);
1072 break;
1073 }
1074 case SCREEN_DIM:
1075 setTimeoutLocked(now, SCREEN_OFF);
1076 break;
1077 }
1078 }
1079 }
1080 }
1081
1082 private void sendNotificationLocked(boolean on, int why)
1083 {
Joe Onorato64c62ba2009-03-24 20:13:57 -07001084 if (!on) {
1085 mStillNeedSleepNotification = false;
1086 }
1087
Joe Onorato128e7292009-03-24 18:41:31 -07001088 // Add to the queue.
1089 int index = 0;
1090 while (mBroadcastQueue[index] != -1) {
1091 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Joe Onorato128e7292009-03-24 18:41:31 -07001093 mBroadcastQueue[index] = on ? 1 : 0;
1094 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095
Joe Onorato128e7292009-03-24 18:41:31 -07001096 // If we added it position 2, then there is a pair that can be stripped.
1097 // If we added it position 1 and we're turning the screen off, we can strip
1098 // the pair and do nothing, because the screen is already off, and therefore
1099 // keyguard has already been enabled.
1100 // However, if we added it at position 1 and we're turning it on, then position
1101 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1102 // on, so have to run the queue then.
1103 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001104 // While we're collapsing them, if it's going off, and the new reason
1105 // is more significant than the first, then use the new one.
1106 if (!on && mBroadcastWhy[0] > why) {
1107 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001108 }
1109 mBroadcastQueue[0] = on ? 1 : 0;
1110 mBroadcastQueue[1] = -1;
1111 mBroadcastQueue[2] = -1;
1112 index = 0;
1113 }
1114 if (index == 1 && !on) {
1115 mBroadcastQueue[0] = -1;
1116 mBroadcastQueue[1] = -1;
1117 index = -1;
1118 // The wake lock was being held, but we're not actually going to do any
1119 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001122 }
1123
1124 // Now send the message.
1125 if (index >= 0) {
1126 // Acquire the broadcast wake lock before changing the power
1127 // state. It will be release after the broadcast is sent.
1128 // We always increment the ref count for each notification in the queue
1129 // and always decrement when that notification is handled.
1130 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001131 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001132 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 }
1135
1136 private Runnable mNotificationTask = new Runnable()
1137 {
1138 public void run()
1139 {
Joe Onorato128e7292009-03-24 18:41:31 -07001140 while (true) {
1141 int value;
1142 int why;
1143 WindowManagerPolicy policy;
1144 synchronized (mLocks) {
1145 value = mBroadcastQueue[0];
1146 why = mBroadcastWhy[0];
1147 for (int i=0; i<2; i++) {
1148 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1149 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1150 }
1151 policy = getPolicyLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
Joe Onorato128e7292009-03-24 18:41:31 -07001153 if (value == 1) {
1154 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155
Joe Onorato128e7292009-03-24 18:41:31 -07001156 policy.screenTurnedOn();
1157 try {
1158 ActivityManagerNative.getDefault().wakingUp();
1159 } catch (RemoteException e) {
1160 // ignore it
1161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162
Joe Onorato128e7292009-03-24 18:41:31 -07001163 if (mSpew) {
1164 Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
1165 }
1166 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1167 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1168 mScreenOnBroadcastDone, mHandler, 0, null, null);
1169 } else {
1170 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001172 mBroadcastWakeLock.mCount);
1173 mBroadcastWakeLock.release();
1174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
Joe Onorato128e7292009-03-24 18:41:31 -07001177 else if (value == 0) {
1178 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Joe Onorato128e7292009-03-24 18:41:31 -07001180 policy.screenTurnedOff(why);
1181 try {
1182 ActivityManagerNative.getDefault().goingToSleep();
1183 } catch (RemoteException e) {
1184 // ignore it.
1185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186
Joe Onorato128e7292009-03-24 18:41:31 -07001187 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1188 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1189 mScreenOffBroadcastDone, mHandler, 0, null, null);
1190 } else {
1191 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001193 mBroadcastWakeLock.mCount);
1194 mBroadcastWakeLock.release();
1195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197 }
Joe Onorato128e7292009-03-24 18:41:31 -07001198 else {
1199 // If we're in this case, then this handler is running for a previous
1200 // paired transaction. mBroadcastWakeLock will already have been released.
1201 break;
1202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
1205 };
1206
1207 long mScreenOnStart;
1208 private BroadcastReceiver mScreenOnBroadcastDone = 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, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1213 mBroadcastWakeLock.release();
1214 }
1215 }
1216 };
1217
1218 long mScreenOffStart;
1219 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1220 public void onReceive(Context context, Intent intent) {
1221 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1224 mBroadcastWakeLock.release();
1225 }
1226 }
1227 };
1228
1229 void logPointerUpEvent() {
1230 if (LOG_TOUCH_DOWNS) {
1231 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1232 mLastTouchDown = 0;
1233 }
1234 }
1235
1236 void logPointerDownEvent() {
1237 if (LOG_TOUCH_DOWNS) {
1238 // If we are not already timing a down/up sequence
1239 if (mLastTouchDown == 0) {
1240 mLastTouchDown = SystemClock.elapsedRealtime();
1241 mTouchCycles++;
1242 }
1243 }
1244 }
1245
1246 /**
1247 * Prevents the screen from turning on even if it *should* turn on due
1248 * to a subsequent full wake lock being acquired.
1249 * <p>
1250 * This is a temporary hack that allows an activity to "cover up" any
1251 * display glitches that happen during the activity's startup
1252 * sequence. (Specifically, this API was added to work around a
1253 * cosmetic bug in the "incoming call" sequence, where the lock screen
1254 * would flicker briefly before the incoming call UI became visible.)
1255 * TODO: There ought to be a more elegant way of doing this,
1256 * probably by having the PowerManager and ActivityManager
1257 * work together to let apps specify that the screen on/off
1258 * state should be synchronized with the Activity lifecycle.
1259 * <p>
1260 * Note that calling preventScreenOn(true) will NOT turn the screen
1261 * off if it's currently on. (This API only affects *future*
1262 * acquisitions of full wake locks.)
1263 * But calling preventScreenOn(false) WILL turn the screen on if
1264 * it's currently off because of a prior preventScreenOn(true) call.
1265 * <p>
1266 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1267 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1268 * call doesn't occur within 5 seconds, we'll turn the screen back on
1269 * ourselves (and log a warning about it); this prevents a buggy app
1270 * from disabling the screen forever.)
1271 * <p>
1272 * TODO: this feature should really be controlled by a new type of poke
1273 * lock (rather than an IPowerManager call).
1274 */
1275 public void preventScreenOn(boolean prevent) {
1276 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1277
1278 synchronized (mLocks) {
1279 if (prevent) {
1280 // First of all, grab a partial wake lock to
1281 // make sure the CPU stays on during the entire
1282 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1283 mPreventScreenOnPartialLock.acquire();
1284
1285 // Post a forceReenableScreen() call (for 5 seconds in the
1286 // future) to make sure the matching preventScreenOn(false) call
1287 // has happened by then.
1288 mHandler.removeCallbacks(mForceReenableScreenTask);
1289 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1290
1291 // Finally, set the flag that prevents the screen from turning on.
1292 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001293 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 mPreventScreenOn = true;
1295 } else {
1296 // (Re)enable the screen.
1297 mPreventScreenOn = false;
1298
1299 // We're "undoing" a the prior preventScreenOn(true) call, so we
1300 // no longer need the 5-second safeguard.
1301 mHandler.removeCallbacks(mForceReenableScreenTask);
1302
1303 // Forcibly turn on the screen if it's supposed to be on. (This
1304 // handles the case where the screen is currently off because of
1305 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001306 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (mSpew) {
1308 Log.d(TAG,
1309 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1310 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001311 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 if (err != 0) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001313 Log.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
1315 }
1316
1317 // Release the partial wake lock that we held during the
1318 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1319 mPreventScreenOnPartialLock.release();
1320 }
1321 }
1322 }
1323
1324 public void setScreenBrightnessOverride(int brightness) {
1325 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1326
1327 synchronized (mLocks) {
1328 if (mScreenBrightnessOverride != brightness) {
1329 mScreenBrightnessOverride = brightness;
1330 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1331 }
1332 }
1333 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001334
1335 public void setButtonBrightnessOverride(int brightness) {
1336 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1337
1338 synchronized (mLocks) {
1339 if (mButtonBrightnessOverride != brightness) {
1340 mButtonBrightnessOverride = brightness;
1341 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1342 }
1343 }
1344 }
1345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 /**
1347 * Sanity-check that gets called 5 seconds after any call to
1348 * preventScreenOn(true). This ensures that the original call
1349 * is followed promptly by a call to preventScreenOn(false).
1350 */
1351 private void forceReenableScreen() {
1352 // We shouldn't get here at all if mPreventScreenOn is false, since
1353 // we should have already removed any existing
1354 // mForceReenableScreenTask messages...
1355 if (!mPreventScreenOn) {
1356 Log.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
1357 return;
1358 }
1359
1360 // Uh oh. It's been 5 seconds since a call to
1361 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1362 // This means the app that called preventScreenOn(true) is either
1363 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1364 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1365 // crashed before doing so.)
1366
1367 // Log a warning, and forcibly turn the screen back on.
1368 Log.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
1369 + "Forcing the screen back on...");
1370 preventScreenOn(false);
1371 }
1372
1373 private Runnable mForceReenableScreenTask = new Runnable() {
1374 public void run() {
1375 forceReenableScreen();
1376 }
1377 };
1378
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001379 private int setScreenStateLocked(boolean on) {
1380 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001381 if (err == 0) {
1382 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1383 if (mUseSoftwareAutoBrightness) {
1384 enableLightSensor(on);
1385 if (!on) {
1386 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001387 mButtonLight.turnOff();
1388 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001389 // clear current value so we will update based on the new conditions
1390 // when the sensor is reenabled.
1391 mLightSensorValue = -1;
1392 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001393 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001394 }
1395 return err;
1396 }
1397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 private void setPowerState(int state)
1399 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001400 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402
Mike Lockwood435eb642009-12-03 08:40:18 -05001403 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 {
1405 synchronized (mLocks) {
1406 int err;
1407
1408 if (mSpew) {
1409 Log.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
1410 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001411 + " noChangeLights=" + noChangeLights
1412 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414
1415 if (noChangeLights) {
1416 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1417 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001418 if (mProximitySensorActive) {
1419 // don't turn on the screen when the proximity sensor lock is held
1420 newState = (newState & ~SCREEN_BRIGHT);
1421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422
1423 if (batteryIsLow()) {
1424 newState |= BATTERY_LOW_BIT;
1425 } else {
1426 newState &= ~BATTERY_LOW_BIT;
1427 }
1428 if (newState == mPowerState) {
1429 return;
1430 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001431
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001432 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 newState |= ALL_BRIGHT;
1434 }
1435
1436 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1437 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1438
Mike Lockwood51b84492009-11-16 21:51:18 -05001439 if (mSpew) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 Log.d(TAG, "setPowerState: mPowerState=" + mPowerState
1441 + " newState=" + newState + " noChangeLights=" + noChangeLights);
1442 Log.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
1443 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
1444 Log.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
1445 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
1446 Log.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
1447 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
1448 Log.d(TAG, " oldScreenOn=" + oldScreenOn
1449 + " newScreenOn=" + newScreenOn);
1450 Log.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
1451 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1452 }
1453
1454 if (mPowerState != newState) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001455 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1457 }
1458
1459 if (oldScreenOn != newScreenOn) {
1460 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001461 // When the user presses the power button, we need to always send out the
1462 // notification that it's going to sleep so the keyguard goes on. But
1463 // we can't do that until the screen fades out, so we don't show the keyguard
1464 // too early.
1465 if (mStillNeedSleepNotification) {
1466 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1467 }
1468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 // Turn on the screen UNLESS there was a prior
1470 // preventScreenOn(true) request. (Note that the lifetime
1471 // of a single preventScreenOn() request is limited to 5
1472 // seconds to prevent a buggy app from disabling the
1473 // screen forever; see forceReenableScreen().)
1474 boolean reallyTurnScreenOn = true;
1475 if (mSpew) {
1476 Log.d(TAG, "- turning screen on... mPreventScreenOn = "
1477 + mPreventScreenOn);
1478 }
1479
1480 if (mPreventScreenOn) {
1481 if (mSpew) {
1482 Log.d(TAG, "- PREVENTING screen from really turning on!");
1483 }
1484 reallyTurnScreenOn = false;
1485 }
1486 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001487 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 long identity = Binder.clearCallingIdentity();
1489 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001490 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 mBatteryStats.noteScreenOn();
1492 } catch (RemoteException e) {
1493 Log.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
1494 } finally {
1495 Binder.restoreCallingIdentity(identity);
1496 }
1497 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001498 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 // But continue as if we really did turn the screen on...
1500 err = 0;
1501 }
1502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 mLastTouchDown = 0;
1504 mTotalTouchDownTime = 0;
1505 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 mTotalTouchDownTime, mTouchCycles);
1508 if (err == 0) {
1509 mPowerState |= SCREEN_ON_BIT;
1510 sendNotificationLocked(true, -1);
1511 }
1512 } else {
Mike Lockwood497087e32009-11-08 18:33:03 -05001513 // cancel light sensor task
1514 mHandler.removeCallbacks(mAutoBrightnessTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 mScreenOffTime = SystemClock.elapsedRealtime();
1516 long identity = Binder.clearCallingIdentity();
1517 try {
1518 mBatteryStats.noteScreenOff();
1519 } catch (RemoteException e) {
1520 Log.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
1521 } finally {
1522 Binder.restoreCallingIdentity(identity);
1523 }
1524 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001525 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001527 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 err = 0;
1530 mLastTouchDown = 0;
1531 }
1532 }
1533 }
1534 }
1535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001536
Mike Lockwood435eb642009-12-03 08:40:18 -05001537 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 // called multiple times in the same state. -joeo
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001543 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001545 mScreenOffReason = reason;
1546 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
1548 return err;
1549 }
1550
1551 private boolean batteryIsLow() {
1552 return (!mIsPowered &&
1553 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1554 }
1555
The Android Open Source Project10592532009-03-18 17:39:46 -07001556 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001557 final int oldState = mPowerState;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001558 newState = applyButtonState(newState);
1559 newState = applyKeyboardState(newState);
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001560 final int realDifference = (newState ^ oldState);
1561 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001563 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 int offMask = 0;
1567 int dimMask = 0;
1568 int onMask = 0;
1569
1570 int preferredBrightness = getPreferredBrightness();
1571 boolean startAnimation = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1574 if (ANIMATE_KEYBOARD_LIGHTS) {
1575 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1576 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001577 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001578 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001580 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001581 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
1582 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
1584 startAnimation = true;
1585 } else {
1586 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001587 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001589 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591 }
1592 }
1593
1594 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1595 if (ANIMATE_BUTTON_LIGHTS) {
1596 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1597 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001598 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001599 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001601 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001602 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
1603 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605 startAnimation = true;
1606 } else {
1607 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001608 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001610 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 }
1613 }
1614
1615 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1616 if (ANIMATE_SCREEN_LIGHTS) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001617 int nominalCurrentValue = -1;
1618 // If there was an actual difference in the light state, then
1619 // figure out the "ideal" current value based on the previous
1620 // state. Otherwise, this is a change due to the brightness
1621 // override, so we want to animate from whatever the current
1622 // value is.
1623 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1624 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
1625 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
1626 nominalCurrentValue = preferredBrightness;
1627 break;
1628 case SCREEN_ON_BIT:
1629 nominalCurrentValue = Power.BRIGHTNESS_DIM;
1630 break;
1631 case 0:
1632 nominalCurrentValue = Power.BRIGHTNESS_OFF;
1633 break;
1634 case SCREEN_BRIGHT_BIT:
1635 default:
1636 // not possible
1637 nominalCurrentValue = (int)mScreenBrightness.curValue;
1638 break;
1639 }
Joe Onorato128e7292009-03-24 18:41:31 -07001640 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001641 int brightness = preferredBrightness;
1642 int steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1644 // dim or turn off backlight, depending on if the screen is on
1645 // the scale is because the brightness ramp isn't linear and this biases
1646 // it so the later parts take longer.
1647 final float scale = 1.5f;
1648 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1649 if (ratio > 1.0f) ratio = 1.0f;
1650 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1652 // was bright
1653 steps = ANIM_STEPS;
1654 } else {
1655 // was dim
1656 steps = (int)(ANIM_STEPS*ratio*scale);
1657 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001658 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 if ((oldState & SCREEN_ON_BIT) != 0) {
1661 // was bright
1662 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1663 } else {
1664 // was dim
1665 steps = (int)(ANIM_STEPS*ratio);
1666 }
1667 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1668 // If the "stay on while plugged in" option is
1669 // turned on, then the screen will often not
1670 // automatically turn off while plugged in. To
1671 // still have a sense of when it is inactive, we
1672 // will then count going dim as turning off.
1673 mScreenOffTime = SystemClock.elapsedRealtime();
1674 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001675 brightness = Power.BRIGHTNESS_DIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001678 long identity = Binder.clearCallingIdentity();
1679 try {
1680 mBatteryStats.noteScreenBrightness(brightness);
1681 } catch (RemoteException e) {
1682 // Nothing interesting to do.
1683 } finally {
1684 Binder.restoreCallingIdentity(identity);
1685 }
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001686 if (mScreenBrightness.setTargetLocked(brightness,
1687 steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue)) {
1688 startAnimation = true;
1689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 } else {
1691 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1692 // dim or turn off backlight, depending on if the screen is on
1693 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001694 offMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001696 dimMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001699 onMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 }
1702 }
1703
1704 if (startAnimation) {
1705 if (mSpew) {
1706 Log.i(TAG, "Scheduling light animator!");
1707 }
1708 mHandler.removeCallbacks(mLightAnimator);
1709 mHandler.post(mLightAnimator);
1710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 if (offMask != 0) {
1713 //Log.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001714 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716 if (dimMask != 0) {
1717 int brightness = Power.BRIGHTNESS_DIM;
1718 if ((newState & BATTERY_LOW_BIT) != 0 &&
1719 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1720 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1721 }
1722 //Log.i(TAG, "Setting brightess dim " + brightness + ": " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001723 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
1725 if (onMask != 0) {
1726 int brightness = getPreferredBrightness();
1727 if ((newState & BATTERY_LOW_BIT) != 0 &&
1728 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1729 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1730 }
1731 //Log.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001732 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735
The Android Open Source Project10592532009-03-18 17:39:46 -07001736 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05001737 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05001738 ? LightsService.BRIGHTNESS_MODE_SENSOR
1739 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07001740 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001741 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07001742 }
1743 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001744 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001745 }
1746 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001747 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750
1751 class BrightnessState {
1752 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 boolean initialized;
1755 int targetValue;
1756 float curValue;
1757 float delta;
1758 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 BrightnessState(int m) {
1761 mask = m;
1762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 public void dump(PrintWriter pw, String prefix) {
1765 pw.println(prefix + "animating=" + animating
1766 + " targetValue=" + targetValue
1767 + " curValue=" + curValue
1768 + " delta=" + delta);
1769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001771 boolean setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07001772 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (!initialized) {
1774 initialized = true;
1775 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001776 } else if (targetValue == target) {
1777 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001780 delta = (targetValue -
1781 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
1782 / stepsToTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 if (mSpew) {
Joe Onorato128e7292009-03-24 18:41:31 -07001784 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 Log.i(TAG, "Setting target " + mask + ": cur=" + curValue
Joe Onorato128e7292009-03-24 18:41:31 -07001786 + " target=" + targetValue + " delta=" + delta
1787 + " nominalCurrentValue=" + nominalCurrentValue
1788 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 animating = true;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001791 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 boolean stepLocked() {
1795 if (!animating) return false;
1796 if (false && mSpew) {
1797 Log.i(TAG, "Step target " + mask + ": cur=" + curValue
1798 + " target=" + targetValue + " delta=" + delta);
1799 }
1800 curValue += delta;
1801 int curIntValue = (int)curValue;
1802 boolean more = true;
1803 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001804 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 more = false;
1806 } else if (delta > 0) {
1807 if (curIntValue >= targetValue) {
1808 curValue = curIntValue = targetValue;
1809 more = false;
1810 }
1811 } else {
1812 if (curIntValue <= targetValue) {
1813 curValue = curIntValue = targetValue;
1814 more = false;
1815 }
1816 }
1817 //Log.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001818 setLightBrightness(mask, curIntValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 animating = more;
1820 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001821 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001822 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
1824 }
1825 return more;
1826 }
1827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 private class LightAnimator implements Runnable {
1830 public void run() {
1831 synchronized (mLocks) {
1832 long now = SystemClock.uptimeMillis();
1833 boolean more = mScreenBrightness.stepLocked();
1834 if (mKeyboardBrightness.stepLocked()) {
1835 more = true;
1836 }
1837 if (mButtonBrightness.stepLocked()) {
1838 more = true;
1839 }
1840 if (more) {
1841 mHandler.postAtTime(mLightAnimator, now+(1000/60));
1842 }
1843 }
1844 }
1845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 private int getPreferredBrightness() {
1848 try {
1849 if (mScreenBrightnessOverride >= 0) {
1850 return mScreenBrightnessOverride;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001851 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
Mike Lockwood27c6dd72009-11-04 08:57:07 -05001852 && mAutoBrightessEnabled) {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001853 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
1856 SCREEN_BRIGHTNESS);
1857 // Don't let applications turn the screen all the way off
1858 return Math.max(brightness, Power.BRIGHTNESS_DIM);
1859 } catch (SettingNotFoundException snfe) {
1860 return Power.BRIGHTNESS_ON;
1861 }
1862 }
1863
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001864 private int applyButtonState(int state) {
1865 int brightness = -1;
1866 if (mButtonBrightnessOverride >= 0) {
1867 brightness = mButtonBrightnessOverride;
1868 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
1869 brightness = mLightSensorButtonBrightness;
1870 }
1871 if (brightness > 0) {
1872 return state | BUTTON_BRIGHT_BIT;
1873 } else if (brightness == 0) {
1874 return state & ~BUTTON_BRIGHT_BIT;
1875 } else {
1876 return state;
1877 }
1878 }
1879
1880 private int applyKeyboardState(int state) {
1881 int brightness = -1;
1882 if (!mKeyboardVisible) {
1883 brightness = 0;
1884 } else if (mButtonBrightnessOverride >= 0) {
1885 brightness = mButtonBrightnessOverride;
1886 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
1887 brightness = mLightSensorKeyboardBrightness;
1888 }
1889 if (brightness > 0) {
1890 return state | KEYBOARD_BRIGHT_BIT;
1891 } else if (brightness == 0) {
1892 return state & ~KEYBOARD_BRIGHT_BIT;
1893 } else {
1894 return state;
1895 }
1896 }
1897
Charles Mendis322591c2009-10-29 11:06:59 -07001898 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 synchronized (mLocks) {
1900 return (mPowerState & SCREEN_ON_BIT) != 0;
1901 }
1902 }
1903
Charles Mendis322591c2009-10-29 11:06:59 -07001904 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 synchronized (mLocks) {
1906 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
1907 }
1908 }
1909
Mike Lockwood497087e32009-11-08 18:33:03 -05001910 private boolean isScreenTurningOffLocked() {
1911 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
1912 }
1913
Mike Lockwood200b30b2009-09-20 00:23:59 -04001914 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05001915 if (isScreenTurningOffLocked()) {
1916 // cancel animation so userActivity will succeed
1917 mScreenBrightness.animating = false;
1918 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04001919 boolean savedActivityAllowed = mUserActivityAllowed;
1920 mUserActivityAllowed = true;
1921 userActivity(SystemClock.uptimeMillis(), false);
1922 mUserActivityAllowed = savedActivityAllowed;
1923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
1926 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1927 userActivity(time, noChangeLights, OTHER_EVENT, force);
1928 }
1929
1930 public void userActivity(long time, boolean noChangeLights) {
1931 userActivity(time, noChangeLights, OTHER_EVENT, false);
1932 }
1933
1934 public void userActivity(long time, boolean noChangeLights, int eventType) {
1935 userActivity(time, noChangeLights, eventType, false);
1936 }
1937
1938 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
1939 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1940
1941 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001942 && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (false) {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001944 Log.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946 return;
1947 }
1948
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07001949 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
1950 && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
1951 || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
1952 if (false) {
1953 Log.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
1954 }
1955 return;
1956 }
1957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (false) {
1959 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
1960 Log.d(TAG, "userActivity !!!");//, new RuntimeException());
1961 } else {
1962 Log.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
1963 }
1964 }
1965
1966 synchronized (mLocks) {
1967 if (mSpew) {
1968 Log.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
1969 + " mUserActivityAllowed=" + mUserActivityAllowed
1970 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07001971 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
1972 + " mProximitySensorActive=" + mProximitySensorActive
1973 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Mike Lockwood05067122009-10-27 23:07:25 -04001975 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05001976 if (isScreenTurningOffLocked()) {
Mike Lockwood05067122009-10-27 23:07:25 -04001977 Log.d(TAG, "ignoring user activity while turning off screen");
1978 return;
1979 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05001980 // Disable proximity sensor if if user presses power key while we are in the
1981 // "waiting for proximity sensor to go negative" state.
1982 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
1983 mProximitySensorActive = false;
1984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 if (mLastEventTime <= time || force) {
1986 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07001987 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001988 // Only turn on button backlights if a button was pressed
1989 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05001990 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
1992 } else {
1993 // don't clear button/keyboard backlights when the screen is touched.
1994 mUserState |= SCREEN_BRIGHT;
1995 }
1996
Dianne Hackborn617f8772009-03-31 15:04:46 -07001997 int uid = Binder.getCallingUid();
1998 long ident = Binder.clearCallingIdentity();
1999 try {
2000 mBatteryStats.noteUserActivity(uid, eventType);
2001 } catch (RemoteException e) {
2002 // Ignore
2003 } finally {
2004 Binder.restoreCallingIdentity(ident);
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
Michael Chane96440f2009-05-06 10:27:36 -07002007 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002008 setPowerState(mUserState | mWakeLockState, noChangeLights,
2009 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 setTimeoutLocked(time, SCREEN_BRIGHT);
2011 }
2012 }
2013 }
2014 }
2015
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002016 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2017 try {
2018 int i;
2019 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2020 if (sensorValue < mAutoBrightnessLevels[i]) {
2021 break;
2022 }
2023 }
2024 return values[i];
2025 } catch (Exception e) {
2026 // guard against null pointer or index out of bounds errors
2027 Log.e(TAG, "getAutoBrightnessValue", e);
2028 return 255;
2029 }
2030 }
2031
Mike Lockwood20f87d72009-11-05 16:08:51 -05002032 private Runnable mProximityTask = new Runnable() {
2033 public void run() {
2034 synchronized (mLocks) {
2035 if (mProximityPendingValue != -1) {
2036 proximityChangedLocked(mProximityPendingValue == 1);
2037 mProximityPendingValue = -1;
2038 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002039 if (mProximityPartialLock.isHeld()) {
2040 mProximityPartialLock.release();
2041 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002042 }
2043 }
2044 };
2045
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002046 private Runnable mAutoBrightnessTask = new Runnable() {
2047 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002048 synchronized (mLocks) {
2049 int value = (int)mLightSensorPendingValue;
2050 if (value >= 0) {
2051 mLightSensorPendingValue = -1;
2052 lightSensorChangedLocked(value);
2053 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002054 }
2055 }
2056 };
2057
2058 private void lightSensorChangedLocked(int value) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002059 if (mDebugLightSensor) {
2060 Log.d(TAG, "lightSensorChangedLocked " + value);
2061 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002062
2063 if (mLightSensorValue != value) {
2064 mLightSensorValue = value;
2065 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
2066 int lcdValue = getAutoBrightnessValue(value, mLcdBacklightValues);
2067 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002068 int keyboardValue;
2069 if (mKeyboardVisible) {
2070 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2071 } else {
2072 keyboardValue = 0;
2073 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002074 mLightSensorScreenBrightness = lcdValue;
2075 mLightSensorButtonBrightness = buttonValue;
2076 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002077
2078 if (mDebugLightSensor) {
2079 Log.d(TAG, "lcdValue " + lcdValue);
2080 Log.d(TAG, "buttonValue " + buttonValue);
2081 Log.d(TAG, "keyboardValue " + keyboardValue);
2082 }
2083
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002084 boolean startAnimation = false;
Mike Lockwood4984e732009-11-01 08:16:33 -05002085 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002086 if (ANIMATE_SCREEN_LIGHTS) {
2087 if (mScreenBrightness.setTargetLocked(lcdValue,
2088 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
2089 (int)mScreenBrightness.curValue)) {
2090 startAnimation = true;
2091 }
2092 } else {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002093 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002094 ? LightsService.BRIGHTNESS_MODE_SENSOR
2095 : LightsService.BRIGHTNESS_MODE_USER);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002096 mLcdLight.setBrightness(lcdValue, brightnessMode);
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) {
2100 if (ANIMATE_BUTTON_LIGHTS) {
2101 if (mButtonBrightness.setTargetLocked(buttonValue,
2102 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2103 (int)mButtonBrightness.curValue)) {
2104 startAnimation = true;
2105 }
2106 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002107 mButtonLight.setBrightness(buttonValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002108 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002109 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002110 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
2111 if (ANIMATE_KEYBOARD_LIGHTS) {
2112 if (mKeyboardBrightness.setTargetLocked(keyboardValue,
2113 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2114 (int)mKeyboardBrightness.curValue)) {
2115 startAnimation = true;
2116 }
2117 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002118 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002119 }
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002120 }
2121 if (startAnimation) {
2122 if (mDebugLightSensor) {
2123 Log.i(TAG, "lightSensorChangedLocked scheduling light animator");
2124 }
2125 mHandler.removeCallbacks(mLightAnimator);
2126 mHandler.post(mLightAnimator);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002127 }
2128 }
2129 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002130 }
2131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 /**
2133 * The user requested that we go to sleep (probably with the power button).
2134 * This overrides all wake locks that are held.
2135 */
2136 public void goToSleep(long time)
2137 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002138 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2139 }
2140
2141 /**
2142 * The user requested that we go to sleep (probably with the power button).
2143 * This overrides all wake locks that are held.
2144 */
2145 public void goToSleepWithReason(long time, int reason)
2146 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2148 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002149 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002154 * Reboot the device immediately, passing 'reason' (may be null)
2155 * to the underlying __reboot system call. Should not return.
2156 */
2157 public void reboot(String reason)
2158 {
2159 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002160
San Mehat1e512792010-01-07 10:40:29 -08002161 /*
2162 * Manually shutdown the MountService to ensure media is
2163 * put into a safe state.
2164 */
2165 IMountService mSvc = IMountService.Stub.asInterface(
2166 ServiceManager.getService("mount"));
2167
2168 if (mSvc != null) {
2169 try {
2170 mSvc.shutdown();
2171 } catch (Exception e) {
2172 Log.e(TAG, "MountService shutdown failed", e);
2173 }
2174 } else {
2175 Log.w(TAG, "MountService unavailable for shutdown");
2176 }
San Mehat14e69af2010-01-06 14:58:18 -08002177
Doug Zongker50a21f42009-11-19 12:49:53 -08002178 try {
2179 Power.reboot(reason);
2180 } catch (IOException e) {
2181 Log.e(TAG, "reboot failed", e);
2182 }
2183 }
2184
Dan Egnor60d87622009-12-16 16:32:58 -08002185 /**
2186 * Crash the runtime (causing a complete restart of the Android framework).
2187 * Requires REBOOT permission. Mostly for testing. Should not return.
2188 */
2189 public void crash(final String message)
2190 {
2191 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2192 Thread t = new Thread("PowerManagerService.crash()") {
2193 public void run() { throw new RuntimeException(message); }
2194 };
2195 try {
2196 t.start();
2197 t.join();
2198 } catch (InterruptedException e) {
2199 Log.wtf(TAG, e);
2200 }
2201 }
2202
Mike Lockwood435eb642009-12-03 08:40:18 -05002203 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204
2205 if (mLastEventTime <= time) {
2206 mLastEventTime = time;
2207 // cancel all of the wake locks
2208 mWakeLockState = SCREEN_OFF;
2209 int N = mLocks.size();
2210 int numCleared = 0;
2211 for (int i=0; i<N; i++) {
2212 WakeLock wl = mLocks.get(i);
2213 if (isScreenLock(wl.flags)) {
2214 mLocks.get(i).activated = false;
2215 numCleared++;
2216 }
2217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002218 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002219 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002221 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 cancelTimerLocked();
2223 }
2224 }
2225
2226 public long timeSinceScreenOn() {
2227 synchronized (mLocks) {
2228 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2229 return 0;
2230 }
2231 return SystemClock.elapsedRealtime() - mScreenOffTime;
2232 }
2233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002236 synchronized (mLocks) {
2237 if (mSpew) {
2238 Log.d(TAG, "setKeyboardVisibility: " + visible);
2239 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002240 if (mKeyboardVisible != visible) {
2241 mKeyboardVisible = visible;
2242 // don't signal user activity if the screen is off; other code
2243 // will take care of turning on due to a true change to the lid
2244 // switch and synchronized with the lock screen.
2245 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002246 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002247 // force recompute of backlight values
2248 if (mLightSensorValue >= 0) {
2249 int value = (int)mLightSensorValue;
2250 mLightSensorValue = -1;
2251 lightSensorChangedLocked(value);
2252 }
2253 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002254 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2255 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002256 }
2257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
2259
2260 /**
2261 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002262 * When disabling user activity we also reset user power state so the keyguard can reset its
2263 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 */
2265 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002266 if (mSpew) {
2267 Log.d(TAG, "enableUserActivity " + enabled);
2268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 synchronized (mLocks) {
2270 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002271 if (!enabled) {
2272 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2273 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
2276 }
2277
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002278 private void setScreenBrightnessMode(int mode) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002279 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Mike Lockwoodf90ffcc2009-11-03 11:41:27 -05002280 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002281 mAutoBrightessEnabled = enabled;
Charles Mendis322591c2009-10-29 11:06:59 -07002282 if (isScreenOn()) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002283 // force recompute of backlight values
2284 if (mLightSensorValue >= 0) {
2285 int value = (int)mLightSensorValue;
2286 mLightSensorValue = -1;
2287 lightSensorChangedLocked(value);
2288 }
Mike Lockwood2d155d22009-10-27 09:32:30 -04002289 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002290 }
2291 }
2292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 /** Sets the screen off timeouts:
2294 * mKeylightDelay
2295 * mDimDelay
2296 * mScreenOffDelay
2297 * */
2298 private void setScreenOffTimeoutsLocked() {
2299 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002300 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 mDimDelay = -1;
2302 mScreenOffDelay = 0;
2303 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2304 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2305 mDimDelay = -1;
2306 mScreenOffDelay = 0;
2307 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002308 int totalDelay = mScreenOffTimeoutSetting;
2309 if (totalDelay > mMaximumScreenOffTimeout) {
2310 totalDelay = mMaximumScreenOffTimeout;
2311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2313 if (totalDelay < 0) {
2314 mScreenOffDelay = Integer.MAX_VALUE;
2315 } else if (mKeylightDelay < totalDelay) {
2316 // subtract the time that the keylight delay. This will give us the
2317 // remainder of the time that we need to sleep to get the accurate
2318 // screen off timeout.
2319 mScreenOffDelay = totalDelay - mKeylightDelay;
2320 } else {
2321 mScreenOffDelay = 0;
2322 }
2323 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2324 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2325 mScreenOffDelay = LONG_DIM_TIME;
2326 } else {
2327 mDimDelay = -1;
2328 }
2329 }
2330 if (mSpew) {
2331 Log.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
2332 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2333 + " mDimScreen=" + mDimScreen);
2334 }
2335 }
2336
2337 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002338 * Refreshes cached secure settings. Called once on startup, and
2339 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 */
Doug Zongker43866e02010-01-07 12:09:54 -08002341 private void updateSettingsValues() {
2342 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002344 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 SHORT_KEYLIGHT_DELAY_DEFAULT);
Doug Zongker43866e02010-01-07 12:09:54 -08002346 // Log.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 }
2348
2349 private class LockList extends ArrayList<WakeLock>
2350 {
2351 void addLock(WakeLock wl)
2352 {
2353 int index = getIndex(wl.binder);
2354 if (index < 0) {
2355 this.add(wl);
2356 }
2357 }
2358
2359 WakeLock removeLock(IBinder binder)
2360 {
2361 int index = getIndex(binder);
2362 if (index >= 0) {
2363 return this.remove(index);
2364 } else {
2365 return null;
2366 }
2367 }
2368
2369 int getIndex(IBinder binder)
2370 {
2371 int N = this.size();
2372 for (int i=0; i<N; i++) {
2373 if (this.get(i).binder == binder) {
2374 return i;
2375 }
2376 }
2377 return -1;
2378 }
2379
2380 int gatherState()
2381 {
2382 int result = 0;
2383 int N = this.size();
2384 for (int i=0; i<N; i++) {
2385 WakeLock wl = this.get(i);
2386 if (wl.activated) {
2387 if (isScreenLock(wl.flags)) {
2388 result |= wl.minState;
2389 }
2390 }
2391 }
2392 return result;
2393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002394
Michael Chane96440f2009-05-06 10:27:36 -07002395 int reactivateScreenLocksLocked()
2396 {
2397 int result = 0;
2398 int N = this.size();
2399 for (int i=0; i<N; i++) {
2400 WakeLock wl = this.get(i);
2401 if (isScreenLock(wl.flags)) {
2402 wl.activated = true;
2403 result |= wl.minState;
2404 }
2405 }
2406 return result;
2407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409
2410 void setPolicy(WindowManagerPolicy p) {
2411 synchronized (mLocks) {
2412 mPolicy = p;
2413 mLocks.notifyAll();
2414 }
2415 }
2416
2417 WindowManagerPolicy getPolicyLocked() {
2418 while (mPolicy == null || !mDoneBooting) {
2419 try {
2420 mLocks.wait();
2421 } catch (InterruptedException e) {
2422 // Ignore
2423 }
2424 }
2425 return mPolicy;
2426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002429 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2430 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2431 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002432 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002433 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood4984e732009-11-01 08:16:33 -05002434 enableLightSensor(true);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002435 }
2436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 synchronized (mLocks) {
2438 Log.d(TAG, "system ready!");
2439 mDoneBooting = true;
Dianne Hackborn617f8772009-03-31 15:04:46 -07002440 long identity = Binder.clearCallingIdentity();
2441 try {
2442 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2443 mBatteryStats.noteScreenOn();
2444 } catch (RemoteException e) {
2445 // Nothing interesting to do.
2446 } finally {
2447 Binder.restoreCallingIdentity(identity);
2448 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002449 }
2450 }
2451
2452 void bootCompleted() {
2453 Log.d(TAG, "bootCompleted");
2454 synchronized (mLocks) {
2455 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2457 updateWakeLockLocked();
2458 mLocks.notifyAll();
2459 }
2460 }
2461
2462 public void monitor() {
2463 synchronized (mLocks) { }
2464 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002465
2466 public int getSupportedWakeLockFlags() {
2467 int result = PowerManager.PARTIAL_WAKE_LOCK
2468 | PowerManager.FULL_WAKE_LOCK
2469 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2470
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002471 if (mProximitySensor != null) {
2472 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2473 }
2474
2475 return result;
2476 }
2477
Mike Lockwood237a2992009-09-15 14:42:16 -04002478 public void setBacklightBrightness(int brightness) {
2479 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2480 // Don't let applications turn the screen all the way off
2481 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002482 mLcdLight.setBrightness(brightness);
2483 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2484 mButtonLight.setBrightness(brightness);
Mike Lockwood237a2992009-09-15 14:42:16 -04002485 long identity = Binder.clearCallingIdentity();
2486 try {
2487 mBatteryStats.noteScreenBrightness(brightness);
2488 } catch (RemoteException e) {
2489 Log.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
2490 } finally {
2491 Binder.restoreCallingIdentity(identity);
2492 }
2493
2494 // update our animation state
2495 if (ANIMATE_SCREEN_LIGHTS) {
2496 mScreenBrightness.curValue = brightness;
2497 mScreenBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002498 mScreenBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002499 }
2500 if (ANIMATE_KEYBOARD_LIGHTS) {
2501 mKeyboardBrightness.curValue = brightness;
2502 mKeyboardBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002503 mKeyboardBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002504 }
2505 if (ANIMATE_BUTTON_LIGHTS) {
2506 mButtonBrightness.curValue = brightness;
2507 mButtonBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002508 mButtonBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002509 }
2510 }
2511
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002512 public void setAttentionLight(boolean on, int color) {
2513 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002514 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002515 }
2516
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002517 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002518 if (mDebugProximitySensor) {
Mike Lockwood36fc3022009-08-25 16:49:06 -07002519 Log.d(TAG, "enableProximityLockLocked");
2520 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002521 if (!mProximitySensorEnabled) {
2522 // clear calling identity so sensor manager battery stats are accurate
2523 long identity = Binder.clearCallingIdentity();
2524 try {
2525 mSensorManager.registerListener(mProximityListener, mProximitySensor,
2526 SensorManager.SENSOR_DELAY_NORMAL);
2527 mProximitySensorEnabled = true;
2528 } finally {
2529 Binder.restoreCallingIdentity(identity);
2530 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002531 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002532 }
2533
2534 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002535 if (mDebugProximitySensor) {
Mike Lockwood36fc3022009-08-25 16:49:06 -07002536 Log.d(TAG, "disableProximityLockLocked");
2537 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002538 if (mProximitySensorEnabled) {
2539 // clear calling identity so sensor manager battery stats are accurate
2540 long identity = Binder.clearCallingIdentity();
2541 try {
2542 mSensorManager.unregisterListener(mProximityListener);
2543 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002544 if (mProximityPartialLock.isHeld()) {
2545 mProximityPartialLock.release();
2546 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002547 mProximitySensorEnabled = false;
2548 } finally {
2549 Binder.restoreCallingIdentity(identity);
2550 }
2551 if (mProximitySensorActive) {
2552 mProximitySensorActive = false;
2553 forceUserActivityLocked();
2554 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002555 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002556 }
2557
Mike Lockwood20f87d72009-11-05 16:08:51 -05002558 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002559 if (mDebugProximitySensor) {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002560 Log.d(TAG, "proximityChangedLocked, active: " + active);
2561 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002562 if (!mProximitySensorEnabled) {
2563 Log.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05002564 return;
2565 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002566 if (active) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002567 goToSleepLocked(SystemClock.uptimeMillis(),
2568 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002569 mProximitySensorActive = true;
2570 } else {
2571 // proximity sensor negative events trigger as user activity.
2572 // temporarily set mUserActivityAllowed to true so this will work
2573 // even when the keyguard is on.
2574 mProximitySensorActive = false;
2575 forceUserActivityLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002576
2577 if (mProximityWakeLockCount == 0) {
2578 // disable sensor if we have no listeners left after proximity negative
2579 disableProximityLockLocked();
2580 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002581 }
2582 }
2583
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002584 private void enableLightSensor(boolean enable) {
2585 if (mDebugLightSensor) {
2586 Log.d(TAG, "enableLightSensor " + enable);
2587 }
2588 if (mSensorManager != null && mLightSensorEnabled != enable) {
2589 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002590 // clear calling identity so sensor manager battery stats are accurate
2591 long identity = Binder.clearCallingIdentity();
2592 try {
2593 if (enable) {
2594 mSensorManager.registerListener(mLightListener, mLightSensor,
2595 SensorManager.SENSOR_DELAY_NORMAL);
2596 } else {
2597 mSensorManager.unregisterListener(mLightListener);
2598 mHandler.removeCallbacks(mAutoBrightnessTask);
2599 }
2600 } finally {
2601 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04002602 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002603 }
2604 }
2605
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002606 SensorEventListener mProximityListener = new SensorEventListener() {
2607 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002608 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002609 synchronized (mLocks) {
2610 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05002611 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
2612 mLastProximityEventTime = milliseconds;
2613 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002614 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05002615
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002616 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05002617 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
2618 distance < mProximitySensor.getMaximumRange());
2619
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002620 if (mDebugProximitySensor) {
2621 Log.d(TAG, "mProximityListener.onSensorChanged active: " + active);
2622 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002623 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
2624 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
2625 mProximityPendingValue = (active ? 1 : 0);
2626 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002627 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002628 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002629 // process the value immediately
2630 mProximityPendingValue = -1;
2631 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002632 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002633
2634 // update mProximityPartialLock state
2635 boolean held = mProximityPartialLock.isHeld();
2636 if (!held && proximityTaskQueued) {
2637 // hold wakelock until mProximityTask runs
2638 mProximityPartialLock.acquire();
2639 } else if (held && !proximityTaskQueued) {
2640 mProximityPartialLock.release();
2641 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002642 }
2643 }
2644
2645 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2646 // ignore
2647 }
2648 };
2649
2650 SensorEventListener mLightListener = new SensorEventListener() {
2651 public void onSensorChanged(SensorEvent event) {
2652 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05002653 // ignore light sensor while screen is turning off
2654 if (isScreenTurningOffLocked()) {
2655 return;
2656 }
2657
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002658 int value = (int)event.values[0];
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002659 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002660 if (mDebugLightSensor) {
2661 Log.d(TAG, "onSensorChanged: light value: " + value);
2662 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002663 mHandler.removeCallbacks(mAutoBrightnessTask);
2664 if (mLightSensorValue != value) {
Mike Lockwood20ee6f22009-11-07 20:33:47 -05002665 if (mLightSensorValue == -1 ||
2666 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
2667 // process the value immediately if screen has just turned on
Mike Lockwood6c97fca2009-10-20 08:10:00 -04002668 lightSensorChangedLocked(value);
2669 } else {
2670 // delay processing to debounce the sensor
2671 mLightSensorPendingValue = value;
2672 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
2673 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002674 } else {
2675 mLightSensorPendingValue = -1;
2676 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002677 }
2678 }
2679
2680 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2681 // ignore
2682 }
2683 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684}