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