keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.car; |
| 17 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 18 | import java.io.PrintWriter; |
| 19 | import java.util.LinkedList; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 20 | import java.util.Map; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 21 | import java.util.Timer; |
| 22 | import java.util.TimerTask; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 23 | import java.util.concurrent.ConcurrentHashMap; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 24 | import java.util.concurrent.CopyOnWriteArrayList; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.car.hal.PowerHalService; |
| 27 | import com.android.car.hal.PowerHalService.PowerState; |
Enrico Granata | b19bc32 | 2017-10-12 12:25:06 -0700 | [diff] [blame] | 28 | import com.android.car.systeminterface.SystemInterface; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 29 | import com.android.internal.annotations.GuardedBy; |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 31 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 32 | import android.car.Car; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 33 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 34 | import android.car.hardware.power.ICarPower; |
| 35 | import android.car.hardware.power.ICarPowerStateListener; |
| 36 | import android.content.Context; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 37 | import android.os.Binder; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 38 | import android.os.Handler; |
| 39 | import android.os.HandlerThread; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 40 | import android.os.IBinder; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 41 | import android.os.Looper; |
| 42 | import android.os.Message; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 43 | import android.os.PowerManager; |
| 44 | import android.os.RemoteCallbackList; |
| 45 | import android.os.RemoteException; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 46 | import android.os.SystemClock; |
| 47 | import android.util.Log; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 48 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 49 | public class CarPowerManagementService extends ICarPower.Stub implements CarServiceBase, |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 50 | PowerHalService.PowerEventListener { |
| 51 | |
| 52 | /** |
| 53 | * Listener for other services to monitor power events. |
| 54 | */ |
| 55 | public interface PowerServiceEventListener { |
| 56 | /** |
| 57 | * Shutdown is happening |
| 58 | */ |
| 59 | void onShutdown(); |
| 60 | |
| 61 | /** |
| 62 | * Entering deep sleep. |
| 63 | */ |
| 64 | void onSleepEntry(); |
| 65 | |
| 66 | /** |
| 67 | * Got out of deep sleep. |
| 68 | */ |
| 69 | void onSleepExit(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Interface for components requiring processing time before shutting-down or |
| 74 | * entering sleep, and wake-up after shut-down. |
| 75 | */ |
| 76 | public interface PowerEventProcessingHandler { |
| 77 | /** |
| 78 | * Called before shutdown or sleep entry to allow running some processing. This call |
| 79 | * should only queue such task in different thread and should return quickly. |
| 80 | * Blocking inside this call can trigger watchdog timer which can terminate the |
| 81 | * whole system. |
| 82 | * @param shuttingDown whether system is shutting down or not (= sleep entry). |
| 83 | * @return time necessary to run processing in ms. should return 0 if there is no |
| 84 | * processing necessary. |
| 85 | */ |
Keun-young Park | 303ea1d | 2016-01-21 11:26:16 -0800 | [diff] [blame] | 86 | long onPrepareShutdown(boolean shuttingDown); |
| 87 | |
| 88 | /** |
| 89 | * Called when power state is changed to ON state. Display can be either on or off. |
| 90 | * @param displayOn |
| 91 | */ |
| 92 | void onPowerOn(boolean displayOn); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Returns wake up time after system is fully shutdown. Power controller will power on |
| 96 | * the system after this time. This power on is meant for regular maintenance kind of |
| 97 | * operation. |
| 98 | * @return 0 of wake up is not necessary. |
| 99 | */ |
| 100 | int getWakeupTime(); |
| 101 | } |
| 102 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 103 | private final Context mContext; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 104 | private final PowerHalService mHal; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 105 | private final SystemInterface mSystemInterface; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 106 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 107 | private final CopyOnWriteArrayList<PowerServiceEventListener> mListeners = |
| 108 | new CopyOnWriteArrayList<>(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 109 | private final CopyOnWriteArrayList<PowerEventProcessingHandlerWrapper> |
| 110 | mPowerEventProcessingHandlers = new CopyOnWriteArrayList<>(); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 111 | private final PowerManagerCallbackList mPowerManagerListeners = new PowerManagerCallbackList(); |
| 112 | private final Map<IBinder, Integer> mPowerManagerListenerTokens = new ConcurrentHashMap<>(); |
| 113 | private int mTokenValue = 1; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 114 | |
| 115 | @GuardedBy("this") |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 116 | private PowerState mCurrentState; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 117 | @GuardedBy("this") |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 118 | private Timer mTimer; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 119 | @GuardedBy("this") |
| 120 | private long mProcessingStartTime; |
| 121 | @GuardedBy("this") |
| 122 | private long mLastSleepEntryTime; |
| 123 | @GuardedBy("this") |
| 124 | private final LinkedList<PowerState> mPendingPowerStates = new LinkedList<>(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 125 | @GuardedBy("this") |
| 126 | private HandlerThread mHandlerThread; |
| 127 | @GuardedBy("this") |
| 128 | private PowerHandler mHandler; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 129 | private int mBootReason; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 130 | private boolean mShutdownOnNextSuspend = false; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 131 | |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 132 | // TODO: Make this OEM configurable. |
| 133 | private final static int APP_EXTEND_MAX_MS = 10000; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 134 | private final static int SHUTDOWN_POLLING_INTERVAL_MS = 2000; |
| 135 | private final static int SHUTDOWN_EXTEND_MAX_MS = 5000; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 136 | |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 137 | private class PowerManagerCallbackList extends RemoteCallbackList<ICarPowerStateListener> { |
| 138 | /** |
| 139 | * Old version of {@link #onCallbackDied(E, Object)} that |
| 140 | * does not provide a cookie. |
| 141 | */ |
| 142 | @Override |
| 143 | public void onCallbackDied(ICarPowerStateListener listener) { |
| 144 | Log.i(CarLog.TAG_POWER, "binderDied " + listener.asBinder()); |
| 145 | CarPowerManagementService.this.doUnregisterListener(listener); |
| 146 | } |
| 147 | } |
| 148 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 149 | public CarPowerManagementService(Context context, PowerHalService powerHal, |
| 150 | SystemInterface systemInterface) { |
| 151 | mContext = context; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 152 | mHal = powerHal; |
| 153 | mSystemInterface = systemInterface; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Create a dummy instance for unit testing purpose only. Instance constructed in this way |
| 158 | * is not safe as members expected to be non-null are null. |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 159 | */ |
| 160 | @VisibleForTesting |
| 161 | protected CarPowerManagementService() { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 162 | mContext = null; |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 163 | mHal = null; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 164 | mSystemInterface = null; |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 165 | mHandlerThread = null; |
Keun-young Park | ca013ae | 2016-02-10 16:22:25 -0800 | [diff] [blame] | 166 | mHandler = new PowerHandler(Looper.getMainLooper()); |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 167 | } |
| 168 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public void init() { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 171 | synchronized (this) { |
| 172 | mHandlerThread = new HandlerThread(CarLog.TAG_POWER); |
| 173 | mHandlerThread.start(); |
| 174 | mHandler = new PowerHandler(mHandlerThread.getLooper()); |
| 175 | } |
| 176 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 177 | mHal.setListener(this); |
| 178 | if (mHal.isPowerStateSupported()) { |
| 179 | mHal.sendBootComplete(); |
| 180 | PowerState currentState = mHal.getCurrentPowerState(); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 181 | if (currentState != null) { |
| 182 | onApPowerStateChange(currentState); |
| 183 | } else { |
| 184 | Log.w(CarLog.TAG_POWER, "Unable to get get current power state during " |
| 185 | + "initialization"); |
| 186 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 187 | } else { |
| 188 | Log.w(CarLog.TAG_POWER, "Vehicle hal does not support power state yet."); |
Keun-young Park | fad5792 | 2016-04-21 11:00:45 -0700 | [diff] [blame] | 189 | onApPowerStateChange(new PowerState(PowerHalService.STATE_ON_FULL, 0)); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 190 | mSystemInterface.switchToFullWakeLock(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 191 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 192 | mSystemInterface.startDisplayStateMonitoring(this); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void release() { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 197 | HandlerThread handlerThread; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 198 | synchronized (this) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 199 | releaseTimerLocked(); |
| 200 | mCurrentState = null; |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 201 | mHandler.cancelAll(); |
| 202 | handlerThread = mHandlerThread; |
| 203 | } |
| 204 | handlerThread.quitSafely(); |
| 205 | try { |
| 206 | handlerThread.join(1000); |
| 207 | } catch (InterruptedException e) { |
| 208 | Log.e(CarLog.TAG_POWER, "Timeout while joining for handler thread to join."); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 209 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 210 | mSystemInterface.stopDisplayStateMonitoring(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 211 | mListeners.clear(); |
Keun-young Park | 303ea1d | 2016-01-21 11:26:16 -0800 | [diff] [blame] | 212 | mPowerEventProcessingHandlers.clear(); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 213 | mPowerManagerListeners.kill(); |
| 214 | mPowerManagerListenerTokens.clear(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 215 | mSystemInterface.releaseAllWakeLocks(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Register listener to monitor power event. There is no unregister counter-part and the list |
| 220 | * will be cleared when the service is released. |
| 221 | * @param listener |
| 222 | */ |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 223 | public synchronized void registerPowerEventListener(PowerServiceEventListener listener) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 224 | mListeners.add(listener); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Register PowerEventPreprocessingHandler to run pre-processing before shutdown or |
| 229 | * sleep entry. There is no unregister counter-part and the list |
| 230 | * will be cleared when the service is released. |
| 231 | * @param handler |
| 232 | */ |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 233 | public synchronized void registerPowerEventProcessingHandler( |
| 234 | PowerEventProcessingHandler handler) { |
| 235 | mPowerEventProcessingHandlers.add(new PowerEventProcessingHandlerWrapper(handler)); |
| 236 | // onPowerOn will not be called if power on notification is already done inside the |
| 237 | // handler thread. So request it once again here. Wrapper will have its own |
| 238 | // gatekeeping to prevent calling onPowerOn twice. |
| 239 | mHandler.handlePowerOn(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Notifies earlier completion of power event processing. PowerEventProcessingHandler quotes |
| 244 | * time necessary from onPrePowerEvent() call, but actual processing can finish earlier than |
| 245 | * that, and this call can be called in such case to trigger shutdown without waiting further. |
| 246 | * |
| 247 | * @param handler PowerEventProcessingHandler that was already registered with |
| 248 | * {@link #registerPowerEventListener(PowerServiceEventListener)} call. If it was not |
| 249 | * registered before, this call will be ignored. |
| 250 | */ |
| 251 | public void notifyPowerEventProcessingCompletion(PowerEventProcessingHandler handler) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 252 | long processingTime = 0; |
| 253 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 254 | if (wrapper.handler == handler) { |
| 255 | wrapper.markProcessingDone(); |
| 256 | } else if (!wrapper.isProcessingDone()) { |
| 257 | processingTime = Math.max(processingTime, wrapper.getProcessingTime()); |
| 258 | } |
| 259 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 260 | synchronized (mPowerManagerListenerTokens) { |
| 261 | if (!mPowerManagerListenerTokens.isEmpty()) { |
| 262 | processingTime += APP_EXTEND_MAX_MS; |
| 263 | } |
| 264 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 265 | long now = SystemClock.elapsedRealtime(); |
| 266 | long startTime; |
| 267 | boolean shouldShutdown = true; |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 268 | PowerHandler powerHandler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 269 | synchronized (this) { |
| 270 | startTime = mProcessingStartTime; |
| 271 | if (mCurrentState == null) { |
| 272 | return; |
| 273 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 274 | if (mCurrentState.mState != PowerHalService.STATE_SHUTDOWN_PREPARE) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 275 | return; |
| 276 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 277 | if (mCurrentState.canEnterDeepSleep() && !mShutdownOnNextSuspend) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 278 | shouldShutdown = false; |
| 279 | if (mLastSleepEntryTime > mProcessingStartTime && mLastSleepEntryTime < now) { |
| 280 | // already slept |
| 281 | return; |
| 282 | } |
| 283 | } |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 284 | powerHandler = mHandler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 285 | } |
| 286 | if ((startTime + processingTime) <= now) { |
| 287 | Log.i(CarLog.TAG_POWER, "Processing all done"); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 288 | powerHandler.handleProcessingComplete(shouldShutdown); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 289 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void dump(PrintWriter writer) { |
| 294 | writer.println("*PowerManagementService*"); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 295 | writer.print("mCurrentState:" + mCurrentState); |
| 296 | writer.print(",mProcessingStartTime:" + mProcessingStartTime); |
| 297 | writer.println(",mLastSleepEntryTime:" + mLastSleepEntryTime); |
| 298 | writer.println("**PowerEventProcessingHandlers"); |
| 299 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 300 | writer.println(wrapper.toString()); |
| 301 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Steve Paik | 110f95f | 2017-11-22 14:44:05 -0800 | [diff] [blame] | 305 | public void onBootReasonReceived(int bootReason) { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 306 | mBootReason = bootReason; |
Steve Paik | 110f95f | 2017-11-22 14:44:05 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 310 | public void onApPowerStateChange(PowerState state) { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 311 | PowerHandler handler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 312 | synchronized (this) { |
| 313 | mPendingPowerStates.addFirst(state); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 314 | handler = mHandler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 315 | } |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 316 | handler.handlePowerStateChange(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 319 | private void doHandlePowerStateChange() { |
| 320 | PowerState state = null; |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 321 | PowerHandler handler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 322 | synchronized (this) { |
| 323 | state = mPendingPowerStates.peekFirst(); |
| 324 | mPendingPowerStates.clear(); |
| 325 | if (state == null) { |
| 326 | return; |
| 327 | } |
| 328 | if (!needPowerStateChange(state)) { |
| 329 | return; |
| 330 | } |
| 331 | // now real power change happens. Whatever was queued before should be all cancelled. |
| 332 | releaseTimerLocked(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 333 | handler = mHandler; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 334 | } |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 335 | handler.cancelProcessingComplete(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 336 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 337 | Log.i(CarLog.TAG_POWER, "Power state change:" + state); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 338 | switch (state.mState) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 339 | case PowerHalService.STATE_ON_DISP_OFF: |
| 340 | handleDisplayOff(state); |
Keun-young Park | 303ea1d | 2016-01-21 11:26:16 -0800 | [diff] [blame] | 341 | notifyPowerOn(false); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 342 | break; |
| 343 | case PowerHalService.STATE_ON_FULL: |
Keun-young Park | 1f4d6a7 | 2016-02-11 09:49:47 -0800 | [diff] [blame] | 344 | handleFullOn(state); |
| 345 | notifyPowerOn(true); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 346 | break; |
| 347 | case PowerHalService.STATE_SHUTDOWN_PREPARE: |
| 348 | handleShutdownPrepare(state); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private void handleDisplayOff(PowerState newState) { |
| 354 | setCurrentState(newState); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 355 | mSystemInterface.setDisplayState(false); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | private void handleFullOn(PowerState newState) { |
| 359 | setCurrentState(newState); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 360 | mSystemInterface.setDisplayState(true); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 363 | @VisibleForTesting |
| 364 | protected void notifyPowerOn(boolean displayOn) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 365 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 366 | wrapper.callOnPowerOn(displayOn); |
Keun-young Park | 303ea1d | 2016-01-21 11:26:16 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 370 | @VisibleForTesting |
| 371 | protected long notifyPrepareShutdown(boolean shuttingDown) { |
| 372 | long processingTimeMs = 0; |
| 373 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 374 | long handlerProcessingTime = wrapper.handler.onPrepareShutdown(shuttingDown); |
| 375 | if (handlerProcessingTime > processingTimeMs) { |
| 376 | processingTimeMs = handlerProcessingTime; |
| 377 | } |
| 378 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 379 | // Add time for powerManager events |
| 380 | processingTimeMs += sendPowerManagerEvent(shuttingDown); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 381 | return processingTimeMs; |
| 382 | } |
| 383 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 384 | private void handleShutdownPrepare(PowerState newState) { |
| 385 | setCurrentState(newState); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 386 | mSystemInterface.setDisplayState(false);; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 387 | boolean shouldShutdown = true; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 388 | if (mHal.isDeepSleepAllowed() && mSystemInterface.isSystemSupportingDeepSleep() && |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 389 | newState.canEnterDeepSleep() && !mShutdownOnNextSuspend) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 390 | Log.i(CarLog.TAG_POWER, "starting sleep"); |
| 391 | shouldShutdown = false; |
| 392 | doHandlePreprocessing(shouldShutdown); |
| 393 | return; |
| 394 | } else if (newState.canPostponeShutdown()) { |
| 395 | Log.i(CarLog.TAG_POWER, "starting shutdown with processing"); |
| 396 | doHandlePreprocessing(shouldShutdown); |
| 397 | } else { |
| 398 | Log.i(CarLog.TAG_POWER, "starting shutdown immediately"); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 399 | synchronized (this) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 400 | releaseTimerLocked(); |
| 401 | } |
| 402 | doHandleShutdown(); |
| 403 | } |
| 404 | } |
| 405 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 406 | @GuardedBy("this") |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 407 | private void releaseTimerLocked() { |
| 408 | if (mTimer != null) { |
| 409 | mTimer.cancel(); |
| 410 | } |
| 411 | mTimer = null; |
| 412 | } |
| 413 | |
| 414 | private void doHandlePreprocessing(boolean shuttingDown) { |
| 415 | long processingTimeMs = 0; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 416 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 417 | long handlerProcessingTime = wrapper.handler.onPrepareShutdown(shuttingDown); |
| 418 | if (handlerProcessingTime > 0) { |
| 419 | wrapper.setProcessingTimeAndResetProcessingDone(handlerProcessingTime); |
| 420 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 421 | if (handlerProcessingTime > processingTimeMs) { |
| 422 | processingTimeMs = handlerProcessingTime; |
| 423 | } |
| 424 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 425 | // Add time for powerManager events |
| 426 | processingTimeMs += sendPowerManagerEvent(shuttingDown); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 427 | if (processingTimeMs > 0) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 428 | int pollingCount = (int)(processingTimeMs / SHUTDOWN_POLLING_INTERVAL_MS) + 1; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 429 | Log.i(CarLog.TAG_POWER, "processing before shutdown expected for :" + processingTimeMs + |
| 430 | " ms, adding polling:" + pollingCount); |
| 431 | synchronized (this) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 432 | mProcessingStartTime = SystemClock.elapsedRealtime(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 433 | releaseTimerLocked(); |
| 434 | mTimer = new Timer(); |
| 435 | mTimer.scheduleAtFixedRate(new ShutdownProcessingTimerTask(shuttingDown, |
| 436 | pollingCount), |
| 437 | 0 /*delay*/, |
| 438 | SHUTDOWN_POLLING_INTERVAL_MS); |
| 439 | } |
| 440 | } else { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 441 | PowerHandler handler; |
| 442 | synchronized (this) { |
| 443 | handler = mHandler; |
| 444 | } |
| 445 | handler.handleProcessingComplete(shuttingDown); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 449 | private long sendPowerManagerEvent(boolean shuttingDown) { |
| 450 | long processingTimeMs = 0; |
| 451 | int newState = shuttingDown ? CarPowerStateListener.SHUTDOWN_ENTER : |
| 452 | CarPowerStateListener.SUSPEND_ENTER; |
| 453 | synchronized (mPowerManagerListenerTokens) { |
| 454 | mPowerManagerListenerTokens.clear(); |
| 455 | int i = mPowerManagerListeners.beginBroadcast(); |
| 456 | while (i-- > 0) { |
| 457 | try { |
| 458 | ICarPowerStateListener listener = mPowerManagerListeners.getBroadcastItem(i); |
| 459 | listener.onStateChanged(newState, mTokenValue); |
| 460 | mPowerManagerListenerTokens.put(listener.asBinder(), mTokenValue); |
| 461 | mTokenValue++; |
| 462 | } catch (RemoteException e) { |
| 463 | // Its likely the connection snapped. Let binder death handle the situation. |
| 464 | Log.e(CarLog.TAG_POWER, "onStateChanged calling failed: " + e); |
| 465 | } |
| 466 | } |
| 467 | mPowerManagerListeners.finishBroadcast(); |
| 468 | if (!mPowerManagerListenerTokens.isEmpty()) { |
| 469 | Log.i(CarLog.TAG_POWER, "mPowerMangerListenerTokens not empty, add APP_EXTEND_MAX_MS"); |
| 470 | processingTimeMs += APP_EXTEND_MAX_MS; |
| 471 | } |
| 472 | } |
| 473 | return processingTimeMs; |
| 474 | } |
| 475 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 476 | private void doHandleDeepSleep() { |
Keun-young Park | 9d4cf62 | 2016-08-04 13:24:28 -0700 | [diff] [blame] | 477 | // keep holding partial wakelock to prevent entering sleep before enterDeepSleep call |
| 478 | // enterDeepSleep should force sleep entry even if wake lock is kept. |
| 479 | mSystemInterface.switchToPartialWakeLock(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 480 | PowerHandler handler; |
| 481 | synchronized (this) { |
| 482 | handler = mHandler; |
| 483 | } |
| 484 | handler.cancelProcessingComplete(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 485 | for (PowerServiceEventListener listener : mListeners) { |
| 486 | listener.onSleepEntry(); |
| 487 | } |
| 488 | int wakeupTimeSec = getWakeupTime(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 489 | mHal.sendSleepEntry(); |
| 490 | synchronized (this) { |
| 491 | mLastSleepEntryTime = SystemClock.elapsedRealtime(); |
| 492 | } |
Steve Paik | 0f9fc00 | 2018-02-09 17:42:00 -0800 | [diff] [blame] | 493 | if (mSystemInterface.enterDeepSleep(wakeupTimeSec) == false) { |
| 494 | // System did not suspend. Need to shutdown |
| 495 | // TODO: Shutdown gracefully |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 496 | Log.e(CarLog.TAG_POWER, "Sleep did not succeed. Need to shutdown"); |
Steve Paik | 0f9fc00 | 2018-02-09 17:42:00 -0800 | [diff] [blame] | 497 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 498 | mHal.sendSleepExit(); |
| 499 | for (PowerServiceEventListener listener : mListeners) { |
| 500 | listener.onSleepExit(); |
| 501 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 502 | // Notify applications |
| 503 | int i = mPowerManagerListeners.beginBroadcast(); |
| 504 | while (i-- > 0) { |
| 505 | try { |
| 506 | ICarPowerStateListener listener = mPowerManagerListeners.getBroadcastItem(i); |
| 507 | listener.onStateChanged(CarPowerStateListener.SUSPEND_EXIT, 0); |
| 508 | } catch (RemoteException e) { |
| 509 | // Its likely the connection snapped. Let binder death handle the situation. |
| 510 | Log.e(CarLog.TAG_POWER, "onStateChanged calling failed: " + e); |
| 511 | } |
| 512 | } |
| 513 | mPowerManagerListeners.finishBroadcast(); |
| 514 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 515 | if (mSystemInterface.isWakeupCausedByTimer()) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 516 | doHandlePreprocessing(false /*shuttingDown*/); |
| 517 | } else { |
| 518 | PowerState currentState = mHal.getCurrentPowerState(); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 519 | if (currentState != null && needPowerStateChange(currentState)) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 520 | onApPowerStateChange(currentState); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 521 | } else { // power controller woke-up but no power state change. Just shutdown. |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 522 | Log.w(CarLog.TAG_POWER, "external sleep wake up, but no power state change:" + |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 523 | currentState); |
| 524 | doHandleShutdown(); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 529 | private void doHandleNotifyPowerOn() { |
| 530 | boolean displayOn = false; |
| 531 | synchronized (this) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 532 | if (mCurrentState != null && mCurrentState.mState == PowerHalService.STATE_ON_FULL) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 533 | displayOn = true; |
| 534 | } |
| 535 | } |
| 536 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 537 | // wrapper will not send it forward if it is already called. |
| 538 | wrapper.callOnPowerOn(displayOn); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | private boolean needPowerStateChange(PowerState newState) { |
| 543 | synchronized (this) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 544 | if (mCurrentState != null && mCurrentState.equals(newState)) { |
| 545 | return false; |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | } |
| 550 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 551 | private void doHandleShutdown() { |
| 552 | // now shutdown |
| 553 | for (PowerServiceEventListener listener : mListeners) { |
| 554 | listener.onShutdown(); |
| 555 | } |
| 556 | int wakeupTimeSec = 0; |
| 557 | if (mHal.isTimedWakeupAllowed()) { |
| 558 | wakeupTimeSec = getWakeupTime(); |
| 559 | } |
| 560 | mHal.sendShutdownStart(wakeupTimeSec); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 561 | mSystemInterface.shutdown(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | private int getWakeupTime() { |
| 565 | int wakeupTimeSec = 0; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 566 | for (PowerEventProcessingHandlerWrapper wrapper : mPowerEventProcessingHandlers) { |
| 567 | int t = wrapper.handler.getWakeupTime(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 568 | if (t > wakeupTimeSec) { |
| 569 | wakeupTimeSec = t; |
| 570 | } |
| 571 | } |
| 572 | return wakeupTimeSec; |
| 573 | } |
| 574 | |
| 575 | private void doHandleProcessingComplete(boolean shutdownWhenCompleted) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 576 | synchronized (this) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 577 | releaseTimerLocked(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 578 | if (!shutdownWhenCompleted && mLastSleepEntryTime > mProcessingStartTime) { |
| 579 | // entered sleep after processing start. So this could be duplicate request. |
| 580 | Log.w(CarLog.TAG_POWER, "Duplicate sleep entry request, ignore"); |
| 581 | return; |
| 582 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 583 | } |
| 584 | if (shutdownWhenCompleted) { |
| 585 | doHandleShutdown(); |
| 586 | } else { |
| 587 | doHandleDeepSleep(); |
| 588 | } |
| 589 | } |
| 590 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 591 | private synchronized void setCurrentState(PowerState state) { |
| 592 | mCurrentState = state; |
| 593 | } |
| 594 | |
| 595 | @Override |
| 596 | public void onDisplayBrightnessChange(int brightness) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 597 | // TODO bug: 32065231 |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | private void doHandleDisplayBrightnessChange(int brightness) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 601 | //TODO bug: 32065231 |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 604 | private void doHandleMainDisplayStateChange(boolean on) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 605 | //TODO bug: 32065231 |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 608 | public void handleMainDisplayChanged(boolean on) { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 609 | PowerHandler handler; |
| 610 | synchronized (this) { |
| 611 | handler = mHandler; |
| 612 | } |
| 613 | handler.handleMainDisplayStateChange(on); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 616 | public synchronized Handler getHandler() { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 617 | return mHandler; |
| 618 | } |
| 619 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 620 | // Binder interface for CarPowerManager |
| 621 | @Override |
| 622 | public void registerListener(ICarPowerStateListener listener) { |
| 623 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 624 | mPowerManagerListeners.register(listener); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void unregisterListener(ICarPowerStateListener listener) { |
| 629 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 630 | doUnregisterListener(listener); |
| 631 | } |
| 632 | |
| 633 | private void doUnregisterListener(ICarPowerStateListener listener) { |
| 634 | boolean found = mPowerManagerListeners.unregister(listener); |
| 635 | |
| 636 | if (found) { |
| 637 | // Remove outstanding token if there is one |
| 638 | IBinder binder = listener.asBinder(); |
| 639 | synchronized (mPowerManagerListenerTokens) { |
| 640 | if (mPowerManagerListenerTokens.containsKey(binder)) { |
| 641 | int token = mPowerManagerListenerTokens.get(binder); |
| 642 | finishedLocked(binder, token); |
| 643 | } |
| 644 | } |
| 645 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public void requestShutdownOnNextSuspend() { |
| 650 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
| 651 | mShutdownOnNextSuspend = true; |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public int getBootReason() { |
| 656 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
| 657 | // Return the most recent bootReason value |
| 658 | return mBootReason; |
| 659 | } |
| 660 | |
| 661 | @Override |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 662 | public void finished(ICarPowerStateListener listener, int token) { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 663 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame^] | 664 | synchronized (mPowerManagerListenerTokens) { |
| 665 | finishedLocked(listener.asBinder(), token); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | private void finishedLocked(IBinder binder, int token) { |
| 670 | int currentToken = mPowerManagerListenerTokens.get(binder); |
| 671 | if (currentToken == token) { |
| 672 | mPowerManagerListenerTokens.remove(binder); |
| 673 | if (mPowerManagerListenerTokens.isEmpty() && |
| 674 | (mCurrentState.mState == PowerHalService.STATE_SHUTDOWN_PREPARE)) { |
| 675 | // All apps are ready to shutdown/suspend. |
| 676 | Log.i(CarLog.TAG_POWER, "Apps are finished, call notifyPowerEventProcessingCompletion"); |
| 677 | notifyPowerEventProcessingCompletion(null); |
| 678 | } |
| 679 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 680 | } |
| 681 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 682 | private class PowerHandler extends Handler { |
| 683 | |
| 684 | private final int MSG_POWER_STATE_CHANGE = 0; |
| 685 | private final int MSG_DISPLAY_BRIGHTNESS_CHANGE = 1; |
| 686 | private final int MSG_MAIN_DISPLAY_STATE_CHANGE = 2; |
| 687 | private final int MSG_PROCESSING_COMPLETE = 3; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 688 | private final int MSG_NOTIFY_POWER_ON = 4; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 689 | |
| 690 | // Do not handle this immediately but with some delay as there can be a race between |
| 691 | // display off due to rear view camera and delivery to here. |
| 692 | private final long MAIN_DISPLAY_EVENT_DELAY_MS = 500; |
| 693 | |
| 694 | private PowerHandler(Looper looper) { |
| 695 | super(looper); |
| 696 | } |
| 697 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 698 | private void handlePowerStateChange() { |
| 699 | Message msg = obtainMessage(MSG_POWER_STATE_CHANGE); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 700 | sendMessage(msg); |
| 701 | } |
| 702 | |
| 703 | private void handleDisplayBrightnessChange(int brightness) { |
| 704 | Message msg = obtainMessage(MSG_DISPLAY_BRIGHTNESS_CHANGE, brightness, 0); |
| 705 | sendMessage(msg); |
| 706 | } |
| 707 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 708 | private void handleMainDisplayStateChange(boolean on) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 709 | removeMessages(MSG_MAIN_DISPLAY_STATE_CHANGE); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 710 | Message msg = obtainMessage(MSG_MAIN_DISPLAY_STATE_CHANGE, Boolean.valueOf(on)); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 711 | sendMessageDelayed(msg, MAIN_DISPLAY_EVENT_DELAY_MS); |
| 712 | } |
| 713 | |
| 714 | private void handleProcessingComplete(boolean shutdownWhenCompleted) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 715 | removeMessages(MSG_PROCESSING_COMPLETE); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 716 | Message msg = obtainMessage(MSG_PROCESSING_COMPLETE, shutdownWhenCompleted ? 1 : 0, 0); |
| 717 | sendMessage(msg); |
| 718 | } |
| 719 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 720 | private void handlePowerOn() { |
| 721 | Message msg = obtainMessage(MSG_NOTIFY_POWER_ON); |
| 722 | sendMessage(msg); |
| 723 | } |
| 724 | |
| 725 | private void cancelProcessingComplete() { |
| 726 | removeMessages(MSG_PROCESSING_COMPLETE); |
| 727 | } |
| 728 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 729 | private void cancelAll() { |
| 730 | removeMessages(MSG_POWER_STATE_CHANGE); |
| 731 | removeMessages(MSG_DISPLAY_BRIGHTNESS_CHANGE); |
| 732 | removeMessages(MSG_MAIN_DISPLAY_STATE_CHANGE); |
| 733 | removeMessages(MSG_PROCESSING_COMPLETE); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 734 | removeMessages(MSG_NOTIFY_POWER_ON); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | @Override |
| 738 | public void handleMessage(Message msg) { |
| 739 | switch (msg.what) { |
| 740 | case MSG_POWER_STATE_CHANGE: |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 741 | doHandlePowerStateChange(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 742 | break; |
| 743 | case MSG_DISPLAY_BRIGHTNESS_CHANGE: |
| 744 | doHandleDisplayBrightnessChange(msg.arg1); |
| 745 | break; |
| 746 | case MSG_MAIN_DISPLAY_STATE_CHANGE: |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 747 | doHandleMainDisplayStateChange((Boolean) msg.obj); |
Keun-young Park | 1f4d6a7 | 2016-02-11 09:49:47 -0800 | [diff] [blame] | 748 | break; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 749 | case MSG_PROCESSING_COMPLETE: |
| 750 | doHandleProcessingComplete(msg.arg1 == 1); |
| 751 | break; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 752 | case MSG_NOTIFY_POWER_ON: |
| 753 | doHandleNotifyPowerOn(); |
| 754 | break; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 759 | private class ShutdownProcessingTimerTask extends TimerTask { |
| 760 | private final boolean mShutdownWhenCompleted; |
| 761 | private final int mExpirationCount; |
| 762 | private int mCurrentCount; |
| 763 | |
| 764 | private ShutdownProcessingTimerTask(boolean shutdownWhenCompleted, int expirationCount) { |
| 765 | mShutdownWhenCompleted = shutdownWhenCompleted; |
| 766 | mExpirationCount = expirationCount; |
| 767 | mCurrentCount = 0; |
| 768 | } |
| 769 | |
| 770 | @Override |
| 771 | public void run() { |
| 772 | mCurrentCount++; |
| 773 | if (mCurrentCount > mExpirationCount) { |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 774 | PowerHandler handler; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 775 | synchronized (CarPowerManagementService.this) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 776 | releaseTimerLocked(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 777 | handler = mHandler; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 778 | } |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 779 | handler.handleProcessingComplete(mShutdownWhenCompleted); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 780 | } else { |
| 781 | mHal.sendShutdownPostpone(SHUTDOWN_EXTEND_MAX_MS); |
| 782 | } |
| 783 | } |
| 784 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 785 | |
| 786 | private static class PowerEventProcessingHandlerWrapper { |
| 787 | public final PowerEventProcessingHandler handler; |
| 788 | private long mProcessingTime = 0; |
| 789 | private boolean mProcessingDone = true; |
| 790 | private boolean mPowerOnSent = false; |
Keun-young Park | d73afae | 2016-04-08 20:03:32 -0700 | [diff] [blame] | 791 | private int mLastDisplayState = -1; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 792 | |
| 793 | public PowerEventProcessingHandlerWrapper(PowerEventProcessingHandler handler) { |
| 794 | this.handler = handler; |
| 795 | } |
| 796 | |
| 797 | public synchronized void setProcessingTimeAndResetProcessingDone(long processingTime) { |
| 798 | mProcessingTime = processingTime; |
| 799 | mProcessingDone = false; |
| 800 | } |
| 801 | |
| 802 | public synchronized long getProcessingTime() { |
| 803 | return mProcessingTime; |
| 804 | } |
| 805 | |
| 806 | public synchronized void markProcessingDone() { |
| 807 | mProcessingDone = true; |
| 808 | } |
| 809 | |
| 810 | public synchronized boolean isProcessingDone() { |
| 811 | return mProcessingDone; |
| 812 | } |
| 813 | |
| 814 | public void callOnPowerOn(boolean displayOn) { |
Keun-young Park | d73afae | 2016-04-08 20:03:32 -0700 | [diff] [blame] | 815 | int newDisplayState = displayOn ? 1 : 0; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 816 | boolean shouldCall = false; |
| 817 | synchronized (this) { |
Keun-young Park | d73afae | 2016-04-08 20:03:32 -0700 | [diff] [blame] | 818 | if (!mPowerOnSent || (mLastDisplayState != newDisplayState)) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 819 | shouldCall = true; |
| 820 | mPowerOnSent = true; |
Keun-young Park | d73afae | 2016-04-08 20:03:32 -0700 | [diff] [blame] | 821 | mLastDisplayState = newDisplayState; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | if (shouldCall) { |
| 825 | handler.onPowerOn(displayOn); |
| 826 | } |
| 827 | } |
| 828 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 829 | @Override |
| 830 | public String toString() { |
| 831 | return "PowerEventProcessingHandlerWrapper [handler=" + handler + ", mProcessingTime=" |
| 832 | + mProcessingTime + ", mProcessingDone=" + mProcessingDone + "]"; |
| 833 | } |
| 834 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 835 | } |