The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import com.android.internal.app.IBatteryStats; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 20 | import com.android.internal.app.ShutdownThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.server.am.BatteryStatsService; |
| 22 | |
| 23 | import android.app.ActivityManagerNative; |
| 24 | import android.app.IActivityManager; |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ContentQueryMap; |
| 27 | import android.content.ContentResolver; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 28 | import android.content.ContentValues; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.pm.PackageManager; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 34 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 36 | import android.hardware.Sensor; |
| 37 | import android.hardware.SensorEvent; |
| 38 | import android.hardware.SensorEventListener; |
| 39 | import android.hardware.SensorManager; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 40 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.BatteryStats; |
| 42 | import android.os.Binder; |
| 43 | import android.os.Handler; |
| 44 | import android.os.HandlerThread; |
| 45 | import android.os.IBinder; |
| 46 | import android.os.IPowerManager; |
| 47 | import android.os.LocalPowerManager; |
| 48 | import android.os.Power; |
| 49 | import android.os.PowerManager; |
| 50 | import android.os.Process; |
| 51 | import android.os.RemoteException; |
| 52 | import android.os.SystemClock; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 53 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.provider.Settings.SettingNotFoundException; |
| 55 | import android.provider.Settings; |
| 56 | import android.util.EventLog; |
| 57 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 58 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.WindowManagerPolicy; |
| 60 | import static android.provider.Settings.System.DIM_SCREEN; |
| 61 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS; |
Dan Murphy | 951764b | 2009-08-27 14:59:03 -0500 | [diff] [blame] | 62 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 63 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
| 65 | import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 66 | import static android.provider.Settings.System.WINDOW_ANIMATION_SCALE; |
| 67 | import static android.provider.Settings.System.TRANSITION_ANIMATION_SCALE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | import java.io.FileDescriptor; |
| 70 | import java.io.PrintWriter; |
| 71 | import java.util.ArrayList; |
| 72 | import java.util.HashMap; |
| 73 | import java.util.Observable; |
| 74 | import java.util.Observer; |
| 75 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 76 | public class PowerManagerService extends IPowerManager.Stub |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 77 | implements LocalPowerManager, Watchdog.Monitor { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | private static final String TAG = "PowerManagerService"; |
| 80 | static final String PARTIAL_NAME = "PowerManagerService"; |
| 81 | |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 82 | static final boolean DEBUG_SCREEN_ON = false; |
| 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static final boolean LOG_PARTIAL_WL = false; |
| 85 | |
| 86 | // Indicates whether touch-down cycles should be logged as part of the |
| 87 | // LOG_POWER_SCREEN_STATE log events |
| 88 | private static final boolean LOG_TOUCH_DOWNS = true; |
| 89 | |
| 90 | private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK |
| 91 | | PowerManager.SCREEN_DIM_WAKE_LOCK |
| 92 | | PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 93 | | PowerManager.FULL_WAKE_LOCK |
| 94 | | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| 96 | // time since last state: time since last event: |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 97 | // The short keylight delay comes from secure settings; this is the default. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec |
| 99 | private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec |
| 100 | private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec |
| 101 | private static final int LONG_DIM_TIME = 7000; // t+N-5 sec |
| 102 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 103 | // How long to wait to debounce light sensor changes. |
Mike Lockwood | 9b813692 | 2009-11-06 15:53:59 -0500 | [diff] [blame] | 104 | private static final int LIGHT_SENSOR_DELAY = 2000; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 105 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 106 | // For debouncing the proximity sensor. |
| 107 | private static final int PROXIMITY_SENSOR_DELAY = 1000; |
| 108 | |
Mike Lockwood | d20ea36 | 2009-09-15 00:13:38 -0400 | [diff] [blame] | 109 | // trigger proximity if distance is less than 5 cm |
| 110 | private static final float PROXIMITY_THRESHOLD = 5.0f; |
| 111 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 112 | // Cached secure settings; see updateSettingsValues() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT; |
| 114 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 115 | // Default timeout for screen off, if not found in settings database = 15 seconds. |
| 116 | private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15000; |
| 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | // flags for setPowerState |
| 119 | private static final int SCREEN_ON_BIT = 0x00000001; |
| 120 | private static final int SCREEN_BRIGHT_BIT = 0x00000002; |
| 121 | private static final int BUTTON_BRIGHT_BIT = 0x00000004; |
| 122 | private static final int KEYBOARD_BRIGHT_BIT = 0x00000008; |
| 123 | private static final int BATTERY_LOW_BIT = 0x00000010; |
| 124 | |
| 125 | // values for setPowerState |
| 126 | |
| 127 | // SCREEN_OFF == everything off |
| 128 | private static final int SCREEN_OFF = 0x00000000; |
| 129 | |
| 130 | // SCREEN_DIM == screen on, screen backlight dim |
| 131 | private static final int SCREEN_DIM = SCREEN_ON_BIT; |
| 132 | |
| 133 | // SCREEN_BRIGHT == screen on, screen backlight bright |
| 134 | private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT; |
| 135 | |
| 136 | // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright |
| 137 | private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT; |
| 138 | |
| 139 | // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright |
| 140 | private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT; |
| 141 | |
| 142 | // used for noChangeLights in setPowerState() |
| 143 | private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT; |
| 144 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 145 | boolean mAnimateScreenLights = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 146 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | static final int ANIM_STEPS = 60/4; |
Mike Lockwood | dd9668e | 2009-10-27 15:47:02 -0400 | [diff] [blame] | 148 | // Slower animation for autobrightness changes |
| 149 | static final int AUTOBRIGHTNESS_ANIM_STEPS = 60; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | |
| 151 | // These magic numbers are the initial state of the LEDs at boot. Ideally |
| 152 | // we should read them from the driver, but our current hardware returns 0 |
| 153 | // for the initial value. Oops! |
| 154 | static final int INITIAL_SCREEN_BRIGHTNESS = 255; |
| 155 | static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| 156 | static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | private final int MY_UID; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 159 | private final int MY_PID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
| 161 | private boolean mDoneBooting = false; |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 162 | private boolean mBootCompleted = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | private int mStayOnConditions = 0; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 164 | private final int[] mBroadcastQueue = new int[] { -1, -1, -1 }; |
| 165 | private final int[] mBroadcastWhy = new int[3]; |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 166 | private boolean mPreparingForScreenOn = false; |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 167 | private boolean mSkippedScreenOn = false; |
| 168 | private boolean mInitialized = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | private int mPartialCount = 0; |
| 170 | private int mPowerState; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 171 | // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER, |
| 172 | // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 173 | private int mScreenOffReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | private int mUserState; |
| 175 | private boolean mKeyboardVisible = false; |
| 176 | private boolean mUserActivityAllowed = true; |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 177 | private int mProximityWakeLockCount = 0; |
| 178 | private boolean mProximitySensorEnabled = false; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 179 | private boolean mProximitySensorActive = false; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 180 | private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active |
| 181 | private long mLastProximityEventTime; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 182 | private int mScreenOffTimeoutSetting; |
| 183 | private int mMaximumScreenOffTimeout = Integer.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | private int mKeylightDelay; |
| 185 | private int mDimDelay; |
| 186 | private int mScreenOffDelay; |
| 187 | private int mWakeLockState; |
| 188 | private long mLastEventTime = 0; |
| 189 | private long mScreenOffTime; |
| 190 | private volatile WindowManagerPolicy mPolicy; |
| 191 | private final LockList mLocks = new LockList(); |
| 192 | private Intent mScreenOffIntent; |
| 193 | private Intent mScreenOnIntent; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 194 | private LightsService mLightsService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | private Context mContext; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 196 | private LightsService.Light mLcdLight; |
| 197 | private LightsService.Light mButtonLight; |
| 198 | private LightsService.Light mKeyboardLight; |
| 199 | private LightsService.Light mAttentionLight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | private UnsynchronizedWakeLock mBroadcastWakeLock; |
| 201 | private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock; |
| 202 | private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock; |
| 203 | private UnsynchronizedWakeLock mPreventScreenOnPartialLock; |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 204 | private UnsynchronizedWakeLock mProximityPartialLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | private HandlerThread mHandlerThread; |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 206 | private HandlerThread mScreenOffThread; |
| 207 | private Handler mScreenOffHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | private Handler mHandler; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 209 | private final TimeoutTask mTimeoutTask = new TimeoutTask(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | private final BrightnessState mScreenBrightness |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 211 | = new BrightnessState(SCREEN_BRIGHT_BIT); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 212 | private boolean mStillNeedSleepNotification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | private boolean mIsPowered = false; |
| 214 | private IActivityManager mActivityService; |
| 215 | private IBatteryStats mBatteryStats; |
| 216 | private BatteryService mBatteryService; |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 217 | private SensorManager mSensorManager; |
| 218 | private Sensor mProximitySensor; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 219 | private Sensor mLightSensor; |
| 220 | private boolean mLightSensorEnabled; |
| 221 | private float mLightSensorValue = -1; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 222 | private boolean mProxIgnoredBecauseScreenTurnedOff = false; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 223 | private int mHighestLightSensorValue = -1; |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 224 | private boolean mLightSensorPendingDecrease = false; |
| 225 | private boolean mLightSensorPendingIncrease = false; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 226 | private float mLightSensorPendingValue = -1; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 227 | private int mLightSensorScreenBrightness = -1; |
| 228 | private int mLightSensorButtonBrightness = -1; |
| 229 | private int mLightSensorKeyboardBrightness = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | private boolean mDimScreen = true; |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 231 | private boolean mIsDocked = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | private long mNextTimeout; |
| 233 | private volatile int mPokey = 0; |
| 234 | private volatile boolean mPokeAwakeOnSet = false; |
| 235 | private volatile boolean mInitComplete = false; |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 236 | private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>(); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 237 | // mLastScreenOnTime is the time the screen was last turned on |
| 238 | private long mLastScreenOnTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | private boolean mPreventScreenOn; |
| 240 | private int mScreenBrightnessOverride = -1; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 241 | private int mButtonBrightnessOverride = -1; |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 242 | private int mScreenBrightnessDim; |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 243 | private boolean mUseSoftwareAutoBrightness; |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 244 | private boolean mAutoBrightessEnabled; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 245 | private int[] mAutoBrightnessLevels; |
| 246 | private int[] mLcdBacklightValues; |
| 247 | private int[] mButtonBacklightValues; |
| 248 | private int[] mKeyboardBacklightValues; |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 249 | private int mLightSensorWarmupTime; |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 250 | boolean mUnplugTurnsOnScreen; |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 251 | private int mWarningSpewThrottleCount; |
| 252 | private long mWarningSpewThrottleTime; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 253 | private int mAnimationSetting = ANIM_SETTING_OFF; |
| 254 | |
| 255 | // Must match with the ISurfaceComposer constants in C++. |
| 256 | private static final int ANIM_SETTING_ON = 0x01; |
| 257 | private static final int ANIM_SETTING_OFF = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | // Used when logging number and duration of touch-down cycles |
| 260 | private long mTotalTouchDownTime; |
| 261 | private long mLastTouchDown; |
| 262 | private int mTouchCycles; |
| 263 | |
| 264 | // could be either static or controllable at runtime |
| 265 | private static final boolean mSpew = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 266 | private static final boolean mDebugProximitySensor = (false || mSpew); |
Mike Lockwood | dd9668e | 2009-10-27 15:47:02 -0400 | [diff] [blame] | 267 | private static final boolean mDebugLightSensor = (false || mSpew); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 268 | |
| 269 | private native void nativeInit(); |
| 270 | private native void nativeSetPowerState(boolean screenOn, boolean screenBright); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 271 | private native void nativeStartSurfaceFlingerAnimation(int mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | static PrintStream mLog; |
| 275 | static { |
| 276 | try { |
| 277 | mLog = new PrintStream("/data/power.log"); |
| 278 | } |
| 279 | catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 280 | android.util.Slog.e(TAG, "Life is hard", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | static class Log { |
| 284 | static void d(String tag, String s) { |
| 285 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 286 | android.util.Slog.d(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | static void i(String tag, String s) { |
| 289 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 290 | android.util.Slog.i(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | } |
| 292 | static void w(String tag, String s) { |
| 293 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 294 | android.util.Slog.w(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | static void e(String tag, String s) { |
| 297 | mLog.println(s); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 298 | android.util.Slog.e(tag, s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | */ |
| 302 | |
| 303 | /** |
| 304 | * This class works around a deadlock between the lock in PowerManager.WakeLock |
| 305 | * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its |
| 306 | * mToken object so it can be accessed from any thread, but it calls into here |
| 307 | * with its lock held. This class is essentially a reimplementation of |
| 308 | * PowerManager.WakeLock, but without that extra synchronized block, because we'll |
| 309 | * only call it with our own locks held. |
| 310 | */ |
| 311 | private class UnsynchronizedWakeLock { |
| 312 | int mFlags; |
| 313 | String mTag; |
| 314 | IBinder mToken; |
| 315 | int mCount = 0; |
| 316 | boolean mRefCounted; |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 317 | boolean mHeld; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | |
| 319 | UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) { |
| 320 | mFlags = flags; |
| 321 | mTag = tag; |
| 322 | mToken = new Binder(); |
| 323 | mRefCounted = refCounted; |
| 324 | } |
| 325 | |
| 326 | public void acquire() { |
| 327 | if (!mRefCounted || mCount++ == 0) { |
| 328 | long ident = Binder.clearCallingIdentity(); |
| 329 | try { |
| 330 | PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 331 | MY_UID, MY_PID, mTag, null); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 332 | mHeld = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } finally { |
| 334 | Binder.restoreCallingIdentity(ident); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | public void release() { |
| 340 | if (!mRefCounted || --mCount == 0) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 341 | PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 342 | mHeld = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | } |
| 344 | if (mCount < 0) { |
| 345 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 346 | } |
| 347 | } |
| 348 | |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 349 | public boolean isHeld() |
| 350 | { |
| 351 | return mHeld; |
| 352 | } |
| 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | public String toString() { |
| 355 | return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags) |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 356 | + " mCount=" + mCount + " mHeld=" + mHeld + ")"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | private final class BatteryReceiver extends BroadcastReceiver { |
| 361 | @Override |
| 362 | public void onReceive(Context context, Intent intent) { |
| 363 | synchronized (mLocks) { |
| 364 | boolean wasPowered = mIsPowered; |
| 365 | mIsPowered = mBatteryService.isPowered(); |
| 366 | |
| 367 | if (mIsPowered != wasPowered) { |
| 368 | // update mStayOnWhilePluggedIn wake lock |
| 369 | updateWakeLockLocked(); |
| 370 | |
| 371 | // treat plugging and unplugging the devices as a user activity. |
| 372 | // users find it disconcerting when they unplug the device |
| 373 | // and it shuts off right away. |
Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 374 | // to avoid turning on the screen when unplugging, we only trigger |
| 375 | // user activity when screen was already on. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | // temporarily set mUserActivityAllowed to true so this will work |
| 377 | // even when the keyguard is on. |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 378 | // However, you can also set config_unplugTurnsOnScreen to have it |
| 379 | // turn on. Some devices want this because they don't have a |
| 380 | // charging LED. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | synchronized (mLocks) { |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 382 | if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0 || |
| 383 | mUnplugTurnsOnScreen) { |
Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 384 | forceUserActivityLocked(); |
| 385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 392 | private final class BootCompletedReceiver extends BroadcastReceiver { |
| 393 | @Override |
| 394 | public void onReceive(Context context, Intent intent) { |
| 395 | bootCompleted(); |
| 396 | } |
| 397 | } |
| 398 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 399 | private final class DockReceiver extends BroadcastReceiver { |
| 400 | @Override |
| 401 | public void onReceive(Context context, Intent intent) { |
| 402 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 403 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 404 | dockStateChanged(state); |
| 405 | } |
| 406 | } |
| 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Set the setting that determines whether the device stays on when plugged in. |
| 410 | * The argument is a bit string, with each bit specifying a power source that, |
| 411 | * when the device is connected to that source, causes the device to stay on. |
| 412 | * See {@link android.os.BatteryManager} for the list of power sources that |
| 413 | * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC} |
| 414 | * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB} |
| 415 | * @param val an {@code int} containing the bits that specify which power sources |
| 416 | * should cause the device to stay on. |
| 417 | */ |
| 418 | public void setStayOnSetting(int val) { |
| 419 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null); |
| 420 | Settings.System.putInt(mContext.getContentResolver(), |
| 421 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, val); |
| 422 | } |
| 423 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 424 | public void setMaximumScreenOffTimeount(int timeMs) { |
| 425 | mContext.enforceCallingOrSelfPermission( |
| 426 | android.Manifest.permission.WRITE_SECURE_SETTINGS, null); |
| 427 | synchronized (mLocks) { |
| 428 | mMaximumScreenOffTimeout = timeMs; |
| 429 | // recalculate everything |
| 430 | setScreenOffTimeoutsLocked(); |
| 431 | } |
| 432 | } |
| 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | private class SettingsObserver implements Observer { |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 435 | private int getInt(String name, int defValue) { |
| 436 | ContentValues values = mSettings.getValues(name); |
| 437 | Integer iVal = values != null ? values.getAsInteger(Settings.System.VALUE) : null; |
| 438 | return iVal != null ? iVal : defValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 441 | private float getFloat(String name, float defValue) { |
| 442 | ContentValues values = mSettings.getValues(name); |
| 443 | Float fVal = values != null ? values.getAsFloat(Settings.System.VALUE) : null; |
| 444 | return fVal != null ? fVal : defValue; |
| 445 | } |
| 446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | public void update(Observable o, Object arg) { |
| 448 | synchronized (mLocks) { |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 449 | // STAY_ON_WHILE_PLUGGED_IN, default to when plugged into AC |
| 450 | mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN, |
| 451 | BatteryManager.BATTERY_PLUGGED_AC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | updateWakeLockLocked(); |
| 453 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 454 | // SCREEN_OFF_TIMEOUT, default to 15 seconds |
| 455 | mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 457 | // DIM_SCREEN |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | //mDimScreen = getInt(DIM_SCREEN) != 0; |
| 459 | |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 460 | // SCREEN_BRIGHTNESS_MODE, default to manual |
| 461 | setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE, |
| 462 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)); |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | // recalculate everything |
| 465 | setScreenOffTimeoutsLocked(); |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 466 | |
| 467 | final float windowScale = getFloat(WINDOW_ANIMATION_SCALE, 1.0f); |
| 468 | final float transitionScale = getFloat(TRANSITION_ANIMATION_SCALE, 1.0f); |
| 469 | mAnimationSetting = 0; |
| 470 | if (windowScale > 0.5f) { |
| 471 | mAnimationSetting |= ANIM_SETTING_OFF; |
| 472 | } |
| 473 | if (transitionScale > 0.5f) { |
| 474 | // Uncomment this if you want the screen-on animation. |
| 475 | // mAnimationSetting |= ANIM_SETTING_ON; |
| 476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 481 | PowerManagerService() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | // Hack to get our uid... should have a func for this. |
| 483 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 484 | MY_UID = Process.myUid(); |
| 485 | MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | Binder.restoreCallingIdentity(token); |
| 487 | |
| 488 | // XXX remove this when the kernel doesn't timeout wake locks |
| 489 | Power.setLastUserActivityTimeout(7*24*3600*1000); // one week |
| 490 | |
| 491 | // assume nothing is on yet |
| 492 | mUserState = mPowerState = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | // Add ourself to the Watchdog monitors. |
| 495 | Watchdog.getInstance().addMonitor(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | private ContentQueryMap mSettings; |
| 499 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 500 | void init(Context context, LightsService lights, IActivityManager activity, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 501 | BatteryService battery) { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 502 | mLightsService = lights; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | mContext = context; |
| 504 | mActivityService = activity; |
| 505 | mBatteryStats = BatteryStatsService.getService(); |
| 506 | mBatteryService = battery; |
| 507 | |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 508 | mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT); |
| 509 | mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS); |
| 510 | mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD); |
| 511 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 512 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 513 | nativeInit(); |
| 514 | synchronized (mLocks) { |
| 515 | updateNativePowerStateLocked(); |
| 516 | } |
| 517 | |
| 518 | mInitComplete = false; |
| 519 | mScreenOffThread = new HandlerThread("PowerManagerService.mScreenOffThread") { |
| 520 | @Override |
| 521 | protected void onLooperPrepared() { |
| 522 | mScreenOffHandler = new Handler(); |
| 523 | synchronized (mScreenOffThread) { |
| 524 | mInitComplete = true; |
| 525 | mScreenOffThread.notifyAll(); |
| 526 | } |
| 527 | } |
| 528 | }; |
| 529 | mScreenOffThread.start(); |
| 530 | |
| 531 | synchronized (mScreenOffThread) { |
| 532 | while (!mInitComplete) { |
| 533 | try { |
| 534 | mScreenOffThread.wait(); |
| 535 | } catch (InterruptedException e) { |
| 536 | // Ignore |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | mInitComplete = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | mHandlerThread = new HandlerThread("PowerManagerService") { |
| 543 | @Override |
| 544 | protected void onLooperPrepared() { |
| 545 | super.onLooperPrepared(); |
| 546 | initInThread(); |
| 547 | } |
| 548 | }; |
| 549 | mHandlerThread.start(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 550 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | synchronized (mHandlerThread) { |
| 552 | while (!mInitComplete) { |
| 553 | try { |
| 554 | mHandlerThread.wait(); |
| 555 | } catch (InterruptedException e) { |
| 556 | // Ignore |
| 557 | } |
| 558 | } |
| 559 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 560 | |
| 561 | nativeInit(); |
| 562 | synchronized (mLocks) { |
| 563 | updateNativePowerStateLocked(); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 564 | // We make sure to start out with the screen on due to user activity. |
| 565 | // (They did just boot their device, after all.) |
| 566 | forceUserActivityLocked(); |
Dianne Hackborn | 4001109 | 2011-09-22 13:37:48 -0700 | [diff] [blame] | 567 | mInitialized = true; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 568 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | void initInThread() { |
| 572 | mHandler = new Handler(); |
| 573 | |
| 574 | mBroadcastWakeLock = new UnsynchronizedWakeLock( |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 575 | PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock( |
| 577 | PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false); |
| 578 | mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock( |
| 579 | PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false); |
| 580 | mPreventScreenOnPartialLock = new UnsynchronizedWakeLock( |
| 581 | PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 582 | mProximityPartialLock = new UnsynchronizedWakeLock( |
| 583 | PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | |
| 585 | mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON); |
| 586 | mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 587 | mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF); |
| 588 | mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 589 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 590 | Resources resources = mContext.getResources(); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 591 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 592 | mAnimateScreenLights = resources.getBoolean( |
| 593 | com.android.internal.R.bool.config_animateScreenLights); |
| 594 | |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 595 | mUnplugTurnsOnScreen = resources.getBoolean( |
| 596 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
| 597 | |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 598 | mScreenBrightnessDim = resources.getInteger( |
| 599 | com.android.internal.R.integer.config_screenBrightnessDim); |
| 600 | |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 601 | // read settings for auto-brightness |
| 602 | mUseSoftwareAutoBrightness = resources.getBoolean( |
| 603 | com.android.internal.R.bool.config_automatic_brightness_available); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 604 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 605 | mAutoBrightnessLevels = resources.getIntArray( |
| 606 | com.android.internal.R.array.config_autoBrightnessLevels); |
| 607 | mLcdBacklightValues = resources.getIntArray( |
| 608 | com.android.internal.R.array.config_autoBrightnessLcdBacklightValues); |
| 609 | mButtonBacklightValues = resources.getIntArray( |
| 610 | com.android.internal.R.array.config_autoBrightnessButtonBacklightValues); |
| 611 | mKeyboardBacklightValues = resources.getIntArray( |
| 612 | com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 613 | mLightSensorWarmupTime = resources.getInteger( |
| 614 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 615 | } |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 616 | |
| 617 | ContentResolver resolver = mContext.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null, |
| 619 | "(" + Settings.System.NAME + "=?) or (" |
| 620 | + Settings.System.NAME + "=?) or (" |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 621 | + Settings.System.NAME + "=?) or (" |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 622 | + Settings.System.NAME + "=?) or (" |
| 623 | + Settings.System.NAME + "=?) or (" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | + Settings.System.NAME + "=?)", |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 625 | new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 626 | SCREEN_BRIGHTNESS_MODE, WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | null); |
| 628 | mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler); |
| 629 | SettingsObserver settingsObserver = new SettingsObserver(); |
| 630 | mSettings.addObserver(settingsObserver); |
| 631 | |
| 632 | // pretend that the settings changed so we will get their initial state |
| 633 | settingsObserver.update(mSettings, null); |
| 634 | |
| 635 | // register for the battery changed notifications |
| 636 | IntentFilter filter = new IntentFilter(); |
| 637 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 638 | mContext.registerReceiver(new BatteryReceiver(), filter); |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 639 | filter = new IntentFilter(); |
| 640 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 641 | mContext.registerReceiver(new BootCompletedReceiver(), filter); |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 642 | filter = new IntentFilter(); |
| 643 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
| 644 | mContext.registerReceiver(new DockReceiver(), filter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 646 | // Listen for secure settings changes |
| 647 | mContext.getContentResolver().registerContentObserver( |
| 648 | Settings.Secure.CONTENT_URI, true, |
| 649 | new ContentObserver(new Handler()) { |
| 650 | public void onChange(boolean selfChange) { |
| 651 | updateSettingsValues(); |
| 652 | } |
| 653 | }); |
| 654 | updateSettingsValues(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | synchronized (mHandlerThread) { |
| 657 | mInitComplete = true; |
| 658 | mHandlerThread.notifyAll(); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | private class WakeLock implements IBinder.DeathRecipient |
| 663 | { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 664 | WakeLock(int f, IBinder b, String t, int u, int p) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | super(); |
| 666 | flags = f; |
| 667 | binder = b; |
| 668 | tag = t; |
| 669 | uid = u == MY_UID ? Process.SYSTEM_UID : u; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 670 | pid = p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | if (u != MY_UID || ( |
| 672 | !"KEEP_SCREEN_ON_FLAG".equals(tag) |
| 673 | && !"KeyInputQueue".equals(tag))) { |
| 674 | monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK |
| 675 | ? BatteryStats.WAKE_TYPE_PARTIAL |
| 676 | : BatteryStats.WAKE_TYPE_FULL; |
| 677 | } else { |
| 678 | monitorType = -1; |
| 679 | } |
| 680 | try { |
| 681 | b.linkToDeath(this, 0); |
| 682 | } catch (RemoteException e) { |
| 683 | binderDied(); |
| 684 | } |
| 685 | } |
| 686 | public void binderDied() { |
| 687 | synchronized (mLocks) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 688 | releaseWakeLockLocked(this.binder, 0, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | final int flags; |
| 692 | final IBinder binder; |
| 693 | final String tag; |
| 694 | final int uid; |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 695 | final int pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | final int monitorType; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 697 | WorkSource ws; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | boolean activated = true; |
| 699 | int minState; |
| 700 | } |
| 701 | |
| 702 | private void updateWakeLockLocked() { |
| 703 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 704 | // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set. |
| 705 | mStayOnWhilePluggedInScreenDimLock.acquire(); |
| 706 | mStayOnWhilePluggedInPartialLock.acquire(); |
| 707 | } else { |
| 708 | mStayOnWhilePluggedInScreenDimLock.release(); |
| 709 | mStayOnWhilePluggedInPartialLock.release(); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | private boolean isScreenLock(int flags) |
| 714 | { |
| 715 | int n = flags & LOCK_MASK; |
| 716 | return n == PowerManager.FULL_WAKE_LOCK |
| 717 | || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 718 | || n == PowerManager.SCREEN_DIM_WAKE_LOCK |
| 719 | || n == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 722 | void enforceWakeSourcePermission(int uid, int pid) { |
| 723 | if (uid == Process.myUid()) { |
| 724 | return; |
| 725 | } |
| 726 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 727 | pid, uid, null); |
| 728 | } |
| 729 | |
| 730 | public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | int uid = Binder.getCallingUid(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 732 | int pid = Binder.getCallingPid(); |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 733 | if (uid != Process.myUid()) { |
| 734 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 735 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 736 | if (ws != null) { |
| 737 | enforceWakeSourcePermission(uid, pid); |
| 738 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | long ident = Binder.clearCallingIdentity(); |
| 740 | try { |
| 741 | synchronized (mLocks) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 742 | acquireWakeLockLocked(flags, lock, uid, pid, tag, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | } |
| 744 | } finally { |
| 745 | Binder.restoreCallingIdentity(ident); |
| 746 | } |
| 747 | } |
| 748 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 749 | void noteStartWakeLocked(WakeLock wl, WorkSource ws) { |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 750 | if (wl.monitorType >= 0) { |
| 751 | long origId = Binder.clearCallingIdentity(); |
| 752 | try { |
| 753 | if (ws != null) { |
| 754 | mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag, |
| 755 | wl.monitorType); |
| 756 | } else { |
| 757 | mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 758 | } |
| 759 | } catch (RemoteException e) { |
| 760 | // Ignore |
| 761 | } finally { |
| 762 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 763 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 767 | void noteStopWakeLocked(WakeLock wl, WorkSource ws) { |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 768 | if (wl.monitorType >= 0) { |
| 769 | long origId = Binder.clearCallingIdentity(); |
| 770 | try { |
| 771 | if (ws != null) { |
| 772 | mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag, |
| 773 | wl.monitorType); |
| 774 | } else { |
| 775 | mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 776 | } |
| 777 | } catch (RemoteException e) { |
| 778 | // Ignore |
| 779 | } finally { |
| 780 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 781 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag, |
| 786 | WorkSource ws) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 788 | Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 791 | if (ws != null && ws.size() == 0) { |
| 792 | ws = null; |
| 793 | } |
| 794 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | int index = mLocks.getIndex(lock); |
| 796 | WakeLock wl; |
| 797 | boolean newlock; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 798 | boolean diffsource; |
| 799 | WorkSource oldsource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | if (index < 0) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 801 | wl = new WakeLock(flags, lock, tag, uid, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | switch (wl.flags & LOCK_MASK) |
| 803 | { |
| 804 | case PowerManager.FULL_WAKE_LOCK: |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 805 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 806 | wl.minState = SCREEN_BRIGHT; |
| 807 | } else { |
| 808 | wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | break; |
| 811 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 812 | wl.minState = SCREEN_BRIGHT; |
| 813 | break; |
| 814 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 815 | wl.minState = SCREEN_DIM; |
| 816 | break; |
| 817 | case PowerManager.PARTIAL_WAKE_LOCK: |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 818 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | break; |
| 820 | default: |
| 821 | // just log and bail. we're in the server, so don't |
| 822 | // throw an exception. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 823 | Slog.e(TAG, "bad wakelock type for lock '" + tag + "' " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | + " flags=" + flags); |
| 825 | return; |
| 826 | } |
| 827 | mLocks.addLock(wl); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 828 | if (ws != null) { |
| 829 | wl.ws = new WorkSource(ws); |
| 830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | newlock = true; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 832 | diffsource = false; |
| 833 | oldsource = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } else { |
| 835 | wl = mLocks.get(index); |
| 836 | newlock = false; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 837 | oldsource = wl.ws; |
| 838 | if (oldsource != null) { |
| 839 | if (ws == null) { |
| 840 | wl.ws = null; |
| 841 | diffsource = true; |
| 842 | } else { |
| 843 | diffsource = oldsource.diff(ws); |
| 844 | } |
| 845 | } else if (ws != null) { |
| 846 | diffsource = true; |
| 847 | } else { |
| 848 | diffsource = false; |
| 849 | } |
| 850 | if (diffsource) { |
| 851 | wl.ws = new WorkSource(ws); |
| 852 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | } |
| 854 | if (isScreenLock(flags)) { |
| 855 | // if this causes a wakeup, we reactivate all of the locks and |
| 856 | // set it to whatever they want. otherwise, we modulate that |
| 857 | // by the current state so we never turn it more on than |
| 858 | // it already is. |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 859 | if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 860 | mProximityWakeLockCount++; |
| 861 | if (mProximityWakeLockCount == 1) { |
| 862 | enableProximityLockLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | } else { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 865 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 866 | int oldWakeLockState = mWakeLockState; |
| 867 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
Mike Lockwood | db97f60 | 2011-09-02 11:59:08 -0400 | [diff] [blame] | 868 | |
| 869 | // Disable proximity sensor if if user presses power key while we are in the |
| 870 | // "waiting for proximity sensor to go negative" state. |
| 871 | if ((mWakeLockState & SCREEN_ON_BIT) != 0 |
| 872 | && mProximitySensorActive && mProximityWakeLockCount == 0) { |
| 873 | mProximitySensorActive = false; |
| 874 | } |
| 875 | |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 876 | if (mSpew) { |
| 877 | Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState) |
| 878 | + " mWakeLockState=0x" |
| 879 | + Integer.toHexString(mWakeLockState) |
| 880 | + " previous wakeLockState=0x" |
| 881 | + Integer.toHexString(oldWakeLockState)); |
| 882 | } |
| 883 | } else { |
| 884 | if (mSpew) { |
| 885 | Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState) |
| 886 | + " mLocks.gatherState()=0x" |
| 887 | + Integer.toHexString(mLocks.gatherState()) |
| 888 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)); |
| 889 | } |
| 890 | mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 892 | setPowerState(mWakeLockState | mUserState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | } |
| 895 | else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 896 | if (newlock) { |
| 897 | mPartialCount++; |
| 898 | if (mPartialCount == 1) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 899 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME); |
| 903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 905 | if (diffsource) { |
| 906 | // If the lock sources have changed, need to first release the |
| 907 | // old ones. |
| 908 | noteStopWakeLocked(wl, oldsource); |
| 909 | } |
| 910 | if (newlock || diffsource) { |
| 911 | noteStartWakeLocked(wl, ws); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) { |
| 916 | int uid = Binder.getCallingUid(); |
| 917 | int pid = Binder.getCallingPid(); |
| 918 | if (ws != null && ws.size() == 0) { |
| 919 | ws = null; |
| 920 | } |
| 921 | if (ws != null) { |
| 922 | enforceWakeSourcePermission(uid, pid); |
| 923 | } |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 924 | synchronized (mLocks) { |
| 925 | int index = mLocks.getIndex(lock); |
| 926 | if (index < 0) { |
| 927 | throw new IllegalArgumentException("Wake lock not active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | } |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 929 | WakeLock wl = mLocks.get(index); |
| 930 | WorkSource oldsource = wl.ws; |
| 931 | wl.ws = ws != null ? new WorkSource(ws) : null; |
| 932 | noteStopWakeLocked(wl, oldsource); |
| 933 | noteStartWakeLocked(wl, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 937 | public void releaseWakeLock(IBinder lock, int flags) { |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 938 | int uid = Binder.getCallingUid(); |
| 939 | if (uid != Process.myUid()) { |
| 940 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | |
| 943 | synchronized (mLocks) { |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 944 | releaseWakeLockLocked(lock, flags, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 948 | private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | WakeLock wl = mLocks.removeLock(lock); |
| 950 | if (wl == null) { |
| 951 | return; |
| 952 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 955 | Slog.d(TAG, "releaseWakeLock flags=0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | + Integer.toHexString(wl.flags) + " tag=" + wl.tag); |
| 957 | } |
| 958 | |
| 959 | if (isScreenLock(wl.flags)) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 960 | if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 961 | mProximityWakeLockCount--; |
| 962 | if (mProximityWakeLockCount == 0) { |
| 963 | if (mProximitySensorActive && |
| 964 | ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) { |
| 965 | // wait for proximity sensor to go negative before disabling sensor |
| 966 | if (mDebugProximitySensor) { |
| 967 | Slog.d(TAG, "waiting for proximity sensor to go negative"); |
| 968 | } |
| 969 | } else { |
| 970 | disableProximityLockLocked(); |
| 971 | } |
| 972 | } |
| 973 | } else { |
| 974 | mWakeLockState = mLocks.gatherState(); |
| 975 | // goes in the middle to reduce flicker |
| 976 | if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) { |
| 977 | userActivity(SystemClock.uptimeMillis(), -1, false, OTHER_EVENT, false); |
| 978 | } |
| 979 | setPowerState(mWakeLockState | mUserState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | } |
| 982 | else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 983 | mPartialCount--; |
| 984 | if (mPartialCount == 0) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 985 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | Power.releaseWakeLock(PARTIAL_NAME); |
| 987 | } |
| 988 | } |
| 989 | // Unlink the lock from the binder. |
| 990 | wl.binder.unlinkToDeath(wl, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | |
Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 992 | noteStopWakeLocked(wl, wl.ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | } |
| 994 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | private class PokeLock implements IBinder.DeathRecipient |
| 996 | { |
| 997 | PokeLock(int p, IBinder b, String t) { |
| 998 | super(); |
| 999 | this.pokey = p; |
| 1000 | this.binder = b; |
| 1001 | this.tag = t; |
| 1002 | try { |
| 1003 | b.linkToDeath(this, 0); |
| 1004 | } catch (RemoteException e) { |
| 1005 | binderDied(); |
| 1006 | } |
| 1007 | } |
| 1008 | public void binderDied() { |
| 1009 | setPokeLock(0, this.binder, this.tag); |
| 1010 | } |
| 1011 | int pokey; |
| 1012 | IBinder binder; |
| 1013 | String tag; |
| 1014 | boolean awakeOnSet; |
| 1015 | } |
| 1016 | |
| 1017 | public void setPokeLock(int pokey, IBinder token, String tag) { |
| 1018 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1019 | if (token == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1020 | Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) { |
| 1025 | throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT" |
| 1026 | + " and POKE_LOCK_MEDIUM_TIMEOUT"); |
| 1027 | } |
| 1028 | |
| 1029 | synchronized (mLocks) { |
| 1030 | if (pokey != 0) { |
| 1031 | PokeLock p = mPokeLocks.get(token); |
| 1032 | int oldPokey = 0; |
| 1033 | if (p != null) { |
| 1034 | oldPokey = p.pokey; |
| 1035 | p.pokey = pokey; |
| 1036 | } else { |
| 1037 | p = new PokeLock(pokey, token, tag); |
| 1038 | mPokeLocks.put(token, p); |
| 1039 | } |
| 1040 | int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1041 | int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
| 1042 | if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) { |
| 1043 | p.awakeOnSet = true; |
| 1044 | } |
| 1045 | } else { |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1046 | PokeLock rLock = mPokeLocks.remove(token); |
| 1047 | if (rLock != null) { |
| 1048 | token.unlinkToDeath(rLock, 0); |
| 1049 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | int oldPokey = mPokey; |
| 1053 | int cumulative = 0; |
| 1054 | boolean oldAwakeOnSet = mPokeAwakeOnSet; |
| 1055 | boolean awakeOnSet = false; |
| 1056 | for (PokeLock p: mPokeLocks.values()) { |
| 1057 | cumulative |= p.pokey; |
| 1058 | if (p.awakeOnSet) { |
| 1059 | awakeOnSet = true; |
| 1060 | } |
| 1061 | } |
| 1062 | mPokey = cumulative; |
| 1063 | mPokeAwakeOnSet = awakeOnSet; |
| 1064 | |
| 1065 | int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1066 | int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | if (oldCumulativeTimeout != newCumulativeTimeout) { |
| 1069 | setScreenOffTimeoutsLocked(); |
| 1070 | // reset the countdown timer, but use the existing nextState so it doesn't |
| 1071 | // change anything |
| 1072 | setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | private static String lockType(int type) |
| 1078 | { |
| 1079 | switch (type) |
| 1080 | { |
| 1081 | case PowerManager.FULL_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1082 | return "FULL_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1084 | return "SCREEN_BRIGHT_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1086 | return "SCREEN_DIM_WAKE_LOCK "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | case PowerManager.PARTIAL_WAKE_LOCK: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1088 | return "PARTIAL_WAKE_LOCK "; |
| 1089 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 1090 | return "PROXIMITY_SCREEN_OFF_WAKE_LOCK"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | default: |
David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1092 | return "??? "; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | private static String dumpPowerState(int state) { |
| 1097 | return (((state & KEYBOARD_BRIGHT_BIT) != 0) |
| 1098 | ? "KEYBOARD_BRIGHT_BIT " : "") |
| 1099 | + (((state & SCREEN_BRIGHT_BIT) != 0) |
| 1100 | ? "SCREEN_BRIGHT_BIT " : "") |
| 1101 | + (((state & SCREEN_ON_BIT) != 0) |
| 1102 | ? "SCREEN_ON_BIT " : "") |
| 1103 | + (((state & BATTERY_LOW_BIT) != 0) |
| 1104 | ? "BATTERY_LOW_BIT " : ""); |
| 1105 | } |
| 1106 | |
| 1107 | @Override |
| 1108 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1109 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1110 | != PackageManager.PERMISSION_GRANTED) { |
| 1111 | pw.println("Permission Denial: can't dump PowerManager from from pid=" |
| 1112 | + Binder.getCallingPid() |
| 1113 | + ", uid=" + Binder.getCallingUid()); |
| 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | long now = SystemClock.uptimeMillis(); |
| 1118 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1119 | synchronized (mLocks) { |
| 1120 | pw.println("Power Manager State:"); |
| 1121 | pw.println(" mIsPowered=" + mIsPowered |
| 1122 | + " mPowerState=" + mPowerState |
| 1123 | + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime) |
| 1124 | + " ms"); |
| 1125 | pw.println(" mPartialCount=" + mPartialCount); |
| 1126 | pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState)); |
| 1127 | pw.println(" mUserState=" + dumpPowerState(mUserState)); |
| 1128 | pw.println(" mPowerState=" + dumpPowerState(mPowerState)); |
| 1129 | pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState())); |
| 1130 | pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now |
| 1131 | + " " + ((mNextTimeout-now)/1000) + "s from now"); |
| 1132 | pw.println(" mDimScreen=" + mDimScreen |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1133 | + " mStayOnConditions=" + mStayOnConditions |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1134 | + " mPreparingForScreenOn=" + mPreparingForScreenOn |
| 1135 | + " mSkippedScreenOn=" + mSkippedScreenOn); |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1136 | pw.println(" mScreenOffReason=" + mScreenOffReason |
| 1137 | + " mUserState=" + mUserState); |
| 1138 | pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1] |
| 1139 | + ',' + mBroadcastQueue[2] + "}"); |
| 1140 | pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1] |
| 1141 | + ',' + mBroadcastWhy[2] + "}"); |
| 1142 | pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet); |
| 1143 | pw.println(" mKeyboardVisible=" + mKeyboardVisible |
| 1144 | + " mUserActivityAllowed=" + mUserActivityAllowed); |
| 1145 | pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay |
| 1146 | + " mScreenOffDelay=" + mScreenOffDelay); |
| 1147 | pw.println(" mPreventScreenOn=" + mPreventScreenOn |
| 1148 | + " mScreenBrightnessOverride=" + mScreenBrightnessOverride |
| 1149 | + " mButtonBrightnessOverride=" + mButtonBrightnessOverride); |
| 1150 | pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting |
| 1151 | + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout); |
| 1152 | pw.println(" mLastScreenOnTime=" + mLastScreenOnTime); |
| 1153 | pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock); |
| 1154 | pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock); |
| 1155 | pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock); |
| 1156 | pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock); |
| 1157 | pw.println(" mProximityPartialLock=" + mProximityPartialLock); |
| 1158 | pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount); |
| 1159 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1160 | pw.println(" mProximitySensorActive=" + mProximitySensorActive); |
| 1161 | pw.println(" mProximityPendingValue=" + mProximityPendingValue); |
| 1162 | pw.println(" mLastProximityEventTime=" + mLastProximityEventTime); |
| 1163 | pw.println(" mLightSensorEnabled=" + mLightSensorEnabled); |
| 1164 | pw.println(" mLightSensorValue=" + mLightSensorValue |
| 1165 | + " mLightSensorPendingValue=" + mLightSensorPendingValue); |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1166 | pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease |
| 1167 | + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease); |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1168 | pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness |
| 1169 | + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness |
| 1170 | + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness); |
| 1171 | pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness); |
| 1172 | pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 1173 | mScreenBrightness.dump(pw, " mScreenBrightness: "); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1174 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1175 | int N = mLocks.size(); |
| 1176 | pw.println(); |
| 1177 | pw.println("mLocks.size=" + N + ":"); |
| 1178 | for (int i=0; i<N; i++) { |
| 1179 | WakeLock wl = mLocks.get(i); |
| 1180 | String type = lockType(wl.flags & LOCK_MASK); |
| 1181 | String acquireCausesWakeup = ""; |
| 1182 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 1183 | acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP "; |
| 1184 | } |
| 1185 | String activated = ""; |
| 1186 | if (wl.activated) { |
| 1187 | activated = " activated"; |
| 1188 | } |
| 1189 | pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup |
Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 1190 | + activated + " (minState=" + wl.minState + ", uid=" + wl.uid |
| 1191 | + ", pid=" + wl.pid + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1193 | |
| 1194 | pw.println(); |
| 1195 | pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":"); |
| 1196 | for (PokeLock p: mPokeLocks.values()) { |
| 1197 | pw.println(" poke lock '" + p.tag + "':" |
Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 1198 | + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0 |
| 1199 | ? " POKE_LOCK_IGNORE_TOUCH_EVENTS" : "") |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1200 | + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0 |
| 1201 | ? " POKE_LOCK_SHORT_TIMEOUT" : "") |
| 1202 | + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0 |
| 1203 | ? " POKE_LOCK_MEDIUM_TIMEOUT" : "")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1205 | |
Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1206 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1210 | private void setTimeoutLocked(long now, int nextState) { |
| 1211 | setTimeoutLocked(now, -1, nextState); |
| 1212 | } |
| 1213 | |
| 1214 | // If they gave a timeoutOverride it is the number of seconds |
| 1215 | // to screen-off. Figure out where in the countdown cycle we |
| 1216 | // should jump to. |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1217 | private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) { |
| 1218 | long timeoutOverride = originalTimeoutOverride; |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1219 | if (mBootCompleted) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1220 | synchronized (mLocks) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1221 | long when = 0; |
| 1222 | if (timeoutOverride <= 0) { |
| 1223 | switch (nextState) |
| 1224 | { |
| 1225 | case SCREEN_BRIGHT: |
| 1226 | when = now + mKeylightDelay; |
| 1227 | break; |
| 1228 | case SCREEN_DIM: |
| 1229 | if (mDimDelay >= 0) { |
| 1230 | when = now + mDimDelay; |
Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1231 | break; |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1232 | } else { |
| 1233 | Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim"); |
| 1234 | } |
| 1235 | case SCREEN_OFF: |
| 1236 | synchronized (mLocks) { |
| 1237 | when = now + mScreenOffDelay; |
| 1238 | } |
| 1239 | break; |
Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1240 | default: |
| 1241 | when = now; |
| 1242 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | } |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1244 | } else { |
| 1245 | override: { |
| 1246 | if (timeoutOverride <= mScreenOffDelay) { |
| 1247 | when = now + timeoutOverride; |
| 1248 | nextState = SCREEN_OFF; |
| 1249 | break override; |
| 1250 | } |
| 1251 | timeoutOverride -= mScreenOffDelay; |
| 1252 | |
| 1253 | if (mDimDelay >= 0) { |
| 1254 | if (timeoutOverride <= mDimDelay) { |
| 1255 | when = now + timeoutOverride; |
| 1256 | nextState = SCREEN_DIM; |
| 1257 | break override; |
| 1258 | } |
| 1259 | timeoutOverride -= mDimDelay; |
| 1260 | } |
| 1261 | |
| 1262 | when = now + timeoutOverride; |
| 1263 | nextState = SCREEN_BRIGHT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | } |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1265 | } |
| 1266 | if (mSpew) { |
| 1267 | Slog.d(TAG, "setTimeoutLocked now=" + now |
| 1268 | + " timeoutOverride=" + timeoutOverride |
| 1269 | + " nextState=" + nextState + " when=" + when); |
| 1270 | } |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1271 | |
| 1272 | mHandler.removeCallbacks(mTimeoutTask); |
| 1273 | mTimeoutTask.nextState = nextState; |
| 1274 | mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0 |
| 1275 | ? (originalTimeoutOverride - timeoutOverride) |
| 1276 | : -1; |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1277 | mHandler.postAtTime(mTimeoutTask, when); |
| 1278 | mNextTimeout = when; // for debugging |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | private void cancelTimerLocked() |
| 1284 | { |
| 1285 | mHandler.removeCallbacks(mTimeoutTask); |
| 1286 | mTimeoutTask.nextState = -1; |
| 1287 | } |
| 1288 | |
| 1289 | private class TimeoutTask implements Runnable |
| 1290 | { |
| 1291 | int nextState; // access should be synchronized on mLocks |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1292 | long remainingTimeoutOverride; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | public void run() |
| 1294 | { |
| 1295 | synchronized (mLocks) { |
| 1296 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1297 | Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | if (nextState == -1) { |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | mUserState = this.nextState; |
| 1305 | setPowerState(this.nextState | mWakeLockState); |
| 1306 | |
| 1307 | long now = SystemClock.uptimeMillis(); |
| 1308 | |
| 1309 | switch (this.nextState) |
| 1310 | { |
| 1311 | case SCREEN_BRIGHT: |
| 1312 | if (mDimDelay >= 0) { |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1313 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | break; |
| 1315 | } |
| 1316 | case SCREEN_DIM: |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1317 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1324 | private void sendNotificationLocked(boolean on, int why) { |
| 1325 | if (!mInitialized) { |
| 1326 | // No notifications sent until first initialization is done. |
| 1327 | // This is so that when we are moving from our initial state |
| 1328 | // which looks like the screen was off to it being on, we do not |
| 1329 | // go through the process of waiting for the higher-level user |
| 1330 | // space to be ready before turning up the display brightness. |
| 1331 | // (And also do not send needless broadcasts about the screen.) |
| 1332 | return; |
| 1333 | } |
Dianne Hackborn | 4001109 | 2011-09-22 13:37:48 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (DEBUG_SCREEN_ON) { |
| 1336 | RuntimeException here = new RuntimeException("here"); |
| 1337 | here.fillInStackTrace(); |
| 1338 | Slog.i(TAG, "sendNotificationLocked: " + on, here); |
| 1339 | } |
| 1340 | |
Joe Onorato | 64c62ba | 2009-03-24 20:13:57 -0700 | [diff] [blame] | 1341 | if (!on) { |
| 1342 | mStillNeedSleepNotification = false; |
| 1343 | } |
| 1344 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1345 | // Add to the queue. |
| 1346 | int index = 0; |
| 1347 | while (mBroadcastQueue[index] != -1) { |
| 1348 | index++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1350 | mBroadcastQueue[index] = on ? 1 : 0; |
| 1351 | mBroadcastWhy[index] = why; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1353 | // If we added it position 2, then there is a pair that can be stripped. |
| 1354 | // If we added it position 1 and we're turning the screen off, we can strip |
| 1355 | // the pair and do nothing, because the screen is already off, and therefore |
| 1356 | // keyguard has already been enabled. |
| 1357 | // However, if we added it at position 1 and we're turning it on, then position |
| 1358 | // 0 was to turn it off, and we can't strip that, because keyguard needs to come |
| 1359 | // on, so have to run the queue then. |
| 1360 | if (index == 2) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1361 | // While we're collapsing them, if it's going off, and the new reason |
| 1362 | // is more significant than the first, then use the new one. |
| 1363 | if (!on && mBroadcastWhy[0] > why) { |
| 1364 | mBroadcastWhy[0] = why; |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1365 | } |
| 1366 | mBroadcastQueue[0] = on ? 1 : 0; |
| 1367 | mBroadcastQueue[1] = -1; |
| 1368 | mBroadcastQueue[2] = -1; |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1369 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
Mike Lockwood | 9c90a37 | 2010-04-13 15:40:27 -0400 | [diff] [blame] | 1370 | mBroadcastWakeLock.release(); |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1371 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
Mike Lockwood | 9c90a37 | 2010-04-13 15:40:27 -0400 | [diff] [blame] | 1372 | mBroadcastWakeLock.release(); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1373 | index = 0; |
| 1374 | } |
| 1375 | if (index == 1 && !on) { |
| 1376 | mBroadcastQueue[0] = -1; |
| 1377 | mBroadcastQueue[1] = -1; |
| 1378 | index = -1; |
| 1379 | // The wake lock was being held, but we're not actually going to do any |
| 1380 | // broadcasts, so release the wake lock. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1381 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1382 | mBroadcastWakeLock.release(); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1385 | // The broadcast queue has changed; make sure the screen is on if it |
| 1386 | // is now possible for it to be. |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1387 | if (mSkippedScreenOn) { |
| 1388 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1389 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1390 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1391 | // Now send the message. |
| 1392 | if (index >= 0) { |
| 1393 | // Acquire the broadcast wake lock before changing the power |
| 1394 | // state. It will be release after the broadcast is sent. |
| 1395 | // We always increment the ref count for each notification in the queue |
| 1396 | // and always decrement when that notification is handled. |
| 1397 | mBroadcastWakeLock.acquire(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1398 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1399 | mHandler.post(mNotificationTask); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1403 | private WindowManagerPolicy.ScreenOnListener mScreenOnListener = |
| 1404 | new WindowManagerPolicy.ScreenOnListener() { |
| 1405 | @Override public void onScreenOn() { |
| 1406 | synchronized (mLocks) { |
| 1407 | if (mPreparingForScreenOn) { |
| 1408 | mPreparingForScreenOn = false; |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1409 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1410 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, |
| 1411 | 4, mBroadcastWakeLock.mCount); |
| 1412 | mBroadcastWakeLock.release(); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | }; |
| 1417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | private Runnable mNotificationTask = new Runnable() |
| 1419 | { |
| 1420 | public void run() |
| 1421 | { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1422 | while (true) { |
| 1423 | int value; |
| 1424 | int why; |
| 1425 | WindowManagerPolicy policy; |
| 1426 | synchronized (mLocks) { |
| 1427 | value = mBroadcastQueue[0]; |
| 1428 | why = mBroadcastWhy[0]; |
| 1429 | for (int i=0; i<2; i++) { |
| 1430 | mBroadcastQueue[i] = mBroadcastQueue[i+1]; |
| 1431 | mBroadcastWhy[i] = mBroadcastWhy[i+1]; |
| 1432 | } |
| 1433 | policy = getPolicyLocked(); |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1434 | if (value == 1 && !mPreparingForScreenOn) { |
| 1435 | mPreparingForScreenOn = true; |
| 1436 | mBroadcastWakeLock.acquire(); |
| 1437 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, |
| 1438 | mBroadcastWakeLock.mCount); |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1441 | if (value == 1) { |
| 1442 | mScreenOnStart = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1443 | |
Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1444 | policy.screenTurningOn(mScreenOnListener); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1445 | try { |
| 1446 | ActivityManagerNative.getDefault().wakingUp(); |
| 1447 | } catch (RemoteException e) { |
| 1448 | // ignore it |
| 1449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1451 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1452 | Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1453 | } |
| 1454 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1455 | mContext.sendOrderedBroadcast(mScreenOnIntent, null, |
| 1456 | mScreenOnBroadcastDone, mHandler, 0, null, null); |
| 1457 | } else { |
| 1458 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1459 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1460 | mBroadcastWakeLock.mCount); |
| 1461 | mBroadcastWakeLock.release(); |
| 1462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | } |
| 1464 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1465 | else if (value == 0) { |
| 1466 | mScreenOffStart = SystemClock.uptimeMillis(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1467 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1468 | policy.screenTurnedOff(why); |
| 1469 | try { |
| 1470 | ActivityManagerNative.getDefault().goingToSleep(); |
| 1471 | } catch (RemoteException e) { |
| 1472 | // ignore it. |
| 1473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1475 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1476 | mContext.sendOrderedBroadcast(mScreenOffIntent, null, |
| 1477 | mScreenOffBroadcastDone, mHandler, 0, null, null); |
| 1478 | } else { |
| 1479 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1480 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1481 | mBroadcastWakeLock.mCount); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1482 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1483 | mBroadcastWakeLock.release(); |
| 1484 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | } |
| 1486 | } |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1487 | else { |
| 1488 | // If we're in this case, then this handler is running for a previous |
| 1489 | // paired transaction. mBroadcastWakeLock will already have been released. |
| 1490 | break; |
| 1491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | }; |
| 1495 | |
| 1496 | long mScreenOnStart; |
| 1497 | private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() { |
| 1498 | public void onReceive(Context context, Intent intent) { |
| 1499 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1500 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount); |
| 1502 | mBroadcastWakeLock.release(); |
| 1503 | } |
| 1504 | } |
| 1505 | }; |
| 1506 | |
| 1507 | long mScreenOffStart; |
| 1508 | private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() { |
| 1509 | public void onReceive(Context context, Intent intent) { |
| 1510 | synchronized (mLocks) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1511 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount); |
| 1513 | mBroadcastWakeLock.release(); |
| 1514 | } |
| 1515 | } |
| 1516 | }; |
| 1517 | |
| 1518 | void logPointerUpEvent() { |
| 1519 | if (LOG_TOUCH_DOWNS) { |
| 1520 | mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown; |
| 1521 | mLastTouchDown = 0; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | void logPointerDownEvent() { |
| 1526 | if (LOG_TOUCH_DOWNS) { |
| 1527 | // If we are not already timing a down/up sequence |
| 1528 | if (mLastTouchDown == 0) { |
| 1529 | mLastTouchDown = SystemClock.elapsedRealtime(); |
| 1530 | mTouchCycles++; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Prevents the screen from turning on even if it *should* turn on due |
| 1537 | * to a subsequent full wake lock being acquired. |
| 1538 | * <p> |
| 1539 | * This is a temporary hack that allows an activity to "cover up" any |
| 1540 | * display glitches that happen during the activity's startup |
| 1541 | * sequence. (Specifically, this API was added to work around a |
| 1542 | * cosmetic bug in the "incoming call" sequence, where the lock screen |
| 1543 | * would flicker briefly before the incoming call UI became visible.) |
| 1544 | * TODO: There ought to be a more elegant way of doing this, |
| 1545 | * probably by having the PowerManager and ActivityManager |
| 1546 | * work together to let apps specify that the screen on/off |
| 1547 | * state should be synchronized with the Activity lifecycle. |
| 1548 | * <p> |
| 1549 | * Note that calling preventScreenOn(true) will NOT turn the screen |
| 1550 | * off if it's currently on. (This API only affects *future* |
| 1551 | * acquisitions of full wake locks.) |
| 1552 | * But calling preventScreenOn(false) WILL turn the screen on if |
| 1553 | * it's currently off because of a prior preventScreenOn(true) call. |
| 1554 | * <p> |
| 1555 | * Any call to preventScreenOn(true) MUST be followed promptly by a call |
| 1556 | * to preventScreenOn(false). In fact, if the preventScreenOn(false) |
| 1557 | * call doesn't occur within 5 seconds, we'll turn the screen back on |
| 1558 | * ourselves (and log a warning about it); this prevents a buggy app |
| 1559 | * from disabling the screen forever.) |
| 1560 | * <p> |
| 1561 | * TODO: this feature should really be controlled by a new type of poke |
| 1562 | * lock (rather than an IPowerManager call). |
| 1563 | */ |
| 1564 | public void preventScreenOn(boolean prevent) { |
| 1565 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1566 | |
| 1567 | synchronized (mLocks) { |
| 1568 | if (prevent) { |
| 1569 | // First of all, grab a partial wake lock to |
| 1570 | // make sure the CPU stays on during the entire |
| 1571 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1572 | mPreventScreenOnPartialLock.acquire(); |
| 1573 | |
| 1574 | // Post a forceReenableScreen() call (for 5 seconds in the |
| 1575 | // future) to make sure the matching preventScreenOn(false) call |
| 1576 | // has happened by then. |
| 1577 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1578 | mHandler.postDelayed(mForceReenableScreenTask, 5000); |
| 1579 | |
| 1580 | // Finally, set the flag that prevents the screen from turning on. |
| 1581 | // (Below, in setPowerState(), we'll check mPreventScreenOn and |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1582 | // we *won't* call setScreenStateLocked(true) if it's set.) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | mPreventScreenOn = true; |
| 1584 | } else { |
| 1585 | // (Re)enable the screen. |
| 1586 | mPreventScreenOn = false; |
| 1587 | |
| 1588 | // We're "undoing" a the prior preventScreenOn(true) call, so we |
| 1589 | // no longer need the 5-second safeguard. |
| 1590 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1591 | |
| 1592 | // Forcibly turn on the screen if it's supposed to be on. (This |
| 1593 | // handles the case where the screen is currently off because of |
| 1594 | // a prior preventScreenOn(true) call.) |
Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 1595 | if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1597 | Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | "preventScreenOn: turning on after a prior preventScreenOn(true)!"); |
| 1599 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1600 | int err = setScreenStateLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | if (err != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1602 | Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | // Release the partial wake lock that we held during the |
| 1607 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1608 | mPreventScreenOnPartialLock.release(); |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | public void setScreenBrightnessOverride(int brightness) { |
| 1614 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1615 | |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1616 | if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | synchronized (mLocks) { |
| 1618 | if (mScreenBrightnessOverride != brightness) { |
| 1619 | mScreenBrightnessOverride = brightness; |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1620 | if (isScreenOn()) { |
| 1621 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1622 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1626 | |
| 1627 | public void setButtonBrightnessOverride(int brightness) { |
| 1628 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1629 | |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1630 | if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness); |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1631 | synchronized (mLocks) { |
| 1632 | if (mButtonBrightnessOverride != brightness) { |
| 1633 | mButtonBrightnessOverride = brightness; |
Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1634 | if (isScreenOn()) { |
| 1635 | updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT); |
| 1636 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | /** |
| 1642 | * Sanity-check that gets called 5 seconds after any call to |
| 1643 | * preventScreenOn(true). This ensures that the original call |
| 1644 | * is followed promptly by a call to preventScreenOn(false). |
| 1645 | */ |
| 1646 | private void forceReenableScreen() { |
| 1647 | // We shouldn't get here at all if mPreventScreenOn is false, since |
| 1648 | // we should have already removed any existing |
| 1649 | // mForceReenableScreenTask messages... |
| 1650 | if (!mPreventScreenOn) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1651 | Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | // Uh oh. It's been 5 seconds since a call to |
| 1656 | // preventScreenOn(true) and we haven't re-enabled the screen yet. |
| 1657 | // This means the app that called preventScreenOn(true) is either |
| 1658 | // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)), |
| 1659 | // or buggy (i.e. it forgot to call preventScreenOn(false), or |
| 1660 | // crashed before doing so.) |
| 1661 | |
| 1662 | // Log a warning, and forcibly turn the screen back on. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1663 | Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | + "Forcing the screen back on..."); |
| 1665 | preventScreenOn(false); |
| 1666 | } |
| 1667 | |
| 1668 | private Runnable mForceReenableScreenTask = new Runnable() { |
| 1669 | public void run() { |
| 1670 | forceReenableScreen(); |
| 1671 | } |
| 1672 | }; |
| 1673 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1674 | private int setScreenStateLocked(boolean on) { |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1675 | if (DEBUG_SCREEN_ON) { |
| 1676 | RuntimeException e = new RuntimeException("here"); |
| 1677 | e.fillInStackTrace(); |
| 1678 | Slog.i(TAG, "Set screen state: " + on, e); |
| 1679 | } |
Dianne Hackborn | 474fd74 | 2011-10-10 18:40:22 -0700 | [diff] [blame] | 1680 | if (on) { |
| 1681 | if ((mPowerState & SCREEN_ON_BIT) == 0 || mSkippedScreenOn) { |
| 1682 | // If we are turning the screen state on, but the screen |
| 1683 | // light is currently off, then make sure that we set the |
| 1684 | // light at this point to 0. This is the case where we are |
| 1685 | // turning on the screen and waiting for the UI to be drawn |
| 1686 | // before showing it to the user. We want the light off |
| 1687 | // until it is ready to be shown to the user, not it using |
| 1688 | // whatever the last value it had. |
| 1689 | mScreenBrightness.forceValueLocked(Power.BRIGHTNESS_OFF); |
| 1690 | } |
| 1691 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1692 | int err = Power.setScreenState(on); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1693 | if (err == 0) { |
| 1694 | mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0); |
| 1695 | if (mUseSoftwareAutoBrightness) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 1696 | enableLightSensorLocked(on); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1697 | if (!on) { |
| 1698 | // make sure button and key backlights are off too |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1699 | mButtonLight.turnOff(); |
| 1700 | mKeyboardLight.turnOff(); |
Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1701 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 1702 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1703 | } |
| 1704 | return err; |
| 1705 | } |
| 1706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | private void setPowerState(int state) |
| 1708 | { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1709 | setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1712 | private void setPowerState(int newState, boolean noChangeLights, int reason) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | { |
| 1714 | synchronized (mLocks) { |
| 1715 | int err; |
| 1716 | |
| 1717 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1718 | Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | + " newState=0x" + Integer.toHexString(newState) |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1720 | + " noChangeLights=" + noChangeLights |
| 1721 | + " reason=" + reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if (noChangeLights) { |
| 1725 | newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK); |
| 1726 | } |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1727 | if (mProximitySensorActive) { |
| 1728 | // don't turn on the screen when the proximity sensor lock is held |
| 1729 | newState = (newState & ~SCREEN_BRIGHT); |
| 1730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | |
| 1732 | if (batteryIsLow()) { |
| 1733 | newState |= BATTERY_LOW_BIT; |
| 1734 | } else { |
| 1735 | newState &= ~BATTERY_LOW_BIT; |
| 1736 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1737 | if (newState == mPowerState && mInitialized) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | return; |
| 1739 | } |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 1740 | |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1741 | if (!mBootCompleted && !mUseSoftwareAutoBrightness) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1742 | newState |= ALL_BRIGHT; |
| 1743 | } |
| 1744 | |
| 1745 | boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; |
| 1746 | boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; |
| 1747 | |
Mike Lockwood | 51b8449 | 2009-11-16 21:51:18 -0500 | [diff] [blame] | 1748 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1749 | Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | + " newState=" + newState + " noChangeLights=" + noChangeLights); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1751 | Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1753 | Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1754 | + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1755 | Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0)); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1757 | Slog.d(TAG, " oldScreenOn=" + oldScreenOn |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | + " newScreenOn=" + newScreenOn); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1759 | Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0)); |
| 1761 | } |
| 1762 | |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1763 | final boolean stateChanged = mPowerState != newState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | |
| 1765 | if (oldScreenOn != newScreenOn) { |
| 1766 | if (newScreenOn) { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1767 | // When the user presses the power button, we need to always send out the |
| 1768 | // notification that it's going to sleep so the keyguard goes on. But |
| 1769 | // we can't do that until the screen fades out, so we don't show the keyguard |
| 1770 | // too early. |
| 1771 | if (mStillNeedSleepNotification) { |
| 1772 | sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 1773 | } |
| 1774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | // Turn on the screen UNLESS there was a prior |
| 1776 | // preventScreenOn(true) request. (Note that the lifetime |
| 1777 | // of a single preventScreenOn() request is limited to 5 |
| 1778 | // seconds to prevent a buggy app from disabling the |
| 1779 | // screen forever; see forceReenableScreen().) |
| 1780 | boolean reallyTurnScreenOn = true; |
| 1781 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1782 | Slog.d(TAG, "- turning screen on... mPreventScreenOn = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | + mPreventScreenOn); |
| 1784 | } |
| 1785 | |
| 1786 | if (mPreventScreenOn) { |
| 1787 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1788 | Slog.d(TAG, "- PREVENTING screen from really turning on!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | } |
| 1790 | reallyTurnScreenOn = false; |
| 1791 | } |
| 1792 | if (reallyTurnScreenOn) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1793 | err = setScreenStateLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | long identity = Binder.clearCallingIdentity(); |
| 1795 | try { |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1796 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | mBatteryStats.noteScreenOn(); |
| 1798 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1799 | Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | } finally { |
| 1801 | Binder.restoreCallingIdentity(identity); |
| 1802 | } |
| 1803 | } else { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1804 | setScreenStateLocked(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1805 | // But continue as if we really did turn the screen on... |
| 1806 | err = 0; |
| 1807 | } |
| 1808 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | mLastTouchDown = 0; |
| 1810 | mTotalTouchDownTime = 0; |
| 1811 | mTouchCycles = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1812 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | mTotalTouchDownTime, mTouchCycles); |
| 1814 | if (err == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | sendNotificationLocked(true, -1); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1816 | // Update the lights *after* taking care of turning the |
| 1817 | // screen on, so we do this after our notifications are |
| 1818 | // enqueued and thus will delay turning on the screen light |
| 1819 | // until the windows are correctly displayed. |
| 1820 | if (stateChanged) { |
| 1821 | updateLightsLocked(newState, 0); |
| 1822 | } |
| 1823 | mPowerState |= SCREEN_ON_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1824 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1825 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1826 | } else { |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1827 | // Update the lights *before* taking care of turning the |
| 1828 | // screen off, so we can initiate any animations that are desired. |
| 1829 | if (stateChanged) { |
| 1830 | updateLightsLocked(newState, 0); |
| 1831 | } |
| 1832 | |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 1833 | // cancel light sensor task |
| 1834 | mHandler.removeCallbacks(mAutoBrightnessTask); |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1835 | mLightSensorPendingDecrease = false; |
| 1836 | mLightSensorPendingIncrease = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1838 | long identity = Binder.clearCallingIdentity(); |
| 1839 | try { |
| 1840 | mBatteryStats.noteScreenOff(); |
| 1841 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1842 | Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | } finally { |
| 1844 | Binder.restoreCallingIdentity(identity); |
| 1845 | } |
| 1846 | mPowerState &= ~SCREEN_ON_BIT; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1847 | mScreenOffReason = reason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | if (!mScreenBrightness.animating) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1849 | err = screenOffFinishedAnimatingLocked(reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1850 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1851 | err = 0; |
| 1852 | mLastTouchDown = 0; |
| 1853 | } |
| 1854 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1855 | } else if (stateChanged) { |
| 1856 | // Screen on/off didn't change, but lights may have. |
| 1857 | updateLightsLocked(newState, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1859 | |
| 1860 | mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK); |
| 1861 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1862 | updateNativePowerStateLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | } |
| 1864 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1865 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1866 | private void updateNativePowerStateLocked() { |
| 1867 | nativeSetPowerState( |
| 1868 | (mPowerState & SCREEN_ON_BIT) != 0, |
| 1869 | (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT); |
| 1870 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1871 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1872 | private int screenOffFinishedAnimatingLocked(int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1873 | // I don't think we need to check the current state here because all of these |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1874 | // Power.setScreenState and sendNotificationLocked can both handle being |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | // called multiple times in the same state. -joeo |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1876 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, |
| 1877 | mTouchCycles); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | mLastTouchDown = 0; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1879 | int err = setScreenStateLocked(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | if (err == 0) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1881 | mScreenOffReason = reason; |
| 1882 | sendNotificationLocked(false, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | } |
| 1884 | return err; |
| 1885 | } |
| 1886 | |
| 1887 | private boolean batteryIsLow() { |
| 1888 | return (!mIsPowered && |
| 1889 | mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD); |
| 1890 | } |
| 1891 | |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1892 | private boolean shouldDeferScreenOnLocked() { |
| 1893 | if (mPreparingForScreenOn) { |
| 1894 | // Currently waiting for confirmation from the policy that it |
| 1895 | // is okay to turn on the screen. Don't allow the screen to go |
| 1896 | // on until that is done. |
| 1897 | if (DEBUG_SCREEN_ON) Slog.i(TAG, |
| 1898 | "updateLights: delaying screen on due to mPreparingForScreenOn"); |
| 1899 | return true; |
| 1900 | } else { |
| 1901 | // If there is a screen-on command in the notification queue, we |
| 1902 | // can't turn the screen on until it has been processed (and we |
| 1903 | // have set mPreparingForScreenOn) or it has been dropped. |
| 1904 | for (int i=0; i<mBroadcastQueue.length; i++) { |
| 1905 | if (mBroadcastQueue[i] == 1) { |
| 1906 | if (DEBUG_SCREEN_ON) Slog.i(TAG, |
| 1907 | "updateLights: delaying screen on due to notification queue"); |
| 1908 | return true; |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1915 | private void updateLightsLocked(int newState, int forceState) { |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1916 | final int oldState = mPowerState; |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1917 | |
| 1918 | // If the screen is not currently on, we will want to delay actually |
| 1919 | // turning the lights on if we are still getting the UI put up. |
| 1920 | if ((oldState&SCREEN_ON_BIT) == 0 || mSkippedScreenOn) { |
| 1921 | // Don't turn screen on until we know we are really ready to. |
| 1922 | // This is to avoid letting the screen go on before things like the |
| 1923 | // lock screen have been displayed. |
| 1924 | if ((mSkippedScreenOn=shouldDeferScreenOnLocked())) { |
| 1925 | newState &= ~(SCREEN_ON_BIT|SCREEN_BRIGHT_BIT); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Joe Onorato | 60607a9 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1929 | if ((newState & SCREEN_ON_BIT) != 0) { |
| 1930 | // Only turn on the buttons or keyboard if the screen is also on. |
| 1931 | // We should never see the buttons on but not the screen. |
| 1932 | newState = applyButtonState(newState); |
| 1933 | newState = applyKeyboardState(newState); |
| 1934 | } |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1935 | final int realDifference = (newState ^ oldState); |
| 1936 | final int difference = realDifference | forceState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | if (difference == 0) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1938 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | int offMask = 0; |
| 1942 | int dimMask = 0; |
| 1943 | int onMask = 0; |
| 1944 | |
| 1945 | int preferredBrightness = getPreferredBrightness(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1946 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1947 | if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1948 | if ((newState & KEYBOARD_BRIGHT_BIT) == 0) { |
| 1949 | offMask |= KEYBOARD_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1951 | onMask |= KEYBOARD_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | if ((difference & BUTTON_BRIGHT_BIT) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1956 | if ((newState & BUTTON_BRIGHT_BIT) == 0) { |
| 1957 | offMask |= BUTTON_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1958 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1959 | onMask |= BUTTON_BRIGHT_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1964 | int nominalCurrentValue = -1; |
| 1965 | // If there was an actual difference in the light state, then |
| 1966 | // figure out the "ideal" current value based on the previous |
| 1967 | // state. Otherwise, this is a change due to the brightness |
| 1968 | // override, so we want to animate from whatever the current |
| 1969 | // value is. |
| 1970 | if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| 1971 | switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { |
| 1972 | case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: |
| 1973 | nominalCurrentValue = preferredBrightness; |
| 1974 | break; |
| 1975 | case SCREEN_ON_BIT: |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 1976 | nominalCurrentValue = mScreenBrightnessDim; |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1977 | break; |
| 1978 | case 0: |
| 1979 | nominalCurrentValue = Power.BRIGHTNESS_OFF; |
| 1980 | break; |
| 1981 | case SCREEN_BRIGHT_BIT: |
| 1982 | default: |
| 1983 | // not possible |
| 1984 | nominalCurrentValue = (int)mScreenBrightness.curValue; |
| 1985 | break; |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1986 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1987 | } |
| 1988 | int brightness = preferredBrightness; |
| 1989 | int steps = ANIM_STEPS; |
| 1990 | if ((newState & SCREEN_BRIGHT_BIT) == 0) { |
| 1991 | // dim or turn off backlight, depending on if the screen is on |
| 1992 | // the scale is because the brightness ramp isn't linear and this biases |
| 1993 | // it so the later parts take longer. |
| 1994 | final float scale = 1.5f; |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 1995 | float ratio = (((float)mScreenBrightnessDim)/preferredBrightness); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1996 | if (ratio > 1.0f) ratio = 1.0f; |
| 1997 | if ((newState & SCREEN_ON_BIT) == 0) { |
| 1998 | if ((oldState & SCREEN_BRIGHT_BIT) != 0) { |
| 1999 | // was bright |
| 2000 | steps = ANIM_STEPS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2001 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2002 | // was dim |
| 2003 | steps = (int)(ANIM_STEPS*ratio*scale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2004 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2005 | brightness = Power.BRIGHTNESS_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2007 | if ((oldState & SCREEN_ON_BIT) != 0) { |
| 2008 | // was bright |
| 2009 | steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale); |
| 2010 | } else { |
| 2011 | // was dim |
| 2012 | steps = (int)(ANIM_STEPS*ratio); |
| 2013 | } |
| 2014 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 2015 | // If the "stay on while plugged in" option is |
| 2016 | // turned on, then the screen will often not |
| 2017 | // automatically turn off while plugged in. To |
| 2018 | // still have a sense of when it is inactive, we |
| 2019 | // will then count going dim as turning off. |
| 2020 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 2021 | } |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2022 | brightness = mScreenBrightnessDim; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | } |
| 2024 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2025 | long identity = Binder.clearCallingIdentity(); |
| 2026 | try { |
| 2027 | mBatteryStats.noteScreenBrightness(brightness); |
| 2028 | } catch (RemoteException e) { |
| 2029 | // Nothing interesting to do. |
| 2030 | } finally { |
| 2031 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2032 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2033 | mScreenBrightness.setTargetLocked(brightness, steps, |
| 2034 | INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 2035 | if (DEBUG_SCREEN_ON) { |
| 2036 | RuntimeException e = new RuntimeException("here"); |
| 2037 | e.fillInStackTrace(); |
| 2038 | Slog.i(TAG, "Setting screen brightness: " + brightness, e); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 2039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2041 | |
Joe Onorato | 60607a9 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 2042 | if (mSpew) { |
| 2043 | Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask) |
| 2044 | + " dimMask=0x" + Integer.toHexString(dimMask) |
| 2045 | + " onMask=0x" + Integer.toHexString(onMask) |
| 2046 | + " difference=0x" + Integer.toHexString(difference) |
| 2047 | + " realDifference=0x" + Integer.toHexString(realDifference) |
| 2048 | + " forceState=0x" + Integer.toHexString(forceState) |
| 2049 | ); |
| 2050 | } |
| 2051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | if (offMask != 0) { |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2053 | if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2054 | setLightBrightness(offMask, Power.BRIGHTNESS_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2055 | } |
| 2056 | if (dimMask != 0) { |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2057 | int brightness = mScreenBrightnessDim; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 2059 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 2060 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 2061 | } |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2062 | if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2063 | setLightBrightness(dimMask, brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2064 | } |
| 2065 | if (onMask != 0) { |
| 2066 | int brightness = getPreferredBrightness(); |
| 2067 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 2068 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 2069 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 2070 | } |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2071 | if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2072 | setLightBrightness(onMask, brightness); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2073 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2076 | private void setLightBrightness(int mask, int value) { |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 2077 | int brightnessMode = (mAutoBrightessEnabled |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 2078 | ? LightsService.BRIGHTNESS_MODE_SENSOR |
| 2079 | : LightsService.BRIGHTNESS_MODE_USER); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2080 | if ((mask & SCREEN_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2081 | mLcdLight.setBrightness(value, brightnessMode); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2082 | } |
| 2083 | if ((mask & BUTTON_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2084 | mButtonLight.setBrightness(value); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2085 | } |
| 2086 | if ((mask & KEYBOARD_BRIGHT_BIT) != 0) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2087 | mKeyboardLight.setBrightness(value); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2088 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2089 | } |
| 2090 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2091 | class BrightnessState implements Runnable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2092 | final int mask; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2093 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2094 | boolean initialized; |
| 2095 | int targetValue; |
| 2096 | float curValue; |
| 2097 | float delta; |
| 2098 | boolean animating; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | BrightnessState(int m) { |
| 2101 | mask = m; |
| 2102 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2104 | public void dump(PrintWriter pw, String prefix) { |
| 2105 | pw.println(prefix + "animating=" + animating |
| 2106 | + " targetValue=" + targetValue |
| 2107 | + " curValue=" + curValue |
| 2108 | + " delta=" + delta); |
| 2109 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2110 | |
Dianne Hackborn | 474fd74 | 2011-10-10 18:40:22 -0700 | [diff] [blame] | 2111 | void forceValueLocked(int value) { |
| 2112 | targetValue = -1; |
| 2113 | curValue = value; |
| 2114 | setLightBrightness(mask, value); |
| 2115 | if (animating) { |
| 2116 | finishAnimationLocked(false, value); |
| 2117 | } |
| 2118 | } |
| 2119 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2120 | void setTargetLocked(int target, int stepsToTarget, int initialValue, |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2121 | int nominalCurrentValue) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2122 | if (!initialized) { |
| 2123 | initialized = true; |
| 2124 | curValue = (float)initialValue; |
Dianne Hackborn | aa80b60 | 2009-10-09 17:38:26 -0700 | [diff] [blame] | 2125 | } else if (targetValue == target) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2126 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | } |
| 2128 | targetValue = target; |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2129 | delta = (targetValue - |
| 2130 | (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue)) |
| 2131 | / stepsToTarget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2132 | if (mSpew) { |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2133 | String noticeMe = nominalCurrentValue == curValue ? "" : " ******************"; |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2134 | Slog.i(TAG, "setTargetLocked mask=" + mask + " curValue=" + curValue |
| 2135 | + " target=" + target + " targetValue=" + targetValue + " delta=" + delta |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2136 | + " nominalCurrentValue=" + nominalCurrentValue |
| 2137 | + noticeMe); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2138 | } |
| 2139 | animating = true; |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2140 | |
| 2141 | if (mSpew) { |
| 2142 | Slog.i(TAG, "scheduling light animator"); |
| 2143 | } |
| 2144 | mScreenOffHandler.removeCallbacks(this); |
| 2145 | mScreenOffHandler.post(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | boolean stepLocked() { |
| 2149 | if (!animating) return false; |
| 2150 | if (false && mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2151 | Slog.i(TAG, "Step target " + mask + ": cur=" + curValue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2152 | + " target=" + targetValue + " delta=" + delta); |
| 2153 | } |
| 2154 | curValue += delta; |
| 2155 | int curIntValue = (int)curValue; |
| 2156 | boolean more = true; |
| 2157 | if (delta == 0) { |
Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2158 | curValue = curIntValue = targetValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | more = false; |
| 2160 | } else if (delta > 0) { |
| 2161 | if (curIntValue >= targetValue) { |
| 2162 | curValue = curIntValue = targetValue; |
| 2163 | more = false; |
| 2164 | } |
| 2165 | } else { |
| 2166 | if (curIntValue <= targetValue) { |
| 2167 | curValue = curIntValue = targetValue; |
| 2168 | more = false; |
| 2169 | } |
| 2170 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2171 | if (mSpew) Slog.d(TAG, "Animating curIntValue=" + curIntValue + ": " + mask); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2172 | setLightBrightness(mask, curIntValue); |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2173 | finishAnimationLocked(more, curIntValue); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2174 | return more; |
| 2175 | } |
| 2176 | |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2177 | void jumpToTargetLocked() { |
| 2178 | if (mSpew) Slog.d(TAG, "jumpToTargetLocked targetValue=" + targetValue + ": " + mask); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2179 | setLightBrightness(mask, targetValue); |
| 2180 | final int tv = targetValue; |
| 2181 | curValue = tv; |
| 2182 | targetValue = -1; |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2183 | finishAnimationLocked(false, tv); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2186 | private void finishAnimationLocked(boolean more, int curIntValue) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | animating = more; |
| 2188 | if (!more) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2189 | if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) { |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2190 | screenOffFinishedAnimatingLocked(mScreenOffReason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2191 | } |
| 2192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2193 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2195 | public void run() { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2196 | if (mAnimateScreenLights) { |
| 2197 | synchronized (mLocks) { |
| 2198 | long now = SystemClock.uptimeMillis(); |
| 2199 | boolean more = mScreenBrightness.stepLocked(); |
| 2200 | if (more) { |
| 2201 | mScreenOffHandler.postAtTime(this, now+(1000/60)); |
| 2202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | } |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2204 | } else { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2205 | synchronized (mLocks) { |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2206 | // we're turning off |
| 2207 | final boolean animate = animating && targetValue == Power.BRIGHTNESS_OFF; |
| 2208 | if (animate) { |
| 2209 | // It's pretty scary to hold mLocks for this long, and we should |
| 2210 | // redesign this, but it works for now. |
| 2211 | nativeStartSurfaceFlingerAnimation( |
| 2212 | mScreenOffReason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 2213 | ? 0 : mAnimationSetting); |
| 2214 | } |
| 2215 | mScreenBrightness.jumpToTargetLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2221 | private int getPreferredBrightness() { |
| 2222 | try { |
| 2223 | if (mScreenBrightnessOverride >= 0) { |
| 2224 | return mScreenBrightnessOverride; |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2225 | } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness |
Mike Lockwood | 27c6dd7 | 2009-11-04 08:57:07 -0500 | [diff] [blame] | 2226 | && mAutoBrightessEnabled) { |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2227 | return mLightSensorScreenBrightness; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2228 | } |
| 2229 | final int brightness = Settings.System.getInt(mContext.getContentResolver(), |
| 2230 | SCREEN_BRIGHTNESS); |
| 2231 | // Don't let applications turn the screen all the way off |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2232 | return Math.max(brightness, mScreenBrightnessDim); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2233 | } catch (SettingNotFoundException snfe) { |
| 2234 | return Power.BRIGHTNESS_ON; |
| 2235 | } |
| 2236 | } |
| 2237 | |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2238 | private int applyButtonState(int state) { |
| 2239 | int brightness = -1; |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2240 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2241 | // do not override brightness if the battery is low |
| 2242 | return state; |
| 2243 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2244 | if (mButtonBrightnessOverride >= 0) { |
| 2245 | brightness = mButtonBrightnessOverride; |
| 2246 | } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2247 | brightness = mLightSensorButtonBrightness; |
| 2248 | } |
| 2249 | if (brightness > 0) { |
| 2250 | return state | BUTTON_BRIGHT_BIT; |
| 2251 | } else if (brightness == 0) { |
| 2252 | return state & ~BUTTON_BRIGHT_BIT; |
| 2253 | } else { |
| 2254 | return state; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | private int applyKeyboardState(int state) { |
| 2259 | int brightness = -1; |
Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2260 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2261 | // do not override brightness if the battery is low |
| 2262 | return state; |
| 2263 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2264 | if (!mKeyboardVisible) { |
| 2265 | brightness = 0; |
| 2266 | } else if (mButtonBrightnessOverride >= 0) { |
| 2267 | brightness = mButtonBrightnessOverride; |
| 2268 | } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2269 | brightness = mLightSensorKeyboardBrightness; |
| 2270 | } |
| 2271 | if (brightness > 0) { |
| 2272 | return state | KEYBOARD_BRIGHT_BIT; |
| 2273 | } else if (brightness == 0) { |
| 2274 | return state & ~KEYBOARD_BRIGHT_BIT; |
| 2275 | } else { |
| 2276 | return state; |
| 2277 | } |
| 2278 | } |
| 2279 | |
Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2280 | public boolean isScreenOn() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | synchronized (mLocks) { |
| 2282 | return (mPowerState & SCREEN_ON_BIT) != 0; |
| 2283 | } |
| 2284 | } |
| 2285 | |
Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2286 | boolean isScreenBright() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2287 | synchronized (mLocks) { |
| 2288 | return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT; |
| 2289 | } |
| 2290 | } |
| 2291 | |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2292 | private boolean isScreenTurningOffLocked() { |
| 2293 | return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0); |
| 2294 | } |
| 2295 | |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2296 | private boolean shouldLog(long time) { |
| 2297 | synchronized (mLocks) { |
| 2298 | if (time > (mWarningSpewThrottleTime + (60*60*1000))) { |
| 2299 | mWarningSpewThrottleTime = time; |
| 2300 | mWarningSpewThrottleCount = 0; |
| 2301 | return true; |
| 2302 | } else if (mWarningSpewThrottleCount < 30) { |
| 2303 | mWarningSpewThrottleCount++; |
| 2304 | return true; |
| 2305 | } else { |
| 2306 | return false; |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2311 | private void forceUserActivityLocked() { |
Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 2312 | if (isScreenTurningOffLocked()) { |
| 2313 | // cancel animation so userActivity will succeed |
| 2314 | mScreenBrightness.animating = false; |
| 2315 | } |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2316 | boolean savedActivityAllowed = mUserActivityAllowed; |
| 2317 | mUserActivityAllowed = true; |
| 2318 | userActivity(SystemClock.uptimeMillis(), false); |
| 2319 | mUserActivityAllowed = savedActivityAllowed; |
| 2320 | } |
| 2321 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2322 | public void userActivityWithForce(long time, boolean noChangeLights, boolean force) { |
| 2323 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2324 | userActivity(time, -1, noChangeLights, OTHER_EVENT, force); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | public void userActivity(long time, boolean noChangeLights) { |
Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2328 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER) |
| 2329 | != PackageManager.PERMISSION_GRANTED) { |
| 2330 | if (shouldLog(time)) { |
| 2331 | Slog.w(TAG, "Caller does not have DEVICE_POWER permission. pid=" |
| 2332 | + Binder.getCallingPid() + " uid=" + Binder.getCallingUid()); |
| 2333 | } |
| 2334 | return; |
| 2335 | } |
| 2336 | |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2337 | userActivity(time, -1, noChangeLights, OTHER_EVENT, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | public void userActivity(long time, boolean noChangeLights, int eventType) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2341 | userActivity(time, -1, noChangeLights, eventType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) { |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2345 | userActivity(time, -1, noChangeLights, eventType, force); |
| 2346 | } |
| 2347 | |
| 2348 | /* |
| 2349 | * Reset the user activity timeout to now + timeout. This overrides whatever else is going |
| 2350 | * on with user activity. Don't use this function. |
| 2351 | */ |
| 2352 | public void clearUserActivityTimeout(long now, long timeout) { |
| 2353 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2354 | Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now"); |
| 2355 | userActivity(now, timeout, false, OTHER_EVENT, false); |
| 2356 | } |
| 2357 | |
| 2358 | private void userActivity(long time, long timeoutOverride, boolean noChangeLights, |
| 2359 | int eventType, boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2360 | |
Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 2361 | if (((mPokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0) && (eventType == TOUCH_EVENT)) { |
Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2362 | if (false) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2363 | Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey)); |
Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2364 | } |
| 2365 | return; |
| 2366 | } |
| 2367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2368 | synchronized (mLocks) { |
| 2369 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2370 | Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | + " mUserActivityAllowed=" + mUserActivityAllowed |
| 2372 | + " mUserState=0x" + Integer.toHexString(mUserState) |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2373 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState) |
| 2374 | + " mProximitySensorActive=" + mProximitySensorActive |
Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 2375 | + " timeoutOverride=" + timeoutOverride |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2376 | + " force=" + force); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | } |
Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2378 | // ignore user activity if we are in the process of turning off the screen |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2379 | if (isScreenTurningOffLocked()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2380 | Slog.d(TAG, "ignoring user activity while turning off screen"); |
Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2381 | return; |
| 2382 | } |
Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 2383 | // Disable proximity sensor if if user presses power key while we are in the |
| 2384 | // "waiting for proximity sensor to go negative" state. |
| 2385 | if (mProximitySensorActive && mProximityWakeLockCount == 0) { |
| 2386 | mProximitySensorActive = false; |
| 2387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2388 | if (mLastEventTime <= time || force) { |
| 2389 | mLastEventTime = time; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2390 | if ((mUserActivityAllowed && !mProximitySensorActive) || force) { |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2391 | // Only turn on button backlights if a button was pressed |
| 2392 | // and auto brightness is disabled |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2393 | if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2394 | mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 2395 | } else { |
| 2396 | // don't clear button/keyboard backlights when the screen is touched. |
| 2397 | mUserState |= SCREEN_BRIGHT; |
| 2398 | } |
| 2399 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2400 | int uid = Binder.getCallingUid(); |
| 2401 | long ident = Binder.clearCallingIdentity(); |
| 2402 | try { |
| 2403 | mBatteryStats.noteUserActivity(uid, eventType); |
| 2404 | } catch (RemoteException e) { |
| 2405 | // Ignore |
| 2406 | } finally { |
| 2407 | Binder.restoreCallingIdentity(ident); |
| 2408 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2409 | |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2410 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2411 | setPowerState(mUserState | mWakeLockState, noChangeLights, |
| 2412 | WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2413 | setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | } |
Mike Lockwood | ef73162 | 2010-01-27 17:51:34 -0500 | [diff] [blame] | 2417 | |
| 2418 | if (mPolicy != null) { |
| 2419 | mPolicy.userActivity(); |
| 2420 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2423 | private int getAutoBrightnessValue(int sensorValue, int[] values) { |
| 2424 | try { |
| 2425 | int i; |
| 2426 | for (i = 0; i < mAutoBrightnessLevels.length; i++) { |
| 2427 | if (sensorValue < mAutoBrightnessLevels[i]) { |
| 2428 | break; |
| 2429 | } |
| 2430 | } |
| 2431 | return values[i]; |
| 2432 | } catch (Exception e) { |
| 2433 | // guard against null pointer or index out of bounds errors |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2434 | Slog.e(TAG, "getAutoBrightnessValue", e); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2435 | return 255; |
| 2436 | } |
| 2437 | } |
| 2438 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2439 | private Runnable mProximityTask = new Runnable() { |
| 2440 | public void run() { |
| 2441 | synchronized (mLocks) { |
| 2442 | if (mProximityPendingValue != -1) { |
| 2443 | proximityChangedLocked(mProximityPendingValue == 1); |
| 2444 | mProximityPendingValue = -1; |
| 2445 | } |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2446 | if (mProximityPartialLock.isHeld()) { |
| 2447 | mProximityPartialLock.release(); |
| 2448 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | }; |
| 2452 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2453 | private Runnable mAutoBrightnessTask = new Runnable() { |
| 2454 | public void run() { |
Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2455 | synchronized (mLocks) { |
Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 2456 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 2457 | int value = (int)mLightSensorPendingValue; |
| 2458 | mLightSensorPendingDecrease = false; |
| 2459 | mLightSensorPendingIncrease = false; |
Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2460 | lightSensorChangedLocked(value); |
| 2461 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | }; |
| 2465 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2466 | private void dockStateChanged(int state) { |
| 2467 | synchronized (mLocks) { |
| 2468 | mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 2469 | if (mIsDocked) { |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 2470 | // allow brightness to decrease when docked |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2471 | mHighestLightSensorValue = -1; |
| 2472 | } |
| 2473 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2474 | // force lights recalculation |
| 2475 | int value = (int)mLightSensorValue; |
| 2476 | mLightSensorValue = -1; |
| 2477 | lightSensorChangedLocked(value); |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2482 | private void lightSensorChangedLocked(int value) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2483 | if (mDebugLightSensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2484 | Slog.d(TAG, "lightSensorChangedLocked " + value); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2485 | } |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2486 | |
Joe Onorato | 06eb33a | 2010-10-25 14:09:21 -0700 | [diff] [blame] | 2487 | // Don't do anything if the screen is off. |
| 2488 | if ((mPowerState & SCREEN_ON_BIT) == 0) { |
| 2489 | if (mDebugLightSensor) { |
| 2490 | Slog.d(TAG, "dropping lightSensorChangedLocked because screen is off"); |
| 2491 | } |
| 2492 | return; |
| 2493 | } |
| 2494 | |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2495 | // do not allow light sensor value to decrease |
| 2496 | if (mHighestLightSensorValue < value) { |
| 2497 | mHighestLightSensorValue = value; |
| 2498 | } |
| 2499 | |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2500 | if (mLightSensorValue != value) { |
| 2501 | mLightSensorValue = value; |
| 2502 | if ((mPowerState & BATTERY_LOW_BIT) == 0) { |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2503 | // use maximum light sensor value seen since screen went on for LCD to avoid flicker |
| 2504 | // we only do this if we are undocked, since lighting should be stable when |
| 2505 | // stationary in a dock. |
| 2506 | int lcdValue = getAutoBrightnessValue( |
| 2507 | (mIsDocked ? value : mHighestLightSensorValue), |
| 2508 | mLcdBacklightValues); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2509 | int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues); |
Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2510 | int keyboardValue; |
| 2511 | if (mKeyboardVisible) { |
| 2512 | keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues); |
| 2513 | } else { |
| 2514 | keyboardValue = 0; |
| 2515 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2516 | mLightSensorScreenBrightness = lcdValue; |
| 2517 | mLightSensorButtonBrightness = buttonValue; |
| 2518 | mLightSensorKeyboardBrightness = keyboardValue; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2519 | |
| 2520 | if (mDebugLightSensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2521 | Slog.d(TAG, "lcdValue " + lcdValue); |
| 2522 | Slog.d(TAG, "buttonValue " + buttonValue); |
| 2523 | Slog.d(TAG, "keyboardValue " + keyboardValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2526 | if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2527 | mScreenBrightness.setTargetLocked(lcdValue, AUTOBRIGHTNESS_ANIM_STEPS, |
| 2528 | INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2529 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2530 | if (mButtonBrightnessOverride < 0) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2531 | mButtonLight.setBrightness(buttonValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2532 | } |
Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2533 | if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) { |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2534 | mKeyboardLight.setBrightness(keyboardValue); |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2538 | } |
| 2539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2540 | /** |
| 2541 | * The user requested that we go to sleep (probably with the power button). |
| 2542 | * This overrides all wake locks that are held. |
| 2543 | */ |
| 2544 | public void goToSleep(long time) |
| 2545 | { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2546 | goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 2547 | } |
| 2548 | |
| 2549 | /** |
| 2550 | * The user requested that we go to sleep (probably with the power button). |
| 2551 | * This overrides all wake locks that are held. |
| 2552 | */ |
| 2553 | public void goToSleepWithReason(long time, int reason) |
| 2554 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2555 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2556 | synchronized (mLocks) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2557 | goToSleepLocked(time, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2558 | } |
| 2559 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | /** |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2562 | * Reboot the device immediately, passing 'reason' (may be null) |
| 2563 | * to the underlying __reboot system call. Should not return. |
| 2564 | */ |
| 2565 | public void reboot(String reason) |
| 2566 | { |
| 2567 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
San Mehat | 14e69af | 2010-01-06 14:58:18 -0800 | [diff] [blame] | 2568 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2569 | if (mHandler == null || !ActivityManagerNative.isSystemReady()) { |
| 2570 | throw new IllegalStateException("Too early to call reboot()"); |
| 2571 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2572 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2573 | final String finalReason = reason; |
| 2574 | Runnable runnable = new Runnable() { |
| 2575 | public void run() { |
| 2576 | synchronized (this) { |
| 2577 | ShutdownThread.reboot(mContext, finalReason, false); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2578 | } |
| 2579 | |
San Mehat | 1e51279 | 2010-01-07 10:40:29 -0800 | [diff] [blame] | 2580 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2581 | }; |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2582 | // ShutdownThread must run on a looper capable of displaying the UI. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2583 | mHandler.post(runnable); |
| 2584 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2585 | // PowerManager.reboot() is documented not to return so just wait for the inevitable. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2586 | synchronized (runnable) { |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2587 | while (true) { |
| 2588 | try { |
| 2589 | runnable.wait(); |
| 2590 | } catch (InterruptedException e) { |
| 2591 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2592 | } |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2596 | /** |
| 2597 | * Crash the runtime (causing a complete restart of the Android framework). |
| 2598 | * Requires REBOOT permission. Mostly for testing. Should not return. |
| 2599 | */ |
| 2600 | public void crash(final String message) |
| 2601 | { |
| 2602 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 2603 | Thread t = new Thread("PowerManagerService.crash()") { |
| 2604 | public void run() { throw new RuntimeException(message); } |
| 2605 | }; |
| 2606 | try { |
| 2607 | t.start(); |
| 2608 | t.join(); |
| 2609 | } catch (InterruptedException e) { |
| 2610 | Log.wtf(TAG, e); |
| 2611 | } |
| 2612 | } |
| 2613 | |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2614 | private void goToSleepLocked(long time, int reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | |
| 2616 | if (mLastEventTime <= time) { |
| 2617 | mLastEventTime = time; |
| 2618 | // cancel all of the wake locks |
| 2619 | mWakeLockState = SCREEN_OFF; |
| 2620 | int N = mLocks.size(); |
| 2621 | int numCleared = 0; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2622 | boolean proxLock = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2623 | for (int i=0; i<N; i++) { |
| 2624 | WakeLock wl = mLocks.get(i); |
| 2625 | if (isScreenLock(wl.flags)) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2626 | if (((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) |
| 2627 | && reason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) { |
| 2628 | proxLock = true; |
| 2629 | } else { |
| 2630 | mLocks.get(i).activated = false; |
| 2631 | numCleared++; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | if (!proxLock) { |
| 2636 | mProxIgnoredBecauseScreenTurnedOff = true; |
| 2637 | if (mDebugProximitySensor) { |
| 2638 | Slog.d(TAG, "setting mProxIgnoredBecauseScreenTurnedOff"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2639 | } |
| 2640 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2641 | EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared); |
Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2642 | mStillNeedSleepNotification = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2643 | mUserState = SCREEN_OFF; |
Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2644 | setPowerState(SCREEN_OFF, false, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2645 | cancelTimerLocked(); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | public long timeSinceScreenOn() { |
| 2650 | synchronized (mLocks) { |
| 2651 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2652 | return 0; |
| 2653 | } |
| 2654 | return SystemClock.elapsedRealtime() - mScreenOffTime; |
| 2655 | } |
| 2656 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2657 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2658 | public void setKeyboardVisibility(boolean visible) { |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2659 | synchronized (mLocks) { |
| 2660 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2661 | Slog.d(TAG, "setKeyboardVisibility: " + visible); |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2662 | } |
Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2663 | if (mKeyboardVisible != visible) { |
| 2664 | mKeyboardVisible = visible; |
| 2665 | // don't signal user activity if the screen is off; other code |
| 2666 | // will take care of turning on due to a true change to the lid |
| 2667 | // switch and synchronized with the lock screen. |
| 2668 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2669 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2670 | // force recompute of backlight values |
| 2671 | if (mLightSensorValue >= 0) { |
| 2672 | int value = (int)mLightSensorValue; |
| 2673 | mLightSensorValue = -1; |
| 2674 | lightSensorChangedLocked(value); |
| 2675 | } |
| 2676 | } |
Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2677 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2678 | } |
Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2679 | } |
| 2680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | /** |
| 2684 | * When the keyguard is up, it manages the power state, and userActivity doesn't do anything. |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2685 | * When disabling user activity we also reset user power state so the keyguard can reset its |
| 2686 | * short screen timeout when keyguard is unhidden. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | */ |
| 2688 | public void enableUserActivity(boolean enabled) { |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2689 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2690 | Slog.d(TAG, "enableUserActivity " + enabled); |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2691 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | synchronized (mLocks) { |
| 2693 | mUserActivityAllowed = enabled; |
Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2694 | if (!enabled) { |
| 2695 | // cancel timeout and clear mUserState so the keyguard can set a short timeout |
| 2696 | setTimeoutLocked(SystemClock.uptimeMillis(), 0); |
| 2697 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2698 | } |
| 2699 | } |
| 2700 | |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2701 | private void setScreenBrightnessMode(int mode) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2702 | synchronized (mLocks) { |
| 2703 | boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC); |
| 2704 | if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) { |
| 2705 | mAutoBrightessEnabled = enabled; |
| 2706 | // This will get us a new value |
| 2707 | enableLightSensorLocked(mAutoBrightessEnabled && isScreenOn()); |
Mike Lockwood | 2d155d2 | 2009-10-27 09:32:30 -0400 | [diff] [blame] | 2708 | } |
Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | /** Sets the screen off timeouts: |
| 2713 | * mKeylightDelay |
| 2714 | * mDimDelay |
| 2715 | * mScreenOffDelay |
| 2716 | * */ |
| 2717 | private void setScreenOffTimeoutsLocked() { |
| 2718 | if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) { |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2719 | mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | mDimDelay = -1; |
| 2721 | mScreenOffDelay = 0; |
| 2722 | } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) { |
| 2723 | mKeylightDelay = MEDIUM_KEYLIGHT_DELAY; |
| 2724 | mDimDelay = -1; |
| 2725 | mScreenOffDelay = 0; |
| 2726 | } else { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2727 | int totalDelay = mScreenOffTimeoutSetting; |
| 2728 | if (totalDelay > mMaximumScreenOffTimeout) { |
| 2729 | totalDelay = mMaximumScreenOffTimeout; |
| 2730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | mKeylightDelay = LONG_KEYLIGHT_DELAY; |
| 2732 | if (totalDelay < 0) { |
Jim Miller | bc4603b | 2010-08-30 21:21:34 -0700 | [diff] [blame] | 2733 | // negative number means stay on as long as possible. |
| 2734 | mScreenOffDelay = mMaximumScreenOffTimeout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | } else if (mKeylightDelay < totalDelay) { |
| 2736 | // subtract the time that the keylight delay. This will give us the |
| 2737 | // remainder of the time that we need to sleep to get the accurate |
| 2738 | // screen off timeout. |
| 2739 | mScreenOffDelay = totalDelay - mKeylightDelay; |
| 2740 | } else { |
| 2741 | mScreenOffDelay = 0; |
| 2742 | } |
| 2743 | if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) { |
| 2744 | mDimDelay = mScreenOffDelay - LONG_DIM_TIME; |
| 2745 | mScreenOffDelay = LONG_DIM_TIME; |
| 2746 | } else { |
| 2747 | mDimDelay = -1; |
| 2748 | } |
| 2749 | } |
| 2750 | if (mSpew) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2751 | Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2752 | + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay |
| 2753 | + " mDimScreen=" + mDimScreen); |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | /** |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2758 | * Refreshes cached secure settings. Called once on startup, and |
| 2759 | * on subsequent changes to secure settings. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2760 | */ |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2761 | private void updateSettingsValues() { |
| 2762 | mShortKeylightDelay = Settings.Secure.getInt( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | mContext.getContentResolver(), |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2764 | Settings.Secure.SHORT_KEYLIGHT_DELAY_MS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2765 | SHORT_KEYLIGHT_DELAY_DEFAULT); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2766 | // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | private class LockList extends ArrayList<WakeLock> |
| 2770 | { |
| 2771 | void addLock(WakeLock wl) |
| 2772 | { |
| 2773 | int index = getIndex(wl.binder); |
| 2774 | if (index < 0) { |
| 2775 | this.add(wl); |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | WakeLock removeLock(IBinder binder) |
| 2780 | { |
| 2781 | int index = getIndex(binder); |
| 2782 | if (index >= 0) { |
| 2783 | return this.remove(index); |
| 2784 | } else { |
| 2785 | return null; |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | int getIndex(IBinder binder) |
| 2790 | { |
| 2791 | int N = this.size(); |
| 2792 | for (int i=0; i<N; i++) { |
| 2793 | if (this.get(i).binder == binder) { |
| 2794 | return i; |
| 2795 | } |
| 2796 | } |
| 2797 | return -1; |
| 2798 | } |
| 2799 | |
| 2800 | int gatherState() |
| 2801 | { |
| 2802 | int result = 0; |
| 2803 | int N = this.size(); |
| 2804 | for (int i=0; i<N; i++) { |
| 2805 | WakeLock wl = this.get(i); |
| 2806 | if (wl.activated) { |
| 2807 | if (isScreenLock(wl.flags)) { |
| 2808 | result |= wl.minState; |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | return result; |
| 2813 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2814 | |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2815 | int reactivateScreenLocksLocked() |
| 2816 | { |
| 2817 | int result = 0; |
| 2818 | int N = this.size(); |
| 2819 | for (int i=0; i<N; i++) { |
| 2820 | WakeLock wl = this.get(i); |
| 2821 | if (isScreenLock(wl.flags)) { |
| 2822 | wl.activated = true; |
| 2823 | result |= wl.minState; |
| 2824 | } |
| 2825 | } |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2826 | if (mDebugProximitySensor) { |
| 2827 | Slog.d(TAG, "reactivateScreenLocksLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 2828 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2829 | } |
| 2830 | mProxIgnoredBecauseScreenTurnedOff = false; |
Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2831 | return result; |
| 2832 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 2835 | public void setPolicy(WindowManagerPolicy p) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2836 | synchronized (mLocks) { |
| 2837 | mPolicy = p; |
| 2838 | mLocks.notifyAll(); |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | WindowManagerPolicy getPolicyLocked() { |
| 2843 | while (mPolicy == null || !mDoneBooting) { |
| 2844 | try { |
| 2845 | mLocks.wait(); |
| 2846 | } catch (InterruptedException e) { |
| 2847 | // Ignore |
| 2848 | } |
| 2849 | } |
| 2850 | return mPolicy; |
| 2851 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2853 | void systemReady() { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2854 | mSensorManager = new SensorManager(mHandlerThread.getLooper()); |
| 2855 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 2856 | // don't bother with the light sensor if auto brightness is handled in hardware |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 2857 | if (mUseSoftwareAutoBrightness) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2858 | mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2859 | } |
| 2860 | |
Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2861 | // wait until sensors are enabled before turning on screen. |
| 2862 | // some devices will not activate the light sensor properly on boot |
| 2863 | // unless we do this. |
| 2864 | if (mUseSoftwareAutoBrightness) { |
| 2865 | // turn the screen on |
| 2866 | setPowerState(SCREEN_BRIGHT); |
| 2867 | } else { |
| 2868 | // turn everything on |
| 2869 | setPowerState(ALL_BRIGHT); |
| 2870 | } |
| 2871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2872 | synchronized (mLocks) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2873 | Slog.d(TAG, "system ready!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | mDoneBooting = true; |
Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2875 | |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2876 | enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled); |
| 2877 | |
Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2878 | long identity = Binder.clearCallingIdentity(); |
| 2879 | try { |
| 2880 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
| 2881 | mBatteryStats.noteScreenOn(); |
| 2882 | } catch (RemoteException e) { |
| 2883 | // Nothing interesting to do. |
| 2884 | } finally { |
| 2885 | Binder.restoreCallingIdentity(identity); |
| 2886 | } |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | void bootCompleted() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2891 | Slog.d(TAG, "bootCompleted"); |
Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2892 | synchronized (mLocks) { |
| 2893 | mBootCompleted = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2895 | updateWakeLockLocked(); |
| 2896 | mLocks.notifyAll(); |
| 2897 | } |
| 2898 | } |
| 2899 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2900 | // for watchdog |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2901 | public void monitor() { |
| 2902 | synchronized (mLocks) { } |
| 2903 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2904 | |
| 2905 | public int getSupportedWakeLockFlags() { |
| 2906 | int result = PowerManager.PARTIAL_WAKE_LOCK |
| 2907 | | PowerManager.FULL_WAKE_LOCK |
| 2908 | | PowerManager.SCREEN_DIM_WAKE_LOCK; |
| 2909 | |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2910 | if (mProximitySensor != null) { |
| 2911 | result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| 2912 | } |
| 2913 | |
| 2914 | return result; |
| 2915 | } |
| 2916 | |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2917 | public void setBacklightBrightness(int brightness) { |
| 2918 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2919 | // Don't let applications turn the screen all the way off |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2920 | synchronized (mLocks) { |
Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2921 | brightness = Math.max(brightness, mScreenBrightnessDim); |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2922 | mLcdLight.setBrightness(brightness); |
| 2923 | mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0); |
| 2924 | mButtonLight.setBrightness(brightness); |
| 2925 | long identity = Binder.clearCallingIdentity(); |
| 2926 | try { |
| 2927 | mBatteryStats.noteScreenBrightness(brightness); |
| 2928 | } catch (RemoteException e) { |
| 2929 | Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e); |
| 2930 | } finally { |
| 2931 | Binder.restoreCallingIdentity(identity); |
| 2932 | } |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2933 | |
Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2934 | // update our animation state |
Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2935 | synchronized (mLocks) { |
| 2936 | mScreenBrightness.targetValue = brightness; |
| 2937 | mScreenBrightness.jumpToTargetLocked(); |
| 2938 | } |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 2942 | public void setAttentionLight(boolean on, int color) { |
| 2943 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2944 | mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0); |
Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 2945 | } |
| 2946 | |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2947 | private void enableProximityLockLocked() { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2948 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2949 | Slog.d(TAG, "enableProximityLockLocked"); |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2950 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2951 | if (!mProximitySensorEnabled) { |
| 2952 | // clear calling identity so sensor manager battery stats are accurate |
| 2953 | long identity = Binder.clearCallingIdentity(); |
| 2954 | try { |
| 2955 | mSensorManager.registerListener(mProximityListener, mProximitySensor, |
| 2956 | SensorManager.SENSOR_DELAY_NORMAL); |
| 2957 | mProximitySensorEnabled = true; |
| 2958 | } finally { |
| 2959 | Binder.restoreCallingIdentity(identity); |
| 2960 | } |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 2961 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | private void disableProximityLockLocked() { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2965 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2966 | Slog.d(TAG, "disableProximityLockLocked"); |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2967 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2968 | if (mProximitySensorEnabled) { |
| 2969 | // clear calling identity so sensor manager battery stats are accurate |
| 2970 | long identity = Binder.clearCallingIdentity(); |
| 2971 | try { |
| 2972 | mSensorManager.unregisterListener(mProximityListener); |
| 2973 | mHandler.removeCallbacks(mProximityTask); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2974 | if (mProximityPartialLock.isHeld()) { |
| 2975 | mProximityPartialLock.release(); |
| 2976 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2977 | mProximitySensorEnabled = false; |
| 2978 | } finally { |
| 2979 | Binder.restoreCallingIdentity(identity); |
| 2980 | } |
| 2981 | if (mProximitySensorActive) { |
| 2982 | mProximitySensorActive = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2983 | if (mDebugProximitySensor) { |
| 2984 | Slog.d(TAG, "disableProximityLockLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 2985 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2986 | } |
| 2987 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 2988 | forceUserActivityLocked(); |
| 2989 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2990 | } |
Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2991 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2994 | private void proximityChangedLocked(boolean active) { |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2995 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2996 | Slog.d(TAG, "proximityChangedLocked, active: " + active); |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2997 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 2998 | if (!mProximitySensorEnabled) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2999 | Slog.d(TAG, "Ignoring proximity change after sensor is disabled"); |
Mike Lockwood | 0d72f7e | 2009-11-05 20:53:00 -0500 | [diff] [blame] | 3000 | return; |
| 3001 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3002 | if (active) { |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3003 | if (mDebugProximitySensor) { |
| 3004 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 3005 | + mProxIgnoredBecauseScreenTurnedOff); |
| 3006 | } |
| 3007 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 3008 | goToSleepLocked(SystemClock.uptimeMillis(), |
| 3009 | WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR); |
| 3010 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3011 | mProximitySensorActive = true; |
| 3012 | } else { |
| 3013 | // proximity sensor negative events trigger as user activity. |
| 3014 | // temporarily set mUserActivityAllowed to true so this will work |
| 3015 | // even when the keyguard is on. |
| 3016 | mProximitySensorActive = false; |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3017 | if (mDebugProximitySensor) { |
| 3018 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 3019 | + mProxIgnoredBecauseScreenTurnedOff); |
| 3020 | } |
| 3021 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 3022 | forceUserActivityLocked(); |
| 3023 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3024 | |
| 3025 | if (mProximityWakeLockCount == 0) { |
| 3026 | // disable sensor if we have no listeners left after proximity negative |
| 3027 | disableProximityLockLocked(); |
| 3028 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 3032 | private void enableLightSensorLocked(boolean enable) { |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3033 | if (mDebugLightSensor) { |
Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 3034 | Slog.d(TAG, "enableLightSensorLocked enable=" + enable |
| 3035 | + " mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 3036 | } |
| 3037 | if (!mAutoBrightessEnabled) { |
| 3038 | enable = false; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3039 | } |
| 3040 | if (mSensorManager != null && mLightSensorEnabled != enable) { |
| 3041 | mLightSensorEnabled = enable; |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3042 | // clear calling identity so sensor manager battery stats are accurate |
| 3043 | long identity = Binder.clearCallingIdentity(); |
| 3044 | try { |
| 3045 | if (enable) { |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 3046 | // reset our highest value when reenabling |
| 3047 | mHighestLightSensorValue = -1; |
| 3048 | // force recompute of backlight values |
| 3049 | if (mLightSensorValue >= 0) { |
| 3050 | int value = (int)mLightSensorValue; |
| 3051 | mLightSensorValue = -1; |
| 3052 | handleLightSensorValue(value); |
| 3053 | } |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3054 | mSensorManager.registerListener(mLightListener, mLightSensor, |
| 3055 | SensorManager.SENSOR_DELAY_NORMAL); |
| 3056 | } else { |
| 3057 | mSensorManager.unregisterListener(mLightListener); |
| 3058 | mHandler.removeCallbacks(mAutoBrightnessTask); |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 3059 | mLightSensorPendingDecrease = false; |
| 3060 | mLightSensorPendingIncrease = false; |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3061 | } |
| 3062 | } finally { |
| 3063 | Binder.restoreCallingIdentity(identity); |
Mike Lockwood | 06952d9 | 2009-08-13 16:05:38 -0400 | [diff] [blame] | 3064 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3068 | SensorEventListener mProximityListener = new SensorEventListener() { |
| 3069 | public void onSensorChanged(SensorEvent event) { |
Mike Lockwood | ba8eb1e | 2009-11-08 19:31:18 -0500 | [diff] [blame] | 3070 | long milliseconds = SystemClock.elapsedRealtime(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3071 | synchronized (mLocks) { |
| 3072 | float distance = event.values[0]; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3073 | long timeSinceLastEvent = milliseconds - mLastProximityEventTime; |
| 3074 | mLastProximityEventTime = milliseconds; |
| 3075 | mHandler.removeCallbacks(mProximityTask); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3076 | boolean proximityTaskQueued = false; |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3077 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3078 | // compare against getMaximumRange to support sensors that only return 0 or 1 |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3079 | boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && |
| 3080 | distance < mProximitySensor.getMaximumRange()); |
| 3081 | |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3082 | if (mDebugProximitySensor) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3083 | Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3084 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3085 | if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) { |
| 3086 | // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing |
| 3087 | mProximityPendingValue = (active ? 1 : 0); |
| 3088 | mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent); |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3089 | proximityTaskQueued = true; |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3090 | } else { |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3091 | // process the value immediately |
| 3092 | mProximityPendingValue = -1; |
| 3093 | proximityChangedLocked(active); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3094 | } |
Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3095 | |
| 3096 | // update mProximityPartialLock state |
| 3097 | boolean held = mProximityPartialLock.isHeld(); |
| 3098 | if (!held && proximityTaskQueued) { |
| 3099 | // hold wakelock until mProximityTask runs |
| 3100 | mProximityPartialLock.acquire(); |
| 3101 | } else if (held && !proximityTaskQueued) { |
| 3102 | mProximityPartialLock.release(); |
| 3103 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 3108 | // ignore |
| 3109 | } |
| 3110 | }; |
| 3111 | |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 3112 | private void handleLightSensorValue(int value) { |
| 3113 | long milliseconds = SystemClock.elapsedRealtime(); |
| 3114 | if (mLightSensorValue == -1 || |
| 3115 | milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) { |
| 3116 | // process the value immediately if screen has just turned on |
| 3117 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3118 | mLightSensorPendingDecrease = false; |
| 3119 | mLightSensorPendingIncrease = false; |
| 3120 | lightSensorChangedLocked(value); |
| 3121 | } else { |
| 3122 | if ((value > mLightSensorValue && mLightSensorPendingDecrease) || |
| 3123 | (value < mLightSensorValue && mLightSensorPendingIncrease) || |
| 3124 | (value == mLightSensorValue) || |
| 3125 | (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) { |
| 3126 | // delay processing to debounce the sensor |
| 3127 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3128 | mLightSensorPendingDecrease = (value < mLightSensorValue); |
| 3129 | mLightSensorPendingIncrease = (value > mLightSensorValue); |
| 3130 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 3131 | mLightSensorPendingValue = value; |
| 3132 | mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY); |
| 3133 | } |
| 3134 | } else { |
| 3135 | mLightSensorPendingValue = value; |
| 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3140 | SensorEventListener mLightListener = new SensorEventListener() { |
| 3141 | public void onSensorChanged(SensorEvent event) { |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 3142 | if (mDebugLightSensor) { |
| 3143 | Slog.d(TAG, "onSensorChanged: light value: " + event.values[0]); |
| 3144 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3145 | synchronized (mLocks) { |
Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 3146 | // ignore light sensor while screen is turning off |
| 3147 | if (isScreenTurningOffLocked()) { |
| 3148 | return; |
| 3149 | } |
Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame^] | 3150 | handleLightSensorValue((int)event.values[0]); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 3155 | // ignore |
| 3156 | } |
| 3157 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3158 | } |