Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.power; |
| 18 | |
Jeff Brown | 13014b5 | 2014-04-07 19:45:27 -0700 | [diff] [blame] | 19 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 20 | import android.app.AppOpsManager; |
Jeff Brown | 13014b5 | 2014-04-07 19:45:27 -0700 | [diff] [blame] | 21 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 22 | import com.android.internal.app.IAppOpsService; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 23 | import com.android.internal.app.IBatteryStats; |
| 24 | import com.android.server.EventLogTags; |
Jeff Brown | 13014b5 | 2014-04-07 19:45:27 -0700 | [diff] [blame] | 25 | import com.android.server.LocalServices; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 26 | |
| 27 | import android.app.ActivityManagerNative; |
| 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 31 | import android.hardware.input.InputManagerInternal; |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 32 | import android.media.AudioManager; |
| 33 | import android.media.Ringtone; |
| 34 | import android.media.RingtoneManager; |
| 35 | import android.net.Uri; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 36 | import android.os.BatteryStats; |
| 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
| 39 | import android.os.Message; |
| 40 | import android.os.PowerManager; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 41 | import android.os.PowerManagerInternal; |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 42 | import android.os.Process; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 43 | import android.os.RemoteException; |
| 44 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 45 | import android.os.UserHandle; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 46 | import android.os.WorkSource; |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 47 | import android.provider.Settings; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 48 | import android.util.EventLog; |
| 49 | import android.util.Slog; |
| 50 | import android.view.WindowManagerPolicy; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Sends broadcasts about important power state changes. |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 54 | * <p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 55 | * This methods of this class may be called by the power manager service while |
| 56 | * its lock is being held. Internally it takes care of sending broadcasts to |
| 57 | * notify other components of the system or applications asynchronously. |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 58 | * </p><p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 59 | * The notifier is designed to collapse unnecessary broadcasts when it is not |
| 60 | * possible for the system to have observed an intermediate state. |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 61 | * </p><p> |
| 62 | * For example, if the device wakes up, goes to sleep, wakes up again and goes to |
| 63 | * sleep again before the wake up notification is sent, then the system will |
| 64 | * be told about only one wake up and sleep. However, we always notify the |
| 65 | * fact that at least one transition occurred. It is especially important to |
| 66 | * tell the system when we go to sleep so that it can lock the keyguard if needed. |
| 67 | * </p> |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 68 | */ |
| 69 | final class Notifier { |
| 70 | private static final String TAG = "PowerManagerNotifier"; |
| 71 | |
| 72 | private static final boolean DEBUG = false; |
| 73 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 74 | private static final int INTERACTIVE_STATE_UNKNOWN = 0; |
| 75 | private static final int INTERACTIVE_STATE_AWAKE = 1; |
| 76 | private static final int INTERACTIVE_STATE_ASLEEP = 2; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 77 | |
| 78 | private static final int MSG_USER_ACTIVITY = 1; |
| 79 | private static final int MSG_BROADCAST = 2; |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 80 | private static final int MSG_WIRELESS_CHARGING_STARTED = 3; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 81 | |
| 82 | private final Object mLock = new Object(); |
| 83 | |
| 84 | private final Context mContext; |
| 85 | private final IBatteryStats mBatteryStats; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 86 | private final IAppOpsService mAppOps; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 87 | private final SuspendBlocker mSuspendBlocker; |
| 88 | private final WindowManagerPolicy mPolicy; |
Jeff Brown | 13014b5 | 2014-04-07 19:45:27 -0700 | [diff] [blame] | 89 | private final ActivityManagerInternal mActivityManagerInternal; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 90 | private final InputManagerInternal mInputManagerInternal; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 91 | |
| 92 | private final NotifierHandler mHandler; |
| 93 | private final Intent mScreenOnIntent; |
| 94 | private final Intent mScreenOffIntent; |
| 95 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 96 | // The current interactive state. |
| 97 | private int mActualInteractiveState; |
| 98 | private int mLastReason; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 99 | |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 100 | // True if there is a pending transition that needs to be reported. |
| 101 | private boolean mPendingWakeUpBroadcast; |
| 102 | private boolean mPendingGoToSleepBroadcast; |
| 103 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 104 | // The currently broadcasted interactive state. This reflects what other parts of the |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 105 | // system have observed. |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 106 | private int mBroadcastedInteractiveState; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 107 | private boolean mBroadcastInProgress; |
| 108 | private long mBroadcastStartTime; |
| 109 | |
| 110 | // True if a user activity message should be sent. |
| 111 | private boolean mUserActivityPending; |
| 112 | |
| 113 | public Notifier(Looper looper, Context context, IBatteryStats batteryStats, |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 114 | IAppOpsService appOps, SuspendBlocker suspendBlocker, |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 115 | WindowManagerPolicy policy) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 116 | mContext = context; |
| 117 | mBatteryStats = batteryStats; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 118 | mAppOps = appOps; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 119 | mSuspendBlocker = suspendBlocker; |
| 120 | mPolicy = policy; |
Jeff Brown | 13014b5 | 2014-04-07 19:45:27 -0700 | [diff] [blame] | 121 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 122 | mInputManagerInternal = LocalServices.getService(InputManagerInternal.class); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 123 | |
| 124 | mHandler = new NotifierHandler(looper); |
| 125 | mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON); |
| 126 | mScreenOnIntent.addFlags( |
| 127 | Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND); |
| 128 | mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF); |
| 129 | mScreenOffIntent.addFlags( |
| 130 | Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND); |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 131 | |
| 132 | // Initialize interactive state for battery stats. |
| 133 | try { |
| 134 | mBatteryStats.noteInteractive(true); |
| 135 | } catch (RemoteException ex) { } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Called when a wake lock is acquired. |
| 140 | */ |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 141 | public void onWakeLockAcquired(int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 142 | int ownerUid, int ownerPid, WorkSource workSource, String historyTag) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 143 | if (DEBUG) { |
| 144 | Slog.d(TAG, "onWakeLockAcquired: flags=" + flags + ", tag=\"" + tag |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 145 | + "\", packageName=" + packageName |
| 146 | + ", ownerUid=" + ownerUid + ", ownerPid=" + ownerPid |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 147 | + ", workSource=" + workSource); |
| 148 | } |
| 149 | |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 150 | try { |
| 151 | final int monitorType = getBatteryStatsWakeLockMonitorType(flags); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 152 | boolean unimportantForLogging = (flags&PowerManager.UNIMPORTANT_FOR_LOGGING) != 0 |
| 153 | && ownerUid == Process.SYSTEM_UID; |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 154 | if (workSource != null) { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 155 | mBatteryStats.noteStartWakelockFromSource(workSource, ownerPid, tag, historyTag, |
| 156 | monitorType, unimportantForLogging); |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 157 | } else { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 158 | mBatteryStats.noteStartWakelock(ownerUid, ownerPid, tag, historyTag, |
| 159 | monitorType, unimportantForLogging); |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 160 | // XXX need to deal with disabled operations. |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 161 | mAppOps.startOperation(AppOpsManager.getToken(mAppOps), |
| 162 | AppOpsManager.OP_WAKE_LOCK, ownerUid, packageName); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 163 | } |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 164 | } catch (RemoteException ex) { |
| 165 | // Ignore |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 170 | * Called when a wake lock is changing. |
| 171 | */ |
| 172 | public void onWakeLockChanging(int flags, String tag, String packageName, |
| 173 | int ownerUid, int ownerPid, WorkSource workSource, String historyTag, |
| 174 | int newFlags, String newTag, String newPackageName, int newOwnerUid, |
| 175 | int newOwnerPid, WorkSource newWorkSource, String newHistoryTag) { |
| 176 | |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 177 | if (workSource != null && newWorkSource != null) { |
Dianne Hackborn | bbb7472 | 2014-03-13 09:50:24 -0700 | [diff] [blame] | 178 | final int monitorType = getBatteryStatsWakeLockMonitorType(flags); |
| 179 | final int newMonitorType = getBatteryStatsWakeLockMonitorType(newFlags); |
| 180 | boolean unimportantForLogging = (newFlags&PowerManager.UNIMPORTANT_FOR_LOGGING) != 0 |
| 181 | && newOwnerUid == Process.SYSTEM_UID; |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 182 | if (DEBUG) { |
| 183 | Slog.d(TAG, "onWakeLockChanging: flags=" + newFlags + ", tag=\"" + newTag |
| 184 | + "\", packageName=" + newPackageName |
| 185 | + ", ownerUid=" + newOwnerUid + ", ownerPid=" + newOwnerPid |
| 186 | + ", workSource=" + newWorkSource); |
| 187 | } |
| 188 | try { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 189 | mBatteryStats.noteChangeWakelockFromSource(workSource, ownerPid, tag, historyTag, |
| 190 | monitorType, newWorkSource, newOwnerPid, newTag, newHistoryTag, |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 191 | newMonitorType, unimportantForLogging); |
| 192 | } catch (RemoteException ex) { |
| 193 | // Ignore |
| 194 | } |
| 195 | } else { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 196 | onWakeLockReleased(flags, tag, packageName, ownerUid, ownerPid, workSource, historyTag); |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 197 | onWakeLockAcquired(newFlags, newTag, newPackageName, newOwnerUid, newOwnerPid, |
| 198 | newWorkSource, newHistoryTag); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 203 | * Called when a wake lock is released. |
| 204 | */ |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 205 | public void onWakeLockReleased(int flags, String tag, String packageName, |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 206 | int ownerUid, int ownerPid, WorkSource workSource, String historyTag) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 207 | if (DEBUG) { |
| 208 | Slog.d(TAG, "onWakeLockReleased: flags=" + flags + ", tag=\"" + tag |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 209 | + "\", packageName=" + packageName |
| 210 | + ", ownerUid=" + ownerUid + ", ownerPid=" + ownerPid |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 211 | + ", workSource=" + workSource); |
| 212 | } |
| 213 | |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 214 | try { |
| 215 | final int monitorType = getBatteryStatsWakeLockMonitorType(flags); |
| 216 | if (workSource != null) { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 217 | mBatteryStats.noteStopWakelockFromSource(workSource, ownerPid, tag, historyTag, |
| 218 | monitorType); |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 219 | } else { |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 220 | mBatteryStats.noteStopWakelock(ownerUid, ownerPid, tag, historyTag, monitorType); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 221 | mAppOps.finishOperation(AppOpsManager.getToken(mAppOps), |
| 222 | AppOpsManager.OP_WAKE_LOCK, ownerUid, packageName); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 223 | } |
Craig Mautner | 259328c | 2012-08-21 19:30:58 -0700 | [diff] [blame] | 224 | } catch (RemoteException ex) { |
| 225 | // Ignore |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 229 | private static int getBatteryStatsWakeLockMonitorType(int flags) { |
| 230 | switch (flags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 231 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 232 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 233 | return BatteryStats.WAKE_TYPE_PARTIAL; |
| 234 | default: |
| 235 | return BatteryStats.WAKE_TYPE_FULL; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 240 | * Notifies that the device is changing wakefulness. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 241 | */ |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 242 | public void onWakefulnessChangeStarted(int wakefulness, int reason) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 243 | if (DEBUG) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 244 | Slog.d(TAG, "onWakefulnessChangeStarted: wakefulness=" + wakefulness |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 245 | + ", reason=" + reason); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 248 | // We handle interactive state changes once they start so that the system can |
| 249 | // set everything up or the user to begin interacting with applications. |
| 250 | final boolean interactive = PowerManagerInternal.isInteractive(wakefulness); |
| 251 | if (interactive) { |
| 252 | handleWakefulnessChange(wakefulness, interactive, reason); |
| 253 | } else { |
| 254 | mLastReason = reason; |
| 255 | } |
| 256 | |
| 257 | // Start input as soon as we start waking up or going to sleep. |
| 258 | mInputManagerInternal.setInteractive(interactive); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Notifies that the device has finished changing wakefulness. |
| 263 | */ |
| 264 | public void onWakefulnessChangeFinished(int wakefulness) { |
| 265 | if (DEBUG) { |
| 266 | Slog.d(TAG, "onWakefulnessChangeFinished: wakefulness=" + wakefulness); |
| 267 | } |
| 268 | |
| 269 | // Handle interactive state changes once they are finished so that the system can |
| 270 | // finish pending transitions (such as turning the screen off) before causing |
| 271 | // applications to change state visibly. |
| 272 | final boolean interactive = PowerManagerInternal.isInteractive(wakefulness); |
| 273 | if (!interactive) { |
| 274 | handleWakefulnessChange(wakefulness, interactive, mLastReason); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | private void handleWakefulnessChange(final int wakefulness, boolean interactive, |
| 279 | final int reason) { |
| 280 | // Tell the activity manager about changes in wakefulness, not just interactivity. |
| 281 | // It needs more granularity than other components. |
| 282 | mHandler.post(new Runnable() { |
| 283 | @Override |
| 284 | public void run() { |
| 285 | mActivityManagerInternal.onWakefulnessChanged(wakefulness); |
| 286 | } |
| 287 | }); |
| 288 | |
| 289 | // Handle changes in the overall interactive state. |
| 290 | boolean interactiveChanged = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 291 | synchronized (mLock) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 292 | // Broadcast interactive state changes. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 293 | if (interactive) { |
| 294 | // Waking up... |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 295 | interactiveChanged = (mActualInteractiveState != INTERACTIVE_STATE_AWAKE); |
| 296 | if (interactiveChanged) { |
| 297 | mActualInteractiveState = INTERACTIVE_STATE_AWAKE; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 298 | mPendingWakeUpBroadcast = true; |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 299 | mHandler.post(new Runnable() { |
| 300 | @Override |
| 301 | public void run() { |
| 302 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, 0, 0, 0); |
Jeff Brown | 36c4db8 | 2014-09-19 12:05:31 -0700 | [diff] [blame] | 303 | mPolicy.wakingUp(); |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 304 | } |
| 305 | }); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 306 | updatePendingBroadcastLocked(); |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 307 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 308 | } else { |
| 309 | // Going to sleep... |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 310 | // This is a good time to make transitions that we don't want the user to see, |
| 311 | // such as bringing the key guard to focus. There's no guarantee for this, |
| 312 | // however because the user could turn the device on again at any time. |
| 313 | // Some things may need to be protected by other mechanisms that defer screen on. |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 314 | interactiveChanged = (mActualInteractiveState != INTERACTIVE_STATE_ASLEEP); |
| 315 | if (interactiveChanged) { |
| 316 | mActualInteractiveState = INTERACTIVE_STATE_ASLEEP; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 317 | mPendingGoToSleepBroadcast = true; |
| 318 | if (mUserActivityPending) { |
| 319 | mUserActivityPending = false; |
| 320 | mHandler.removeMessages(MSG_USER_ACTIVITY); |
| 321 | } |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 322 | mHandler.post(new Runnable() { |
| 323 | @Override |
| 324 | public void run() { |
| 325 | int why = WindowManagerPolicy.OFF_BECAUSE_OF_USER; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 326 | switch (reason) { |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 327 | case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN: |
| 328 | why = WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN; |
| 329 | break; |
| 330 | case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT: |
| 331 | why = WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT; |
| 332 | break; |
| 333 | } |
| 334 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, why, 0, 0); |
| 335 | mPolicy.goingToSleep(why); |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 336 | } |
| 337 | }); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 338 | updatePendingBroadcastLocked(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 339 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 342 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 343 | // Notify battery stats. |
| 344 | if (interactiveChanged) { |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 345 | try { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 346 | mBatteryStats.noteInteractive(interactive); |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 347 | } catch (RemoteException ex) { } |
| 348 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Called when there has been user activity. |
| 353 | */ |
| 354 | public void onUserActivity(int event, int uid) { |
| 355 | if (DEBUG) { |
| 356 | Slog.d(TAG, "onUserActivity: event=" + event + ", uid=" + uid); |
| 357 | } |
| 358 | |
| 359 | try { |
| 360 | mBatteryStats.noteUserActivity(uid, event); |
| 361 | } catch (RemoteException ex) { |
| 362 | // Ignore |
| 363 | } |
| 364 | |
| 365 | synchronized (mLock) { |
| 366 | if (!mUserActivityPending) { |
| 367 | mUserActivityPending = true; |
| 368 | Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY); |
| 369 | msg.setAsynchronous(true); |
| 370 | mHandler.sendMessage(msg); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 375 | /** |
| 376 | * Called when wireless charging has started so as to provide user feedback. |
| 377 | */ |
| 378 | public void onWirelessChargingStarted() { |
| 379 | if (DEBUG) { |
| 380 | Slog.d(TAG, "onWirelessChargingStarted"); |
| 381 | } |
| 382 | |
| 383 | mSuspendBlocker.acquire(); |
| 384 | Message msg = mHandler.obtainMessage(MSG_WIRELESS_CHARGING_STARTED); |
| 385 | msg.setAsynchronous(true); |
| 386 | mHandler.sendMessage(msg); |
| 387 | } |
| 388 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 389 | private void updatePendingBroadcastLocked() { |
| 390 | if (!mBroadcastInProgress |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 391 | && mActualInteractiveState != INTERACTIVE_STATE_UNKNOWN |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 392 | && (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 393 | || mActualInteractiveState != mBroadcastedInteractiveState)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 394 | mBroadcastInProgress = true; |
| 395 | mSuspendBlocker.acquire(); |
| 396 | Message msg = mHandler.obtainMessage(MSG_BROADCAST); |
| 397 | msg.setAsynchronous(true); |
| 398 | mHandler.sendMessage(msg); |
| 399 | } |
| 400 | } |
| 401 | |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 402 | private void finishPendingBroadcastLocked() { |
| 403 | mBroadcastInProgress = false; |
| 404 | mSuspendBlocker.release(); |
| 405 | } |
| 406 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 407 | private void sendUserActivity() { |
| 408 | synchronized (mLock) { |
| 409 | if (!mUserActivityPending) { |
| 410 | return; |
| 411 | } |
| 412 | mUserActivityPending = false; |
| 413 | } |
| 414 | |
| 415 | mPolicy.userActivity(); |
| 416 | } |
| 417 | |
| 418 | private void sendNextBroadcast() { |
| 419 | final int powerState; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 420 | synchronized (mLock) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 421 | if (mBroadcastedInteractiveState == INTERACTIVE_STATE_UNKNOWN) { |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 422 | // Broadcasted power state is unknown. Send wake up. |
| 423 | mPendingWakeUpBroadcast = false; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 424 | mBroadcastedInteractiveState = INTERACTIVE_STATE_AWAKE; |
| 425 | } else if (mBroadcastedInteractiveState == INTERACTIVE_STATE_AWAKE) { |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 426 | // Broadcasted power state is awake. Send asleep if needed. |
| 427 | if (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 428 | || mActualInteractiveState == INTERACTIVE_STATE_ASLEEP) { |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 429 | mPendingGoToSleepBroadcast = false; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 430 | mBroadcastedInteractiveState = INTERACTIVE_STATE_ASLEEP; |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 431 | } else { |
| 432 | finishPendingBroadcastLocked(); |
| 433 | return; |
| 434 | } |
| 435 | } else { |
| 436 | // Broadcasted power state is asleep. Send awake if needed. |
| 437 | if (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 438 | || mActualInteractiveState == INTERACTIVE_STATE_AWAKE) { |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 439 | mPendingWakeUpBroadcast = false; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 440 | mBroadcastedInteractiveState = INTERACTIVE_STATE_AWAKE; |
Jeff Brown | 5430835 | 2012-10-04 17:59:58 -0700 | [diff] [blame] | 441 | } else { |
| 442 | finishPendingBroadcastLocked(); |
| 443 | return; |
| 444 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 447 | mBroadcastStartTime = SystemClock.uptimeMillis(); |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 448 | powerState = mBroadcastedInteractiveState; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, 1); |
| 452 | |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 453 | if (powerState == INTERACTIVE_STATE_AWAKE) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 454 | sendWakeUpBroadcast(); |
| 455 | } else { |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 456 | sendGoToSleepBroadcast(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | private void sendWakeUpBroadcast() { |
| 461 | if (DEBUG) { |
| 462 | Slog.d(TAG, "Sending wake up broadcast."); |
| 463 | } |
| 464 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 465 | if (ActivityManagerNative.isSystemReady()) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 466 | mContext.sendOrderedBroadcastAsUser(mScreenOnIntent, UserHandle.ALL, null, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 467 | mWakeUpBroadcastDone, mHandler, 0, null, null); |
| 468 | } else { |
| 469 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2, 1); |
| 470 | sendNextBroadcast(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | private final BroadcastReceiver mWakeUpBroadcastDone = new BroadcastReceiver() { |
| 475 | @Override |
| 476 | public void onReceive(Context context, Intent intent) { |
| 477 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1, |
| 478 | SystemClock.uptimeMillis() - mBroadcastStartTime, 1); |
| 479 | sendNextBroadcast(); |
| 480 | } |
| 481 | }; |
| 482 | |
Jim Miller | c522d16 | 2014-07-01 17:10:21 -0700 | [diff] [blame] | 483 | private void sendGoToSleepBroadcast() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 484 | if (DEBUG) { |
| 485 | Slog.d(TAG, "Sending go to sleep broadcast."); |
| 486 | } |
| 487 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 488 | if (ActivityManagerNative.isSystemReady()) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 489 | mContext.sendOrderedBroadcastAsUser(mScreenOffIntent, UserHandle.ALL, null, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 490 | mGoToSleepBroadcastDone, mHandler, 0, null, null); |
| 491 | } else { |
| 492 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3, 1); |
| 493 | sendNextBroadcast(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private final BroadcastReceiver mGoToSleepBroadcastDone = new BroadcastReceiver() { |
| 498 | @Override |
| 499 | public void onReceive(Context context, Intent intent) { |
| 500 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0, |
| 501 | SystemClock.uptimeMillis() - mBroadcastStartTime, 1); |
| 502 | sendNextBroadcast(); |
| 503 | } |
| 504 | }; |
| 505 | |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 506 | private void playWirelessChargingStartedSound() { |
| 507 | final String soundPath = Settings.Global.getString(mContext.getContentResolver(), |
| 508 | Settings.Global.WIRELESS_CHARGING_STARTED_SOUND); |
| 509 | if (soundPath != null) { |
| 510 | final Uri soundUri = Uri.parse("file://" + soundPath); |
| 511 | if (soundUri != null) { |
| 512 | final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); |
| 513 | if (sfx != null) { |
| 514 | sfx.setStreamType(AudioManager.STREAM_SYSTEM); |
| 515 | sfx.play(); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | mSuspendBlocker.release(); |
| 521 | } |
| 522 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 523 | private final class NotifierHandler extends Handler { |
| 524 | public NotifierHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 525 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public void handleMessage(Message msg) { |
| 530 | switch (msg.what) { |
| 531 | case MSG_USER_ACTIVITY: |
| 532 | sendUserActivity(); |
| 533 | break; |
| 534 | |
| 535 | case MSG_BROADCAST: |
| 536 | sendNextBroadcast(); |
| 537 | break; |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 538 | |
| 539 | case MSG_WIRELESS_CHARGING_STARTED: |
| 540 | playWirelessChargingStartedSound(); |
| 541 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | } |