blob: ad30ffcc0780aa6b3b8f41400e613bd260fb4604 [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;
31import android.database.Cursor;
32import android.os.BatteryStats;
33import android.os.Binder;
34import android.os.Handler;
35import android.os.HandlerThread;
36import android.os.IBinder;
37import android.os.IPowerManager;
38import android.os.LocalPowerManager;
39import android.os.Power;
40import android.os.PowerManager;
41import android.os.Process;
42import android.os.RemoteException;
43import android.os.SystemClock;
44import android.provider.Settings.SettingNotFoundException;
45import android.provider.Settings;
46import android.util.EventLog;
47import android.util.Log;
48import android.view.WindowManagerPolicy;
49import static android.provider.Settings.System.DIM_SCREEN;
50import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
51import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
52import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
53
54import java.io.FileDescriptor;
55import java.io.PrintWriter;
56import java.util.ArrayList;
57import java.util.HashMap;
58import java.util.Observable;
59import java.util.Observer;
60
61class PowerManagerService extends IPowerManager.Stub implements LocalPowerManager, Watchdog.Monitor {
62
63 private static final String TAG = "PowerManagerService";
64 static final String PARTIAL_NAME = "PowerManagerService";
65
66 private static final boolean LOG_PARTIAL_WL = false;
67
68 // Indicates whether touch-down cycles should be logged as part of the
69 // LOG_POWER_SCREEN_STATE log events
70 private static final boolean LOG_TOUCH_DOWNS = true;
71
72 private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK
73 | PowerManager.SCREEN_DIM_WAKE_LOCK
74 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
75 | PowerManager.FULL_WAKE_LOCK;
76
77 // time since last state: time since last event:
78 // The short keylight delay comes from Gservices; this is the default.
79 private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
80 private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec
81 private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec
82 private static final int LONG_DIM_TIME = 7000; // t+N-5 sec
83
84 // Cached Gservices settings; see updateGservicesValues()
85 private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
86
87 // flags for setPowerState
88 private static final int SCREEN_ON_BIT = 0x00000001;
89 private static final int SCREEN_BRIGHT_BIT = 0x00000002;
90 private static final int BUTTON_BRIGHT_BIT = 0x00000004;
91 private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
92 private static final int BATTERY_LOW_BIT = 0x00000010;
93
94 // values for setPowerState
95
96 // SCREEN_OFF == everything off
97 private static final int SCREEN_OFF = 0x00000000;
98
99 // SCREEN_DIM == screen on, screen backlight dim
100 private static final int SCREEN_DIM = SCREEN_ON_BIT;
101
102 // SCREEN_BRIGHT == screen on, screen backlight bright
103 private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
104
105 // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
106 private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
107
108 // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
109 private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
110
111 // used for noChangeLights in setPowerState()
112 private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
113
114 static final boolean ANIMATE_SCREEN_LIGHTS = true;
115 static final boolean ANIMATE_BUTTON_LIGHTS = false;
116 static final boolean ANIMATE_KEYBOARD_LIGHTS = false;
117
118 static final int ANIM_STEPS = 60/4;
119
120 // These magic numbers are the initial state of the LEDs at boot. Ideally
121 // we should read them from the driver, but our current hardware returns 0
122 // for the initial value. Oops!
123 static final int INITIAL_SCREEN_BRIGHTNESS = 255;
124 static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF;
125 static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF;
126
127 static final int LOG_POWER_SLEEP_REQUESTED = 2724;
128 static final int LOG_POWER_SCREEN_BROADCAST_SEND = 2725;
129 static final int LOG_POWER_SCREEN_BROADCAST_DONE = 2726;
130 static final int LOG_POWER_SCREEN_BROADCAST_STOP = 2727;
131 static final int LOG_POWER_SCREEN_STATE = 2728;
132 static final int LOG_POWER_PARTIAL_WAKE_STATE = 2729;
133
134 private final int MY_UID;
135
136 private boolean mDoneBooting = false;
137 private int mStayOnConditions = 0;
138 private int mNotificationQueue = -1;
139 private int mNotificationWhy;
140 private int mPartialCount = 0;
141 private int mPowerState;
142 private boolean mOffBecauseOfUser;
143 private int mUserState;
144 private boolean mKeyboardVisible = false;
145 private boolean mUserActivityAllowed = true;
146 private int mTotalDelaySetting;
147 private int mKeylightDelay;
148 private int mDimDelay;
149 private int mScreenOffDelay;
150 private int mWakeLockState;
151 private long mLastEventTime = 0;
152 private long mScreenOffTime;
153 private volatile WindowManagerPolicy mPolicy;
154 private final LockList mLocks = new LockList();
155 private Intent mScreenOffIntent;
156 private Intent mScreenOnIntent;
157 private Context mContext;
158 private UnsynchronizedWakeLock mBroadcastWakeLock;
159 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
160 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
161 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
162 private HandlerThread mHandlerThread;
163 private Handler mHandler;
164 private TimeoutTask mTimeoutTask = new TimeoutTask();
165 private LightAnimator mLightAnimator = new LightAnimator();
166 private final BrightnessState mScreenBrightness
167 = new BrightnessState(Power.SCREEN_LIGHT);
168 private final BrightnessState mKeyboardBrightness
169 = new BrightnessState(Power.KEYBOARD_LIGHT);
170 private final BrightnessState mButtonBrightness
171 = new BrightnessState(Power.BUTTON_LIGHT);
172 private boolean mIsPowered = false;
173 private IActivityManager mActivityService;
174 private IBatteryStats mBatteryStats;
175 private BatteryService mBatteryService;
176 private boolean mDimScreen = true;
177 private long mNextTimeout;
178 private volatile int mPokey = 0;
179 private volatile boolean mPokeAwakeOnSet = false;
180 private volatile boolean mInitComplete = false;
181 private HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
182 private long mScreenOnTime;
183 private long mScreenOnStartTime;
184 private boolean mPreventScreenOn;
185 private int mScreenBrightnessOverride = -1;
186
187 // Used when logging number and duration of touch-down cycles
188 private long mTotalTouchDownTime;
189 private long mLastTouchDown;
190 private int mTouchCycles;
191
192 // could be either static or controllable at runtime
193 private static final boolean mSpew = false;
194
195 /*
196 static PrintStream mLog;
197 static {
198 try {
199 mLog = new PrintStream("/data/power.log");
200 }
201 catch (FileNotFoundException e) {
202 android.util.Log.e(TAG, "Life is hard", e);
203 }
204 }
205 static class Log {
206 static void d(String tag, String s) {
207 mLog.println(s);
208 android.util.Log.d(tag, s);
209 }
210 static void i(String tag, String s) {
211 mLog.println(s);
212 android.util.Log.i(tag, s);
213 }
214 static void w(String tag, String s) {
215 mLog.println(s);
216 android.util.Log.w(tag, s);
217 }
218 static void e(String tag, String s) {
219 mLog.println(s);
220 android.util.Log.e(tag, s);
221 }
222 }
223 */
224
225 /**
226 * This class works around a deadlock between the lock in PowerManager.WakeLock
227 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
228 * mToken object so it can be accessed from any thread, but it calls into here
229 * with its lock held. This class is essentially a reimplementation of
230 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
231 * only call it with our own locks held.
232 */
233 private class UnsynchronizedWakeLock {
234 int mFlags;
235 String mTag;
236 IBinder mToken;
237 int mCount = 0;
238 boolean mRefCounted;
239
240 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
241 mFlags = flags;
242 mTag = tag;
243 mToken = new Binder();
244 mRefCounted = refCounted;
245 }
246
247 public void acquire() {
248 if (!mRefCounted || mCount++ == 0) {
249 long ident = Binder.clearCallingIdentity();
250 try {
251 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
252 MY_UID, mTag);
253 } finally {
254 Binder.restoreCallingIdentity(ident);
255 }
256 }
257 }
258
259 public void release() {
260 if (!mRefCounted || --mCount == 0) {
261 PowerManagerService.this.releaseWakeLockLocked(mToken, false);
262 }
263 if (mCount < 0) {
264 throw new RuntimeException("WakeLock under-locked " + mTag);
265 }
266 }
267
268 public String toString() {
269 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
270 + " mCount=" + mCount + ")";
271 }
272 }
273
274 private final class BatteryReceiver extends BroadcastReceiver {
275 @Override
276 public void onReceive(Context context, Intent intent) {
277 synchronized (mLocks) {
278 boolean wasPowered = mIsPowered;
279 mIsPowered = mBatteryService.isPowered();
280
281 if (mIsPowered != wasPowered) {
282 // update mStayOnWhilePluggedIn wake lock
283 updateWakeLockLocked();
284
285 // treat plugging and unplugging the devices as a user activity.
286 // users find it disconcerting when they unplug the device
287 // and it shuts off right away.
288 // temporarily set mUserActivityAllowed to true so this will work
289 // even when the keyguard is on.
290 synchronized (mLocks) {
291 boolean savedActivityAllowed = mUserActivityAllowed;
292 mUserActivityAllowed = true;
293 userActivity(SystemClock.uptimeMillis(), false);
294 mUserActivityAllowed = savedActivityAllowed;
295 }
296 }
297 }
298 }
299 }
300
301 /**
302 * Set the setting that determines whether the device stays on when plugged in.
303 * The argument is a bit string, with each bit specifying a power source that,
304 * when the device is connected to that source, causes the device to stay on.
305 * See {@link android.os.BatteryManager} for the list of power sources that
306 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
307 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
308 * @param val an {@code int} containing the bits that specify which power sources
309 * should cause the device to stay on.
310 */
311 public void setStayOnSetting(int val) {
312 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
313 Settings.System.putInt(mContext.getContentResolver(),
314 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
315 }
316
317 private class SettingsObserver implements Observer {
318 private int getInt(String name) {
319 return mSettings.getValues(name).getAsInteger(Settings.System.VALUE);
320 }
321
322 public void update(Observable o, Object arg) {
323 synchronized (mLocks) {
324 // STAY_ON_WHILE_PLUGGED_IN
325 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN);
326 updateWakeLockLocked();
327
328 // SCREEN_OFF_TIMEOUT
329 mTotalDelaySetting = getInt(SCREEN_OFF_TIMEOUT);
330
331 // DIM_SCREEN
332 //mDimScreen = getInt(DIM_SCREEN) != 0;
333
334 // recalculate everything
335 setScreenOffTimeoutsLocked();
336 }
337 }
338 }
339
340 PowerManagerService()
341 {
342 // Hack to get our uid... should have a func for this.
343 long token = Binder.clearCallingIdentity();
344 MY_UID = Binder.getCallingUid();
345 Binder.restoreCallingIdentity(token);
346
347 // XXX remove this when the kernel doesn't timeout wake locks
348 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
349
350 // assume nothing is on yet
351 mUserState = mPowerState = 0;
352
353 // Add ourself to the Watchdog monitors.
354 Watchdog.getInstance().addMonitor(this);
355 mScreenOnStartTime = SystemClock.elapsedRealtime();
356 }
357
358 private ContentQueryMap mSettings;
359
360 void init(Context context, IActivityManager activity, BatteryService battery) {
361 mContext = context;
362 mActivityService = activity;
363 mBatteryStats = BatteryStatsService.getService();
364 mBatteryService = battery;
365
366 mHandlerThread = new HandlerThread("PowerManagerService") {
367 @Override
368 protected void onLooperPrepared() {
369 super.onLooperPrepared();
370 initInThread();
371 }
372 };
373 mHandlerThread.start();
374
375 synchronized (mHandlerThread) {
376 while (!mInitComplete) {
377 try {
378 mHandlerThread.wait();
379 } catch (InterruptedException e) {
380 // Ignore
381 }
382 }
383 }
384 }
385
386 void initInThread() {
387 mHandler = new Handler();
388
389 mBroadcastWakeLock = new UnsynchronizedWakeLock(
390 PowerManager.PARTIAL_WAKE_LOCK, "sleep_notification", true);
391 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
392 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
393 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
394 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
395 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
396 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
397
398 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
399 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
400 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
401 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
402
403 ContentResolver resolver = mContext.getContentResolver();
404 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
405 "(" + Settings.System.NAME + "=?) or ("
406 + Settings.System.NAME + "=?) or ("
407 + Settings.System.NAME + "=?)",
408 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN},
409 null);
410 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
411 SettingsObserver settingsObserver = new SettingsObserver();
412 mSettings.addObserver(settingsObserver);
413
414 // pretend that the settings changed so we will get their initial state
415 settingsObserver.update(mSettings, null);
416
417 // register for the battery changed notifications
418 IntentFilter filter = new IntentFilter();
419 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
420 mContext.registerReceiver(new BatteryReceiver(), filter);
421
422 // Listen for Gservices changes
423 IntentFilter gservicesChangedFilter =
424 new IntentFilter(Settings.Gservices.CHANGED_ACTION);
425 mContext.registerReceiver(new GservicesChangedReceiver(), gservicesChangedFilter);
426 // And explicitly do the initial update of our cached settings
427 updateGservicesValues();
428
429 // turn everything on
430 setPowerState(ALL_BRIGHT);
431
432 synchronized (mHandlerThread) {
433 mInitComplete = true;
434 mHandlerThread.notifyAll();
435 }
436 }
437
438 private class WakeLock implements IBinder.DeathRecipient
439 {
440 WakeLock(int f, IBinder b, String t, int u) {
441 super();
442 flags = f;
443 binder = b;
444 tag = t;
445 uid = u == MY_UID ? Process.SYSTEM_UID : u;
446 if (u != MY_UID || (
447 !"KEEP_SCREEN_ON_FLAG".equals(tag)
448 && !"KeyInputQueue".equals(tag))) {
449 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
450 ? BatteryStats.WAKE_TYPE_PARTIAL
451 : BatteryStats.WAKE_TYPE_FULL;
452 } else {
453 monitorType = -1;
454 }
455 try {
456 b.linkToDeath(this, 0);
457 } catch (RemoteException e) {
458 binderDied();
459 }
460 }
461 public void binderDied() {
462 synchronized (mLocks) {
463 releaseWakeLockLocked(this.binder, true);
464 }
465 }
466 final int flags;
467 final IBinder binder;
468 final String tag;
469 final int uid;
470 final int monitorType;
471 boolean activated = true;
472 int minState;
473 }
474
475 private void updateWakeLockLocked() {
476 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
477 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
478 mStayOnWhilePluggedInScreenDimLock.acquire();
479 mStayOnWhilePluggedInPartialLock.acquire();
480 } else {
481 mStayOnWhilePluggedInScreenDimLock.release();
482 mStayOnWhilePluggedInPartialLock.release();
483 }
484 }
485
486 private boolean isScreenLock(int flags)
487 {
488 int n = flags & LOCK_MASK;
489 return n == PowerManager.FULL_WAKE_LOCK
490 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
491 || n == PowerManager.SCREEN_DIM_WAKE_LOCK;
492 }
493
494 public void acquireWakeLock(int flags, IBinder lock, String tag) {
495 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
496 int uid = Binder.getCallingUid();
497 long ident = Binder.clearCallingIdentity();
498 try {
499 synchronized (mLocks) {
500 acquireWakeLockLocked(flags, lock, uid, tag);
501 }
502 } finally {
503 Binder.restoreCallingIdentity(ident);
504 }
505 }
506
507 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, String tag) {
508 int acquireUid = -1;
509 String acquireName = null;
510 int acquireType = -1;
511
512 if (mSpew) {
513 Log.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
514 }
515
516 int index = mLocks.getIndex(lock);
517 WakeLock wl;
518 boolean newlock;
519 if (index < 0) {
520 wl = new WakeLock(flags, lock, tag, uid);
521 switch (wl.flags & LOCK_MASK)
522 {
523 case PowerManager.FULL_WAKE_LOCK:
524 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
525 break;
526 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
527 wl.minState = SCREEN_BRIGHT;
528 break;
529 case PowerManager.SCREEN_DIM_WAKE_LOCK:
530 wl.minState = SCREEN_DIM;
531 break;
532 case PowerManager.PARTIAL_WAKE_LOCK:
533 break;
534 default:
535 // just log and bail. we're in the server, so don't
536 // throw an exception.
537 Log.e(TAG, "bad wakelock type for lock '" + tag + "' "
538 + " flags=" + flags);
539 return;
540 }
541 mLocks.addLock(wl);
542 newlock = true;
543 } else {
544 wl = mLocks.get(index);
545 newlock = false;
546 }
547 if (isScreenLock(flags)) {
548 // if this causes a wakeup, we reactivate all of the locks and
549 // set it to whatever they want. otherwise, we modulate that
550 // by the current state so we never turn it more on than
551 // it already is.
552 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
553 reactivateWakeLocksLocked();
554 if (mSpew) {
555 Log.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
556 + " mLocks.gatherState()=0x"
557 + Integer.toHexString(mLocks.gatherState())
558 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
559 }
560 mWakeLockState = mLocks.gatherState();
561 } else {
562 if (mSpew) {
563 Log.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
564 + " mLocks.gatherState()=0x"
565 + Integer.toHexString(mLocks.gatherState())
566 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
567 }
568 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
569 }
570 setPowerState(mWakeLockState | mUserState);
571 }
572 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
573 if (newlock) {
574 mPartialCount++;
575 if (mPartialCount == 1) {
576 if (LOG_PARTIAL_WL) EventLog.writeEvent(LOG_POWER_PARTIAL_WAKE_STATE, 1, tag);
577 }
578 }
579 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
580 }
581 if (newlock) {
582 acquireUid = wl.uid;
583 acquireName = wl.tag;
584 acquireType = wl.monitorType;
585 }
586
587 if (acquireType >= 0) {
588 try {
589 mBatteryStats.noteStartWakelock(acquireUid, acquireName, acquireType);
590 } catch (RemoteException e) {
591 // Ignore
592 }
593 }
594 }
595
596 public void releaseWakeLock(IBinder lock) {
597 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
598
599 synchronized (mLocks) {
600 releaseWakeLockLocked(lock, false);
601 }
602 }
603
604 private void releaseWakeLockLocked(IBinder lock, boolean death) {
605 int releaseUid;
606 String releaseName;
607 int releaseType;
608
609 WakeLock wl = mLocks.removeLock(lock);
610 if (wl == null) {
611 return;
612 }
613
614 if (mSpew) {
615 Log.d(TAG, "releaseWakeLock flags=0x"
616 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
617 }
618
619 if (isScreenLock(wl.flags)) {
620 mWakeLockState = mLocks.gatherState();
621 // goes in the middle to reduce flicker
622 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
623 userActivity(SystemClock.uptimeMillis(), false);
624 }
625 setPowerState(mWakeLockState | mUserState);
626 }
627 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
628 mPartialCount--;
629 if (mPartialCount == 0) {
630 if (LOG_PARTIAL_WL) EventLog.writeEvent(LOG_POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
631 Power.releaseWakeLock(PARTIAL_NAME);
632 }
633 }
634 // Unlink the lock from the binder.
635 wl.binder.unlinkToDeath(wl, 0);
636 releaseUid = wl.uid;
637 releaseName = wl.tag;
638 releaseType = wl.monitorType;
639
640 if (releaseType >= 0) {
641 long origId = Binder.clearCallingIdentity();
642 try {
643 mBatteryStats.noteStopWakelock(releaseUid, releaseName, releaseType);
644 } catch (RemoteException e) {
645 // Ignore
646 } finally {
647 Binder.restoreCallingIdentity(origId);
648 }
649 }
650 }
651
652 private void reactivateWakeLocksLocked()
653 {
654 int N = mLocks.size();
655 for (int i=0; i<N; i++) {
656 WakeLock wl = mLocks.get(i);
657 if (isScreenLock(wl.flags)) {
658 mLocks.get(i).activated = true;
659 }
660 }
661 }
662
663 private class PokeLock implements IBinder.DeathRecipient
664 {
665 PokeLock(int p, IBinder b, String t) {
666 super();
667 this.pokey = p;
668 this.binder = b;
669 this.tag = t;
670 try {
671 b.linkToDeath(this, 0);
672 } catch (RemoteException e) {
673 binderDied();
674 }
675 }
676 public void binderDied() {
677 setPokeLock(0, this.binder, this.tag);
678 }
679 int pokey;
680 IBinder binder;
681 String tag;
682 boolean awakeOnSet;
683 }
684
685 public void setPokeLock(int pokey, IBinder token, String tag) {
686 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
687 if (token == null) {
688 Log.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
689 return;
690 }
691
692 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
693 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
694 + " and POKE_LOCK_MEDIUM_TIMEOUT");
695 }
696
697 synchronized (mLocks) {
698 if (pokey != 0) {
699 PokeLock p = mPokeLocks.get(token);
700 int oldPokey = 0;
701 if (p != null) {
702 oldPokey = p.pokey;
703 p.pokey = pokey;
704 } else {
705 p = new PokeLock(pokey, token, tag);
706 mPokeLocks.put(token, p);
707 }
708 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
709 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
710 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
711 p.awakeOnSet = true;
712 }
713 } else {
714 mPokeLocks.remove(token);
715 }
716
717 int oldPokey = mPokey;
718 int cumulative = 0;
719 boolean oldAwakeOnSet = mPokeAwakeOnSet;
720 boolean awakeOnSet = false;
721 for (PokeLock p: mPokeLocks.values()) {
722 cumulative |= p.pokey;
723 if (p.awakeOnSet) {
724 awakeOnSet = true;
725 }
726 }
727 mPokey = cumulative;
728 mPokeAwakeOnSet = awakeOnSet;
729
730 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
731 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
732
733 if (oldCumulativeTimeout != newCumulativeTimeout) {
734 setScreenOffTimeoutsLocked();
735 // reset the countdown timer, but use the existing nextState so it doesn't
736 // change anything
737 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
738 }
739 }
740 }
741
742 private static String lockType(int type)
743 {
744 switch (type)
745 {
746 case PowerManager.FULL_WAKE_LOCK:
747 return "FULL_WAKE_LOCK ";
748 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
749 return "SCREEN_BRIGHT_WAKE_LOCK";
750 case PowerManager.SCREEN_DIM_WAKE_LOCK:
751 return "SCREEN_DIM_WAKE_LOCK ";
752 case PowerManager.PARTIAL_WAKE_LOCK:
753 return "PARTIAL_WAKE_LOCK ";
754 default:
755 return "??? ";
756 }
757 }
758
759 private static String dumpPowerState(int state) {
760 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
761 ? "KEYBOARD_BRIGHT_BIT " : "")
762 + (((state & SCREEN_BRIGHT_BIT) != 0)
763 ? "SCREEN_BRIGHT_BIT " : "")
764 + (((state & SCREEN_ON_BIT) != 0)
765 ? "SCREEN_ON_BIT " : "")
766 + (((state & BATTERY_LOW_BIT) != 0)
767 ? "BATTERY_LOW_BIT " : "");
768 }
769
770 @Override
771 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
772 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
773 != PackageManager.PERMISSION_GRANTED) {
774 pw.println("Permission Denial: can't dump PowerManager from from pid="
775 + Binder.getCallingPid()
776 + ", uid=" + Binder.getCallingUid());
777 return;
778 }
779
780 long now = SystemClock.uptimeMillis();
781
782 pw.println("Power Manager State:");
783 pw.println(" mIsPowered=" + mIsPowered
784 + " mPowerState=" + mPowerState
785 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
786 + " ms");
787 pw.println(" mPartialCount=" + mPartialCount);
788 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
789 pw.println(" mUserState=" + dumpPowerState(mUserState));
790 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
791 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
792 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
793 + " " + ((mNextTimeout-now)/1000) + "s from now");
794 pw.println(" mDimScreen=" + mDimScreen
795 + " mStayOnConditions=" + mStayOnConditions);
796 pw.println(" mOffBecauseOfUser=" + mOffBecauseOfUser
797 + " mUserState=" + mUserState);
798 pw.println(" mNotificationQueue=" + mNotificationQueue
799 + " mNotificationWhy=" + mNotificationWhy);
800 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
801 pw.println(" mKeyboardVisible=" + mKeyboardVisible
802 + " mUserActivityAllowed=" + mUserActivityAllowed);
803 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
804 + " mScreenOffDelay=" + mScreenOffDelay);
805 pw.println(" mPreventScreenOn=" + mPreventScreenOn
806 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride);
807 pw.println(" mTotalDelaySetting=" + mTotalDelaySetting);
808 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
809 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
810 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
811 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
812 mScreenBrightness.dump(pw, " mScreenBrightness: ");
813 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
814 mButtonBrightness.dump(pw, " mButtonBrightness: ");
815
816 int N = mLocks.size();
817 pw.println();
818 pw.println("mLocks.size=" + N + ":");
819 for (int i=0; i<N; i++) {
820 WakeLock wl = mLocks.get(i);
821 String type = lockType(wl.flags & LOCK_MASK);
822 String acquireCausesWakeup = "";
823 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
824 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
825 }
826 String activated = "";
827 if (wl.activated) {
828 activated = " activated";
829 }
830 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
831 + activated + " (minState=" + wl.minState + ")");
832 }
833
834 pw.println();
835 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
836 for (PokeLock p: mPokeLocks.values()) {
837 pw.println(" poke lock '" + p.tag + "':"
838 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
839 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
840 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
841 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
842 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
843 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
844 }
845
846 pw.println();
847 }
848
849 private void setTimeoutLocked(long now, int nextState)
850 {
851 if (mDoneBooting) {
852 mHandler.removeCallbacks(mTimeoutTask);
853 mTimeoutTask.nextState = nextState;
854 long when = now;
855 switch (nextState)
856 {
857 case SCREEN_BRIGHT:
858 when += mKeylightDelay;
859 break;
860 case SCREEN_DIM:
861 if (mDimDelay >= 0) {
862 when += mDimDelay;
863 break;
864 } else {
865 Log.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
866 }
867 case SCREEN_OFF:
868 synchronized (mLocks) {
869 when += mScreenOffDelay;
870 }
871 break;
872 }
873 if (mSpew) {
874 Log.d(TAG, "setTimeoutLocked now=" + now + " nextState=" + nextState
875 + " when=" + when);
876 }
877 mHandler.postAtTime(mTimeoutTask, when);
878 mNextTimeout = when; // for debugging
879 }
880 }
881
882 private void cancelTimerLocked()
883 {
884 mHandler.removeCallbacks(mTimeoutTask);
885 mTimeoutTask.nextState = -1;
886 }
887
888 private class TimeoutTask implements Runnable
889 {
890 int nextState; // access should be synchronized on mLocks
891 public void run()
892 {
893 synchronized (mLocks) {
894 if (mSpew) {
895 Log.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
896 }
897
898 if (nextState == -1) {
899 return;
900 }
901
902 mUserState = this.nextState;
903 setPowerState(this.nextState | mWakeLockState);
904
905 long now = SystemClock.uptimeMillis();
906
907 switch (this.nextState)
908 {
909 case SCREEN_BRIGHT:
910 if (mDimDelay >= 0) {
911 setTimeoutLocked(now, SCREEN_DIM);
912 break;
913 }
914 case SCREEN_DIM:
915 setTimeoutLocked(now, SCREEN_OFF);
916 break;
917 }
918 }
919 }
920 }
921
922 private void sendNotificationLocked(boolean on, int why)
923 {
924
925 if (!on) {
926 mNotificationWhy = why;
927 }
928
929 int value = on ? 1 : 0;
930 if (mNotificationQueue == -1) {
931 // empty
932 // Acquire the broadcast wake lock before changing the power
933 // state. It will be release after the broadcast is sent.
934 mBroadcastWakeLock.acquire();
935 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
936 mNotificationQueue = value;
937 mHandler.post(mNotificationTask);
938 } else if (mNotificationQueue != value) {
939 // it's a pair, so cancel it
940 mNotificationQueue = -1;
941 mHandler.removeCallbacks(mNotificationTask);
942 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
943 mBroadcastWakeLock.release();
944 } else {
945 // else, same so do nothing -- maybe we should warn?
946 Log.w(TAG, "Duplicate notification: on=" + on + " why=" + why);
947 }
948 }
949
950 private Runnable mNotificationTask = new Runnable()
951 {
952 public void run()
953 {
954 int value;
955 int why;
956 WindowManagerPolicy policy;
957 synchronized (mLocks) {
958 policy = getPolicyLocked();
959 value = mNotificationQueue;
960 why = mNotificationWhy;
961 mNotificationQueue = -1;
962 }
963 if (value == 1) {
964 mScreenOnStart = SystemClock.uptimeMillis();
965
966 policy.screenTurnedOn();
967 try {
968 ActivityManagerNative.getDefault().wakingUp();
969 } catch (RemoteException e) {
970 // ignore it
971 }
972
973 if (mSpew) {
974 Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
975 }
976 if (mContext != null && ActivityManagerNative.isSystemReady()) {
977 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
978 mScreenOnBroadcastDone, mHandler, 0, null, null);
979 } else {
980 synchronized (mLocks) {
981 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 2,
982 mBroadcastWakeLock.mCount);
983 mBroadcastWakeLock.release();
984 }
985 }
986 }
987 else if (value == 0) {
988 mScreenOffStart = SystemClock.uptimeMillis();
989
990 policy.screenTurnedOff(why);
991 try {
992 ActivityManagerNative.getDefault().goingToSleep();
993 } catch (RemoteException e) {
994 // ignore it.
995 }
996
997 if (mContext != null && ActivityManagerNative.isSystemReady()) {
998 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
999 mScreenOffBroadcastDone, mHandler, 0, null, null);
1000 } else {
1001 synchronized (mLocks) {
1002 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 3,
1003 mBroadcastWakeLock.mCount);
1004 mBroadcastWakeLock.release();
1005 }
1006 }
1007 }
1008 else {
1009 // If we're in this case, then this handler is running for a previous
1010 // paired transaction. mBroadcastWakeLock will already have been released
1011 // in sendNotificationLocked.
1012 }
1013 }
1014 };
1015
1016 long mScreenOnStart;
1017 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1018 public void onReceive(Context context, Intent intent) {
1019 synchronized (mLocks) {
1020 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_DONE, 1,
1021 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1022 mBroadcastWakeLock.release();
1023 }
1024 }
1025 };
1026
1027 long mScreenOffStart;
1028 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1029 public void onReceive(Context context, Intent intent) {
1030 synchronized (mLocks) {
1031 EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_DONE, 0,
1032 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1033 mBroadcastWakeLock.release();
1034 }
1035 }
1036 };
1037
1038 void logPointerUpEvent() {
1039 if (LOG_TOUCH_DOWNS) {
1040 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1041 mLastTouchDown = 0;
1042 }
1043 }
1044
1045 void logPointerDownEvent() {
1046 if (LOG_TOUCH_DOWNS) {
1047 // If we are not already timing a down/up sequence
1048 if (mLastTouchDown == 0) {
1049 mLastTouchDown = SystemClock.elapsedRealtime();
1050 mTouchCycles++;
1051 }
1052 }
1053 }
1054
1055 /**
1056 * Prevents the screen from turning on even if it *should* turn on due
1057 * to a subsequent full wake lock being acquired.
1058 * <p>
1059 * This is a temporary hack that allows an activity to "cover up" any
1060 * display glitches that happen during the activity's startup
1061 * sequence. (Specifically, this API was added to work around a
1062 * cosmetic bug in the "incoming call" sequence, where the lock screen
1063 * would flicker briefly before the incoming call UI became visible.)
1064 * TODO: There ought to be a more elegant way of doing this,
1065 * probably by having the PowerManager and ActivityManager
1066 * work together to let apps specify that the screen on/off
1067 * state should be synchronized with the Activity lifecycle.
1068 * <p>
1069 * Note that calling preventScreenOn(true) will NOT turn the screen
1070 * off if it's currently on. (This API only affects *future*
1071 * acquisitions of full wake locks.)
1072 * But calling preventScreenOn(false) WILL turn the screen on if
1073 * it's currently off because of a prior preventScreenOn(true) call.
1074 * <p>
1075 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1076 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1077 * call doesn't occur within 5 seconds, we'll turn the screen back on
1078 * ourselves (and log a warning about it); this prevents a buggy app
1079 * from disabling the screen forever.)
1080 * <p>
1081 * TODO: this feature should really be controlled by a new type of poke
1082 * lock (rather than an IPowerManager call).
1083 */
1084 public void preventScreenOn(boolean prevent) {
1085 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1086
1087 synchronized (mLocks) {
1088 if (prevent) {
1089 // First of all, grab a partial wake lock to
1090 // make sure the CPU stays on during the entire
1091 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1092 mPreventScreenOnPartialLock.acquire();
1093
1094 // Post a forceReenableScreen() call (for 5 seconds in the
1095 // future) to make sure the matching preventScreenOn(false) call
1096 // has happened by then.
1097 mHandler.removeCallbacks(mForceReenableScreenTask);
1098 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1099
1100 // Finally, set the flag that prevents the screen from turning on.
1101 // (Below, in setPowerState(), we'll check mPreventScreenOn and
1102 // we *won't* call Power.setScreenState(true) if it's set.)
1103 mPreventScreenOn = true;
1104 } else {
1105 // (Re)enable the screen.
1106 mPreventScreenOn = false;
1107
1108 // We're "undoing" a the prior preventScreenOn(true) call, so we
1109 // no longer need the 5-second safeguard.
1110 mHandler.removeCallbacks(mForceReenableScreenTask);
1111
1112 // Forcibly turn on the screen if it's supposed to be on. (This
1113 // handles the case where the screen is currently off because of
1114 // a prior preventScreenOn(true) call.)
1115 if ((mPowerState & SCREEN_ON_BIT) != 0) {
1116 if (mSpew) {
1117 Log.d(TAG,
1118 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1119 }
1120 int err = Power.setScreenState(true);
1121 if (err != 0) {
1122 Log.w(TAG, "preventScreenOn: error from Power.setScreenState(): " + err);
1123 }
1124 }
1125
1126 // Release the partial wake lock that we held during the
1127 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1128 mPreventScreenOnPartialLock.release();
1129 }
1130 }
1131 }
1132
1133 public void setScreenBrightnessOverride(int brightness) {
1134 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1135
1136 synchronized (mLocks) {
1137 if (mScreenBrightnessOverride != brightness) {
1138 mScreenBrightnessOverride = brightness;
1139 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1140 }
1141 }
1142 }
1143
1144 /**
1145 * Sanity-check that gets called 5 seconds after any call to
1146 * preventScreenOn(true). This ensures that the original call
1147 * is followed promptly by a call to preventScreenOn(false).
1148 */
1149 private void forceReenableScreen() {
1150 // We shouldn't get here at all if mPreventScreenOn is false, since
1151 // we should have already removed any existing
1152 // mForceReenableScreenTask messages...
1153 if (!mPreventScreenOn) {
1154 Log.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
1155 return;
1156 }
1157
1158 // Uh oh. It's been 5 seconds since a call to
1159 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1160 // This means the app that called preventScreenOn(true) is either
1161 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1162 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1163 // crashed before doing so.)
1164
1165 // Log a warning, and forcibly turn the screen back on.
1166 Log.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
1167 + "Forcing the screen back on...");
1168 preventScreenOn(false);
1169 }
1170
1171 private Runnable mForceReenableScreenTask = new Runnable() {
1172 public void run() {
1173 forceReenableScreen();
1174 }
1175 };
1176
1177 private void setPowerState(int state)
1178 {
1179 setPowerState(state, false, false);
1180 }
1181
1182 private void setPowerState(int newState, boolean noChangeLights, boolean becauseOfUser)
1183 {
1184 synchronized (mLocks) {
1185 int err;
1186
1187 if (mSpew) {
1188 Log.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
1189 + " newState=0x" + Integer.toHexString(newState)
1190 + " noChangeLights=" + noChangeLights);
1191 }
1192
1193 if (noChangeLights) {
1194 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1195 }
1196
1197 if (batteryIsLow()) {
1198 newState |= BATTERY_LOW_BIT;
1199 } else {
1200 newState &= ~BATTERY_LOW_BIT;
1201 }
1202 if (newState == mPowerState) {
1203 return;
1204 }
1205
1206 if (!mDoneBooting) {
1207 newState |= ALL_BRIGHT;
1208 }
1209
1210 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1211 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1212
1213 if (mSpew) {
1214 Log.d(TAG, "setPowerState: mPowerState=" + mPowerState
1215 + " newState=" + newState + " noChangeLights=" + noChangeLights);
1216 Log.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
1217 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
1218 Log.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
1219 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
1220 Log.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
1221 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
1222 Log.d(TAG, " oldScreenOn=" + oldScreenOn
1223 + " newScreenOn=" + newScreenOn);
1224 Log.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
1225 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1226 }
1227
1228 if (mPowerState != newState) {
1229 err = updateLightsLocked(newState, 0);
1230 if (err != 0) {
1231 return;
1232 }
1233 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1234 }
1235
1236 if (oldScreenOn != newScreenOn) {
1237 if (newScreenOn) {
1238 // Turn on the screen UNLESS there was a prior
1239 // preventScreenOn(true) request. (Note that the lifetime
1240 // of a single preventScreenOn() request is limited to 5
1241 // seconds to prevent a buggy app from disabling the
1242 // screen forever; see forceReenableScreen().)
1243 boolean reallyTurnScreenOn = true;
1244 if (mSpew) {
1245 Log.d(TAG, "- turning screen on... mPreventScreenOn = "
1246 + mPreventScreenOn);
1247 }
1248
1249 if (mPreventScreenOn) {
1250 if (mSpew) {
1251 Log.d(TAG, "- PREVENTING screen from really turning on!");
1252 }
1253 reallyTurnScreenOn = false;
1254 }
1255 if (reallyTurnScreenOn) {
1256 err = Power.setScreenState(true);
1257 long identity = Binder.clearCallingIdentity();
1258 try {
1259 mBatteryStats.noteScreenOn();
1260 } catch (RemoteException e) {
1261 Log.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
1262 } finally {
1263 Binder.restoreCallingIdentity(identity);
1264 }
1265 } else {
1266 Power.setScreenState(false);
1267 // But continue as if we really did turn the screen on...
1268 err = 0;
1269 }
1270
1271 mScreenOnStartTime = SystemClock.elapsedRealtime();
1272 mLastTouchDown = 0;
1273 mTotalTouchDownTime = 0;
1274 mTouchCycles = 0;
1275 EventLog.writeEvent(LOG_POWER_SCREEN_STATE, 1, becauseOfUser ? 1 : 0,
1276 mTotalTouchDownTime, mTouchCycles);
1277 if (err == 0) {
1278 mPowerState |= SCREEN_ON_BIT;
1279 sendNotificationLocked(true, -1);
1280 }
1281 } else {
1282 mScreenOffTime = SystemClock.elapsedRealtime();
1283 long identity = Binder.clearCallingIdentity();
1284 try {
1285 mBatteryStats.noteScreenOff();
1286 } catch (RemoteException e) {
1287 Log.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
1288 } finally {
1289 Binder.restoreCallingIdentity(identity);
1290 }
1291 mPowerState &= ~SCREEN_ON_BIT;
1292 if (!mScreenBrightness.animating) {
1293 err = screenOffFinishedAnimating(becauseOfUser);
1294 } else {
1295 mOffBecauseOfUser = becauseOfUser;
1296 err = 0;
1297 mLastTouchDown = 0;
1298 }
1299 }
1300 }
1301 }
1302 }
1303
1304 private int screenOffFinishedAnimating(boolean becauseOfUser) {
1305 // I don't think we need to check the current state here because all of these
1306 // Power.setScreenState and sendNotificationLocked can both handle being
1307 // called multiple times in the same state. -joeo
1308 EventLog.writeEvent(LOG_POWER_SCREEN_STATE, 0, becauseOfUser ? 1 : 0,
1309 mTotalTouchDownTime, mTouchCycles);
1310 mLastTouchDown = 0;
1311 int err = Power.setScreenState(false);
1312 if (mScreenOnStartTime != 0) {
1313 mScreenOnTime += SystemClock.elapsedRealtime() - mScreenOnStartTime;
1314 mScreenOnStartTime = 0;
1315 }
1316 if (err == 0) {
1317 int why = becauseOfUser
1318 ? WindowManagerPolicy.OFF_BECAUSE_OF_USER
1319 : WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT;
1320 sendNotificationLocked(false, why);
1321 }
1322 return err;
1323 }
1324
1325 private boolean batteryIsLow() {
1326 return (!mIsPowered &&
1327 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1328 }
1329
1330 private int updateLightsLocked(int newState, int forceState) {
1331 int oldState = mPowerState;
1332 int difference = (newState ^ oldState) | forceState;
1333 if (difference == 0) {
1334 return 0;
1335 }
1336
1337 int offMask = 0;
1338 int dimMask = 0;
1339 int onMask = 0;
1340
1341 int preferredBrightness = getPreferredBrightness();
1342 boolean startAnimation = false;
1343
1344 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1345 if (ANIMATE_KEYBOARD_LIGHTS) {
1346 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1347 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
1348 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS);
1349 } else {
1350 mKeyboardBrightness.setTargetLocked(preferredBrightness,
1351 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS);
1352 }
1353 startAnimation = true;
1354 } else {
1355 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1356 offMask |= Power.KEYBOARD_LIGHT;
1357 } else {
1358 onMask |= Power.KEYBOARD_LIGHT;
1359 }
1360 }
1361 }
1362
1363 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1364 if (ANIMATE_BUTTON_LIGHTS) {
1365 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1366 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
1367 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS);
1368 } else {
1369 mButtonBrightness.setTargetLocked(preferredBrightness,
1370 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS);
1371 }
1372 startAnimation = true;
1373 } else {
1374 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1375 offMask |= Power.BUTTON_LIGHT;
1376 } else {
1377 onMask |= Power.BUTTON_LIGHT;
1378 }
1379 }
1380 }
1381
1382 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1383 if (ANIMATE_SCREEN_LIGHTS) {
1384 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1385 // dim or turn off backlight, depending on if the screen is on
1386 // the scale is because the brightness ramp isn't linear and this biases
1387 // it so the later parts take longer.
1388 final float scale = 1.5f;
1389 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1390 if (ratio > 1.0f) ratio = 1.0f;
1391 if ((newState & SCREEN_ON_BIT) == 0) {
1392 int steps;
1393 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1394 // was bright
1395 steps = ANIM_STEPS;
1396 } else {
1397 // was dim
1398 steps = (int)(ANIM_STEPS*ratio*scale);
1399 }
1400 mScreenBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
1401 steps, INITIAL_SCREEN_BRIGHTNESS);
1402 } else {
1403 int steps;
1404 if ((oldState & SCREEN_ON_BIT) != 0) {
1405 // was bright
1406 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1407 } else {
1408 // was dim
1409 steps = (int)(ANIM_STEPS*ratio);
1410 }
1411 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1412 // If the "stay on while plugged in" option is
1413 // turned on, then the screen will often not
1414 // automatically turn off while plugged in. To
1415 // still have a sense of when it is inactive, we
1416 // will then count going dim as turning off.
1417 mScreenOffTime = SystemClock.elapsedRealtime();
1418 }
1419 mScreenBrightness.setTargetLocked(Power.BRIGHTNESS_DIM,
1420 steps, INITIAL_SCREEN_BRIGHTNESS);
1421 }
1422 } else {
1423 mScreenBrightness.setTargetLocked(preferredBrightness,
1424 ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS);
1425 }
1426 startAnimation = true;
1427 } else {
1428 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1429 // dim or turn off backlight, depending on if the screen is on
1430 if ((newState & SCREEN_ON_BIT) == 0) {
1431 offMask |= Power.SCREEN_LIGHT;
1432 } else {
1433 dimMask |= Power.SCREEN_LIGHT;
1434 }
1435 } else {
1436 onMask |= Power.SCREEN_LIGHT;
1437 }
1438 }
1439 }
1440
1441 if (startAnimation) {
1442 if (mSpew) {
1443 Log.i(TAG, "Scheduling light animator!");
1444 }
1445 mHandler.removeCallbacks(mLightAnimator);
1446 mHandler.post(mLightAnimator);
1447 }
1448
1449 int err = 0;
1450 if (offMask != 0) {
1451 //Log.i(TAG, "Setting brightess off: " + offMask);
1452 err |= Power.setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
1453 }
1454 if (dimMask != 0) {
1455 int brightness = Power.BRIGHTNESS_DIM;
1456 if ((newState & BATTERY_LOW_BIT) != 0 &&
1457 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1458 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1459 }
1460 //Log.i(TAG, "Setting brightess dim " + brightness + ": " + offMask);
1461 err |= Power.setLightBrightness(dimMask, brightness);
1462 }
1463 if (onMask != 0) {
1464 int brightness = getPreferredBrightness();
1465 if ((newState & BATTERY_LOW_BIT) != 0 &&
1466 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1467 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1468 }
1469 //Log.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
1470 err |= Power.setLightBrightness(onMask, brightness);
1471 }
1472
1473 return err;
1474 }
1475
1476 class BrightnessState {
1477 final int mask;
1478
1479 boolean initialized;
1480 int targetValue;
1481 float curValue;
1482 float delta;
1483 boolean animating;
1484
1485 BrightnessState(int m) {
1486 mask = m;
1487 }
1488
1489 public void dump(PrintWriter pw, String prefix) {
1490 pw.println(prefix + "animating=" + animating
1491 + " targetValue=" + targetValue
1492 + " curValue=" + curValue
1493 + " delta=" + delta);
1494 }
1495
1496 void setTargetLocked(int target, int stepsToTarget, int initialValue) {
1497 if (!initialized) {
1498 initialized = true;
1499 curValue = (float)initialValue;
1500 }
1501 targetValue = target;
1502 delta = (targetValue-curValue) / stepsToTarget;
1503 if (mSpew) {
1504 Log.i(TAG, "Setting target " + mask + ": cur=" + curValue
1505 + " target=" + targetValue + " delta=" + delta);
1506 }
1507 animating = true;
1508 }
1509
1510 boolean stepLocked() {
1511 if (!animating) return false;
1512 if (false && mSpew) {
1513 Log.i(TAG, "Step target " + mask + ": cur=" + curValue
1514 + " target=" + targetValue + " delta=" + delta);
1515 }
1516 curValue += delta;
1517 int curIntValue = (int)curValue;
1518 boolean more = true;
1519 if (delta == 0) {
1520 more = false;
1521 } else if (delta > 0) {
1522 if (curIntValue >= targetValue) {
1523 curValue = curIntValue = targetValue;
1524 more = false;
1525 }
1526 } else {
1527 if (curIntValue <= targetValue) {
1528 curValue = curIntValue = targetValue;
1529 more = false;
1530 }
1531 }
1532 //Log.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
1533 Power.setLightBrightness(mask, curIntValue);
1534 animating = more;
1535 if (!more) {
1536 if (mask == Power.SCREEN_LIGHT && curIntValue == Power.BRIGHTNESS_OFF) {
1537 screenOffFinishedAnimating(mOffBecauseOfUser);
1538 }
1539 }
1540 return more;
1541 }
1542 }
1543
1544 private class LightAnimator implements Runnable {
1545 public void run() {
1546 synchronized (mLocks) {
1547 long now = SystemClock.uptimeMillis();
1548 boolean more = mScreenBrightness.stepLocked();
1549 if (mKeyboardBrightness.stepLocked()) {
1550 more = true;
1551 }
1552 if (mButtonBrightness.stepLocked()) {
1553 more = true;
1554 }
1555 if (more) {
1556 mHandler.postAtTime(mLightAnimator, now+(1000/60));
1557 }
1558 }
1559 }
1560 }
1561
1562 private int getPreferredBrightness() {
1563 try {
1564 if (mScreenBrightnessOverride >= 0) {
1565 return mScreenBrightnessOverride;
1566 }
1567 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
1568 SCREEN_BRIGHTNESS);
1569 // Don't let applications turn the screen all the way off
1570 return Math.max(brightness, Power.BRIGHTNESS_DIM);
1571 } catch (SettingNotFoundException snfe) {
1572 return Power.BRIGHTNESS_ON;
1573 }
1574 }
1575
1576 boolean screenIsOn() {
1577 synchronized (mLocks) {
1578 return (mPowerState & SCREEN_ON_BIT) != 0;
1579 }
1580 }
1581
1582 boolean screenIsBright() {
1583 synchronized (mLocks) {
1584 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
1585 }
1586 }
1587
1588 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
1589 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1590 userActivity(time, noChangeLights, OTHER_EVENT, force);
1591 }
1592
1593 public void userActivity(long time, boolean noChangeLights) {
1594 userActivity(time, noChangeLights, OTHER_EVENT, false);
1595 }
1596
1597 public void userActivity(long time, boolean noChangeLights, int eventType) {
1598 userActivity(time, noChangeLights, eventType, false);
1599 }
1600
1601 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
1602 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1603
1604 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
1605 && !((eventType == OTHER_EVENT) || (eventType == BUTTON_EVENT))) {
1606 if (false) {
1607 Log.d(TAG, "dropping mPokey=0x" + Integer.toHexString(mPokey));
1608 }
1609 return;
1610 }
1611
1612 if (false) {
1613 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
1614 Log.d(TAG, "userActivity !!!");//, new RuntimeException());
1615 } else {
1616 Log.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
1617 }
1618 }
1619
1620 synchronized (mLocks) {
1621 if (mSpew) {
1622 Log.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
1623 + " mUserActivityAllowed=" + mUserActivityAllowed
1624 + " mUserState=0x" + Integer.toHexString(mUserState)
1625 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
1626 }
1627 if (mLastEventTime <= time || force) {
1628 mLastEventTime = time;
1629 if (mUserActivityAllowed || force) {
1630 // Only turn on button backlights if a button was pressed.
1631 if (eventType == BUTTON_EVENT) {
1632 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
1633 } else {
1634 // don't clear button/keyboard backlights when the screen is touched.
1635 mUserState |= SCREEN_BRIGHT;
1636 }
1637
1638 reactivateWakeLocksLocked();
1639 mWakeLockState = mLocks.gatherState();
1640 setPowerState(mUserState | mWakeLockState, noChangeLights, true);
1641 setTimeoutLocked(time, SCREEN_BRIGHT);
1642 }
1643 }
1644 }
1645 }
1646
1647 /**
1648 * The user requested that we go to sleep (probably with the power button).
1649 * This overrides all wake locks that are held.
1650 */
1651 public void goToSleep(long time)
1652 {
1653 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1654 synchronized (mLocks) {
1655 goToSleepLocked(time);
1656 }
1657 }
1658
1659 /**
1660 * Returns the time the screen has been on since boot, in millis.
1661 * @return screen on time
1662 */
1663 public long getScreenOnTime() {
1664 synchronized (mLocks) {
1665 if (mScreenOnStartTime == 0) {
1666 return mScreenOnTime;
1667 } else {
1668 return SystemClock.elapsedRealtime() - mScreenOnStartTime + mScreenOnTime;
1669 }
1670 }
1671 }
1672
1673 private void goToSleepLocked(long time) {
1674
1675 if (mLastEventTime <= time) {
1676 mLastEventTime = time;
1677 // cancel all of the wake locks
1678 mWakeLockState = SCREEN_OFF;
1679 int N = mLocks.size();
1680 int numCleared = 0;
1681 for (int i=0; i<N; i++) {
1682 WakeLock wl = mLocks.get(i);
1683 if (isScreenLock(wl.flags)) {
1684 mLocks.get(i).activated = false;
1685 numCleared++;
1686 }
1687 }
1688 EventLog.writeEvent(LOG_POWER_SLEEP_REQUESTED, numCleared);
1689 mUserState = SCREEN_OFF;
1690 setPowerState(SCREEN_OFF, false, true);
1691 cancelTimerLocked();
1692 }
1693 }
1694
1695 public long timeSinceScreenOn() {
1696 synchronized (mLocks) {
1697 if ((mPowerState & SCREEN_ON_BIT) != 0) {
1698 return 0;
1699 }
1700 return SystemClock.elapsedRealtime() - mScreenOffTime;
1701 }
1702 }
1703
1704 public void setKeyboardVisibility(boolean visible) {
1705 mKeyboardVisible = visible;
1706 }
1707
1708 /**
1709 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
1710 */
1711 public void enableUserActivity(boolean enabled) {
1712 synchronized (mLocks) {
1713 mUserActivityAllowed = enabled;
1714 mLastEventTime = SystemClock.uptimeMillis(); // we might need to pass this in
1715 }
1716 }
1717
1718 /** Sets the screen off timeouts:
1719 * mKeylightDelay
1720 * mDimDelay
1721 * mScreenOffDelay
1722 * */
1723 private void setScreenOffTimeoutsLocked() {
1724 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
1725 mKeylightDelay = mShortKeylightDelay; // Configurable via Gservices
1726 mDimDelay = -1;
1727 mScreenOffDelay = 0;
1728 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
1729 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
1730 mDimDelay = -1;
1731 mScreenOffDelay = 0;
1732 } else {
1733 int totalDelay = mTotalDelaySetting;
1734 mKeylightDelay = LONG_KEYLIGHT_DELAY;
1735 if (totalDelay < 0) {
1736 mScreenOffDelay = Integer.MAX_VALUE;
1737 } else if (mKeylightDelay < totalDelay) {
1738 // subtract the time that the keylight delay. This will give us the
1739 // remainder of the time that we need to sleep to get the accurate
1740 // screen off timeout.
1741 mScreenOffDelay = totalDelay - mKeylightDelay;
1742 } else {
1743 mScreenOffDelay = 0;
1744 }
1745 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
1746 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
1747 mScreenOffDelay = LONG_DIM_TIME;
1748 } else {
1749 mDimDelay = -1;
1750 }
1751 }
1752 if (mSpew) {
1753 Log.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
1754 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
1755 + " mDimScreen=" + mDimScreen);
1756 }
1757 }
1758
1759 /**
1760 * Refreshes cached Gservices settings. Called once on startup, and
1761 * on subsequent Settings.Gservices.CHANGED_ACTION broadcasts (see
1762 * GservicesChangedReceiver).
1763 */
1764 private void updateGservicesValues() {
1765 mShortKeylightDelay = Settings.Gservices.getInt(
1766 mContext.getContentResolver(),
1767 Settings.Gservices.SHORT_KEYLIGHT_DELAY_MS,
1768 SHORT_KEYLIGHT_DELAY_DEFAULT);
1769 // Log.i(TAG, "updateGservicesValues(): mShortKeylightDelay now " + mShortKeylightDelay);
1770 }
1771
1772 /**
1773 * Receiver for the Gservices.CHANGED_ACTION broadcast intent,
1774 * which tells us we need to refresh our cached Gservices settings.
1775 */
1776 private class GservicesChangedReceiver extends BroadcastReceiver {
1777 @Override
1778 public void onReceive(Context context, Intent intent) {
1779 // Log.i(TAG, "GservicesChangedReceiver.onReceive(): " + intent);
1780 updateGservicesValues();
1781 }
1782 }
1783
1784 private class LockList extends ArrayList<WakeLock>
1785 {
1786 void addLock(WakeLock wl)
1787 {
1788 int index = getIndex(wl.binder);
1789 if (index < 0) {
1790 this.add(wl);
1791 }
1792 }
1793
1794 WakeLock removeLock(IBinder binder)
1795 {
1796 int index = getIndex(binder);
1797 if (index >= 0) {
1798 return this.remove(index);
1799 } else {
1800 return null;
1801 }
1802 }
1803
1804 int getIndex(IBinder binder)
1805 {
1806 int N = this.size();
1807 for (int i=0; i<N; i++) {
1808 if (this.get(i).binder == binder) {
1809 return i;
1810 }
1811 }
1812 return -1;
1813 }
1814
1815 int gatherState()
1816 {
1817 int result = 0;
1818 int N = this.size();
1819 for (int i=0; i<N; i++) {
1820 WakeLock wl = this.get(i);
1821 if (wl.activated) {
1822 if (isScreenLock(wl.flags)) {
1823 result |= wl.minState;
1824 }
1825 }
1826 }
1827 return result;
1828 }
1829 }
1830
1831 void setPolicy(WindowManagerPolicy p) {
1832 synchronized (mLocks) {
1833 mPolicy = p;
1834 mLocks.notifyAll();
1835 }
1836 }
1837
1838 WindowManagerPolicy getPolicyLocked() {
1839 while (mPolicy == null || !mDoneBooting) {
1840 try {
1841 mLocks.wait();
1842 } catch (InterruptedException e) {
1843 // Ignore
1844 }
1845 }
1846 return mPolicy;
1847 }
1848
1849 void systemReady() {
1850 synchronized (mLocks) {
1851 Log.d(TAG, "system ready!");
1852 mDoneBooting = true;
1853 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
1854 updateWakeLockLocked();
1855 mLocks.notifyAll();
1856 }
1857 }
1858
1859 public void monitor() {
1860 synchronized (mLocks) { }
1861 }
1862}