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