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