Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 android.car.hardware.power; |
| 18 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
| 20 | import android.car.Car; |
| 21 | import android.car.CarManagerBase; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.os.Handler; |
| 24 | import android.os.IBinder; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.util.Log; |
| 27 | |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
| 29 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 30 | import java.util.concurrent.CancellationException; |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 31 | import java.util.concurrent.CompletableFuture; |
| 32 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 33 | /** |
| 34 | * API for receiving power state change notifications. |
| 35 | * @hide |
| 36 | */ |
| 37 | @SystemApi |
| 38 | public class CarPowerManager implements CarManagerBase { |
| 39 | private final static boolean DBG = false; |
| 40 | private final static String TAG = "CarPowerManager"; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 41 | |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 42 | private final Object mLock = new Object(); |
| 43 | private final ICarPower mService; |
| 44 | |
| 45 | private CarPowerStateListener mListener; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 46 | private CarPowerStateListenerWithCompletion mListenerWithCompletion; |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 47 | private CompletableFuture<Void> mFuture; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 48 | @GuardedBy("mLock") |
| 49 | private ICarPowerStateListener mListenerToService; |
| 50 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 53 | * Applications set a {@link CarPowerStateListener} for power state event updates. |
| 54 | */ |
| 55 | public interface CarPowerStateListener { |
| 56 | /** |
Steve Paik | fe90022 | 2018-03-01 18:07:38 -0800 | [diff] [blame] | 57 | * onStateChanged() states. These definitions must match the ones located in the native |
| 58 | * CarPowerManager: packages/services/Car/car-lib/native/CarPowerManager/CarPowerManager.h |
Steve Paik | fe90022 | 2018-03-01 18:07:38 -0800 | [diff] [blame] | 59 | */ |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 62 | * Android is up, but vendor is controlling the audio / display |
| 63 | * @hide |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 64 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 65 | int WAIT_FOR_VHAL = 1; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 66 | /** |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 67 | * Enter suspend state. CPMS is switching to WAIT_FOR_FINISHED state. |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 68 | * @hide |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 69 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 70 | int SUSPEND_ENTER = 2; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 71 | /** |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 72 | * Wake up from suspend. |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 73 | * @hide |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 74 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 75 | int SUSPEND_EXIT = 3; |
| 76 | /** |
| 77 | * Enter shutdown state. CPMS is switching to WAIT_FOR_FINISHED state. |
| 78 | * @hide |
| 79 | */ |
| 80 | int SHUTDOWN_ENTER = 5; |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 81 | /** |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 82 | * On state |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 83 | * @hide |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 84 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 85 | int ON = 6; |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 86 | /** |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 87 | * State where system is getting ready for shutdown or suspend. Application is expected to |
| 88 | * cleanup and be ready to suspend |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 89 | * @hide |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 90 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 91 | int SHUTDOWN_PREPARE = 7; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 92 | /** |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 93 | * Shutdown is cancelled, return to normal state. |
| 94 | * @hide |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 95 | */ |
Serik Beketayev | 92d952c | 2019-02-08 11:07:45 -0800 | [diff] [blame] | 96 | int SHUTDOWN_CANCELLED = 8; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 99 | * Called when power state changes. This callback is available to |
| 100 | * any listener, even if it is not running in the system process. |
| 101 | * @param state New power state of device. |
| 102 | * @hide |
| 103 | */ |
| 104 | void onStateChanged(int state); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Applications set a {@link CarPowerStateListenerWithCompletion} for power state |
| 109 | * event updates where a CompletableFuture is used. |
| 110 | * @hide |
| 111 | */ |
| 112 | public interface CarPowerStateListenerWithCompletion { |
| 113 | /** |
| 114 | * Called when power state changes. This callback is only for listeners |
| 115 | * that are running in the system process. |
| 116 | * @param state New power state of device. |
| 117 | * @param future CompletableFuture used by Car modules to notify CPMS that they |
| 118 | * are ready to continue shutting down. CPMS will wait until this |
| 119 | * future is completed. |
Serik Beketayev | 8a1d236 | 2018-10-26 09:08:09 -0700 | [diff] [blame] | 120 | * @hide |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 121 | */ |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 122 | void onStateChanged(int state, CompletableFuture<Void> future); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get an instance of the CarPowerManager. |
| 127 | * |
| 128 | * Should not be obtained directly by clients, use {@link Car#getCarManager(String)} instead. |
| 129 | * @param service |
| 130 | * @param context |
| 131 | * @param handler |
| 132 | * @hide |
| 133 | */ |
| 134 | public CarPowerManager(IBinder service, Context context, Handler handler) { |
| 135 | mService = ICarPower.Stub.asInterface(service); |
| 136 | } |
| 137 | |
| 138 | /** |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 139 | * Request power manager to shutdown in lieu of suspend at the next opportunity. |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 140 | * @hide |
| 141 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 142 | public void requestShutdownOnNextSuspend() { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 143 | try { |
| 144 | mService.requestShutdownOnNextSuspend(); |
| 145 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 146 | throw e.rethrowFromSystemServer(); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 151 | * Schedule next wake up time in CarPowerManagementSystem |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 152 | * @hide |
| 153 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 154 | public void scheduleNextWakeupTime(int seconds) { |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 155 | try { |
| 156 | mService.scheduleNextWakeupTime(seconds); |
| 157 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 158 | throw e.rethrowFromSystemServer(); |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 163 | * Sets a listener to receive power state changes. Only one listener may be set at a |
| 164 | * time for an instance of CarPowerManager. |
| 165 | * The listener is assumed to completely handle the 'onStateChanged' before returning. |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 166 | * |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 167 | * @param listener |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 168 | * @throws IllegalStateException |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 169 | * @hide |
| 170 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 171 | public void setListener(CarPowerStateListener listener) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 172 | synchronized (mLock) { |
| 173 | if (mListener != null || mListenerWithCompletion != null) { |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 174 | throw new IllegalStateException("Listener must be cleared first"); |
| 175 | } |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 176 | // Update listener |
| 177 | mListener = listener; |
| 178 | setServiceForListenerLocked(false); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets a listener to receive power state changes. Only one listener may be set at a |
| 184 | * time for an instance of CarPowerManager. |
| 185 | * For calls that require completion before continue, we attach a {@link CompletableFuture} |
| 186 | * which is being used as a signal that caller is finished and ready to proceed. |
| 187 | * Once future is completed, the {@link finished} method will automatically be called to notify |
| 188 | * {@link CarPowerManagementService} that the application has handled the |
| 189 | * {@link #SHUTDOWN_PREPARE} state transition. |
| 190 | * |
| 191 | * @param listener |
| 192 | * @throws IllegalStateException |
| 193 | * @hide |
| 194 | */ |
| 195 | public void setListenerWithCompletion(CarPowerStateListenerWithCompletion listener) { |
| 196 | synchronized(mLock) { |
| 197 | if (mListener != null || mListenerWithCompletion != null) { |
| 198 | throw new IllegalStateException("Listener must be cleared first"); |
| 199 | } |
| 200 | // Update listener |
| 201 | mListenerWithCompletion = listener; |
| 202 | setServiceForListenerLocked(true); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private void setServiceForListenerLocked(boolean useCompletion) { |
| 207 | if (mListenerToService == null) { |
| 208 | ICarPowerStateListener listenerToService = new ICarPowerStateListener.Stub() { |
| 209 | @Override |
| 210 | public void onStateChanged(int state) throws RemoteException { |
| 211 | if (useCompletion) { |
| 212 | // Update CompletableFuture. This will recreate it or just clean it up. |
| 213 | updateFuture(state); |
| 214 | // Notify user that the state has changed and supply a future |
| 215 | mListenerWithCompletion.onStateChanged(state, mFuture); |
| 216 | } else { |
| 217 | // Notify the user without supplying a future |
| 218 | mListener.onStateChanged(state); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 219 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 220 | } |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 221 | }; |
| 222 | try { |
| 223 | if (useCompletion) { |
| 224 | mService.registerListenerWithCompletion(listenerToService); |
| 225 | } else { |
| 226 | mService.registerListener(listenerToService); |
| 227 | } |
| 228 | mListenerToService = listenerToService; |
| 229 | } catch (RemoteException e) { |
| 230 | throw e.rethrowFromSystemServer(); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 231 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Removes the listener from {@link CarPowerManagementService} |
| 237 | * @hide |
| 238 | */ |
| 239 | public void clearListener() { |
| 240 | ICarPowerStateListener listenerToService; |
| 241 | synchronized (mLock) { |
| 242 | listenerToService = mListenerToService; |
| 243 | mListenerToService = null; |
| 244 | mListener = null; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 245 | mListenerWithCompletion = null; |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 246 | cleanupFuture(); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if (listenerToService == null) { |
| 250 | Log.w(TAG, "unregisterListener: listener was not registered"); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | try { |
| 255 | mService.unregisterListener(listenerToService); |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 256 | } catch (RemoteException e) { |
| 257 | throw e.rethrowFromSystemServer(); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 261 | private void updateFuture(int state) { |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 262 | cleanupFuture(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 263 | if (state == CarPowerStateListener.SHUTDOWN_PREPARE) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 264 | // Create a CompletableFuture and pass it to the listener. |
| 265 | // When the listener completes the future, tell |
| 266 | // CarPowerManagementService that this action is finished. |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 267 | mFuture = new CompletableFuture<>(); |
| 268 | mFuture.whenComplete((result, exception) -> { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 269 | if (exception != null && !(exception instanceof CancellationException)) { |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 270 | Log.e(TAG, "Exception occurred while waiting for future", exception); |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 271 | } |
| 272 | try { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 273 | mService.finished(mListenerToService); |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 274 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 275 | throw e.rethrowFromSystemServer(); |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 276 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 277 | }); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 281 | private void cleanupFuture() { |
| 282 | if (mFuture != null) { |
| 283 | if (!mFuture.isDone()) { |
| 284 | mFuture.cancel(false); |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 285 | } |
Serik Beketayev | b64dc14 | 2018-08-23 13:59:36 -0700 | [diff] [blame] | 286 | mFuture = null; |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | /** @hide */ |
| 291 | @Override |
| 292 | public void onCarDisconnected() { |
| 293 | ICarPowerStateListener listenerToService; |
| 294 | synchronized (mLock) { |
| 295 | listenerToService = mListenerToService; |
| 296 | } |
| 297 | |
| 298 | if (listenerToService != null) { |
| 299 | clearListener(); |
| 300 | } |
| 301 | } |
| 302 | } |