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