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