blob: 5d32b74314c9a09f086c08a7b5109add99ef848e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import com.android.internal.app.IBatteryStats;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080020import com.android.internal.app.ShutdownThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.server.am.BatteryStatsService;
22
23import android.app.ActivityManagerNative;
24import android.app.IActivityManager;
25import android.content.BroadcastReceiver;
26import android.content.ContentQueryMap;
27import android.content.ContentResolver;
Amith Yamasani8b619832010-09-22 16:11:59 -070028import android.content.ContentValues;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070033import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080034import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.database.Cursor;
Mike Lockwoodbc706a02009-07-27 13:50:57 -070036import android.hardware.Sensor;
37import android.hardware.SensorEvent;
38import android.hardware.SensorEventListener;
39import android.hardware.SensorManager;
Amith Yamasani8b619832010-09-22 16:11:59 -070040import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.BatteryStats;
42import android.os.Binder;
Doug Zongker43866e02010-01-07 12:09:54 -080043import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.HandlerThread;
46import android.os.IBinder;
47import android.os.IPowerManager;
48import android.os.LocalPowerManager;
49import android.os.Power;
50import android.os.PowerManager;
51import android.os.Process;
52import android.os.RemoteException;
San Mehat14e69af2010-01-06 14:58:18 -080053import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.SystemClock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070055import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.provider.Settings.SettingNotFoundException;
57import android.provider.Settings;
58import android.util.EventLog;
59import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080060import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.WindowManagerPolicy;
62import static android.provider.Settings.System.DIM_SCREEN;
63import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
Dan Murphy951764b2009-08-27 14:59:03 -050064import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
Mike Lockwooddc3494e2009-10-14 21:17:09 -070065import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
67import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
68
69import java.io.FileDescriptor;
Doug Zongker50a21f42009-11-19 12:49:53 -080070import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import java.io.PrintWriter;
72import java.util.ArrayList;
73import java.util.HashMap;
74import java.util.Observable;
75import java.util.Observer;
76
Mike Lockwoodbc706a02009-07-27 13:50:57 -070077class PowerManagerService extends IPowerManager.Stub
Mike Lockwood8738e0c2009-10-04 08:44:47 -040078 implements LocalPowerManager, Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 private static final String TAG = "PowerManagerService";
81 static final String PARTIAL_NAME = "PowerManagerService";
82
83 private static final boolean LOG_PARTIAL_WL = false;
84
85 // Indicates whether touch-down cycles should be logged as part of the
86 // LOG_POWER_SCREEN_STATE log events
87 private static final boolean LOG_TOUCH_DOWNS = true;
88
89 private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK
90 | PowerManager.SCREEN_DIM_WAKE_LOCK
91 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Mike Lockwoodbc706a02009-07-27 13:50:57 -070092 | PowerManager.FULL_WAKE_LOCK
93 | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 // time since last state: time since last event:
Doug Zongker43866e02010-01-07 12:09:54 -080096 // The short keylight delay comes from secure settings; this is the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
98 private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec
99 private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec
100 private static final int LONG_DIM_TIME = 7000; // t+N-5 sec
101
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700102 // How long to wait to debounce light sensor changes.
Mike Lockwood9b8136922009-11-06 15:53:59 -0500103 private static final int LIGHT_SENSOR_DELAY = 2000;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700104
Mike Lockwood20f87d72009-11-05 16:08:51 -0500105 // For debouncing the proximity sensor.
106 private static final int PROXIMITY_SENSOR_DELAY = 1000;
107
Mike Lockwoodd20ea362009-09-15 00:13:38 -0400108 // trigger proximity if distance is less than 5 cm
109 private static final float PROXIMITY_THRESHOLD = 5.0f;
110
Doug Zongker43866e02010-01-07 12:09:54 -0800111 // Cached secure settings; see updateSettingsValues()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
113
Amith Yamasani8b619832010-09-22 16:11:59 -0700114 // Default timeout for screen off, if not found in settings database = 15 seconds.
115 private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15000;
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 // flags for setPowerState
118 private static final int SCREEN_ON_BIT = 0x00000001;
119 private static final int SCREEN_BRIGHT_BIT = 0x00000002;
120 private static final int BUTTON_BRIGHT_BIT = 0x00000004;
121 private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
122 private static final int BATTERY_LOW_BIT = 0x00000010;
123
124 // values for setPowerState
125
126 // SCREEN_OFF == everything off
127 private static final int SCREEN_OFF = 0x00000000;
128
129 // SCREEN_DIM == screen on, screen backlight dim
130 private static final int SCREEN_DIM = SCREEN_ON_BIT;
131
132 // SCREEN_BRIGHT == screen on, screen backlight bright
133 private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
134
135 // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
136 private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
137
138 // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
139 private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
140
141 // used for noChangeLights in setPowerState()
142 private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
143
144 static final boolean ANIMATE_SCREEN_LIGHTS = true;
145 static final boolean ANIMATE_BUTTON_LIGHTS = false;
146 static final boolean ANIMATE_KEYBOARD_LIGHTS = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int ANIM_STEPS = 60/4;
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400149 // Slower animation for autobrightness changes
150 static final int AUTOBRIGHTNESS_ANIM_STEPS = 60;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 // These magic numbers are the initial state of the LEDs at boot. Ideally
153 // we should read them from the driver, but our current hardware returns 0
154 // for the initial value. Oops!
155 static final int INITIAL_SCREEN_BRIGHTNESS = 255;
156 static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF;
157 static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private final int MY_UID;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700160 private final int MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162 private boolean mDoneBooting = false;
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500163 private boolean mBootCompleted = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private int mStayOnConditions = 0;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500165 private final int[] mBroadcastQueue = new int[] { -1, -1, -1 };
166 private final int[] mBroadcastWhy = new int[3];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private int mPartialCount = 0;
168 private int mPowerState;
Mike Lockwood435eb642009-12-03 08:40:18 -0500169 // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER,
170 // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
171 private int mScreenOffReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 private int mUserState;
173 private boolean mKeyboardVisible = false;
174 private boolean mUserActivityAllowed = true;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500175 private int mProximityWakeLockCount = 0;
176 private boolean mProximitySensorEnabled = false;
Mike Lockwood36fc3022009-08-25 16:49:06 -0700177 private boolean mProximitySensorActive = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -0500178 private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active
179 private long mLastProximityEventTime;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800180 private int mScreenOffTimeoutSetting;
181 private int mMaximumScreenOffTimeout = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private int mKeylightDelay;
183 private int mDimDelay;
184 private int mScreenOffDelay;
185 private int mWakeLockState;
186 private long mLastEventTime = 0;
187 private long mScreenOffTime;
188 private volatile WindowManagerPolicy mPolicy;
189 private final LockList mLocks = new LockList();
190 private Intent mScreenOffIntent;
191 private Intent mScreenOnIntent;
Mike Lockwood3a322132009-11-24 00:30:52 -0500192 private LightsService mLightsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 private Context mContext;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500194 private LightsService.Light mLcdLight;
195 private LightsService.Light mButtonLight;
196 private LightsService.Light mKeyboardLight;
197 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 private UnsynchronizedWakeLock mBroadcastWakeLock;
199 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
200 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
201 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500202 private UnsynchronizedWakeLock mProximityPartialLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private HandlerThread mHandlerThread;
204 private Handler mHandler;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500205 private final TimeoutTask mTimeoutTask = new TimeoutTask();
206 private final LightAnimator mLightAnimator = new LightAnimator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 private final BrightnessState mScreenBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700208 = new BrightnessState(SCREEN_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 private final BrightnessState mKeyboardBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700210 = new BrightnessState(KEYBOARD_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 private final BrightnessState mButtonBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700212 = new BrightnessState(BUTTON_BRIGHT_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -0700213 private boolean mStillNeedSleepNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 private boolean mIsPowered = false;
215 private IActivityManager mActivityService;
216 private IBatteryStats mBatteryStats;
217 private BatteryService mBatteryService;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700218 private SensorManager mSensorManager;
219 private Sensor mProximitySensor;
Mike Lockwood8738e0c2009-10-04 08:44:47 -0400220 private Sensor mLightSensor;
221 private boolean mLightSensorEnabled;
222 private float mLightSensorValue = -1;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500223 private int mHighestLightSensorValue = -1;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700224 private float mLightSensorPendingValue = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500225 private int mLightSensorScreenBrightness = -1;
226 private int mLightSensorButtonBrightness = -1;
227 private int mLightSensorKeyboardBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 private boolean mDimScreen = true;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500229 private boolean mIsDocked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 private long mNextTimeout;
231 private volatile int mPokey = 0;
232 private volatile boolean mPokeAwakeOnSet = false;
233 private volatile boolean mInitComplete = false;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500234 private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500235 // mLastScreenOnTime is the time the screen was last turned on
236 private long mLastScreenOnTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 private boolean mPreventScreenOn;
238 private int mScreenBrightnessOverride = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500239 private int mButtonBrightnessOverride = -1;
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400240 private boolean mUseSoftwareAutoBrightness;
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700241 private boolean mAutoBrightessEnabled;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700242 private int[] mAutoBrightnessLevels;
243 private int[] mLcdBacklightValues;
244 private int[] mButtonBacklightValues;
245 private int[] mKeyboardBacklightValues;
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500246 private int mLightSensorWarmupTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 // Used when logging number and duration of touch-down cycles
249 private long mTotalTouchDownTime;
250 private long mLastTouchDown;
251 private int mTouchCycles;
252
253 // could be either static or controllable at runtime
254 private static final boolean mSpew = false;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500255 private static final boolean mDebugProximitySensor = (true || mSpew);
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400256 private static final boolean mDebugLightSensor = (false || mSpew);
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700257
258 private native void nativeInit();
259 private native void nativeSetPowerState(boolean screenOn, boolean screenBright);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 /*
262 static PrintStream mLog;
263 static {
264 try {
265 mLog = new PrintStream("/data/power.log");
266 }
267 catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800268 android.util.Slog.e(TAG, "Life is hard", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270 }
271 static class Log {
272 static void d(String tag, String s) {
273 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800274 android.util.Slog.d(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 static void i(String tag, String s) {
277 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800278 android.util.Slog.i(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280 static void w(String tag, String s) {
281 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800282 android.util.Slog.w(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
284 static void e(String tag, String s) {
285 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800286 android.util.Slog.e(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 }
289 */
290
291 /**
292 * This class works around a deadlock between the lock in PowerManager.WakeLock
293 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
294 * mToken object so it can be accessed from any thread, but it calls into here
295 * with its lock held. This class is essentially a reimplementation of
296 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
297 * only call it with our own locks held.
298 */
299 private class UnsynchronizedWakeLock {
300 int mFlags;
301 String mTag;
302 IBinder mToken;
303 int mCount = 0;
304 boolean mRefCounted;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500305 boolean mHeld;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
307 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
308 mFlags = flags;
309 mTag = tag;
310 mToken = new Binder();
311 mRefCounted = refCounted;
312 }
313
314 public void acquire() {
315 if (!mRefCounted || mCount++ == 0) {
316 long ident = Binder.clearCallingIdentity();
317 try {
318 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700319 MY_UID, MY_PID, mTag, null);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500320 mHeld = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 } finally {
322 Binder.restoreCallingIdentity(ident);
323 }
324 }
325 }
326
327 public void release() {
328 if (!mRefCounted || --mCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500329 PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500330 mHeld = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332 if (mCount < 0) {
333 throw new RuntimeException("WakeLock under-locked " + mTag);
334 }
335 }
336
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500337 public boolean isHeld()
338 {
339 return mHeld;
340 }
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 public String toString() {
343 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500344 + " mCount=" + mCount + " mHeld=" + mHeld + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346 }
347
348 private final class BatteryReceiver extends BroadcastReceiver {
349 @Override
350 public void onReceive(Context context, Intent intent) {
351 synchronized (mLocks) {
352 boolean wasPowered = mIsPowered;
353 mIsPowered = mBatteryService.isPowered();
354
355 if (mIsPowered != wasPowered) {
356 // update mStayOnWhilePluggedIn wake lock
357 updateWakeLockLocked();
358
359 // treat plugging and unplugging the devices as a user activity.
360 // users find it disconcerting when they unplug the device
361 // and it shuts off right away.
Mike Lockwood84a89342010-03-01 21:28:58 -0500362 // to avoid turning on the screen when unplugging, we only trigger
363 // user activity when screen was already on.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 // temporarily set mUserActivityAllowed to true so this will work
365 // even when the keyguard is on.
366 synchronized (mLocks) {
Mike Lockwood84a89342010-03-01 21:28:58 -0500367 if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0) {
368 forceUserActivityLocked();
369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372 }
373 }
374 }
375
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500376 private final class BootCompletedReceiver extends BroadcastReceiver {
377 @Override
378 public void onReceive(Context context, Intent intent) {
379 bootCompleted();
380 }
381 }
382
Mike Lockwoodb2865412010-02-02 22:40:33 -0500383 private final class DockReceiver extends BroadcastReceiver {
384 @Override
385 public void onReceive(Context context, Intent intent) {
386 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
387 Intent.EXTRA_DOCK_STATE_UNDOCKED);
388 dockStateChanged(state);
389 }
390 }
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Set the setting that determines whether the device stays on when plugged in.
394 * The argument is a bit string, with each bit specifying a power source that,
395 * when the device is connected to that source, causes the device to stay on.
396 * See {@link android.os.BatteryManager} for the list of power sources that
397 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
398 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
399 * @param val an {@code int} containing the bits that specify which power sources
400 * should cause the device to stay on.
401 */
402 public void setStayOnSetting(int val) {
403 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
404 Settings.System.putInt(mContext.getContentResolver(),
405 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
406 }
407
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800408 public void setMaximumScreenOffTimeount(int timeMs) {
409 mContext.enforceCallingOrSelfPermission(
410 android.Manifest.permission.WRITE_SECURE_SETTINGS, null);
411 synchronized (mLocks) {
412 mMaximumScreenOffTimeout = timeMs;
413 // recalculate everything
414 setScreenOffTimeoutsLocked();
415 }
416 }
417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 private class SettingsObserver implements Observer {
Amith Yamasani8b619832010-09-22 16:11:59 -0700419 private int getInt(String name, int defValue) {
420 ContentValues values = mSettings.getValues(name);
421 Integer iVal = values != null ? values.getAsInteger(Settings.System.VALUE) : null;
422 return iVal != null ? iVal : defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424
425 public void update(Observable o, Object arg) {
426 synchronized (mLocks) {
Amith Yamasani8b619832010-09-22 16:11:59 -0700427 // STAY_ON_WHILE_PLUGGED_IN, default to when plugged into AC
428 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN,
429 BatteryManager.BATTERY_PLUGGED_AC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 updateWakeLockLocked();
431
Amith Yamasani8b619832010-09-22 16:11:59 -0700432 // SCREEN_OFF_TIMEOUT, default to 15 seconds
433 mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
435 // DIM_SCREEN
436 //mDimScreen = getInt(DIM_SCREEN) != 0;
437
Amith Yamasani8b619832010-09-22 16:11:59 -0700438 // SCREEN_BRIGHTNESS_MODE, default to manual
439 setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE,
440 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL));
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 // recalculate everything
443 setScreenOffTimeoutsLocked();
444 }
445 }
446 }
447
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700448 PowerManagerService() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 // Hack to get our uid... should have a func for this.
450 long token = Binder.clearCallingIdentity();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700451 MY_UID = Process.myUid();
452 MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 Binder.restoreCallingIdentity(token);
454
455 // XXX remove this when the kernel doesn't timeout wake locks
456 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
457
458 // assume nothing is on yet
459 mUserState = mPowerState = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 // Add ourself to the Watchdog monitors.
462 Watchdog.getInstance().addMonitor(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464
465 private ContentQueryMap mSettings;
466
Mike Lockwood3a322132009-11-24 00:30:52 -0500467 void init(Context context, LightsService lights, IActivityManager activity,
The Android Open Source Project10592532009-03-18 17:39:46 -0700468 BatteryService battery) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500469 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext = context;
471 mActivityService = activity;
472 mBatteryStats = BatteryStatsService.getService();
473 mBatteryService = battery;
474
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500475 mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT);
476 mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS);
477 mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD);
478 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 mHandlerThread = new HandlerThread("PowerManagerService") {
481 @Override
482 protected void onLooperPrepared() {
483 super.onLooperPrepared();
484 initInThread();
485 }
486 };
487 mHandlerThread.start();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 synchronized (mHandlerThread) {
490 while (!mInitComplete) {
491 try {
492 mHandlerThread.wait();
493 } catch (InterruptedException e) {
494 // Ignore
495 }
496 }
497 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700498
499 nativeInit();
500 synchronized (mLocks) {
501 updateNativePowerStateLocked();
502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 void initInThread() {
506 mHandler = new Handler();
507
508 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700509 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
511 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
512 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
513 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
514 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
515 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500516 mProximityPartialLock = new UnsynchronizedWakeLock(
517 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
520 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
521 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
522 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
523
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700524 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400525
526 // read settings for auto-brightness
527 mUseSoftwareAutoBrightness = resources.getBoolean(
528 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400529 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700530 mAutoBrightnessLevels = resources.getIntArray(
531 com.android.internal.R.array.config_autoBrightnessLevels);
532 mLcdBacklightValues = resources.getIntArray(
533 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
534 mButtonBacklightValues = resources.getIntArray(
535 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
536 mKeyboardBacklightValues = resources.getIntArray(
537 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500538 mLightSensorWarmupTime = resources.getInteger(
539 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700540 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700541
542 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
544 "(" + Settings.System.NAME + "=?) or ("
545 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700546 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 + Settings.System.NAME + "=?)",
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700548 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN,
549 SCREEN_BRIGHTNESS_MODE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 null);
551 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
552 SettingsObserver settingsObserver = new SettingsObserver();
553 mSettings.addObserver(settingsObserver);
554
555 // pretend that the settings changed so we will get their initial state
556 settingsObserver.update(mSettings, null);
557
558 // register for the battery changed notifications
559 IntentFilter filter = new IntentFilter();
560 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
561 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500562 filter = new IntentFilter();
563 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
564 mContext.registerReceiver(new BootCompletedReceiver(), filter);
Mike Lockwoodb2865412010-02-02 22:40:33 -0500565 filter = new IntentFilter();
566 filter.addAction(Intent.ACTION_DOCK_EVENT);
567 mContext.registerReceiver(new DockReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
Doug Zongker43866e02010-01-07 12:09:54 -0800569 // Listen for secure settings changes
570 mContext.getContentResolver().registerContentObserver(
571 Settings.Secure.CONTENT_URI, true,
572 new ContentObserver(new Handler()) {
573 public void onChange(boolean selfChange) {
574 updateSettingsValues();
575 }
576 });
577 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 synchronized (mHandlerThread) {
580 mInitComplete = true;
581 mHandlerThread.notifyAll();
582 }
583 }
584
585 private class WakeLock implements IBinder.DeathRecipient
586 {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700587 WakeLock(int f, IBinder b, String t, int u, int p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 super();
589 flags = f;
590 binder = b;
591 tag = t;
592 uid = u == MY_UID ? Process.SYSTEM_UID : u;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700593 pid = p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 if (u != MY_UID || (
595 !"KEEP_SCREEN_ON_FLAG".equals(tag)
596 && !"KeyInputQueue".equals(tag))) {
597 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
598 ? BatteryStats.WAKE_TYPE_PARTIAL
599 : BatteryStats.WAKE_TYPE_FULL;
600 } else {
601 monitorType = -1;
602 }
603 try {
604 b.linkToDeath(this, 0);
605 } catch (RemoteException e) {
606 binderDied();
607 }
608 }
609 public void binderDied() {
610 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500611 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613 }
614 final int flags;
615 final IBinder binder;
616 final String tag;
617 final int uid;
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400618 final int pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 final int monitorType;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700620 WorkSource ws;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 boolean activated = true;
622 int minState;
623 }
624
625 private void updateWakeLockLocked() {
626 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
627 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
628 mStayOnWhilePluggedInScreenDimLock.acquire();
629 mStayOnWhilePluggedInPartialLock.acquire();
630 } else {
631 mStayOnWhilePluggedInScreenDimLock.release();
632 mStayOnWhilePluggedInPartialLock.release();
633 }
634 }
635
636 private boolean isScreenLock(int flags)
637 {
638 int n = flags & LOCK_MASK;
639 return n == PowerManager.FULL_WAKE_LOCK
640 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
641 || n == PowerManager.SCREEN_DIM_WAKE_LOCK;
642 }
643
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700644 void enforceWakeSourcePermission(int uid, int pid) {
645 if (uid == Process.myUid()) {
646 return;
647 }
648 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
649 pid, uid, null);
650 }
651
652 public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 int uid = Binder.getCallingUid();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700654 int pid = Binder.getCallingPid();
Michael Chane96440f2009-05-06 10:27:36 -0700655 if (uid != Process.myUid()) {
656 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
657 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700658 if (ws != null) {
659 enforceWakeSourcePermission(uid, pid);
660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 long ident = Binder.clearCallingIdentity();
662 try {
663 synchronized (mLocks) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700664 acquireWakeLockLocked(flags, lock, uid, pid, tag, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 } finally {
667 Binder.restoreCallingIdentity(ident);
668 }
669 }
670
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700671 void noteStartWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700672 if (wl.monitorType >= 0) {
673 long origId = Binder.clearCallingIdentity();
674 try {
675 if (ws != null) {
676 mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
677 wl.monitorType);
678 } else {
679 mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
680 }
681 } catch (RemoteException e) {
682 // Ignore
683 } finally {
684 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700685 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700686 }
687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700689 void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700690 if (wl.monitorType >= 0) {
691 long origId = Binder.clearCallingIdentity();
692 try {
693 if (ws != null) {
694 mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
695 wl.monitorType);
696 } else {
697 mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
698 }
699 } catch (RemoteException e) {
700 // Ignore
701 } finally {
702 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700703 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700704 }
705 }
706
707 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
708 WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700713 if (ws != null && ws.size() == 0) {
714 ws = null;
715 }
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 int index = mLocks.getIndex(lock);
718 WakeLock wl;
719 boolean newlock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700720 boolean diffsource;
721 WorkSource oldsource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 if (index < 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700723 wl = new WakeLock(flags, lock, tag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 switch (wl.flags & LOCK_MASK)
725 {
726 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500727 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400728 wl.minState = SCREEN_BRIGHT;
729 } else {
730 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 break;
733 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
734 wl.minState = SCREEN_BRIGHT;
735 break;
736 case PowerManager.SCREEN_DIM_WAKE_LOCK:
737 wl.minState = SCREEN_DIM;
738 break;
739 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700740 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 break;
742 default:
743 // just log and bail. we're in the server, so don't
744 // throw an exception.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800745 Slog.e(TAG, "bad wakelock type for lock '" + tag + "' "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 + " flags=" + flags);
747 return;
748 }
749 mLocks.addLock(wl);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700750 if (ws != null) {
751 wl.ws = new WorkSource(ws);
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 newlock = true;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700754 diffsource = false;
755 oldsource = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 } else {
757 wl = mLocks.get(index);
758 newlock = false;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700759 oldsource = wl.ws;
760 if (oldsource != null) {
761 if (ws == null) {
762 wl.ws = null;
763 diffsource = true;
764 } else {
765 diffsource = oldsource.diff(ws);
766 }
767 } else if (ws != null) {
768 diffsource = true;
769 } else {
770 diffsource = false;
771 }
772 if (diffsource) {
773 wl.ws = new WorkSource(ws);
774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776 if (isScreenLock(flags)) {
777 // if this causes a wakeup, we reactivate all of the locks and
778 // set it to whatever they want. otherwise, we modulate that
779 // by the current state so we never turn it more on than
780 // it already is.
781 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
Michael Chane96440f2009-05-06 10:27:36 -0700782 int oldWakeLockState = mWakeLockState;
783 mWakeLockState = mLocks.reactivateScreenLocksLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800785 Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
Michael Chane96440f2009-05-06 10:27:36 -0700786 + " mWakeLockState=0x"
787 + Integer.toHexString(mWakeLockState)
788 + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 } else {
791 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 + " mLocks.gatherState()=0x"
794 + Integer.toHexString(mLocks.gatherState())
795 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
796 }
797 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
798 }
799 setPowerState(mWakeLockState | mUserState);
800 }
801 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
802 if (newlock) {
803 mPartialCount++;
804 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807 }
808 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700809 } else if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500810 mProximityWakeLockCount++;
811 if (mProximityWakeLockCount == 1) {
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700812 enableProximityLockLocked();
813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700816 if (diffsource) {
817 // If the lock sources have changed, need to first release the
818 // old ones.
819 noteStopWakeLocked(wl, oldsource);
820 }
821 if (newlock || diffsource) {
822 noteStartWakeLocked(wl, ws);
823 }
824 }
825
826 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
827 int uid = Binder.getCallingUid();
828 int pid = Binder.getCallingPid();
829 if (ws != null && ws.size() == 0) {
830 ws = null;
831 }
832 if (ws != null) {
833 enforceWakeSourcePermission(uid, pid);
834 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700835 synchronized (mLocks) {
836 int index = mLocks.getIndex(lock);
837 if (index < 0) {
838 throw new IllegalArgumentException("Wake lock not active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700840 WakeLock wl = mLocks.get(index);
841 WorkSource oldsource = wl.ws;
842 wl.ws = ws != null ? new WorkSource(ws) : null;
843 noteStopWakeLocked(wl, oldsource);
844 noteStartWakeLocked(wl, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846 }
847
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500848 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700849 int uid = Binder.getCallingUid();
850 if (uid != Process.myUid()) {
851 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853
854 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500855 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857 }
858
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500859 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 WakeLock wl = mLocks.removeLock(lock);
861 if (wl == null) {
862 return;
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800866 Slog.d(TAG, "releaseWakeLock flags=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
868 }
869
870 if (isScreenLock(wl.flags)) {
871 mWakeLockState = mLocks.gatherState();
872 // goes in the middle to reduce flicker
873 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
874 userActivity(SystemClock.uptimeMillis(), false);
875 }
876 setPowerState(mWakeLockState | mUserState);
877 }
878 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
879 mPartialCount--;
880 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 Power.releaseWakeLock(PARTIAL_NAME);
883 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700884 } else if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500885 mProximityWakeLockCount--;
886 if (mProximityWakeLockCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500887 if (mProximitySensorActive &&
888 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500889 // wait for proximity sensor to go negative before disabling sensor
890 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800891 Slog.d(TAG, "waiting for proximity sensor to go negative");
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500892 }
893 } else {
894 disableProximityLockLocked();
895 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898 // Unlink the lock from the binder.
899 wl.binder.unlinkToDeath(wl, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
Dianne Hackborn70be1672010-09-14 11:13:03 -0700901 noteStopWakeLocked(wl, wl.ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 private class PokeLock implements IBinder.DeathRecipient
905 {
906 PokeLock(int p, IBinder b, String t) {
907 super();
908 this.pokey = p;
909 this.binder = b;
910 this.tag = t;
911 try {
912 b.linkToDeath(this, 0);
913 } catch (RemoteException e) {
914 binderDied();
915 }
916 }
917 public void binderDied() {
918 setPokeLock(0, this.binder, this.tag);
919 }
920 int pokey;
921 IBinder binder;
922 String tag;
923 boolean awakeOnSet;
924 }
925
926 public void setPokeLock(int pokey, IBinder token, String tag) {
927 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
928 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 return;
931 }
932
933 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
934 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
935 + " and POKE_LOCK_MEDIUM_TIMEOUT");
936 }
937
938 synchronized (mLocks) {
939 if (pokey != 0) {
940 PokeLock p = mPokeLocks.get(token);
941 int oldPokey = 0;
942 if (p != null) {
943 oldPokey = p.pokey;
944 p.pokey = pokey;
945 } else {
946 p = new PokeLock(pokey, token, tag);
947 mPokeLocks.put(token, p);
948 }
949 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
950 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
951 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
952 p.awakeOnSet = true;
953 }
954 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700955 PokeLock rLock = mPokeLocks.remove(token);
956 if (rLock != null) {
957 token.unlinkToDeath(rLock, 0);
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960
961 int oldPokey = mPokey;
962 int cumulative = 0;
963 boolean oldAwakeOnSet = mPokeAwakeOnSet;
964 boolean awakeOnSet = false;
965 for (PokeLock p: mPokeLocks.values()) {
966 cumulative |= p.pokey;
967 if (p.awakeOnSet) {
968 awakeOnSet = true;
969 }
970 }
971 mPokey = cumulative;
972 mPokeAwakeOnSet = awakeOnSet;
973
974 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
975 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (oldCumulativeTimeout != newCumulativeTimeout) {
978 setScreenOffTimeoutsLocked();
979 // reset the countdown timer, but use the existing nextState so it doesn't
980 // change anything
981 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
982 }
983 }
984 }
985
986 private static String lockType(int type)
987 {
988 switch (type)
989 {
990 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700991 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700993 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700995 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700997 return "PARTIAL_WAKE_LOCK ";
998 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
999 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 default:
David Brown251faa62009-08-02 22:04:36 -07001001 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 }
1004
1005 private static String dumpPowerState(int state) {
1006 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
1007 ? "KEYBOARD_BRIGHT_BIT " : "")
1008 + (((state & SCREEN_BRIGHT_BIT) != 0)
1009 ? "SCREEN_BRIGHT_BIT " : "")
1010 + (((state & SCREEN_ON_BIT) != 0)
1011 ? "SCREEN_ON_BIT " : "")
1012 + (((state & BATTERY_LOW_BIT) != 0)
1013 ? "BATTERY_LOW_BIT " : "");
1014 }
1015
1016 @Override
1017 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1018 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1019 != PackageManager.PERMISSION_GRANTED) {
1020 pw.println("Permission Denial: can't dump PowerManager from from pid="
1021 + Binder.getCallingPid()
1022 + ", uid=" + Binder.getCallingUid());
1023 return;
1024 }
1025
1026 long now = SystemClock.uptimeMillis();
1027
Mike Lockwoodca44df82010-02-25 13:48:49 -05001028 synchronized (mLocks) {
1029 pw.println("Power Manager State:");
1030 pw.println(" mIsPowered=" + mIsPowered
1031 + " mPowerState=" + mPowerState
1032 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
1033 + " ms");
1034 pw.println(" mPartialCount=" + mPartialCount);
1035 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
1036 pw.println(" mUserState=" + dumpPowerState(mUserState));
1037 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
1038 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
1039 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
1040 + " " + ((mNextTimeout-now)/1000) + "s from now");
1041 pw.println(" mDimScreen=" + mDimScreen
1042 + " mStayOnConditions=" + mStayOnConditions);
1043 pw.println(" mScreenOffReason=" + mScreenOffReason
1044 + " mUserState=" + mUserState);
1045 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
1046 + ',' + mBroadcastQueue[2] + "}");
1047 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
1048 + ',' + mBroadcastWhy[2] + "}");
1049 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
1050 pw.println(" mKeyboardVisible=" + mKeyboardVisible
1051 + " mUserActivityAllowed=" + mUserActivityAllowed);
1052 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
1053 + " mScreenOffDelay=" + mScreenOffDelay);
1054 pw.println(" mPreventScreenOn=" + mPreventScreenOn
1055 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
1056 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
1057 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
1058 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
1059 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
1060 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
1061 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
1062 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
1063 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
1064 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
1065 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
1066 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1067 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
1068 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
1069 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
1070 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
1071 pw.println(" mLightSensorValue=" + mLightSensorValue
1072 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
1073 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
1074 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
1075 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
1076 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
1077 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
1078 mScreenBrightness.dump(pw, " mScreenBrightness: ");
1079 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
1080 mButtonBrightness.dump(pw, " mButtonBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
Mike Lockwoodca44df82010-02-25 13:48:49 -05001082 int N = mLocks.size();
1083 pw.println();
1084 pw.println("mLocks.size=" + N + ":");
1085 for (int i=0; i<N; i++) {
1086 WakeLock wl = mLocks.get(i);
1087 String type = lockType(wl.flags & LOCK_MASK);
1088 String acquireCausesWakeup = "";
1089 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
1090 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
1091 }
1092 String activated = "";
1093 if (wl.activated) {
1094 activated = " activated";
1095 }
1096 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001097 + activated + " (minState=" + wl.minState + ", uid=" + wl.uid
1098 + ", pid=" + wl.pid + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Mike Lockwoodca44df82010-02-25 13:48:49 -05001100
1101 pw.println();
1102 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
1103 for (PokeLock p: mPokeLocks.values()) {
1104 pw.println(" poke lock '" + p.tag + "':"
1105 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
1106 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
1107 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
1108 ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
1109 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1110 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1111 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1112 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
Mike Lockwoodca44df82010-02-25 13:48:49 -05001115 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118
Joe Onorato7999bff2010-07-24 11:50:05 -04001119 private void setTimeoutLocked(long now, int nextState) {
1120 setTimeoutLocked(now, -1, nextState);
1121 }
1122
1123 // If they gave a timeoutOverride it is the number of seconds
1124 // to screen-off. Figure out where in the countdown cycle we
1125 // should jump to.
Joe Onorato797e6882010-08-26 14:46:01 -04001126 private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
1127 long timeoutOverride = originalTimeoutOverride;
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001128 if (mBootCompleted) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001129 synchronized (mLocks) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001130 long when = 0;
1131 if (timeoutOverride <= 0) {
1132 switch (nextState)
1133 {
1134 case SCREEN_BRIGHT:
1135 when = now + mKeylightDelay;
1136 break;
1137 case SCREEN_DIM:
1138 if (mDimDelay >= 0) {
1139 when = now + mDimDelay;
Andreas Huber84047bc2010-07-27 16:49:10 -07001140 break;
Joe Onorato7999bff2010-07-24 11:50:05 -04001141 } else {
1142 Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1143 }
1144 case SCREEN_OFF:
1145 synchronized (mLocks) {
1146 when = now + mScreenOffDelay;
1147 }
1148 break;
Andreas Huber84047bc2010-07-27 16:49:10 -07001149 default:
1150 when = now;
1151 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001153 } else {
1154 override: {
1155 if (timeoutOverride <= mScreenOffDelay) {
1156 when = now + timeoutOverride;
1157 nextState = SCREEN_OFF;
1158 break override;
1159 }
1160 timeoutOverride -= mScreenOffDelay;
1161
1162 if (mDimDelay >= 0) {
1163 if (timeoutOverride <= mDimDelay) {
1164 when = now + timeoutOverride;
1165 nextState = SCREEN_DIM;
1166 break override;
1167 }
1168 timeoutOverride -= mDimDelay;
1169 }
1170
1171 when = now + timeoutOverride;
1172 nextState = SCREEN_BRIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001174 }
1175 if (mSpew) {
1176 Slog.d(TAG, "setTimeoutLocked now=" + now
1177 + " timeoutOverride=" + timeoutOverride
1178 + " nextState=" + nextState + " when=" + when);
1179 }
Joe Onorato797e6882010-08-26 14:46:01 -04001180
1181 mHandler.removeCallbacks(mTimeoutTask);
1182 mTimeoutTask.nextState = nextState;
1183 mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
1184 ? (originalTimeoutOverride - timeoutOverride)
1185 : -1;
Joe Onorato7999bff2010-07-24 11:50:05 -04001186 mHandler.postAtTime(mTimeoutTask, when);
1187 mNextTimeout = when; // for debugging
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
1191
1192 private void cancelTimerLocked()
1193 {
1194 mHandler.removeCallbacks(mTimeoutTask);
1195 mTimeoutTask.nextState = -1;
1196 }
1197
1198 private class TimeoutTask implements Runnable
1199 {
1200 int nextState; // access should be synchronized on mLocks
Joe Onorato797e6882010-08-26 14:46:01 -04001201 long remainingTimeoutOverride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 public void run()
1203 {
1204 synchronized (mLocks) {
1205 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208
1209 if (nextState == -1) {
1210 return;
1211 }
1212
1213 mUserState = this.nextState;
1214 setPowerState(this.nextState | mWakeLockState);
1215
1216 long now = SystemClock.uptimeMillis();
1217
1218 switch (this.nextState)
1219 {
1220 case SCREEN_BRIGHT:
1221 if (mDimDelay >= 0) {
Joe Onorato797e6882010-08-26 14:46:01 -04001222 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 break;
1224 }
1225 case SCREEN_DIM:
Joe Onorato797e6882010-08-26 14:46:01 -04001226 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 break;
1228 }
1229 }
1230 }
1231 }
1232
1233 private void sendNotificationLocked(boolean on, int why)
1234 {
Joe Onorato64c62ba2009-03-24 20:13:57 -07001235 if (!on) {
1236 mStillNeedSleepNotification = false;
1237 }
1238
Joe Onorato128e7292009-03-24 18:41:31 -07001239 // Add to the queue.
1240 int index = 0;
1241 while (mBroadcastQueue[index] != -1) {
1242 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
Joe Onorato128e7292009-03-24 18:41:31 -07001244 mBroadcastQueue[index] = on ? 1 : 0;
1245 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246
Joe Onorato128e7292009-03-24 18:41:31 -07001247 // If we added it position 2, then there is a pair that can be stripped.
1248 // If we added it position 1 and we're turning the screen off, we can strip
1249 // the pair and do nothing, because the screen is already off, and therefore
1250 // keyguard has already been enabled.
1251 // However, if we added it at position 1 and we're turning it on, then position
1252 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1253 // on, so have to run the queue then.
1254 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001255 // While we're collapsing them, if it's going off, and the new reason
1256 // is more significant than the first, then use the new one.
1257 if (!on && mBroadcastWhy[0] > why) {
1258 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001259 }
1260 mBroadcastQueue[0] = on ? 1 : 0;
1261 mBroadcastQueue[1] = -1;
1262 mBroadcastQueue[2] = -1;
Mike Lockwood9c90a372010-04-13 15:40:27 -04001263 mBroadcastWakeLock.release();
1264 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001265 index = 0;
1266 }
1267 if (index == 1 && !on) {
1268 mBroadcastQueue[0] = -1;
1269 mBroadcastQueue[1] = -1;
1270 index = -1;
1271 // The wake lock was being held, but we're not actually going to do any
1272 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001275 }
1276
1277 // Now send the message.
1278 if (index >= 0) {
1279 // Acquire the broadcast wake lock before changing the power
1280 // state. It will be release after the broadcast is sent.
1281 // We always increment the ref count for each notification in the queue
1282 // and always decrement when that notification is handled.
1283 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001285 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 }
1288
1289 private Runnable mNotificationTask = new Runnable()
1290 {
1291 public void run()
1292 {
Joe Onorato128e7292009-03-24 18:41:31 -07001293 while (true) {
1294 int value;
1295 int why;
1296 WindowManagerPolicy policy;
1297 synchronized (mLocks) {
1298 value = mBroadcastQueue[0];
1299 why = mBroadcastWhy[0];
1300 for (int i=0; i<2; i++) {
1301 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1302 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1303 }
1304 policy = getPolicyLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
Joe Onorato128e7292009-03-24 18:41:31 -07001306 if (value == 1) {
1307 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
Joe Onorato128e7292009-03-24 18:41:31 -07001309 policy.screenTurnedOn();
1310 try {
1311 ActivityManagerNative.getDefault().wakingUp();
1312 } catch (RemoteException e) {
1313 // ignore it
1314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315
Joe Onorato128e7292009-03-24 18:41:31 -07001316 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001317 Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
Joe Onorato128e7292009-03-24 18:41:31 -07001318 }
1319 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1320 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1321 mScreenOnBroadcastDone, mHandler, 0, null, null);
1322 } else {
1323 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001325 mBroadcastWakeLock.mCount);
1326 mBroadcastWakeLock.release();
1327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 }
1329 }
Joe Onorato128e7292009-03-24 18:41:31 -07001330 else if (value == 0) {
1331 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
Joe Onorato128e7292009-03-24 18:41:31 -07001333 policy.screenTurnedOff(why);
1334 try {
1335 ActivityManagerNative.getDefault().goingToSleep();
1336 } catch (RemoteException e) {
1337 // ignore it.
1338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
Joe Onorato128e7292009-03-24 18:41:31 -07001340 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1341 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1342 mScreenOffBroadcastDone, mHandler, 0, null, null);
1343 } else {
1344 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001346 mBroadcastWakeLock.mCount);
1347 mBroadcastWakeLock.release();
1348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350 }
Joe Onorato128e7292009-03-24 18:41:31 -07001351 else {
1352 // If we're in this case, then this handler is running for a previous
1353 // paired transaction. mBroadcastWakeLock will already have been released.
1354 break;
1355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
1357 }
1358 };
1359
1360 long mScreenOnStart;
1361 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1362 public void onReceive(Context context, Intent intent) {
1363 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1366 mBroadcastWakeLock.release();
1367 }
1368 }
1369 };
1370
1371 long mScreenOffStart;
1372 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1373 public void onReceive(Context context, Intent intent) {
1374 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1377 mBroadcastWakeLock.release();
1378 }
1379 }
1380 };
1381
1382 void logPointerUpEvent() {
1383 if (LOG_TOUCH_DOWNS) {
1384 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1385 mLastTouchDown = 0;
1386 }
1387 }
1388
1389 void logPointerDownEvent() {
1390 if (LOG_TOUCH_DOWNS) {
1391 // If we are not already timing a down/up sequence
1392 if (mLastTouchDown == 0) {
1393 mLastTouchDown = SystemClock.elapsedRealtime();
1394 mTouchCycles++;
1395 }
1396 }
1397 }
1398
1399 /**
1400 * Prevents the screen from turning on even if it *should* turn on due
1401 * to a subsequent full wake lock being acquired.
1402 * <p>
1403 * This is a temporary hack that allows an activity to "cover up" any
1404 * display glitches that happen during the activity's startup
1405 * sequence. (Specifically, this API was added to work around a
1406 * cosmetic bug in the "incoming call" sequence, where the lock screen
1407 * would flicker briefly before the incoming call UI became visible.)
1408 * TODO: There ought to be a more elegant way of doing this,
1409 * probably by having the PowerManager and ActivityManager
1410 * work together to let apps specify that the screen on/off
1411 * state should be synchronized with the Activity lifecycle.
1412 * <p>
1413 * Note that calling preventScreenOn(true) will NOT turn the screen
1414 * off if it's currently on. (This API only affects *future*
1415 * acquisitions of full wake locks.)
1416 * But calling preventScreenOn(false) WILL turn the screen on if
1417 * it's currently off because of a prior preventScreenOn(true) call.
1418 * <p>
1419 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1420 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1421 * call doesn't occur within 5 seconds, we'll turn the screen back on
1422 * ourselves (and log a warning about it); this prevents a buggy app
1423 * from disabling the screen forever.)
1424 * <p>
1425 * TODO: this feature should really be controlled by a new type of poke
1426 * lock (rather than an IPowerManager call).
1427 */
1428 public void preventScreenOn(boolean prevent) {
1429 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1430
1431 synchronized (mLocks) {
1432 if (prevent) {
1433 // First of all, grab a partial wake lock to
1434 // make sure the CPU stays on during the entire
1435 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1436 mPreventScreenOnPartialLock.acquire();
1437
1438 // Post a forceReenableScreen() call (for 5 seconds in the
1439 // future) to make sure the matching preventScreenOn(false) call
1440 // has happened by then.
1441 mHandler.removeCallbacks(mForceReenableScreenTask);
1442 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1443
1444 // Finally, set the flag that prevents the screen from turning on.
1445 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001446 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 mPreventScreenOn = true;
1448 } else {
1449 // (Re)enable the screen.
1450 mPreventScreenOn = false;
1451
1452 // We're "undoing" a the prior preventScreenOn(true) call, so we
1453 // no longer need the 5-second safeguard.
1454 mHandler.removeCallbacks(mForceReenableScreenTask);
1455
1456 // Forcibly turn on the screen if it's supposed to be on. (This
1457 // handles the case where the screen is currently off because of
1458 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001459 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001461 Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1463 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001464 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (err != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001466 Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 }
1468 }
1469
1470 // Release the partial wake lock that we held during the
1471 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1472 mPreventScreenOnPartialLock.release();
1473 }
1474 }
1475 }
1476
1477 public void setScreenBrightnessOverride(int brightness) {
1478 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1479
Mike Lockwoodf527c712010-06-10 14:12:33 -04001480 if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 synchronized (mLocks) {
1482 if (mScreenBrightnessOverride != brightness) {
1483 mScreenBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001484 if (isScreenOn()) {
1485 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488 }
1489 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001490
1491 public void setButtonBrightnessOverride(int brightness) {
1492 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1493
Mike Lockwoodf527c712010-06-10 14:12:33 -04001494 if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness);
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001495 synchronized (mLocks) {
1496 if (mButtonBrightnessOverride != brightness) {
1497 mButtonBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001498 if (isScreenOn()) {
1499 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1500 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001501 }
1502 }
1503 }
1504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
1506 * Sanity-check that gets called 5 seconds after any call to
1507 * preventScreenOn(true). This ensures that the original call
1508 * is followed promptly by a call to preventScreenOn(false).
1509 */
1510 private void forceReenableScreen() {
1511 // We shouldn't get here at all if mPreventScreenOn is false, since
1512 // we should have already removed any existing
1513 // mForceReenableScreenTask messages...
1514 if (!mPreventScreenOn) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001515 Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 return;
1517 }
1518
1519 // Uh oh. It's been 5 seconds since a call to
1520 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1521 // This means the app that called preventScreenOn(true) is either
1522 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1523 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1524 // crashed before doing so.)
1525
1526 // Log a warning, and forcibly turn the screen back on.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 + "Forcing the screen back on...");
1529 preventScreenOn(false);
1530 }
1531
1532 private Runnable mForceReenableScreenTask = new Runnable() {
1533 public void run() {
1534 forceReenableScreen();
1535 }
1536 };
1537
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001538 private int setScreenStateLocked(boolean on) {
1539 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001540 if (err == 0) {
1541 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1542 if (mUseSoftwareAutoBrightness) {
1543 enableLightSensor(on);
1544 if (!on) {
1545 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001546 mButtonLight.turnOff();
1547 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001548 // clear current value so we will update based on the new conditions
1549 // when the sensor is reenabled.
1550 mLightSensorValue = -1;
Mike Lockwoodb2865412010-02-02 22:40:33 -05001551 // reset our highest light sensor value when the screen turns off
1552 mHighestLightSensorValue = -1;
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001553 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001554 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001555 }
1556 return err;
1557 }
1558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 private void setPowerState(int state)
1560 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001561 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
1563
Mike Lockwood435eb642009-12-03 08:40:18 -05001564 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 {
1566 synchronized (mLocks) {
1567 int err;
1568
1569 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001570 Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001572 + " noChangeLights=" + noChangeLights
1573 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575
1576 if (noChangeLights) {
1577 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1578 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001579 if (mProximitySensorActive) {
1580 // don't turn on the screen when the proximity sensor lock is held
1581 newState = (newState & ~SCREEN_BRIGHT);
1582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583
1584 if (batteryIsLow()) {
1585 newState |= BATTERY_LOW_BIT;
1586 } else {
1587 newState &= ~BATTERY_LOW_BIT;
1588 }
1589 if (newState == mPowerState) {
1590 return;
1591 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001592
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001593 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 newState |= ALL_BRIGHT;
1595 }
1596
1597 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1598 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1599
Mike Lockwood51b84492009-11-16 21:51:18 -05001600 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001601 Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 + " newState=" + newState + " noChangeLights=" + noChangeLights);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001605 Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001607 Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001609 Slog.d(TAG, " oldScreenOn=" + oldScreenOn
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 + " newScreenOn=" + newScreenOn);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001611 Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1613 }
1614
1615 if (mPowerState != newState) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001616 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1618 }
1619
1620 if (oldScreenOn != newScreenOn) {
1621 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001622 // When the user presses the power button, we need to always send out the
1623 // notification that it's going to sleep so the keyguard goes on. But
1624 // we can't do that until the screen fades out, so we don't show the keyguard
1625 // too early.
1626 if (mStillNeedSleepNotification) {
1627 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1628 }
1629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 // Turn on the screen UNLESS there was a prior
1631 // preventScreenOn(true) request. (Note that the lifetime
1632 // of a single preventScreenOn() request is limited to 5
1633 // seconds to prevent a buggy app from disabling the
1634 // screen forever; see forceReenableScreen().)
1635 boolean reallyTurnScreenOn = true;
1636 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001637 Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 + mPreventScreenOn);
1639 }
1640
1641 if (mPreventScreenOn) {
1642 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001643 Slog.d(TAG, "- PREVENTING screen from really turning on!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 reallyTurnScreenOn = false;
1646 }
1647 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001648 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 long identity = Binder.clearCallingIdentity();
1650 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001651 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 mBatteryStats.noteScreenOn();
1653 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001654 Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 } finally {
1656 Binder.restoreCallingIdentity(identity);
1657 }
1658 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001659 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 // But continue as if we really did turn the screen on...
1661 err = 0;
1662 }
1663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 mLastTouchDown = 0;
1665 mTotalTouchDownTime = 0;
1666 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 mTotalTouchDownTime, mTouchCycles);
1669 if (err == 0) {
1670 mPowerState |= SCREEN_ON_BIT;
1671 sendNotificationLocked(true, -1);
1672 }
1673 } else {
Mike Lockwood497087e32009-11-08 18:33:03 -05001674 // cancel light sensor task
1675 mHandler.removeCallbacks(mAutoBrightnessTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 mScreenOffTime = SystemClock.elapsedRealtime();
1677 long identity = Binder.clearCallingIdentity();
1678 try {
1679 mBatteryStats.noteScreenOff();
1680 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001681 Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } finally {
1683 Binder.restoreCallingIdentity(identity);
1684 }
1685 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001686 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001688 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 err = 0;
1691 mLastTouchDown = 0;
1692 }
1693 }
1694 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001695
1696 updateNativePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001699
1700 private void updateNativePowerStateLocked() {
1701 nativeSetPowerState(
1702 (mPowerState & SCREEN_ON_BIT) != 0,
1703 (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
1704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
Mike Lockwood435eb642009-12-03 08:40:18 -05001706 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 // called multiple times in the same state. -joeo
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001712 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001714 mScreenOffReason = reason;
1715 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717 return err;
1718 }
1719
1720 private boolean batteryIsLow() {
1721 return (!mIsPowered &&
1722 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1723 }
1724
The Android Open Source Project10592532009-03-18 17:39:46 -07001725 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001726 final int oldState = mPowerState;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001727 newState = applyButtonState(newState);
1728 newState = applyKeyboardState(newState);
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001729 final int realDifference = (newState ^ oldState);
1730 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001732 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 int offMask = 0;
1736 int dimMask = 0;
1737 int onMask = 0;
1738
1739 int preferredBrightness = getPreferredBrightness();
1740 boolean startAnimation = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1743 if (ANIMATE_KEYBOARD_LIGHTS) {
1744 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1745 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001746 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001747 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001749 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001750 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
1751 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753 startAnimation = true;
1754 } else {
1755 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001756 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001758 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
1761 }
1762
1763 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1764 if (ANIMATE_BUTTON_LIGHTS) {
1765 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1766 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001767 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001768 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001770 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001771 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
1772 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774 startAnimation = true;
1775 } else {
1776 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001777 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001779 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
1781 }
1782 }
1783
1784 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1785 if (ANIMATE_SCREEN_LIGHTS) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001786 int nominalCurrentValue = -1;
1787 // If there was an actual difference in the light state, then
1788 // figure out the "ideal" current value based on the previous
1789 // state. Otherwise, this is a change due to the brightness
1790 // override, so we want to animate from whatever the current
1791 // value is.
1792 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1793 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
1794 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
1795 nominalCurrentValue = preferredBrightness;
1796 break;
1797 case SCREEN_ON_BIT:
1798 nominalCurrentValue = Power.BRIGHTNESS_DIM;
1799 break;
1800 case 0:
1801 nominalCurrentValue = Power.BRIGHTNESS_OFF;
1802 break;
1803 case SCREEN_BRIGHT_BIT:
1804 default:
1805 // not possible
1806 nominalCurrentValue = (int)mScreenBrightness.curValue;
1807 break;
1808 }
Joe Onorato128e7292009-03-24 18:41:31 -07001809 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001810 int brightness = preferredBrightness;
1811 int steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1813 // dim or turn off backlight, depending on if the screen is on
1814 // the scale is because the brightness ramp isn't linear and this biases
1815 // it so the later parts take longer.
1816 final float scale = 1.5f;
1817 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1818 if (ratio > 1.0f) ratio = 1.0f;
1819 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1821 // was bright
1822 steps = ANIM_STEPS;
1823 } else {
1824 // was dim
1825 steps = (int)(ANIM_STEPS*ratio*scale);
1826 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001827 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if ((oldState & SCREEN_ON_BIT) != 0) {
1830 // was bright
1831 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1832 } else {
1833 // was dim
1834 steps = (int)(ANIM_STEPS*ratio);
1835 }
1836 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1837 // If the "stay on while plugged in" option is
1838 // turned on, then the screen will often not
1839 // automatically turn off while plugged in. To
1840 // still have a sense of when it is inactive, we
1841 // will then count going dim as turning off.
1842 mScreenOffTime = SystemClock.elapsedRealtime();
1843 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 brightness = Power.BRIGHTNESS_DIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001847 long identity = Binder.clearCallingIdentity();
1848 try {
1849 mBatteryStats.noteScreenBrightness(brightness);
1850 } catch (RemoteException e) {
1851 // Nothing interesting to do.
1852 } finally {
1853 Binder.restoreCallingIdentity(identity);
1854 }
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001855 if (mScreenBrightness.setTargetLocked(brightness,
1856 steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue)) {
1857 startAnimation = true;
1858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 } else {
1860 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1861 // dim or turn off backlight, depending on if the screen is on
1862 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001863 offMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001865 dimMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 }
1867 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001868 onMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870 }
1871 }
1872
1873 if (startAnimation) {
1874 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001875 Slog.i(TAG, "Scheduling light animator!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 }
1877 mHandler.removeCallbacks(mLightAnimator);
1878 mHandler.post(mLightAnimator);
1879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 if (offMask != 0) {
Mike Lockwood48358bd2010-04-17 22:29:20 -04001882 if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001883 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 if (dimMask != 0) {
1886 int brightness = Power.BRIGHTNESS_DIM;
1887 if ((newState & BATTERY_LOW_BIT) != 0 &&
1888 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1889 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1890 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001891 if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001892 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894 if (onMask != 0) {
1895 int brightness = getPreferredBrightness();
1896 if ((newState & BATTERY_LOW_BIT) != 0 &&
1897 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1898 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1899 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001900 if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001901 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904
The Android Open Source Project10592532009-03-18 17:39:46 -07001905 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05001906 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05001907 ? LightsService.BRIGHTNESS_MODE_SENSOR
1908 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07001909 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001910 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07001911 }
1912 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001913 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001914 }
1915 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001916 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919
1920 class BrightnessState {
1921 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 boolean initialized;
1924 int targetValue;
1925 float curValue;
1926 float delta;
1927 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 BrightnessState(int m) {
1930 mask = m;
1931 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 public void dump(PrintWriter pw, String prefix) {
1934 pw.println(prefix + "animating=" + animating
1935 + " targetValue=" + targetValue
1936 + " curValue=" + curValue
1937 + " delta=" + delta);
1938 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001939
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001940 boolean setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07001941 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 if (!initialized) {
1943 initialized = true;
1944 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001945 } else if (targetValue == target) {
1946 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
1948 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001949 delta = (targetValue -
1950 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
1951 / stepsToTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 if (mSpew) {
Joe Onorato128e7292009-03-24 18:41:31 -07001953 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
Joe Onorato8a9b2202010-02-26 18:56:32 -08001954 Slog.i(TAG, "Setting target " + mask + ": cur=" + curValue
Joe Onorato128e7292009-03-24 18:41:31 -07001955 + " target=" + targetValue + " delta=" + delta
1956 + " nominalCurrentValue=" + nominalCurrentValue
1957 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 animating = true;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001960 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 boolean stepLocked() {
1964 if (!animating) return false;
1965 if (false && mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001966 Slog.i(TAG, "Step target " + mask + ": cur=" + curValue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 + " target=" + targetValue + " delta=" + delta);
1968 }
1969 curValue += delta;
1970 int curIntValue = (int)curValue;
1971 boolean more = true;
1972 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001973 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 more = false;
1975 } else if (delta > 0) {
1976 if (curIntValue >= targetValue) {
1977 curValue = curIntValue = targetValue;
1978 more = false;
1979 }
1980 } else {
1981 if (curIntValue <= targetValue) {
1982 curValue = curIntValue = targetValue;
1983 more = false;
1984 }
1985 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001986 //Slog.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001987 setLightBrightness(mask, curIntValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 animating = more;
1989 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001990 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001991 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 }
1994 return more;
1995 }
1996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 private class LightAnimator implements Runnable {
1999 public void run() {
2000 synchronized (mLocks) {
2001 long now = SystemClock.uptimeMillis();
2002 boolean more = mScreenBrightness.stepLocked();
2003 if (mKeyboardBrightness.stepLocked()) {
2004 more = true;
2005 }
2006 if (mButtonBrightness.stepLocked()) {
2007 more = true;
2008 }
2009 if (more) {
2010 mHandler.postAtTime(mLightAnimator, now+(1000/60));
2011 }
2012 }
2013 }
2014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 private int getPreferredBrightness() {
2017 try {
2018 if (mScreenBrightnessOverride >= 0) {
2019 return mScreenBrightnessOverride;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002020 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
Mike Lockwood27c6dd72009-11-04 08:57:07 -05002021 && mAutoBrightessEnabled) {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002022 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
2025 SCREEN_BRIGHTNESS);
2026 // Don't let applications turn the screen all the way off
2027 return Math.max(brightness, Power.BRIGHTNESS_DIM);
2028 } catch (SettingNotFoundException snfe) {
2029 return Power.BRIGHTNESS_ON;
2030 }
2031 }
2032
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002033 private int applyButtonState(int state) {
2034 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002035 if ((state & BATTERY_LOW_BIT) != 0) {
2036 // do not override brightness if the battery is low
2037 return state;
2038 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002039 if (mButtonBrightnessOverride >= 0) {
2040 brightness = mButtonBrightnessOverride;
2041 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
2042 brightness = mLightSensorButtonBrightness;
2043 }
2044 if (brightness > 0) {
2045 return state | BUTTON_BRIGHT_BIT;
2046 } else if (brightness == 0) {
2047 return state & ~BUTTON_BRIGHT_BIT;
2048 } else {
2049 return state;
2050 }
2051 }
2052
2053 private int applyKeyboardState(int state) {
2054 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002055 if ((state & BATTERY_LOW_BIT) != 0) {
2056 // do not override brightness if the battery is low
2057 return state;
2058 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002059 if (!mKeyboardVisible) {
2060 brightness = 0;
2061 } else if (mButtonBrightnessOverride >= 0) {
2062 brightness = mButtonBrightnessOverride;
2063 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
2064 brightness = mLightSensorKeyboardBrightness;
2065 }
2066 if (brightness > 0) {
2067 return state | KEYBOARD_BRIGHT_BIT;
2068 } else if (brightness == 0) {
2069 return state & ~KEYBOARD_BRIGHT_BIT;
2070 } else {
2071 return state;
2072 }
2073 }
2074
Charles Mendis322591c2009-10-29 11:06:59 -07002075 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 synchronized (mLocks) {
2077 return (mPowerState & SCREEN_ON_BIT) != 0;
2078 }
2079 }
2080
Charles Mendis322591c2009-10-29 11:06:59 -07002081 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 synchronized (mLocks) {
2083 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
2084 }
2085 }
2086
Mike Lockwood497087e32009-11-08 18:33:03 -05002087 private boolean isScreenTurningOffLocked() {
2088 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
2089 }
2090
Mike Lockwood200b30b2009-09-20 00:23:59 -04002091 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05002092 if (isScreenTurningOffLocked()) {
2093 // cancel animation so userActivity will succeed
2094 mScreenBrightness.animating = false;
2095 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002096 boolean savedActivityAllowed = mUserActivityAllowed;
2097 mUserActivityAllowed = true;
2098 userActivity(SystemClock.uptimeMillis(), false);
2099 mUserActivityAllowed = savedActivityAllowed;
2100 }
2101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
2103 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Joe Onorato7999bff2010-07-24 11:50:05 -04002104 userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
2106
2107 public void userActivity(long time, boolean noChangeLights) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002108 userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 }
2110
2111 public void userActivity(long time, boolean noChangeLights, int eventType) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002112 userActivity(time, -1, noChangeLights, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
2114
2115 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002116 userActivity(time, -1, noChangeLights, eventType, force);
2117 }
2118
2119 /*
2120 * Reset the user activity timeout to now + timeout. This overrides whatever else is going
2121 * on with user activity. Don't use this function.
2122 */
2123 public void clearUserActivityTimeout(long now, long timeout) {
2124 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2125 Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2126 userActivity(now, timeout, false, OTHER_EVENT, false);
2127 }
2128
2129 private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2130 int eventType, boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2132
2133 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002134 && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002136 Slog.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
2138 return;
2139 }
2140
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002141 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
2142 && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
2143 || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
2144 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002146 }
2147 return;
2148 }
2149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 if (false) {
2151 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002152 Slog.d(TAG, "userActivity !!!");//, new RuntimeException());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002154 Slog.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156 }
2157
2158 synchronized (mLocks) {
2159 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002160 Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 + " mUserActivityAllowed=" + mUserActivityAllowed
2162 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07002163 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
2164 + " mProximitySensorActive=" + mProximitySensorActive
Joe Onorato797e6882010-08-26 14:46:01 -04002165 + " timeoutOverride=" + timeoutOverride
Mike Lockwood36fc3022009-08-25 16:49:06 -07002166 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
Mike Lockwood05067122009-10-27 23:07:25 -04002168 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05002169 if (isScreenTurningOffLocked()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002170 Slog.d(TAG, "ignoring user activity while turning off screen");
Mike Lockwood05067122009-10-27 23:07:25 -04002171 return;
2172 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002173 // Disable proximity sensor if if user presses power key while we are in the
2174 // "waiting for proximity sensor to go negative" state.
2175 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
2176 mProximitySensorActive = false;
2177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 if (mLastEventTime <= time || force) {
2179 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002180 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002181 // Only turn on button backlights if a button was pressed
2182 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05002183 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
2185 } else {
2186 // don't clear button/keyboard backlights when the screen is touched.
2187 mUserState |= SCREEN_BRIGHT;
2188 }
2189
Dianne Hackborn617f8772009-03-31 15:04:46 -07002190 int uid = Binder.getCallingUid();
2191 long ident = Binder.clearCallingIdentity();
2192 try {
2193 mBatteryStats.noteUserActivity(uid, eventType);
2194 } catch (RemoteException e) {
2195 // Ignore
2196 } finally {
2197 Binder.restoreCallingIdentity(ident);
2198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002199
Michael Chane96440f2009-05-06 10:27:36 -07002200 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002201 setPowerState(mUserState | mWakeLockState, noChangeLights,
2202 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
Joe Onorato7999bff2010-07-24 11:50:05 -04002203 setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205 }
2206 }
Mike Lockwoodef731622010-01-27 17:51:34 -05002207
2208 if (mPolicy != null) {
2209 mPolicy.userActivity();
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 }
2212
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002213 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2214 try {
2215 int i;
2216 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2217 if (sensorValue < mAutoBrightnessLevels[i]) {
2218 break;
2219 }
2220 }
2221 return values[i];
2222 } catch (Exception e) {
2223 // guard against null pointer or index out of bounds errors
Joe Onorato8a9b2202010-02-26 18:56:32 -08002224 Slog.e(TAG, "getAutoBrightnessValue", e);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002225 return 255;
2226 }
2227 }
2228
Mike Lockwood20f87d72009-11-05 16:08:51 -05002229 private Runnable mProximityTask = new Runnable() {
2230 public void run() {
2231 synchronized (mLocks) {
2232 if (mProximityPendingValue != -1) {
2233 proximityChangedLocked(mProximityPendingValue == 1);
2234 mProximityPendingValue = -1;
2235 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002236 if (mProximityPartialLock.isHeld()) {
2237 mProximityPartialLock.release();
2238 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002239 }
2240 }
2241 };
2242
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002243 private Runnable mAutoBrightnessTask = new Runnable() {
2244 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002245 synchronized (mLocks) {
2246 int value = (int)mLightSensorPendingValue;
2247 if (value >= 0) {
2248 mLightSensorPendingValue = -1;
2249 lightSensorChangedLocked(value);
2250 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002251 }
2252 }
2253 };
2254
Mike Lockwoodb2865412010-02-02 22:40:33 -05002255 private void dockStateChanged(int state) {
2256 synchronized (mLocks) {
2257 mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2258 if (mIsDocked) {
2259 mHighestLightSensorValue = -1;
2260 }
2261 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2262 // force lights recalculation
2263 int value = (int)mLightSensorValue;
2264 mLightSensorValue = -1;
2265 lightSensorChangedLocked(value);
2266 }
2267 }
2268 }
2269
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002270 private void lightSensorChangedLocked(int value) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002271 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002272 Slog.d(TAG, "lightSensorChangedLocked " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002273 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002274
Mike Lockwoodb2865412010-02-02 22:40:33 -05002275 // do not allow light sensor value to decrease
2276 if (mHighestLightSensorValue < value) {
2277 mHighestLightSensorValue = value;
2278 }
2279
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002280 if (mLightSensorValue != value) {
2281 mLightSensorValue = value;
2282 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
Mike Lockwoodb2865412010-02-02 22:40:33 -05002283 // use maximum light sensor value seen since screen went on for LCD to avoid flicker
2284 // we only do this if we are undocked, since lighting should be stable when
2285 // stationary in a dock.
2286 int lcdValue = getAutoBrightnessValue(
2287 (mIsDocked ? value : mHighestLightSensorValue),
2288 mLcdBacklightValues);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002289 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002290 int keyboardValue;
2291 if (mKeyboardVisible) {
2292 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2293 } else {
2294 keyboardValue = 0;
2295 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002296 mLightSensorScreenBrightness = lcdValue;
2297 mLightSensorButtonBrightness = buttonValue;
2298 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002299
2300 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002301 Slog.d(TAG, "lcdValue " + lcdValue);
2302 Slog.d(TAG, "buttonValue " + buttonValue);
2303 Slog.d(TAG, "keyboardValue " + keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002304 }
2305
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002306 boolean startAnimation = false;
Mike Lockwood4984e732009-11-01 08:16:33 -05002307 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002308 if (ANIMATE_SCREEN_LIGHTS) {
2309 if (mScreenBrightness.setTargetLocked(lcdValue,
2310 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
2311 (int)mScreenBrightness.curValue)) {
2312 startAnimation = true;
2313 }
2314 } else {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002315 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002316 ? LightsService.BRIGHTNESS_MODE_SENSOR
2317 : LightsService.BRIGHTNESS_MODE_USER);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002318 mLcdLight.setBrightness(lcdValue, brightnessMode);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002319 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002320 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002321 if (mButtonBrightnessOverride < 0) {
2322 if (ANIMATE_BUTTON_LIGHTS) {
2323 if (mButtonBrightness.setTargetLocked(buttonValue,
2324 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2325 (int)mButtonBrightness.curValue)) {
2326 startAnimation = true;
2327 }
2328 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002329 mButtonLight.setBrightness(buttonValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002330 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002331 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002332 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
2333 if (ANIMATE_KEYBOARD_LIGHTS) {
2334 if (mKeyboardBrightness.setTargetLocked(keyboardValue,
2335 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2336 (int)mKeyboardBrightness.curValue)) {
2337 startAnimation = true;
2338 }
2339 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002340 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002341 }
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002342 }
2343 if (startAnimation) {
2344 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002345 Slog.i(TAG, "lightSensorChangedLocked scheduling light animator");
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002346 }
2347 mHandler.removeCallbacks(mLightAnimator);
2348 mHandler.post(mLightAnimator);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002349 }
2350 }
2351 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002352 }
2353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 /**
2355 * The user requested that we go to sleep (probably with the power button).
2356 * This overrides all wake locks that are held.
2357 */
2358 public void goToSleep(long time)
2359 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002360 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2361 }
2362
2363 /**
2364 * The user requested that we go to sleep (probably with the power button).
2365 * This overrides all wake locks that are held.
2366 */
2367 public void goToSleepWithReason(long time, int reason)
2368 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2370 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002371 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002376 * Reboot the device immediately, passing 'reason' (may be null)
2377 * to the underlying __reboot system call. Should not return.
2378 */
2379 public void reboot(String reason)
2380 {
2381 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002382
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002383 if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
2384 throw new IllegalStateException("Too early to call reboot()");
2385 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002386
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002387 final String finalReason = reason;
2388 Runnable runnable = new Runnable() {
2389 public void run() {
2390 synchronized (this) {
2391 ShutdownThread.reboot(mContext, finalReason, false);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002392 }
2393
San Mehat1e512792010-01-07 10:40:29 -08002394 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002395 };
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002396 // ShutdownThread must run on a looper capable of displaying the UI.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002397 mHandler.post(runnable);
2398
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002399 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002400 synchronized (runnable) {
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002401 while (true) {
2402 try {
2403 runnable.wait();
2404 } catch (InterruptedException e) {
2405 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002406 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002407 }
2408 }
2409
Dan Egnor60d87622009-12-16 16:32:58 -08002410 /**
2411 * Crash the runtime (causing a complete restart of the Android framework).
2412 * Requires REBOOT permission. Mostly for testing. Should not return.
2413 */
2414 public void crash(final String message)
2415 {
2416 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2417 Thread t = new Thread("PowerManagerService.crash()") {
2418 public void run() { throw new RuntimeException(message); }
2419 };
2420 try {
2421 t.start();
2422 t.join();
2423 } catch (InterruptedException e) {
2424 Log.wtf(TAG, e);
2425 }
2426 }
2427
Mike Lockwood435eb642009-12-03 08:40:18 -05002428 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429
2430 if (mLastEventTime <= time) {
2431 mLastEventTime = time;
2432 // cancel all of the wake locks
2433 mWakeLockState = SCREEN_OFF;
2434 int N = mLocks.size();
2435 int numCleared = 0;
2436 for (int i=0; i<N; i++) {
2437 WakeLock wl = mLocks.get(i);
2438 if (isScreenLock(wl.flags)) {
2439 mLocks.get(i).activated = false;
2440 numCleared++;
2441 }
2442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002443 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002444 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002446 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 cancelTimerLocked();
2448 }
2449 }
2450
2451 public long timeSinceScreenOn() {
2452 synchronized (mLocks) {
2453 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2454 return 0;
2455 }
2456 return SystemClock.elapsedRealtime() - mScreenOffTime;
2457 }
2458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002461 synchronized (mLocks) {
2462 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002463 Slog.d(TAG, "setKeyboardVisibility: " + visible);
Mike Lockwooda625b382009-09-12 17:36:03 -07002464 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002465 if (mKeyboardVisible != visible) {
2466 mKeyboardVisible = visible;
2467 // don't signal user activity if the screen is off; other code
2468 // will take care of turning on due to a true change to the lid
2469 // switch and synchronized with the lock screen.
2470 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002471 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002472 // force recompute of backlight values
2473 if (mLightSensorValue >= 0) {
2474 int value = (int)mLightSensorValue;
2475 mLightSensorValue = -1;
2476 lightSensorChangedLocked(value);
2477 }
2478 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002479 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2480 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002481 }
2482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
2484
2485 /**
2486 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002487 * When disabling user activity we also reset user power state so the keyguard can reset its
2488 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 */
2490 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002491 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002492 Slog.d(TAG, "enableUserActivity " + enabled);
Mike Lockwood50c548d2009-11-09 16:02:06 -05002493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 synchronized (mLocks) {
2495 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002496 if (!enabled) {
2497 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2498 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 }
2501 }
2502
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002503 private void setScreenBrightnessMode(int mode) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002504 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Mike Lockwoodf90ffcc2009-11-03 11:41:27 -05002505 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002506 mAutoBrightessEnabled = enabled;
Charles Mendis322591c2009-10-29 11:06:59 -07002507 if (isScreenOn()) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002508 // force recompute of backlight values
2509 if (mLightSensorValue >= 0) {
2510 int value = (int)mLightSensorValue;
2511 mLightSensorValue = -1;
2512 lightSensorChangedLocked(value);
2513 }
Mike Lockwood2d155d22009-10-27 09:32:30 -04002514 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002515 }
2516 }
2517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 /** Sets the screen off timeouts:
2519 * mKeylightDelay
2520 * mDimDelay
2521 * mScreenOffDelay
2522 * */
2523 private void setScreenOffTimeoutsLocked() {
2524 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002525 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 mDimDelay = -1;
2527 mScreenOffDelay = 0;
2528 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2529 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2530 mDimDelay = -1;
2531 mScreenOffDelay = 0;
2532 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002533 int totalDelay = mScreenOffTimeoutSetting;
2534 if (totalDelay > mMaximumScreenOffTimeout) {
2535 totalDelay = mMaximumScreenOffTimeout;
2536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2538 if (totalDelay < 0) {
2539 mScreenOffDelay = Integer.MAX_VALUE;
2540 } else if (mKeylightDelay < totalDelay) {
2541 // subtract the time that the keylight delay. This will give us the
2542 // remainder of the time that we need to sleep to get the accurate
2543 // screen off timeout.
2544 mScreenOffDelay = totalDelay - mKeylightDelay;
2545 } else {
2546 mScreenOffDelay = 0;
2547 }
2548 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2549 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2550 mScreenOffDelay = LONG_DIM_TIME;
2551 } else {
2552 mDimDelay = -1;
2553 }
2554 }
2555 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002556 Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2558 + " mDimScreen=" + mDimScreen);
2559 }
2560 }
2561
2562 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002563 * Refreshes cached secure settings. Called once on startup, and
2564 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 */
Doug Zongker43866e02010-01-07 12:09:54 -08002566 private void updateSettingsValues() {
2567 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002569 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 SHORT_KEYLIGHT_DELAY_DEFAULT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002571 // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 }
2573
2574 private class LockList extends ArrayList<WakeLock>
2575 {
2576 void addLock(WakeLock wl)
2577 {
2578 int index = getIndex(wl.binder);
2579 if (index < 0) {
2580 this.add(wl);
2581 }
2582 }
2583
2584 WakeLock removeLock(IBinder binder)
2585 {
2586 int index = getIndex(binder);
2587 if (index >= 0) {
2588 return this.remove(index);
2589 } else {
2590 return null;
2591 }
2592 }
2593
2594 int getIndex(IBinder binder)
2595 {
2596 int N = this.size();
2597 for (int i=0; i<N; i++) {
2598 if (this.get(i).binder == binder) {
2599 return i;
2600 }
2601 }
2602 return -1;
2603 }
2604
2605 int gatherState()
2606 {
2607 int result = 0;
2608 int N = this.size();
2609 for (int i=0; i<N; i++) {
2610 WakeLock wl = this.get(i);
2611 if (wl.activated) {
2612 if (isScreenLock(wl.flags)) {
2613 result |= wl.minState;
2614 }
2615 }
2616 }
2617 return result;
2618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002619
Michael Chane96440f2009-05-06 10:27:36 -07002620 int reactivateScreenLocksLocked()
2621 {
2622 int result = 0;
2623 int N = this.size();
2624 for (int i=0; i<N; i++) {
2625 WakeLock wl = this.get(i);
2626 if (isScreenLock(wl.flags)) {
2627 wl.activated = true;
2628 result |= wl.minState;
2629 }
2630 }
2631 return result;
2632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
2634
2635 void setPolicy(WindowManagerPolicy p) {
2636 synchronized (mLocks) {
2637 mPolicy = p;
2638 mLocks.notifyAll();
2639 }
2640 }
2641
2642 WindowManagerPolicy getPolicyLocked() {
2643 while (mPolicy == null || !mDoneBooting) {
2644 try {
2645 mLocks.wait();
2646 } catch (InterruptedException e) {
2647 // Ignore
2648 }
2649 }
2650 return mPolicy;
2651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002654 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2655 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2656 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002657 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002658 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood4984e732009-11-01 08:16:33 -05002659 enableLightSensor(true);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002660 }
2661
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002662 // wait until sensors are enabled before turning on screen.
2663 // some devices will not activate the light sensor properly on boot
2664 // unless we do this.
2665 if (mUseSoftwareAutoBrightness) {
2666 // turn the screen on
2667 setPowerState(SCREEN_BRIGHT);
2668 } else {
2669 // turn everything on
2670 setPowerState(ALL_BRIGHT);
2671 }
2672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 synchronized (mLocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002674 Slog.d(TAG, "system ready!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 mDoneBooting = true;
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002676
Dianne Hackborn617f8772009-03-31 15:04:46 -07002677 long identity = Binder.clearCallingIdentity();
2678 try {
2679 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2680 mBatteryStats.noteScreenOn();
2681 } catch (RemoteException e) {
2682 // Nothing interesting to do.
2683 } finally {
2684 Binder.restoreCallingIdentity(identity);
2685 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002686 }
2687 }
2688
2689 void bootCompleted() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002690 Slog.d(TAG, "bootCompleted");
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002691 synchronized (mLocks) {
2692 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2694 updateWakeLockLocked();
2695 mLocks.notifyAll();
2696 }
2697 }
2698
2699 public void monitor() {
2700 synchronized (mLocks) { }
2701 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002702
2703 public int getSupportedWakeLockFlags() {
2704 int result = PowerManager.PARTIAL_WAKE_LOCK
2705 | PowerManager.FULL_WAKE_LOCK
2706 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2707
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002708 if (mProximitySensor != null) {
2709 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2710 }
2711
2712 return result;
2713 }
2714
Mike Lockwood237a2992009-09-15 14:42:16 -04002715 public void setBacklightBrightness(int brightness) {
2716 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2717 // Don't let applications turn the screen all the way off
2718 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002719 mLcdLight.setBrightness(brightness);
2720 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2721 mButtonLight.setBrightness(brightness);
Mike Lockwood237a2992009-09-15 14:42:16 -04002722 long identity = Binder.clearCallingIdentity();
2723 try {
2724 mBatteryStats.noteScreenBrightness(brightness);
2725 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002726 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
Mike Lockwood237a2992009-09-15 14:42:16 -04002727 } finally {
2728 Binder.restoreCallingIdentity(identity);
2729 }
2730
2731 // update our animation state
2732 if (ANIMATE_SCREEN_LIGHTS) {
2733 mScreenBrightness.curValue = brightness;
2734 mScreenBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002735 mScreenBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002736 }
2737 if (ANIMATE_KEYBOARD_LIGHTS) {
2738 mKeyboardBrightness.curValue = brightness;
2739 mKeyboardBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002740 mKeyboardBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002741 }
2742 if (ANIMATE_BUTTON_LIGHTS) {
2743 mButtonBrightness.curValue = brightness;
2744 mButtonBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002745 mButtonBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002746 }
2747 }
2748
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002749 public void setAttentionLight(boolean on, int color) {
2750 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002751 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002752 }
2753
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002754 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002755 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002756 Slog.d(TAG, "enableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002757 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002758 if (!mProximitySensorEnabled) {
2759 // clear calling identity so sensor manager battery stats are accurate
2760 long identity = Binder.clearCallingIdentity();
2761 try {
2762 mSensorManager.registerListener(mProximityListener, mProximitySensor,
2763 SensorManager.SENSOR_DELAY_NORMAL);
2764 mProximitySensorEnabled = true;
2765 } finally {
2766 Binder.restoreCallingIdentity(identity);
2767 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002768 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002769 }
2770
2771 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002772 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002773 Slog.d(TAG, "disableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002774 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002775 if (mProximitySensorEnabled) {
2776 // clear calling identity so sensor manager battery stats are accurate
2777 long identity = Binder.clearCallingIdentity();
2778 try {
2779 mSensorManager.unregisterListener(mProximityListener);
2780 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002781 if (mProximityPartialLock.isHeld()) {
2782 mProximityPartialLock.release();
2783 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002784 mProximitySensorEnabled = false;
2785 } finally {
2786 Binder.restoreCallingIdentity(identity);
2787 }
2788 if (mProximitySensorActive) {
2789 mProximitySensorActive = false;
2790 forceUserActivityLocked();
2791 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002792 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002793 }
2794
Mike Lockwood20f87d72009-11-05 16:08:51 -05002795 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002796 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002797 Slog.d(TAG, "proximityChangedLocked, active: " + active);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002798 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002799 if (!mProximitySensorEnabled) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002800 Slog.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05002801 return;
2802 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002803 if (active) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002804 goToSleepLocked(SystemClock.uptimeMillis(),
2805 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002806 mProximitySensorActive = true;
2807 } else {
2808 // proximity sensor negative events trigger as user activity.
2809 // temporarily set mUserActivityAllowed to true so this will work
2810 // even when the keyguard is on.
2811 mProximitySensorActive = false;
2812 forceUserActivityLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002813
2814 if (mProximityWakeLockCount == 0) {
2815 // disable sensor if we have no listeners left after proximity negative
2816 disableProximityLockLocked();
2817 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002818 }
2819 }
2820
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002821 private void enableLightSensor(boolean enable) {
2822 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002823 Slog.d(TAG, "enableLightSensor " + enable);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002824 }
2825 if (mSensorManager != null && mLightSensorEnabled != enable) {
2826 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002827 // clear calling identity so sensor manager battery stats are accurate
2828 long identity = Binder.clearCallingIdentity();
2829 try {
2830 if (enable) {
2831 mSensorManager.registerListener(mLightListener, mLightSensor,
2832 SensorManager.SENSOR_DELAY_NORMAL);
2833 } else {
2834 mSensorManager.unregisterListener(mLightListener);
2835 mHandler.removeCallbacks(mAutoBrightnessTask);
2836 }
2837 } finally {
2838 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04002839 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002840 }
2841 }
2842
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002843 SensorEventListener mProximityListener = new SensorEventListener() {
2844 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002845 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002846 synchronized (mLocks) {
2847 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05002848 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
2849 mLastProximityEventTime = milliseconds;
2850 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002851 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05002852
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002853 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05002854 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
2855 distance < mProximitySensor.getMaximumRange());
2856
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002857 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002858 Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active);
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002859 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002860 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
2861 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
2862 mProximityPendingValue = (active ? 1 : 0);
2863 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002864 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002865 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002866 // process the value immediately
2867 mProximityPendingValue = -1;
2868 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002869 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002870
2871 // update mProximityPartialLock state
2872 boolean held = mProximityPartialLock.isHeld();
2873 if (!held && proximityTaskQueued) {
2874 // hold wakelock until mProximityTask runs
2875 mProximityPartialLock.acquire();
2876 } else if (held && !proximityTaskQueued) {
2877 mProximityPartialLock.release();
2878 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002879 }
2880 }
2881
2882 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2883 // ignore
2884 }
2885 };
2886
2887 SensorEventListener mLightListener = new SensorEventListener() {
2888 public void onSensorChanged(SensorEvent event) {
2889 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05002890 // ignore light sensor while screen is turning off
2891 if (isScreenTurningOffLocked()) {
2892 return;
2893 }
2894
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002895 int value = (int)event.values[0];
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002896 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002897 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002898 Slog.d(TAG, "onSensorChanged: light value: " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002899 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002900 mHandler.removeCallbacks(mAutoBrightnessTask);
2901 if (mLightSensorValue != value) {
Mike Lockwood20ee6f22009-11-07 20:33:47 -05002902 if (mLightSensorValue == -1 ||
2903 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
2904 // process the value immediately if screen has just turned on
Mike Lockwood6c97fca2009-10-20 08:10:00 -04002905 lightSensorChangedLocked(value);
2906 } else {
2907 // delay processing to debounce the sensor
2908 mLightSensorPendingValue = value;
2909 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
2910 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002911 } else {
2912 mLightSensorPendingValue = -1;
2913 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002914 }
2915 }
2916
2917 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2918 // ignore
2919 }
2920 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921}