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