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 | */ |
Eric Jeong | c1f53b2 | 2021-02-18 00:40:36 -0800 | [diff] [blame] | 16 | |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 17 | package com.android.car.power; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 18 | |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 22 | import android.car.Car; |
| 23 | import android.car.hardware.power.CarPowerManager.CarPowerStateListener; |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 24 | import android.car.hardware.power.CarPowerPolicy; |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 25 | import android.car.hardware.power.CarPowerPolicyFilter; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 26 | import android.car.hardware.power.ICarPower; |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 27 | import android.car.hardware.power.ICarPowerPolicyListener; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 28 | import android.car.hardware.power.ICarPowerStateListener; |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 29 | import android.content.ComponentName; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 32 | import android.content.pm.UserInfo; |
Felipe Leme | 848eb65 | 2019-12-18 09:53:18 -0800 | [diff] [blame] | 33 | import android.content.res.Resources; |
Eric Jeong | 0d3bb4f | 2020-11-06 11:31:45 -0800 | [diff] [blame] | 34 | import android.frameworks.automotive.powerpolicy.internal.ICarPowerPolicySystemNotification; |
| 35 | import android.frameworks.automotive.powerpolicy.internal.PolicyState; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 36 | import android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateReport; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 37 | import android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateReq; |
Eric Jeong | e701e0d | 2020-12-16 17:54:00 -0800 | [diff] [blame] | 38 | import android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateShutdownParam; |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 39 | import android.net.wifi.WifiManager; |
Keun young Park | 8c152cb | 2019-05-09 18:04:31 -0700 | [diff] [blame] | 40 | import android.os.Build; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 41 | import android.os.Handler; |
| 42 | import android.os.HandlerThread; |
| 43 | import android.os.IBinder; |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 44 | import android.os.IInterface; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 45 | import android.os.Looper; |
| 46 | import android.os.Message; |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 47 | import android.os.PowerManager; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 48 | import android.os.RemoteCallbackList; |
| 49 | import android.os.RemoteException; |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 50 | import android.os.ServiceManager; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 51 | import android.os.SystemClock; |
Keun young Park | 8c152cb | 2019-05-09 18:04:31 -0700 | [diff] [blame] | 52 | import android.os.SystemProperties; |
Keun young Park | 9b3f266 | 2019-03-19 10:30:25 -0700 | [diff] [blame] | 53 | import android.os.UserHandle; |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 54 | import android.os.UserManager; |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 55 | import android.util.AtomicFile; |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 56 | import android.util.IndentingPrintWriter; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 57 | import android.util.SparseArray; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 58 | |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 59 | import com.android.car.CarLocalServices; |
| 60 | import com.android.car.CarLog; |
| 61 | import com.android.car.CarServiceBase; |
| 62 | import com.android.car.CarServiceUtils; |
| 63 | import com.android.car.CarStatsLogHelper; |
| 64 | import com.android.car.ICarImpl; |
| 65 | import com.android.car.R; |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 66 | import com.android.car.am.ContinuousBlankActivity; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 67 | import com.android.car.hal.PowerHalService; |
| 68 | import com.android.car.hal.PowerHalService.PowerState; |
Enrico Granata | b19bc32 | 2017-10-12 12:25:06 -0700 | [diff] [blame] | 69 | import com.android.car.systeminterface.SystemInterface; |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 70 | import com.android.car.user.CarUserNoticeService; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 71 | import com.android.car.user.CarUserService; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 72 | import com.android.internal.annotations.GuardedBy; |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 73 | import com.android.internal.annotations.VisibleForTesting; |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 74 | import com.android.internal.os.IResultReceiver; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 75 | import com.android.internal.util.Preconditions; |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 76 | import com.android.internal.util.function.pooled.PooledLambda; |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 77 | import com.android.server.utils.Slogf; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 78 | |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 79 | import java.io.BufferedReader; |
| 80 | import java.io.BufferedWriter; |
| 81 | import java.io.File; |
| 82 | import java.io.FileOutputStream; |
| 83 | import java.io.IOException; |
| 84 | import java.io.InputStreamReader; |
| 85 | import java.io.OutputStreamWriter; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 86 | import java.lang.ref.WeakReference; |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 87 | import java.nio.charset.StandardCharsets; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 88 | import java.util.HashSet; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 89 | import java.util.LinkedList; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 90 | import java.util.Set; |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 91 | import java.util.Timer; |
| 92 | import java.util.TimerTask; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 93 | |
Serik Beketayev | 1d9e0d8 | 2018-09-11 08:19:33 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Power Management service class for cars. Controls the power states and interacts with other |
| 96 | * parts of the system to ensure its own state. |
| 97 | */ |
| 98 | public class CarPowerManagementService extends ICarPower.Stub implements |
| 99 | CarServiceBase, PowerHalService.PowerEventListener { |
Eric Jeong | c1f53b2 | 2021-02-18 00:40:36 -0800 | [diff] [blame] | 100 | public static final String SILENT_MODE_FORCED_SILENT = |
| 101 | SilentModeHandler.SILENT_MODE_FORCED_SILENT; |
| 102 | public static final String SILENT_MODE_FORCED_NON_SILENT = |
| 103 | SilentModeHandler.SILENT_MODE_FORCED_NON_SILENT; |
| 104 | public static final String SILENT_MODE_NON_FORCED = SilentModeHandler.SILENT_MODE_NON_FORCED; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 105 | |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 106 | private static final String TAG = CarLog.tagFor(CarPowerManagementService.class); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 107 | private static final String WIFI_STATE_FILENAME = "wifi_state"; |
| 108 | private static final String WIFI_STATE_MODIFIED = "forcibly_disabled"; |
| 109 | private static final String WIFI_STATE_ORIGINAL = "original"; |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 110 | // If Suspend to RAM fails, we retry with an exponential back-off: |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 111 | // The wait interval will be 10 msec, 20 msec, 40 msec, ... |
Eric Jeong | ed0b712 | 2021-03-11 17:44:28 -0800 | [diff] [blame] | 112 | // Once the wait interval goes beyond 100 msec, it is fixed at 100 msec. |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 113 | private static final long INITIAL_SUSPEND_RETRY_INTERVAL_MS = 10; |
Eric Jeong | ed0b712 | 2021-03-11 17:44:28 -0800 | [diff] [blame] | 114 | private static final long MAX_RETRY_INTERVAL_MS = 100; |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 115 | // Minimum and maximum wait duration before the system goes into Suspend to RAM. |
| 116 | private static final long MIN_SUSPEND_WAIT_DURATION_MS = 0; |
| 117 | private static final long MAX_SUSPEND_WAIT_DURATION_MS = 3 * 60 * 1000; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 118 | |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 119 | private static final long CAR_POWER_POLICY_DAEMON_FIND_MARGINAL_TIME_MS = 300; |
| 120 | private static final long CAR_POWER_POLICY_DAEMON_BIND_RETRY_INTERVAL_MS = 500; |
| 121 | private static final int CAR_POWER_POLICY_DAEMON_BIND_MAX_RETRY = 3; |
| 122 | private static final String CAR_POWER_POLICY_DAEMON_INTERFACE = |
| 123 | "carpowerpolicy_system_notification"; |
| 124 | |
| 125 | // TODO: Make this OEM configurable. |
| 126 | private static final int SHUTDOWN_POLLING_INTERVAL_MS = 2000; |
| 127 | private static final int SHUTDOWN_EXTEND_MAX_MS = 5000; |
| 128 | |
| 129 | // maxGarageModeRunningDurationInSecs should be equal or greater than this. 15 min for now. |
| 130 | private static final int MIN_MAX_GARAGE_MODE_DURATION_MS = 15 * 60 * 1000; |
| 131 | |
| 132 | // in secs |
| 133 | private static final String PROP_MAX_GARAGE_MODE_DURATION_OVERRIDE = |
| 134 | "android.car.garagemodeduration"; |
| 135 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 136 | private final Object mLock = new Object(); |
| 137 | private final Object mSimulationWaitObject = new Object(); |
| 138 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 139 | private final Context mContext; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 140 | private final PowerHalService mHal; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 141 | private final SystemInterface mSystemInterface; |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 142 | // The listeners that complete simply by returning from onStateChanged() |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 143 | private final PowerManagerCallbackList<ICarPowerStateListener> mPowerManagerListeners = |
| 144 | new PowerManagerCallbackList<>( |
| 145 | l -> CarPowerManagementService.this.doUnregisterListener(l)); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 146 | // The listeners that must indicate asynchronous completion by calling finished(). |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 147 | private final PowerManagerCallbackList<ICarPowerStateListener> |
| 148 | mPowerManagerListenersWithCompletion = new PowerManagerCallbackList<>( |
| 149 | l -> CarPowerManagementService.this.doUnregisterListener(l)); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 150 | |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 151 | @GuardedBy("mLock") |
| 152 | private final Set<IBinder> mListenersWeAreWaitingFor = new HashSet<>(); |
| 153 | @GuardedBy("mLock") |
| 154 | private final LinkedList<CpmsState> mPendingPowerStates = new LinkedList<>(); |
| 155 | private final HandlerThread mHandlerThread = CarServiceUtils.getHandlerThread( |
| 156 | getClass().getSimpleName()); |
| 157 | private final PowerHandler mHandler = new PowerHandler(mHandlerThread.getLooper(), this); |
| 158 | |
| 159 | private final UserManager mUserManager; |
| 160 | private final CarUserService mUserService; |
| 161 | |
| 162 | private final WifiManager mWifiManager; |
| 163 | private final AtomicFile mWifiStateFile; |
| 164 | |
| 165 | // This is a temp work-around to reduce user switching delay after wake-up. |
| 166 | private final boolean mSwitchGuestUserBeforeSleep; |
| 167 | |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 168 | // CPMS tries to enter Suspend to RAM within the duration specified at |
| 169 | // mMaxSuspendWaitDurationMs. The default max duration is MAX_SUSPEND_WAIT_DRATION, and can be |
| 170 | // overridden by setting config_maxSuspendWaitDuration in an overrlay resource. |
| 171 | // The valid range is MIN_SUSPEND_WAIT_DRATION to MAX_SUSPEND_WAIT_DURATION. |
| 172 | private final long mMaxSuspendWaitDurationMs; |
| 173 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 174 | @GuardedBy("mSimulationWaitObject") |
| 175 | private boolean mWakeFromSimulatedSleep; |
| 176 | @GuardedBy("mSimulationWaitObject") |
| 177 | private boolean mInSimulatedDeepSleepMode; |
| 178 | |
| 179 | @GuardedBy("mLock") |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 180 | private CpmsState mCurrentState; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 181 | @GuardedBy("mLock") |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 182 | private Timer mTimer; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 183 | @GuardedBy("mLock") |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 184 | private long mProcessingStartTime; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 185 | @GuardedBy("mLock") |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 186 | private long mLastSleepEntryTime; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 187 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 188 | @GuardedBy("mLock") |
Serik Beketayev | f8287a0 | 2019-02-08 12:31:10 -0800 | [diff] [blame] | 189 | private boolean mTimerActive; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 190 | @GuardedBy("mLock") |
| 191 | private int mNextWakeupSec; |
| 192 | @GuardedBy("mLock") |
| 193 | private boolean mShutdownOnFinish; |
| 194 | @GuardedBy("mLock") |
Jim Kaye | dab62d8 | 2019-12-18 14:52:19 -0800 | [diff] [blame] | 195 | private boolean mShutdownOnNextSuspend; |
| 196 | @GuardedBy("mLock") |
Keun young Park | 9b3f266 | 2019-03-19 10:30:25 -0700 | [diff] [blame] | 197 | private boolean mIsBooting = true; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 198 | @GuardedBy("mLock") |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 199 | private int mShutdownPrepareTimeMs = MIN_MAX_GARAGE_MODE_DURATION_MS; |
| 200 | @GuardedBy("mLock") |
| 201 | private int mShutdownPollingIntervalMs = SHUTDOWN_POLLING_INTERVAL_MS; |
| 202 | @GuardedBy("mLock") |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 203 | private boolean mRebootAfterGarageMode; |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 204 | @GuardedBy("mLock") |
| 205 | private boolean mGarageModeShouldExitImmediately; |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 206 | |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 207 | @GuardedBy("mLock") |
| 208 | private ICarPowerPolicySystemNotification mCarPowerPolicyDaemon; |
| 209 | @GuardedBy("mLock") |
| 210 | private boolean mConnectionInProgress; |
| 211 | private BinderHandler mBinderHandler; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 212 | @GuardedBy("mLock") |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 213 | private String mCurrentPowerPolicyId; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 214 | @GuardedBy("mLock") |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 215 | private String mPendingPowerPolicyId; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 216 | @GuardedBy("mLock") |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 217 | private String mCurrentPowerPolicyGroupId; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 218 | @GuardedBy("mLock") |
| 219 | private boolean mIsPowerPolicyLocked; |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 220 | @GuardedBy("mLock") |
| 221 | private boolean mHasControlOverDaemon; |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 222 | |
| 223 | @GuardedBy("mLock") |
| 224 | @Nullable |
| 225 | private IResultReceiver mFactoryResetCallback; |
| 226 | |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 227 | private final PowerManagerCallbackList<ICarPowerPolicyListener> mPowerPolicyListeners = |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 228 | new PowerManagerCallbackList<>( |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 229 | l -> CarPowerManagementService.this.mPowerPolicyListeners.unregister(l)); |
Keun young Park | 62de552 | 2020-03-10 10:47:09 -0700 | [diff] [blame] | 230 | |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 231 | private final PowerComponentHandler mPowerComponentHandler; |
Eric Jeong | 91969f2 | 2020-10-05 17:47:41 -0700 | [diff] [blame] | 232 | private final PolicyReader mPolicyReader = new PolicyReader(); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 233 | private final SilentModeHandler mSilentModeHandler; |
Eric Jeong | 91969f2 | 2020-10-05 17:47:41 -0700 | [diff] [blame] | 234 | |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 235 | interface ActionOnDeath<T extends IInterface> { |
| 236 | void take(T listener); |
| 237 | } |
| 238 | |
| 239 | private final class PowerManagerCallbackList<T extends IInterface> extends |
| 240 | RemoteCallbackList<T> { |
| 241 | private ActionOnDeath<T> mActionOnDeath; |
| 242 | |
| 243 | PowerManagerCallbackList(ActionOnDeath<T> action) { |
| 244 | mActionOnDeath = action; |
| 245 | } |
| 246 | |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Old version of {@link #onCallbackDied(E, Object)} that |
| 249 | * does not provide a cookie. |
| 250 | */ |
| 251 | @Override |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 252 | public void onCallbackDied(T listener) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 253 | Slogf.i(TAG, "binderDied %s", listener.asBinder()); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 254 | mActionOnDeath.take(listener); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Felipe Leme | 848eb65 | 2019-12-18 09:53:18 -0800 | [diff] [blame] | 258 | public CarPowerManagementService(Context context, PowerHalService powerHal, |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 259 | SystemInterface systemInterface, CarUserService carUserService, |
| 260 | ICarPowerPolicySystemNotification powerPolicyDaemon) { |
felipeal | 283b58f | 2020-04-01 12:49:05 -0700 | [diff] [blame] | 261 | this(context, context.getResources(), powerHal, systemInterface, UserManager.get(context), |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 262 | carUserService, powerPolicyDaemon, |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 263 | new PowerComponentHandler(context, systemInterface), |
| 264 | /* silentModeHwStatePath= */ null, /* silentModeKernelStatePath= */ null, |
| 265 | /* bootReason= */ null); |
Felipe Leme | 848eb65 | 2019-12-18 09:53:18 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @VisibleForTesting |
Jim Kaye | 18624bf | 2020-05-01 16:14:12 -0700 | [diff] [blame] | 269 | public CarPowerManagementService(Context context, Resources resources, PowerHalService powerHal, |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 270 | SystemInterface systemInterface, UserManager userManager, CarUserService carUserService, |
| 271 | ICarPowerPolicySystemNotification powerPolicyDaemon, |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 272 | PowerComponentHandler powerComponentHandler, @Nullable String silentModeHwStatePath, |
| 273 | @Nullable String silentModeKernelStatePath, @Nullable String bootReason) { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 274 | mContext = context; |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 275 | mHal = powerHal; |
| 276 | mSystemInterface = systemInterface; |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 277 | mUserManager = userManager; |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 278 | mShutdownPrepareTimeMs = resources.getInteger( |
Keun young Park | 4b4ddda | 2019-03-26 18:47:37 -0700 | [diff] [blame] | 279 | R.integer.maxGarageModeRunningDurationInSecs) * 1000; |
Keun young Park | 62de552 | 2020-03-10 10:47:09 -0700 | [diff] [blame] | 280 | mSwitchGuestUserBeforeSleep = resources.getBoolean( |
| 281 | R.bool.config_switchGuestUserBeforeGoingSleep); |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 282 | if (mShutdownPrepareTimeMs < MIN_MAX_GARAGE_MODE_DURATION_MS) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 283 | Slogf.w(TAG, |
| 284 | "maxGarageModeRunningDurationInSecs smaller than minimum required, " |
| 285 | + "resource:%d(ms) while should exceed:%d(ms), Ignore resource.", |
| 286 | mShutdownPrepareTimeMs, MIN_MAX_GARAGE_MODE_DURATION_MS); |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 287 | mShutdownPrepareTimeMs = MIN_MAX_GARAGE_MODE_DURATION_MS; |
Keun young Park | 4b4ddda | 2019-03-26 18:47:37 -0700 | [diff] [blame] | 288 | } |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 289 | mUserService = carUserService; |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 290 | mCarPowerPolicyDaemon = powerPolicyDaemon; |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 291 | if (powerPolicyDaemon != null) { |
| 292 | // For testing purpose |
| 293 | mHasControlOverDaemon = true; |
| 294 | } |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 295 | mWifiManager = context.getSystemService(WifiManager.class); |
| 296 | mWifiStateFile = new AtomicFile( |
| 297 | new File(mSystemInterface.getSystemCarDir(), WIFI_STATE_FILENAME)); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 298 | mPowerComponentHandler = powerComponentHandler; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 299 | mSilentModeHandler = new SilentModeHandler(this, silentModeHwStatePath, |
| 300 | silentModeKernelStatePath, bootReason); |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 301 | mMaxSuspendWaitDurationMs = Math.max(MIN_SUSPEND_WAIT_DURATION_MS, |
| 302 | Math.min(getMaxSuspendWaitDurationConfig(), MAX_SUSPEND_WAIT_DURATION_MS)); |
Yao Chen | dacd724 | 2016-01-26 14:42:42 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 305 | /** |
| 306 | * Overrides timers to keep testing time short. |
| 307 | * |
| 308 | * <p>Passing in {@code 0} resets the value to the default. |
| 309 | */ |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 310 | @VisibleForTesting |
Jim Kaye | 18624bf | 2020-05-01 16:14:12 -0700 | [diff] [blame] | 311 | public void setShutdownTimersForTest(int pollingIntervalMs, int shutdownTimeoutMs) { |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 312 | synchronized (mLock) { |
| 313 | mShutdownPollingIntervalMs = |
| 314 | (pollingIntervalMs == 0) ? SHUTDOWN_POLLING_INTERVAL_MS : pollingIntervalMs; |
| 315 | mShutdownPrepareTimeMs = |
| 316 | (shutdownTimeoutMs == 0) ? SHUTDOWN_EXTEND_MAX_MS : shutdownTimeoutMs; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Keun young Park | 57fd08e | 2019-12-13 09:24:59 -0800 | [diff] [blame] | 320 | @VisibleForTesting |
| 321 | protected HandlerThread getHandlerThread() { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 322 | return mHandlerThread; |
Keun young Park | 57fd08e | 2019-12-13 09:24:59 -0800 | [diff] [blame] | 323 | } |
| 324 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public void init() { |
Eric Jeong | 91969f2 | 2020-10-05 17:47:41 -0700 | [diff] [blame] | 327 | mPolicyReader.init(); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 328 | mPowerComponentHandler.init(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 329 | mHal.setListener(this); |
| 330 | if (mHal.isPowerStateSupported()) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 331 | // Initialize CPMS in WAIT_FOR_VHAL state |
| 332 | onApPowerStateChange(CpmsState.WAIT_FOR_VHAL, CarPowerStateListener.WAIT_FOR_VHAL); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 333 | } else { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 334 | Slogf.w(TAG, "Vehicle hal does not support power state yet."); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 335 | onApPowerStateChange(CpmsState.ON, CarPowerStateListener.ON); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 336 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 337 | mSystemInterface.startDisplayStateMonitoring(this); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 338 | connectToPowerPolicyDaemon(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void release() { |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 343 | if (mBinderHandler != null) { |
| 344 | mBinderHandler.unlinkToDeath(); |
| 345 | } |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 346 | synchronized (mLock) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 347 | releaseTimerLocked(); |
| 348 | mCurrentState = null; |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 349 | mCarPowerPolicyDaemon = null; |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 350 | mHandler.cancelAll(); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 351 | mListenersWeAreWaitingFor.clear(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 352 | } |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 353 | mSystemInterface.stopDisplayStateMonitoring(); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 354 | mPowerManagerListeners.kill(); |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 355 | mPowerPolicyListeners.kill(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 356 | mSystemInterface.releaseAllWakeLocks(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 357 | } |
| 358 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 359 | @Override |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 360 | public void dump(IndentingPrintWriter writer) { |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 361 | synchronized (mLock) { |
| 362 | writer.println("*PowerManagementService*"); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 363 | writer.printf("mCurrentState: %s\n", mCurrentState); |
| 364 | writer.printf("mProcessingStartTime: %d\n", mProcessingStartTime); |
| 365 | writer.printf("mLastSleepEntryTime: %d\n", mLastSleepEntryTime); |
| 366 | writer.printf("mNextWakeupSec: %d\n", mNextWakeupSec); |
| 367 | writer.printf("mShutdownOnNextSuspend: %b\n", mShutdownOnNextSuspend); |
| 368 | writer.printf("mShutdownOnFinish: %b\n", mShutdownOnFinish); |
| 369 | writer.printf("mShutdownPollingIntervalMs: %d\n", mShutdownPollingIntervalMs); |
| 370 | writer.printf("mShutdownPrepareTimeMs: %d\n", mShutdownPrepareTimeMs); |
| 371 | writer.printf("mRebootAfterGarageMode: %b\n", mRebootAfterGarageMode); |
| 372 | writer.printf("mSwitchGuestUserBeforeSleep: %b\n", mSwitchGuestUserBeforeSleep); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 373 | writer.printf("mCurrentPowerPolicyId: %s\n", mCurrentPowerPolicyId); |
| 374 | writer.printf("mPendingPowerPolicyId: %s\n", mPendingPowerPolicyId); |
| 375 | writer.printf("mCurrentPowerPolicyGroupId: %s\n", mCurrentPowerPolicyGroupId); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 376 | writer.printf("mIsPowerPolicyLocked: %b\n", mIsPowerPolicyLocked); |
| 377 | writer.printf("mMaxSuspendWaitDurationMs: %d\n", mMaxSuspendWaitDurationMs); |
| 378 | writer.printf("config_maxSuspendWaitDuration: %d\n", getMaxSuspendWaitDurationConfig()); |
| 379 | writer.printf("# of power policy change listener: %d\n", |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 380 | mPowerPolicyListeners.getRegisteredCallbackCount()); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 381 | writer.printf("mFactoryResetCallback: %s\n", mFactoryResetCallback); |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 382 | } |
Eric Jeong | 91969f2 | 2020-10-05 17:47:41 -0700 | [diff] [blame] | 383 | mPolicyReader.dump(writer); |
Eric Jeong | 8f0d3c4 | 2021-03-16 20:12:24 -0700 | [diff] [blame] | 384 | mPowerComponentHandler.dump(writer); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 385 | mSilentModeHandler.dump(writer); |
Steve Paik | 110f95f | 2017-11-22 14:44:05 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 389 | public void onApPowerStateChange(PowerState state) { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 390 | synchronized (mLock) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 391 | mPendingPowerStates.addFirst(new CpmsState(state)); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 392 | mLock.notify(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 393 | } |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 394 | mHandler.handlePowerStateChange(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Keun young Park | 9b3f266 | 2019-03-19 10:30:25 -0700 | [diff] [blame] | 397 | @VisibleForTesting |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 398 | void setStateForWakeUp() { |
| 399 | mSilentModeHandler.init(); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 400 | synchronized (mLock) { |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 401 | mIsBooting = false; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 402 | } |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 403 | handleWaitForVhal(new CpmsState(CpmsState.WAIT_FOR_VHAL, |
| 404 | CarPowerStateListener.WAIT_FOR_VHAL)); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 405 | Slogf.d(TAG, "setStateForTesting(): mIsBooting is set to false and power state is switched " |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 406 | + "to Wait For Vhal"); |
Keun young Park | 9b3f266 | 2019-03-19 10:30:25 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 409 | /** |
| 410 | * Initiate state change from CPMS directly. |
| 411 | */ |
| 412 | private void onApPowerStateChange(int apState, int carPowerStateListenerState) { |
| 413 | CpmsState newState = new CpmsState(apState, carPowerStateListenerState); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 414 | synchronized (mLock) { |
Jim Kaye | 9e802be | 2020-10-07 12:50:41 -0700 | [diff] [blame] | 415 | if (newState.mState == CpmsState.WAIT_FOR_FINISH) { |
| 416 | // We are ready to shut down. Suppress this transition if |
| 417 | // there is a request to cancel the shutdown (WAIT_FOR_VHAL). |
| 418 | for (int idx = 0; idx < mPendingPowerStates.size(); idx++) { |
| 419 | if (mPendingPowerStates.get(idx).mState == CpmsState.WAIT_FOR_VHAL) { |
| 420 | // Completely ignore this WAIT_FOR_FINISH |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 425 | mPendingPowerStates.addFirst(newState); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 426 | mLock.notify(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 427 | } |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 428 | mHandler.handlePowerStateChange(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 431 | private void doHandlePowerStateChange() { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 432 | CpmsState state; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 433 | synchronized (mLock) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 434 | state = mPendingPowerStates.peekFirst(); |
| 435 | mPendingPowerStates.clear(); |
| 436 | if (state == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 437 | Slogf.e(TAG, "Null power state was requested"); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 438 | return; |
| 439 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 440 | Slogf.i(TAG, "doHandlePowerStateChange: newState=%s", state.name()); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 441 | if (!needPowerStateChangeLocked(state)) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | // now real power change happens. Whatever was queued before should be all cancelled. |
| 445 | releaseTimerLocked(); |
Jim Kaye | 765e79e | 2020-06-04 13:48:54 -0700 | [diff] [blame] | 446 | mCurrentState = state; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 447 | } |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 448 | mHandler.cancelProcessingComplete(); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 449 | Slogf.i(TAG, "setCurrentState %s", state); |
Muhammad Qureshi | 94524cd | 2020-01-25 23:47:38 -0800 | [diff] [blame] | 450 | CarStatsLogHelper.logPowerState(state.mState); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 451 | switch (state.mState) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 452 | case CpmsState.WAIT_FOR_VHAL: |
| 453 | handleWaitForVhal(state); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 454 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 455 | case CpmsState.ON: |
| 456 | handleOn(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 457 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 458 | case CpmsState.SHUTDOWN_PREPARE: |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 459 | handleShutdownPrepare(state); |
| 460 | break; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 461 | case CpmsState.SIMULATE_SLEEP: |
| 462 | simulateShutdownPrepare(); |
| 463 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 464 | case CpmsState.WAIT_FOR_FINISH: |
| 465 | handleWaitForFinish(state); |
| 466 | break; |
| 467 | case CpmsState.SUSPEND: |
| 468 | // Received FINISH from VHAL |
| 469 | handleFinish(); |
| 470 | break; |
| 471 | default: |
| 472 | // Illegal state |
| 473 | // TODO: Throw exception? |
| 474 | break; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 478 | private void handleWaitForVhal(CpmsState state) { |
| 479 | int carPowerStateListenerState = state.mCarPowerStateListenerState; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 480 | // TODO(b/177478420): Restore Wifi, Audio, Location, and Bluetooth, if they are artificially |
| 481 | // modified for S2R. |
Eric Jeong | c1f53b2 | 2021-02-18 00:40:36 -0800 | [diff] [blame] | 482 | mSilentModeHandler.querySilentModeHwState(); |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 483 | sendPowerManagerEvent(carPowerStateListenerState); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 484 | // Inspect CarPowerStateListenerState to decide which message to send via VHAL |
| 485 | switch (carPowerStateListenerState) { |
| 486 | case CarPowerStateListener.WAIT_FOR_VHAL: |
| 487 | mHal.sendWaitForVhal(); |
| 488 | break; |
| 489 | case CarPowerStateListener.SHUTDOWN_CANCELLED: |
Jim Kaye | dab62d8 | 2019-12-18 14:52:19 -0800 | [diff] [blame] | 490 | mShutdownOnNextSuspend = false; // This cancels the "NextSuspend" |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 491 | mHal.sendShutdownCancel(); |
| 492 | break; |
| 493 | case CarPowerStateListener.SUSPEND_EXIT: |
| 494 | mHal.sendSleepExit(); |
| 495 | break; |
| 496 | } |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 497 | restoreWifi(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 500 | private void updateCarUserNoticeServiceIfNecessary() { |
| 501 | try { |
| 502 | int currentUserId = ActivityManager.getCurrentUser(); |
| 503 | UserInfo currentUserInfo = mUserManager.getUserInfo(currentUserId); |
| 504 | CarUserNoticeService carUserNoticeService = |
| 505 | CarLocalServices.getService(CarUserNoticeService.class); |
| 506 | if (currentUserInfo != null && currentUserInfo.isGuest() |
| 507 | && carUserNoticeService != null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 508 | Slogf.i(TAG, "Car user notice service will ignore all messages before user " |
| 509 | + "switch."); |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 510 | Intent intent = new Intent(); |
| 511 | intent.setComponent(new ComponentName(mContext.getPackageName(), |
| 512 | ContinuousBlankActivity.class.getName())); |
| 513 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 514 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 515 | carUserNoticeService.ignoreUserNotice(currentUserId); |
| 516 | } |
| 517 | } catch (Exception e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 518 | Slogf.w(TAG, e, "Cannot ignore user notice for current user"); |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 522 | @VisibleForTesting |
| 523 | void handleOn() { |
| 524 | if (factoryResetIfNeeded()) return; |
| 525 | |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 526 | // If current user is a Guest User, we want to inform CarUserNoticeService not to show |
| 527 | // notice for current user, and show user notice only for the target user. |
Keun young Park | 62de552 | 2020-03-10 10:47:09 -0700 | [diff] [blame] | 528 | if (!mSwitchGuestUserBeforeSleep) { |
| 529 | updateCarUserNoticeServiceIfNecessary(); |
| 530 | } |
Mayank Garg | fc24d57 | 2020-02-14 11:32:34 -0800 | [diff] [blame] | 531 | |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 532 | boolean isPreemptive; |
| 533 | synchronized (mLock) { |
| 534 | isPreemptive = mPolicyReader.isPreemptivePowerPolicy(mCurrentPowerPolicyId); |
| 535 | } |
| 536 | if (!mSilentModeHandler.isSilentMode() && isPreemptive) { |
| 537 | cancelPreemptivePowerPolicy(); |
| 538 | } else { |
| 539 | applyDefaultPowerPolicyForState(VehicleApPowerStateReport.ON, |
| 540 | PolicyReader.POWER_POLICY_ID_ALL_ON); |
| 541 | } |
| 542 | |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 543 | sendPowerManagerEvent(CarPowerStateListener.ON); |
felipeal | 524e265 | 2020-05-27 19:26:46 -0700 | [diff] [blame] | 544 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 545 | mHal.sendOn(); |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 546 | |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 547 | synchronized (mLock) { |
| 548 | if (mIsBooting) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 549 | Slogf.d(TAG, "handleOn(): called on boot"); |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 550 | mIsBooting = false; |
| 551 | return; |
| 552 | } |
| 553 | } |
| 554 | |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 555 | try { |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 556 | mUserService.onResume(); |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 557 | } catch (Exception e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 558 | Slogf.e(TAG, e, "Could not switch user on resume"); |
Felipe Leme | f45ee50 | 2019-12-19 10:00:14 -0800 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 562 | private boolean factoryResetIfNeeded() { |
| 563 | IResultReceiver callback; |
| 564 | synchronized (mLock) { |
| 565 | if (mFactoryResetCallback == null) return false; |
| 566 | callback = mFactoryResetCallback; |
| 567 | } |
| 568 | |
| 569 | try { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 570 | Slogf.i(TAG, "Factory resetting as it was delayed by user"); |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 571 | callback.send(/* resultCode= */ 0, /* resultData= */ null); |
| 572 | return true; |
| 573 | } catch (Exception e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 574 | Slogf.wtf(TAG, e, "Should have factory reset, but failed"); |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | } |
| 578 | |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 579 | private void applyDefaultPowerPolicyForState(int state, @Nullable String fallbackPolicyId) { |
| 580 | CarPowerPolicy policy; |
| 581 | synchronized (mLock) { |
| 582 | policy = mPolicyReader |
| 583 | .getDefaultPowerPolicyForState(mCurrentPowerPolicyGroupId, state); |
| 584 | } |
| 585 | if (policy == null && fallbackPolicyId == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 586 | Slogf.w(TAG, "No default power policy for %s is found", |
| 587 | PolicyReader.powerStateToString(state)); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | String policyId = policy == null ? fallbackPolicyId : policy.getPolicyId(); |
| 591 | applyPowerPolicy(policyId, /* upToDaemon= */ true); |
| 592 | } |
| 593 | |
Felipe Leme | 6162a09 | 2021-01-07 16:41:33 -0800 | [diff] [blame] | 594 | /** |
| 595 | * Sets the callback used to factory reset the device on resume when the user delayed it. |
| 596 | */ |
| 597 | public void setFactoryResetCallback(IResultReceiver callback) { |
| 598 | synchronized (mLock) { |
| 599 | mFactoryResetCallback = callback; |
| 600 | } |
| 601 | } |
| 602 | |
Felipe Leme | e3cab98 | 2020-03-12 11:39:29 -0700 | [diff] [blame] | 603 | /** |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 604 | * Tells Garage Mode if it should run normally, or just |
| 605 | * exit immediately without indicating 'idle' |
| 606 | * @return True if no idle jobs should be run |
| 607 | * @hide |
| 608 | */ |
| 609 | public boolean garageModeShouldExitImmediately() { |
| 610 | synchronized (mLock) { |
| 611 | return mGarageModeShouldExitImmediately; |
| 612 | } |
| 613 | } |
| 614 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 615 | private void handleShutdownPrepare(CpmsState newState) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 616 | // Shutdown on finish if the system doesn't support deep sleep or doesn't allow it. |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 617 | synchronized (mLock) { |
Jim Kaye | dab62d8 | 2019-12-18 14:52:19 -0800 | [diff] [blame] | 618 | mShutdownOnFinish = mShutdownOnNextSuspend |
| 619 | || !mHal.isDeepSleepAllowed() |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 620 | || !mSystemInterface.isSystemSupportingDeepSleep() |
| 621 | || !newState.mCanSleep; |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 622 | mGarageModeShouldExitImmediately = !newState.mCanPostpone; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 623 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 624 | Slogf.i(TAG, |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 625 | (newState.mCanPostpone |
| 626 | ? "starting shutdown prepare with Garage Mode" |
| 627 | : "starting shutdown prepare without Garage Mode")); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 628 | makeSureNoUserInteraction(); |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 629 | sendPowerManagerEvent(CarPowerStateListener.SHUTDOWN_PREPARE); |
| 630 | mHal.sendShutdownPrepare(); |
| 631 | doHandlePreprocessing(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 634 | // Simulate system shutdown to Deep Sleep |
| 635 | private void simulateShutdownPrepare() { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 636 | Slogf.i(TAG, "starting shutdown prepare"); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 637 | makeSureNoUserInteraction(); |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 638 | sendPowerManagerEvent(CarPowerStateListener.SHUTDOWN_PREPARE); |
| 639 | mHal.sendShutdownPrepare(); |
| 640 | doHandlePreprocessing(); |
| 641 | } |
| 642 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 643 | private void handleWaitForFinish(CpmsState state) { |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 644 | sendPowerManagerEvent(state.mCarPowerStateListenerState); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 645 | int wakeupSec; |
| 646 | synchronized (mLock) { |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 647 | // If we're shutting down immediately, don't schedule |
| 648 | // a wakeup time. |
| 649 | wakeupSec = mGarageModeShouldExitImmediately ? 0 : mNextWakeupSec; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 650 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 651 | switch (state.mCarPowerStateListenerState) { |
| 652 | case CarPowerStateListener.SUSPEND_ENTER: |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 653 | mHal.sendSleepEntry(wakeupSec); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 654 | break; |
| 655 | case CarPowerStateListener.SHUTDOWN_ENTER: |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 656 | mHal.sendShutdownStart(wakeupSec); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | private void handleFinish() { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 662 | boolean simulatedMode; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 663 | synchronized (mSimulationWaitObject) { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 664 | simulatedMode = mInSimulatedDeepSleepMode; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 665 | } |
| 666 | boolean mustShutDown; |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 667 | boolean forceReboot; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 668 | synchronized (mLock) { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 669 | mustShutDown = mShutdownOnFinish && !simulatedMode; |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 670 | forceReboot = mRebootAfterGarageMode; |
| 671 | mRebootAfterGarageMode = false; |
| 672 | } |
| 673 | if (forceReboot) { |
| 674 | PowerManager powerManager = mContext.getSystemService(PowerManager.class); |
| 675 | if (powerManager == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 676 | Slogf.wtf(TAG, "No PowerManager. Cannot reboot."); |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 677 | } else { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 678 | Slogf.i(TAG, "GarageMode has completed. Forcing reboot."); |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 679 | powerManager.reboot("GarageModeReboot"); |
| 680 | throw new AssertionError("Should not return from PowerManager.reboot()"); |
| 681 | } |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 682 | } |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 683 | // To make Kernel implementation simpler when going into sleep. |
| 684 | disableWifi(); |
| 685 | |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 686 | if (mustShutDown) { |
Serik Beketayev | dfedda2 | 2018-12-11 16:56:01 -0800 | [diff] [blame] | 687 | // shutdown HU |
| 688 | mSystemInterface.shutdown(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 689 | } else { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 690 | doHandleDeepSleep(simulatedMode); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 691 | } |
Jim Kaye | dab62d8 | 2019-12-18 14:52:19 -0800 | [diff] [blame] | 692 | mShutdownOnNextSuspend = false; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 695 | private void restoreWifi() { |
| 696 | boolean needToRestore = readWifiModifiedState(); |
| 697 | if (needToRestore) { |
| 698 | if (!mWifiManager.isWifiEnabled()) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 699 | Slogf.i(TAG, "Wifi has been enabled to restore the last setting"); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 700 | mWifiManager.setWifiEnabled(true); |
| 701 | } |
| 702 | // Update the persistent data as wifi is not modified by car framework. |
| 703 | saveWifiModifiedState(false); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | private void disableWifi() { |
| 708 | boolean wifiEnabled = mWifiManager.isWifiEnabled(); |
| 709 | boolean wifiModifiedState = readWifiModifiedState(); |
| 710 | if (wifiEnabled != wifiModifiedState) { |
| 711 | saveWifiModifiedState(wifiEnabled); |
| 712 | } |
| 713 | if (!wifiEnabled) return; |
| 714 | |
| 715 | mWifiManager.setWifiEnabled(false); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 716 | Slogf.i(TAG, "Wifi has been disabled and the last setting was saved"); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | private void saveWifiModifiedState(boolean forciblyDisabled) { |
| 720 | FileOutputStream fos; |
| 721 | try { |
| 722 | fos = mWifiStateFile.startWrite(); |
| 723 | } catch (IOException e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 724 | Slogf.e(TAG, e, "Cannot create %s", WIFI_STATE_FILENAME); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
| 728 | try (BufferedWriter writer = new BufferedWriter( |
| 729 | new OutputStreamWriter(fos, StandardCharsets.UTF_8))) { |
| 730 | writer.write(forciblyDisabled ? WIFI_STATE_MODIFIED : WIFI_STATE_ORIGINAL); |
| 731 | writer.newLine(); |
| 732 | writer.flush(); |
| 733 | mWifiStateFile.finishWrite(fos); |
| 734 | } catch (IOException e) { |
| 735 | mWifiStateFile.failWrite(fos); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 736 | Slogf.e(TAG, e, "Writing %s failed", WIFI_STATE_FILENAME); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | private boolean readWifiModifiedState() { |
| 741 | boolean needToRestore = false; |
| 742 | boolean invalidState = false; |
| 743 | |
| 744 | try (BufferedReader reader = new BufferedReader( |
| 745 | new InputStreamReader(mWifiStateFile.openRead(), StandardCharsets.UTF_8))) { |
| 746 | String line = reader.readLine(); |
| 747 | if (line == null) { |
| 748 | needToRestore = false; |
| 749 | invalidState = true; |
| 750 | } else { |
| 751 | line = line.trim(); |
| 752 | needToRestore = WIFI_STATE_MODIFIED.equals(line); |
| 753 | invalidState = !(needToRestore || WIFI_STATE_ORIGINAL.equals(line)); |
| 754 | } |
| 755 | } catch (IOException e) { |
| 756 | // If a file named wifi_state doesn't exist, we will not modify Wifi at system start. |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 757 | Slogf.w(TAG, "Failed to read %s: %s", WIFI_STATE_FILENAME, e); |
Eric Jeong | 26f47c7 | 2020-07-22 15:45:24 -0700 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | if (invalidState) { |
| 761 | mWifiStateFile.delete(); |
| 762 | } |
| 763 | |
| 764 | return needToRestore; |
| 765 | } |
| 766 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 767 | @GuardedBy("mLock") |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 768 | private void releaseTimerLocked() { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 769 | if (mTimer != null) { |
| 770 | mTimer.cancel(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 771 | } |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 772 | mTimer = null; |
| 773 | mTimerActive = false; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 776 | private void doHandlePreprocessing() { |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 777 | int intervalMs; |
| 778 | int pollingCount; |
| 779 | synchronized (mLock) { |
| 780 | intervalMs = mShutdownPollingIntervalMs; |
| 781 | pollingCount = (mShutdownPrepareTimeMs / mShutdownPollingIntervalMs) + 1; |
| 782 | } |
Keun young Park | 8c152cb | 2019-05-09 18:04:31 -0700 | [diff] [blame] | 783 | if (Build.IS_USERDEBUG || Build.IS_ENG) { |
| 784 | int shutdownPrepareTimeOverrideInSecs = |
| 785 | SystemProperties.getInt(PROP_MAX_GARAGE_MODE_DURATION_OVERRIDE, -1); |
| 786 | if (shutdownPrepareTimeOverrideInSecs >= 0) { |
| 787 | pollingCount = |
Jim Kaye | 253dfff | 2020-01-29 09:45:22 -0800 | [diff] [blame] | 788 | (shutdownPrepareTimeOverrideInSecs * 1000 / intervalMs) |
Keun young Park | 8c152cb | 2019-05-09 18:04:31 -0700 | [diff] [blame] | 789 | + 1; |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 790 | Slogf.i(TAG, "Garage mode duration overridden secs: %d", |
| 791 | shutdownPrepareTimeOverrideInSecs); |
Keun young Park | 8c152cb | 2019-05-09 18:04:31 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 794 | Slogf.i(TAG, "processing before shutdown expected for: %dms, adding polling:%d", |
| 795 | mShutdownPrepareTimeMs, pollingCount); |
Mikhail Petrov | 8d970a4 | 2021-04-26 20:08:12 -0700 | [diff] [blame] | 796 | boolean allAreComplete; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 797 | synchronized (mLock) { |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 798 | mProcessingStartTime = SystemClock.elapsedRealtime(); |
| 799 | releaseTimerLocked(); |
Mikhail Petrov | 8d970a4 | 2021-04-26 20:08:12 -0700 | [diff] [blame] | 800 | allAreComplete = mListenersWeAreWaitingFor.isEmpty(); |
| 801 | if (allAreComplete) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 802 | Slogf.i(TAG, "Listener queue is empty, don't start polling"); |
Mikhail Petrov | 8d970a4 | 2021-04-26 20:08:12 -0700 | [diff] [blame] | 803 | } else { |
| 804 | mTimer = new Timer(); |
| 805 | mTimerActive = true; |
| 806 | mTimer.scheduleAtFixedRate( |
| 807 | new ShutdownProcessingTimerTask(pollingCount), |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 808 | /* delay= */ 0, |
Mikhail Petrov | 8d970a4 | 2021-04-26 20:08:12 -0700 | [diff] [blame] | 809 | intervalMs); |
| 810 | } |
| 811 | } |
| 812 | if (allAreComplete) { |
| 813 | signalComplete(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 814 | } |
Mayank Garg | 7e1450b | 2020-08-07 18:15:15 -0700 | [diff] [blame] | 815 | // allowUserSwitch value doesn't matter for onSuspend = true |
Mayank Garg | 0baf88a | 2020-08-30 21:57:36 -0700 | [diff] [blame] | 816 | mUserService.onSuspend(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 819 | private void sendPowerManagerEvent(int newState) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 820 | // Broadcast to the listeners that do not signal completion |
| 821 | notifyListeners(mPowerManagerListeners, newState); |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 822 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 823 | // SHUTDOWN_PREPARE is the only state where we need |
| 824 | // to maintain callbacks from listener components. |
| 825 | boolean allowCompletion = (newState == CarPowerStateListener.SHUTDOWN_PREPARE); |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 826 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 827 | // Fully populate mListenersWeAreWaitingFor before calling any onStateChanged() |
| 828 | // for the listeners that signal completion. |
| 829 | // Otherwise, if the first listener calls finish() synchronously, we will |
| 830 | // see the list go empty and we will think that we are done. |
| 831 | boolean haveSomeCompleters = false; |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 832 | PowerManagerCallbackList<ICarPowerStateListener> completingListeners = |
| 833 | new PowerManagerCallbackList(l -> { }); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 834 | synchronized (mLock) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 835 | mListenersWeAreWaitingFor.clear(); |
| 836 | int idx = mPowerManagerListenersWithCompletion.beginBroadcast(); |
| 837 | while (idx-- > 0) { |
| 838 | ICarPowerStateListener listener = |
| 839 | mPowerManagerListenersWithCompletion.getBroadcastItem(idx); |
| 840 | completingListeners.register(listener); |
| 841 | if (allowCompletion) { |
| 842 | mListenersWeAreWaitingFor.add(listener.asBinder()); |
| 843 | haveSomeCompleters = true; |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 844 | } |
| 845 | } |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 846 | mPowerManagerListenersWithCompletion.finishBroadcast(); |
| 847 | } |
| 848 | // Broadcast to the listeners that DO signal completion |
| 849 | notifyListeners(completingListeners, newState); |
| 850 | |
| 851 | if (allowCompletion && !haveSomeCompleters) { |
| 852 | // No jobs need to signal completion. So we are now complete. |
| 853 | signalComplete(); |
Serik Beketayev | 004ce58 | 2019-01-27 17:56:26 -0800 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 857 | private void notifyListeners(PowerManagerCallbackList<ICarPowerStateListener> listenerList, |
| 858 | int newState) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 859 | int idx = listenerList.beginBroadcast(); |
| 860 | while (idx-- > 0) { |
| 861 | ICarPowerStateListener listener = listenerList.getBroadcastItem(idx); |
| 862 | try { |
| 863 | listener.onStateChanged(newState); |
| 864 | } catch (RemoteException e) { |
| 865 | // It's likely the connection snapped. Let binder death handle the situation. |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 866 | Slogf.e(TAG, e, "onStateChanged() call failed"); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 867 | } |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 868 | } |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 869 | listenerList.finishBroadcast(); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 870 | } |
| 871 | |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 872 | private void doHandleDeepSleep(boolean simulatedMode) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 873 | int status = applyPreemptivePowerPolicy(PolicyReader.POWER_POLICY_ID_SUSPEND_TO_RAM); |
| 874 | if (status != PolicyOperationStatus.OK) { |
| 875 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(status)); |
| 876 | } |
Keun-young Park | 9d4cf62 | 2016-08-04 13:24:28 -0700 | [diff] [blame] | 877 | // keep holding partial wakelock to prevent entering sleep before enterDeepSleep call |
| 878 | // enterDeepSleep should force sleep entry even if wake lock is kept. |
| 879 | mSystemInterface.switchToPartialWakeLock(); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 880 | mHandler.cancelProcessingComplete(); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 881 | synchronized (mLock) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 882 | mLastSleepEntryTime = SystemClock.elapsedRealtime(); |
| 883 | } |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 884 | int nextListenerState; |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 885 | if (simulatedMode) { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 886 | simulateSleepByWaiting(); |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 887 | nextListenerState = CarPowerStateListener.SHUTDOWN_CANCELLED; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 888 | } else { |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 889 | boolean sleepSucceeded = suspendWithRetries(); |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 890 | if (!sleepSucceeded) { |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 891 | // Suspend failed and we shut down instead. |
| 892 | // We either won't get here at all or we will power off very soon. |
Jim Kaye | 32ea4ce | 2019-12-03 15:00:51 -0800 | [diff] [blame] | 893 | return; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 894 | } |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 895 | // We suspended and have now resumed |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 896 | nextListenerState = CarPowerStateListener.SUSPEND_EXIT; |
Steve Paik | 0f9fc00 | 2018-02-09 17:42:00 -0800 | [diff] [blame] | 897 | } |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 898 | synchronized (mLock) { |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 899 | // Any wakeup time from before is no longer valid. |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 900 | mNextWakeupSec = 0; |
| 901 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 902 | Slogf.i(TAG, "Resuming after suspending"); |
Serik Beketayev | 74debf2 | 2018-10-04 12:18:09 -0700 | [diff] [blame] | 903 | mSystemInterface.refreshDisplayBrightness(); |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 904 | onApPowerStateChange(CpmsState.WAIT_FOR_VHAL, nextListenerState); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 907 | private boolean needPowerStateChangeLocked(@NonNull CpmsState newState) { |
| 908 | if (mCurrentState == null) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 909 | return true; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 910 | } else if (mCurrentState.equals(newState)) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 911 | Slogf.d(TAG, "Requested state is already in effect: %s", newState.name()); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 912 | return false; |
| 913 | } |
| 914 | |
| 915 | // The following switch/case enforces the allowed state transitions. |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 916 | boolean transitionAllowed = false; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 917 | switch (mCurrentState.mState) { |
| 918 | case CpmsState.WAIT_FOR_VHAL: |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 919 | transitionAllowed = (newState.mState == CpmsState.ON) |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 920 | || (newState.mState == CpmsState.SHUTDOWN_PREPARE); |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 921 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 922 | case CpmsState.SUSPEND: |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 923 | transitionAllowed = newState.mState == CpmsState.WAIT_FOR_VHAL; |
| 924 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 925 | case CpmsState.ON: |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 926 | transitionAllowed = (newState.mState == CpmsState.SHUTDOWN_PREPARE) |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 927 | || (newState.mState == CpmsState.SIMULATE_SLEEP); |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 928 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 929 | case CpmsState.SHUTDOWN_PREPARE: |
Jim Kaye | 49fbfd0 | 2019-10-09 09:45:18 -0700 | [diff] [blame] | 930 | // If VHAL sends SHUTDOWN_IMMEDIATELY or SLEEP_IMMEDIATELY while in |
| 931 | // SHUTDOWN_PREPARE state, do it. |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 932 | transitionAllowed = |
| 933 | ((newState.mState == CpmsState.SHUTDOWN_PREPARE) && !newState.mCanPostpone) |
| 934 | || (newState.mState == CpmsState.WAIT_FOR_FINISH) |
| 935 | || (newState.mState == CpmsState.WAIT_FOR_VHAL); |
| 936 | break; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 937 | case CpmsState.SIMULATE_SLEEP: |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 938 | transitionAllowed = true; |
| 939 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 940 | case CpmsState.WAIT_FOR_FINISH: |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 941 | transitionAllowed = (newState.mState == CpmsState.SUSPEND |
Jim Kaye | 78a1bce | 2020-04-16 15:33:45 -0700 | [diff] [blame] | 942 | || newState.mState == CpmsState.WAIT_FOR_VHAL); |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 943 | break; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 944 | default: |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 945 | Slogf.e(TAG, "Unexpected current state: currentState=%s, newState=%s", |
| 946 | mCurrentState.name(), newState.name()); |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 947 | transitionAllowed = true; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 948 | } |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 949 | if (!transitionAllowed) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 950 | Slogf.e(TAG, "Requested power transition is not allowed: %s --> %s", |
| 951 | mCurrentState.name(), newState.name()); |
Jim Kaye | 24ddc86 | 2020-04-30 14:05:17 -0700 | [diff] [blame] | 952 | } |
| 953 | return transitionAllowed; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 956 | private void doHandleProcessingComplete() { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 957 | int listenerState; |
| 958 | synchronized (mLock) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 959 | releaseTimerLocked(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 960 | if (!mShutdownOnFinish && mLastSleepEntryTime > mProcessingStartTime) { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 961 | // entered sleep after processing start. So this could be duplicate request. |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 962 | Slogf.w(TAG, "Duplicate sleep entry request, ignore"); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 963 | return; |
| 964 | } |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 965 | listenerState = mShutdownOnFinish |
| 966 | ? CarPowerStateListener.SHUTDOWN_ENTER : CarPowerStateListener.SUSPEND_ENTER; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 967 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 968 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 969 | onApPowerStateChange(CpmsState.WAIT_FOR_FINISH, listenerState); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | @Override |
| 973 | public void onDisplayBrightnessChange(int brightness) { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 974 | mHandler.handleDisplayBrightnessChange(brightness); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | private void doHandleDisplayBrightnessChange(int brightness) { |
Steve Paik | 87b36fd | 2018-03-13 19:25:47 -0700 | [diff] [blame] | 978 | mSystemInterface.setDisplayBrightness(brightness); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 981 | private void doHandleMainDisplayStateChange(boolean on) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 982 | Slogf.w(TAG, "Unimplemented: doHandleMainDisplayStateChange() - on = %b", on); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 985 | /** |
| 986 | * Handles when a main display changes. |
| 987 | */ |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 988 | public void handleMainDisplayChanged(boolean on) { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 989 | mHandler.handleMainDisplayStateChange(on); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Steve Paik | 47cdf7b | 2018-03-16 17:27:43 -0700 | [diff] [blame] | 992 | /** |
| 993 | * Send display brightness to VHAL. |
| 994 | * @param brightness value 0-100% |
| 995 | */ |
| 996 | public void sendDisplayBrightness(int brightness) { |
| 997 | mHal.sendDisplayBrightness(brightness); |
| 998 | } |
| 999 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1000 | /** |
| 1001 | * Get the PowerHandler that we use to change power states |
| 1002 | */ |
| 1003 | public Handler getHandler() { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1004 | return mHandler; |
| 1005 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1008 | // Binder interface for general use. |
| 1009 | // The listener is not required (or allowed) to call finished(). |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1010 | @Override |
| 1011 | public void registerListener(ICarPowerStateListener listener) { |
| 1012 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 1013 | mPowerManagerListeners.register(listener); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | // Binder interface for Car services only. |
| 1017 | // After the listener completes its processing, it must call finished(). |
| 1018 | @Override |
| 1019 | public void registerListenerWithCompletion(ICarPowerStateListener listener) { |
| 1020 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Keun young Park | aabecd9 | 2019-05-03 17:31:27 -0700 | [diff] [blame] | 1021 | ICarImpl.assertCallingFromSystemProcessOrSelf(); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1022 | |
| 1023 | mPowerManagerListenersWithCompletion.register(listener); |
| 1024 | // TODO: Need to send current state to newly registered listener? If so, need to handle |
| 1025 | // completion for SHUTDOWN_PREPARE state |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | @Override |
| 1029 | public void unregisterListener(ICarPowerStateListener listener) { |
| 1030 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 1031 | doUnregisterListener(listener); |
| 1032 | } |
| 1033 | |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1034 | @Override |
| 1035 | public void requestShutdownOnNextSuspend() { |
| 1036 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1037 | synchronized (mLock) { |
Jim Kaye | dab62d8 | 2019-12-18 14:52:19 -0800 | [diff] [blame] | 1038 | mShutdownOnNextSuspend = true; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1039 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | @Override |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1043 | public void finished(ICarPowerStateListener listener) { |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1044 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Keun young Park | aabecd9 | 2019-05-03 17:31:27 -0700 | [diff] [blame] | 1045 | ICarImpl.assertCallingFromSystemProcessOrSelf(); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1046 | finishedImpl(listener.asBinder()); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 1049 | @Override |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1050 | public void scheduleNextWakeupTime(int seconds) { |
Jim Kaye | 1842e82 | 2020-05-04 13:08:45 -0700 | [diff] [blame] | 1051 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 1052 | if (seconds < 0) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1053 | Slogf.w(TAG, "Next wake up time is negative. Ignoring!"); |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 1054 | return; |
| 1055 | } |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1056 | boolean timedWakeupAllowed = mHal.isTimedWakeupAllowed(); |
| 1057 | synchronized (mLock) { |
| 1058 | if (!timedWakeupAllowed) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1059 | Slogf.w(TAG, "Setting timed wakeups are disabled in HAL. Skipping"); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1060 | mNextWakeupSec = 0; |
| 1061 | return; |
| 1062 | } |
| 1063 | if (mNextWakeupSec == 0 || mNextWakeupSec > seconds) { |
| 1064 | // The new value is sooner than the old value. Take the new value. |
| 1065 | mNextWakeupSec = seconds; |
| 1066 | } else { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1067 | Slogf.d(TAG, "Tried to schedule next wake up, but already had shorter " |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1068 | + "scheduled time"); |
| 1069 | } |
Serik Beketayev | 6e4c782 | 2018-08-27 08:20:09 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Jim Kaye | 1842e82 | 2020-05-04 13:08:45 -0700 | [diff] [blame] | 1073 | @Override |
| 1074 | public int getPowerState() { |
| 1075 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
| 1076 | synchronized (mLock) { |
| 1077 | return (mCurrentState == null) ? CarPowerStateListener.INVALID |
| 1078 | : mCurrentState.mCarPowerStateListenerState; |
| 1079 | } |
| 1080 | } |
| 1081 | |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1082 | /** |
| 1083 | * @see android.car.hardware.power.CarPowerManager#getCurrentPowerPolicy |
| 1084 | */ |
| 1085 | @Override |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1086 | public CarPowerPolicy getCurrentPowerPolicy() { |
| 1087 | ICarImpl.assertPermission(mContext, Car.PERMISSION_READ_CAR_POWER_POLICY); |
Eric Jeong | 8f0d3c4 | 2021-03-16 20:12:24 -0700 | [diff] [blame] | 1088 | return mPowerComponentHandler.getAccumulatedPolicy(); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * @see android.car.hardware.power.CarPowerManager#applyPowerPolicy |
| 1093 | */ |
| 1094 | @Override |
| 1095 | public void applyPowerPolicy(String policyId) { |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1096 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CONTROL_CAR_POWER_POLICY); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1097 | Preconditions.checkArgument(policyId != null, "policyId cannot be null"); |
Eric Jeong | 078ada0 | 2021-01-20 18:11:17 -0800 | [diff] [blame] | 1098 | Preconditions.checkArgument(!policyId.startsWith(PolicyReader.SYSTEM_POWER_POLICY_PREFIX), |
| 1099 | "System power policy cannot be applied by apps"); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1100 | int status = applyPowerPolicy(policyId, true); |
| 1101 | if (status != PolicyOperationStatus.OK) { |
| 1102 | throw new IllegalArgumentException(PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1107 | * @see android.car.hardware.power.CarPowerManager#setPowerPolicyGroup |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1108 | */ |
| 1109 | @Override |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1110 | public void setPowerPolicyGroup(String policyGroupId) { |
| 1111 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CONTROL_CAR_POWER_POLICY); |
| 1112 | Preconditions.checkArgument(policyGroupId != null, "policyGroupId cannot be null"); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1113 | int status = setCurrentPowerPolicyGroup(policyGroupId); |
| 1114 | if (status != PolicyOperationStatus.OK) { |
| 1115 | throw new IllegalArgumentException(PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1116 | } |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /** |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1120 | * @see android.car.hardware.power.CarPowerManager#addPowerPolicyListener |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1121 | */ |
| 1122 | @Override |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1123 | public void addPowerPolicyListener(CarPowerPolicyFilter filter, |
| 1124 | ICarPowerPolicyListener listener) { |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1125 | ICarImpl.assertPermission(mContext, Car.PERMISSION_READ_CAR_POWER_POLICY); |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1126 | mPowerPolicyListeners.register(listener, filter); |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * @see android.car.hardware.power.CarPowerManager#removePowerPolicyListener |
| 1131 | */ |
| 1132 | @Override |
| 1133 | public void removePowerPolicyListener(ICarPowerPolicyListener listener) { |
| 1134 | ICarImpl.assertPermission(mContext, Car.PERMISSION_READ_CAR_POWER_POLICY); |
| 1135 | mPowerPolicyListeners.unregister(listener); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1138 | void notifySilentModeChange(boolean silent) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1139 | Slogf.i(TAG, "Silent mode is set to %b", silent); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1140 | mSilentModeHandler.updateKernelSilentMode(silent); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1141 | if (silent) { |
| 1142 | applyPreemptivePowerPolicy(PolicyReader.POWER_POLICY_ID_NO_USER_INTERACTION); |
| 1143 | } else { |
| 1144 | cancelPreemptivePowerPolicy(); |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1148 | private void doUnregisterListener(ICarPowerStateListener listener) { |
| 1149 | mPowerManagerListeners.unregister(listener); |
| 1150 | boolean found = mPowerManagerListenersWithCompletion.unregister(listener); |
| 1151 | if (found) { |
| 1152 | // Remove this from the completion list (if it's there) |
| 1153 | finishedImpl(listener.asBinder()); |
| 1154 | } |
| 1155 | } |
| 1156 | |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1157 | private void finishedImpl(IBinder binder) { |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 1158 | boolean allAreComplete; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1159 | synchronized (mLock) { |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 1160 | mListenersWeAreWaitingFor.remove(binder); |
| 1161 | allAreComplete = mListenersWeAreWaitingFor.isEmpty(); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1162 | } |
| 1163 | if (allAreComplete) { |
| 1164 | signalComplete(); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | private void signalComplete() { |
| 1169 | if (mCurrentState.mState == CpmsState.SHUTDOWN_PREPARE |
| 1170 | || mCurrentState.mState == CpmsState.SIMULATE_SLEEP) { |
| 1171 | PowerHandler powerHandler; |
| 1172 | // All apps are ready to shutdown/suspend. |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1173 | synchronized (mLock) { |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1174 | if (!mShutdownOnFinish) { |
| 1175 | if (mLastSleepEntryTime > mProcessingStartTime |
| 1176 | && mLastSleepEntryTime < SystemClock.elapsedRealtime()) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1177 | Slogf.i(TAG, "signalComplete: Already slept!"); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1178 | return; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1179 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1180 | } |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1181 | powerHandler = mHandler; |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 1182 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1183 | Slogf.i(TAG, "Apps are finished, call handleProcessingComplete()"); |
Jim Kaye | e513316 | 2019-04-22 12:50:27 -0700 | [diff] [blame] | 1184 | powerHandler.handleProcessingComplete(); |
Steve Paik | 0312408 | 2018-02-16 11:19:26 -0800 | [diff] [blame] | 1185 | } |
Steve Paik | 388d777 | 2018-02-12 10:54:51 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1188 | private void initializePowerPolicy() { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1189 | Slogf.i(TAG, "CPMS is taking control from carpowerpolicyd"); |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1190 | ICarPowerPolicySystemNotification daemon; |
| 1191 | synchronized (mLock) { |
| 1192 | daemon = mCarPowerPolicyDaemon; |
| 1193 | } |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1194 | PolicyState state; |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1195 | if (daemon != null) { |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1196 | try { |
| 1197 | state = daemon.notifyCarServiceReady(); |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1198 | } catch (RemoteException e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1199 | Slogf.e(TAG, e, "Failed to tell car power policy daemon that CarService is ready"); |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1200 | return; |
| 1201 | } |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1202 | } else { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1203 | Slogf.w(TAG, "Failed to notify car service is ready. car power policy daemon is not " |
Eric Jeong | 1ed5560 | 2021-02-24 11:21:14 -0800 | [diff] [blame] | 1204 | + "available"); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | String currentPowerPolicyId; |
| 1209 | String currentPolicyGroupId; |
| 1210 | synchronized (mLock) { |
| 1211 | mHasControlOverDaemon = true; |
| 1212 | currentPowerPolicyId = mCurrentPowerPolicyId; |
| 1213 | currentPolicyGroupId = mCurrentPowerPolicyGroupId; |
| 1214 | } |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1215 | // If the current power policy or the policy group has been modified by CPMS, we ignore |
Eric Jeong | 7edd8b2 | 2021-05-05 16:35:33 -0700 | [diff] [blame] | 1216 | // the power policy or the policy group passed from car power policy daemon, and notifies |
| 1217 | // the current power policy to the daemon. |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1218 | if (currentPowerPolicyId == null || currentPowerPolicyId.isEmpty()) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1219 | int status = applyPowerPolicy(state.policyId, false); |
| 1220 | if (status != PolicyOperationStatus.OK) { |
| 1221 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1222 | } |
Eric Jeong | 7edd8b2 | 2021-05-05 16:35:33 -0700 | [diff] [blame] | 1223 | } else { |
| 1224 | notifyPowerPolicyChangeToDaemon(currentPowerPolicyId); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1225 | } |
| 1226 | if (currentPolicyGroupId == null || currentPolicyGroupId.isEmpty()) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1227 | int status = setCurrentPowerPolicyGroup(state.policyGroupId); |
| 1228 | if (status != PolicyOperationStatus.OK) { |
| 1229 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1230 | } |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1231 | } |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1232 | mSilentModeHandler.init(); |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1235 | @PolicyOperationStatus.ErrorCode |
| 1236 | private int setCurrentPowerPolicyGroup(String policyGroupId) { |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1237 | if (!mPolicyReader.isPowerPolicyGroupAvailable(policyGroupId)) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1238 | int error = PolicyOperationStatus.ERROR_SET_POWER_POLICY_GROUP; |
| 1239 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(error, |
| 1240 | policyGroupId + " is not registered")); |
| 1241 | return error; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1242 | } |
| 1243 | synchronized (mLock) { |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1244 | mCurrentPowerPolicyGroupId = policyGroupId; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1245 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1246 | return PolicyOperationStatus.OK; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1249 | @PolicyOperationStatus.ErrorCode |
| 1250 | private int applyPowerPolicy(@Nullable String policyId, boolean upToDaemon) { |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1251 | CarPowerPolicy policy = mPolicyReader.getPowerPolicy(policyId); |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1252 | if (policy == null) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1253 | int error = PolicyOperationStatus.ERROR_APPLY_POWER_POLICY; |
| 1254 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(error, policyId)); |
| 1255 | return error; |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1256 | } |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1257 | synchronized (mLock) { |
| 1258 | if (mIsPowerPolicyLocked) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1259 | Slogf.i(TAG, "Power policy is locked. The request policy(%s) will be applied when " |
| 1260 | + "power policy becomes unlocked", policyId); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1261 | mPendingPowerPolicyId = policyId; |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1262 | return PolicyOperationStatus.OK; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1263 | } |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1264 | mCurrentPowerPolicyId = policyId; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1265 | } |
Eric Jeong | 03e0cc3 | 2020-10-28 18:21:20 -0700 | [diff] [blame] | 1266 | mPowerComponentHandler.applyPowerPolicy(policy); |
Eric Jeong | 4d8b487 | 2020-11-09 13:12:30 -0800 | [diff] [blame] | 1267 | notifyPowerPolicyChange(policyId, upToDaemon); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1268 | Slogf.i(TAG, "The current power policy is %s", policyId); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1269 | return PolicyOperationStatus.OK; |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1272 | @PolicyOperationStatus.ErrorCode |
| 1273 | private int applyPreemptivePowerPolicy(String policyId) { |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1274 | CarPowerPolicy policy = mPolicyReader.getPreemptivePowerPolicy(policyId); |
| 1275 | if (policy == null) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1276 | int error = PolicyOperationStatus.ERROR_NOT_REGISTERED_POWER_POLICY_ID; |
| 1277 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(error, policyId)); |
| 1278 | return error; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1279 | } |
| 1280 | synchronized (mLock) { |
| 1281 | mIsPowerPolicyLocked = true; |
| 1282 | if (!mPolicyReader.isPreemptivePowerPolicy(mCurrentPowerPolicyId)) { |
| 1283 | mPendingPowerPolicyId = mCurrentPowerPolicyId; |
| 1284 | } |
| 1285 | mCurrentPowerPolicyId = policyId; |
| 1286 | } |
| 1287 | mPowerComponentHandler.applyPowerPolicy(policy); |
| 1288 | notifyPowerPolicyChange(policyId, true); |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1289 | Slogf.i(TAG, "The current power policy is %s", policyId); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1290 | return PolicyOperationStatus.OK; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | private void cancelPreemptivePowerPolicy() { |
| 1294 | String policyId; |
| 1295 | synchronized (mLock) { |
| 1296 | if (!mIsPowerPolicyLocked) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1297 | Slogf.w(TAG, "Failed to cancel system power policy: the current policy is not the " |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1298 | + "system power policy"); |
| 1299 | return; |
| 1300 | } |
| 1301 | mIsPowerPolicyLocked = false; |
| 1302 | policyId = mPendingPowerPolicyId; |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1303 | mPendingPowerPolicyId = null; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1304 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1305 | int status = applyPowerPolicy(policyId, true); |
| 1306 | if (status != PolicyOperationStatus.OK) { |
| 1307 | Slogf.w(TAG, "Failed to cancel system power policy: %s", |
| 1308 | PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | private void notifyPowerPolicyChangeToDaemon(String policyId) { |
| 1313 | ICarPowerPolicySystemNotification daemon; |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1314 | boolean hadPendingPolicyNotification; |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1315 | synchronized (mLock) { |
| 1316 | daemon = mCarPowerPolicyDaemon; |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1317 | if (daemon == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1318 | Slogf.e(TAG, "Failed to notify car power policy daemon: the daemon is not ready"); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1319 | return; |
| 1320 | } |
| 1321 | if (!mHasControlOverDaemon) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1322 | Slogf.w(TAG, "Notifying policy change is deferred: CPMS has not yet taken control"); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1325 | } |
| 1326 | try { |
| 1327 | daemon.notifyPowerPolicyChange(policyId); |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1328 | } catch (RemoteException | IllegalStateException e) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1329 | Slogf.e(TAG, e, "Failed to notify car power policy daemon of a new power policy(%s)", |
| 1330 | policyId); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
Eric Jeong | 4d8b487 | 2020-11-09 13:12:30 -0800 | [diff] [blame] | 1334 | private void notifyPowerPolicyChange(String policyId, boolean upToDaemon) { |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1335 | // Notify system clients |
Eric Jeong | 4d8b487 | 2020-11-09 13:12:30 -0800 | [diff] [blame] | 1336 | if (upToDaemon) { |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1337 | notifyPowerPolicyChangeToDaemon(policyId); |
Eric Jeong | 4d8b487 | 2020-11-09 13:12:30 -0800 | [diff] [blame] | 1338 | } |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1339 | |
| 1340 | // Notify Java clients |
Eric Jeong | 8f0d3c4 | 2021-03-16 20:12:24 -0700 | [diff] [blame] | 1341 | CarPowerPolicy accumulatedPolicy = mPowerComponentHandler.getAccumulatedPolicy(); |
| 1342 | CarPowerPolicy appliedPolicy = mPolicyReader.isPreemptivePowerPolicy(policyId) |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1343 | ? mPolicyReader.getPreemptivePowerPolicy(policyId) |
| 1344 | : mPolicyReader.getPowerPolicy(policyId); |
Eric Jeong | 8f0d3c4 | 2021-03-16 20:12:24 -0700 | [diff] [blame] | 1345 | if (appliedPolicy == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1346 | Slogf.wtf(TAG, "The new power policy(%s) should exist", policyId); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1347 | } |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1348 | int idx = mPowerPolicyListeners.beginBroadcast(); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1349 | while (idx-- > 0) { |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1350 | ICarPowerPolicyListener listener = mPowerPolicyListeners.getBroadcastItem(idx); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1351 | CarPowerPolicyFilter filter = |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1352 | (CarPowerPolicyFilter) mPowerPolicyListeners.getBroadcastCookie(idx); |
Eric Jeong | 1e5aeb4 | 2021-04-05 00:14:19 -0700 | [diff] [blame] | 1353 | if (!mPowerComponentHandler.isComponentChanged(filter)) { |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1354 | continue; |
| 1355 | } |
| 1356 | try { |
Eric Jeong | 8f0d3c4 | 2021-03-16 20:12:24 -0700 | [diff] [blame] | 1357 | listener.onPolicyChanged(appliedPolicy, accumulatedPolicy); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1358 | } catch (RemoteException e) { |
| 1359 | // It's likely the connection snapped. Let binder death handle the situation. |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1360 | Slogf.e(TAG, e, "onPolicyChanged() call failed: policyId = %s", policyId); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1361 | } |
| 1362 | } |
Eric Jeong | d48f707 | 2021-02-25 18:21:06 -0800 | [diff] [blame] | 1363 | mPowerPolicyListeners.finishBroadcast(); |
Eric Jeong | 5876268 | 2020-11-30 21:51:32 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1366 | private void makeSureNoUserInteraction() { |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1367 | mSilentModeHandler.updateKernelSilentMode(true); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1368 | int status = applyPreemptivePowerPolicy(PolicyReader.POWER_POLICY_ID_NO_USER_INTERACTION); |
| 1369 | if (status != PolicyOperationStatus.OK) { |
| 1370 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(status)); |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1371 | } |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1374 | private void connectToPowerPolicyDaemon() { |
| 1375 | synchronized (mLock) { |
| 1376 | if (mCarPowerPolicyDaemon != null || mConnectionInProgress) { |
| 1377 | return; |
| 1378 | } |
| 1379 | mConnectionInProgress = true; |
| 1380 | } |
| 1381 | connectToDaemonHelper(CAR_POWER_POLICY_DAEMON_BIND_MAX_RETRY); |
| 1382 | } |
| 1383 | |
| 1384 | private void connectToDaemonHelper(int retryCount) { |
| 1385 | if (retryCount <= 0) { |
| 1386 | synchronized (mLock) { |
| 1387 | mConnectionInProgress = false; |
| 1388 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1389 | Slogf.e(TAG, "Cannot reconnect to car power policyd daemon after retrying %d times", |
| 1390 | CAR_POWER_POLICY_DAEMON_BIND_MAX_RETRY); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1391 | return; |
| 1392 | } |
| 1393 | if (makeBinderConnection()) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1394 | Slogf.i(TAG, "Connected to car power policy daemon"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1395 | initializePowerPolicy(); |
| 1396 | return; |
| 1397 | } |
| 1398 | mHandler.sendMessageDelayed(PooledLambda.obtainMessage( |
| 1399 | CarPowerManagementService::connectToDaemonHelper, |
| 1400 | CarPowerManagementService.this, retryCount - 1), |
| 1401 | CAR_POWER_POLICY_DAEMON_BIND_RETRY_INTERVAL_MS); |
| 1402 | } |
| 1403 | |
| 1404 | private boolean makeBinderConnection() { |
| 1405 | long currentTimeMs = SystemClock.uptimeMillis(); |
| 1406 | IBinder binder = ServiceManager.getService(CAR_POWER_POLICY_DAEMON_INTERFACE); |
| 1407 | if (binder == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1408 | Slogf.w(TAG, "Finding car power policy daemon failed. Power policy management is not " |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1409 | + "supported"); |
| 1410 | return false; |
| 1411 | } |
| 1412 | long elapsedTimeMs = SystemClock.uptimeMillis() - currentTimeMs; |
| 1413 | if (elapsedTimeMs > CAR_POWER_POLICY_DAEMON_FIND_MARGINAL_TIME_MS) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1414 | Slogf.wtf(TAG, "Finding car power policy daemon took too long(%dms)", elapsedTimeMs); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | ICarPowerPolicySystemNotification daemon = |
| 1418 | ICarPowerPolicySystemNotification.Stub.asInterface(binder); |
| 1419 | if (daemon == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1420 | Slogf.w(TAG, "Getting car power policy daemon interface failed. Power policy management" |
| 1421 | + " is not supported"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1422 | return false; |
| 1423 | } |
| 1424 | synchronized (mLock) { |
| 1425 | mCarPowerPolicyDaemon = daemon; |
| 1426 | mConnectionInProgress = false; |
| 1427 | } |
| 1428 | mBinderHandler = new BinderHandler(daemon); |
| 1429 | mBinderHandler.linkToDeath(); |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
| 1433 | private final class BinderHandler implements IBinder.DeathRecipient { |
| 1434 | private ICarPowerPolicySystemNotification mDaemon; |
| 1435 | |
| 1436 | private BinderHandler(ICarPowerPolicySystemNotification daemon) { |
| 1437 | mDaemon = daemon; |
| 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | public void binderDied() { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1442 | Slogf.w(TAG, "Car power policy daemon died: reconnecting"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1443 | unlinkToDeath(); |
| 1444 | mDaemon = null; |
| 1445 | synchronized (mLock) { |
| 1446 | mCarPowerPolicyDaemon = null; |
Eric Jeong | 7993009 | 2021-04-26 23:55:59 -0700 | [diff] [blame] | 1447 | mHasControlOverDaemon = false; |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1448 | } |
| 1449 | mHandler.sendMessageDelayed(PooledLambda.obtainMessage( |
| 1450 | CarPowerManagementService::connectToDaemonHelper, |
| 1451 | CarPowerManagementService.this, CAR_POWER_POLICY_DAEMON_BIND_MAX_RETRY), |
| 1452 | CAR_POWER_POLICY_DAEMON_BIND_RETRY_INTERVAL_MS); |
| 1453 | } |
| 1454 | |
| 1455 | private void linkToDeath() { |
| 1456 | if (mDaemon == null) { |
| 1457 | return; |
| 1458 | } |
| 1459 | IBinder binder = mDaemon.asBinder(); |
| 1460 | if (binder == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1461 | Slogf.w(TAG, "Linking to binder death recipient skipped"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1462 | return; |
| 1463 | } |
| 1464 | try { |
| 1465 | binder.linkToDeath(this, 0); |
| 1466 | } catch (RemoteException e) { |
| 1467 | mDaemon = null; |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1468 | Slogf.w(TAG, e, "Linking to binder death recipient failed: %s"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | private void unlinkToDeath() { |
| 1473 | if (mDaemon == null) { |
| 1474 | return; |
| 1475 | } |
| 1476 | IBinder binder = mDaemon.asBinder(); |
| 1477 | if (binder == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1478 | Slogf.w(TAG, "Unlinking from binder death recipient skipped"); |
Eric Jeong | 20a13b5 | 2020-10-02 23:50:40 -0700 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | binder.unlinkToDeath(this, 0); |
| 1482 | } |
| 1483 | } |
| 1484 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1485 | private static final class PowerHandler extends Handler { |
| 1486 | private static final String TAG = PowerHandler.class.getSimpleName(); |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 1487 | private static final int MSG_POWER_STATE_CHANGE = 0; |
| 1488 | private static final int MSG_DISPLAY_BRIGHTNESS_CHANGE = 1; |
| 1489 | private static final int MSG_MAIN_DISPLAY_STATE_CHANGE = 2; |
| 1490 | private static final int MSG_PROCESSING_COMPLETE = 3; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1491 | |
| 1492 | // Do not handle this immediately but with some delay as there can be a race between |
| 1493 | // display off due to rear view camera and delivery to here. |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 1494 | private static final long MAIN_DISPLAY_EVENT_DELAY_MS = 500; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1495 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1496 | private final WeakReference<CarPowerManagementService> mService; |
| 1497 | |
| 1498 | private PowerHandler(Looper looper, CarPowerManagementService service) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1499 | super(looper); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1500 | mService = new WeakReference<CarPowerManagementService>(service); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1503 | private void handlePowerStateChange() { |
| 1504 | Message msg = obtainMessage(MSG_POWER_STATE_CHANGE); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1505 | sendMessage(msg); |
| 1506 | } |
| 1507 | |
| 1508 | private void handleDisplayBrightnessChange(int brightness) { |
| 1509 | Message msg = obtainMessage(MSG_DISPLAY_BRIGHTNESS_CHANGE, brightness, 0); |
| 1510 | sendMessage(msg); |
| 1511 | } |
| 1512 | |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1513 | private void handleMainDisplayStateChange(boolean on) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1514 | removeMessages(MSG_MAIN_DISPLAY_STATE_CHANGE); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1515 | Message msg = obtainMessage(MSG_MAIN_DISPLAY_STATE_CHANGE, Boolean.valueOf(on)); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1516 | sendMessageDelayed(msg, MAIN_DISPLAY_EVENT_DELAY_MS); |
| 1517 | } |
| 1518 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1519 | private void handleProcessingComplete() { |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1520 | removeMessages(MSG_PROCESSING_COMPLETE); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1521 | Message msg = obtainMessage(MSG_PROCESSING_COMPLETE); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1522 | sendMessage(msg); |
| 1523 | } |
| 1524 | |
| 1525 | private void cancelProcessingComplete() { |
| 1526 | removeMessages(MSG_PROCESSING_COMPLETE); |
| 1527 | } |
| 1528 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1529 | private void cancelAll() { |
| 1530 | removeMessages(MSG_POWER_STATE_CHANGE); |
| 1531 | removeMessages(MSG_DISPLAY_BRIGHTNESS_CHANGE); |
| 1532 | removeMessages(MSG_MAIN_DISPLAY_STATE_CHANGE); |
| 1533 | removeMessages(MSG_PROCESSING_COMPLETE); |
| 1534 | } |
| 1535 | |
| 1536 | @Override |
| 1537 | public void handleMessage(Message msg) { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1538 | CarPowerManagementService service = mService.get(); |
| 1539 | if (service == null) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1540 | Slogf.i(TAG, "handleMessage null service"); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1541 | return; |
| 1542 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1543 | switch (msg.what) { |
| 1544 | case MSG_POWER_STATE_CHANGE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1545 | service.doHandlePowerStateChange(); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1546 | break; |
| 1547 | case MSG_DISPLAY_BRIGHTNESS_CHANGE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1548 | service.doHandleDisplayBrightnessChange(msg.arg1); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1549 | break; |
| 1550 | case MSG_MAIN_DISPLAY_STATE_CHANGE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1551 | service.doHandleMainDisplayStateChange((Boolean) msg.obj); |
Keun-young Park | 1f4d6a7 | 2016-02-11 09:49:47 -0800 | [diff] [blame] | 1552 | break; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1553 | case MSG_PROCESSING_COMPLETE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1554 | service.doHandleProcessingComplete(); |
Keun-young Park | fd3fbf7 | 2016-01-22 17:00:17 -0800 | [diff] [blame] | 1555 | break; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1560 | private class ShutdownProcessingTimerTask extends TimerTask { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1561 | private final int mExpirationCount; |
| 1562 | private int mCurrentCount; |
| 1563 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1564 | private ShutdownProcessingTimerTask(int expirationCount) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1565 | mExpirationCount = expirationCount; |
| 1566 | mCurrentCount = 0; |
| 1567 | } |
| 1568 | |
| 1569 | @Override |
| 1570 | public void run() { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1571 | synchronized (mLock) { |
Serik Beketayev | f8287a0 | 2019-02-08 12:31:10 -0800 | [diff] [blame] | 1572 | if (!mTimerActive) { |
| 1573 | // Ignore timer expiration since we got cancelled |
| 1574 | return; |
| 1575 | } |
| 1576 | mCurrentCount++; |
| 1577 | if (mCurrentCount > mExpirationCount) { |
| 1578 | PowerHandler handler; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1579 | releaseTimerLocked(); |
Pavel Maltsev | ddbff98 | 2017-03-22 14:49:02 -0700 | [diff] [blame] | 1580 | handler = mHandler; |
Serik Beketayev | f8287a0 | 2019-02-08 12:31:10 -0800 | [diff] [blame] | 1581 | handler.handleProcessingComplete(); |
| 1582 | } else { |
| 1583 | mHal.sendShutdownPostpone(SHUTDOWN_EXTEND_MAX_MS); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1584 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | } |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1588 | |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1589 | // Send the command to enter Suspend to RAM. |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 1590 | // If the command is not successful, try again with an exponential back-off. |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1591 | // If it fails repeatedly, send the command to shut down. |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 1592 | // If we decide to go to a different power state, abort this retry mechanism. |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1593 | // Returns true if we successfully suspended. |
| 1594 | private boolean suspendWithRetries() { |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 1595 | long retryIntervalMs = INITIAL_SUSPEND_RETRY_INTERVAL_MS; |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 1596 | long totalWaitDurationMs = 0; |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1597 | |
| 1598 | while (true) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1599 | Slogf.i(TAG, "Entering Suspend to RAM"); |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1600 | boolean suspendSucceeded = mSystemInterface.enterDeepSleep(); |
| 1601 | if (suspendSucceeded) { |
| 1602 | return true; |
| 1603 | } |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 1604 | if (totalWaitDurationMs >= mMaxSuspendWaitDurationMs) { |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | // We failed to suspend. Block the thread briefly and try again. |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 1608 | synchronized (mLock) { |
| 1609 | if (mPendingPowerStates.isEmpty()) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1610 | Slogf.w(TAG, "Failed to Suspend; will retry after %dms", retryIntervalMs); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 1611 | try { |
| 1612 | mLock.wait(retryIntervalMs); |
Jim Kaye | be23e54 | 2020-08-11 12:07:11 -0700 | [diff] [blame] | 1613 | } catch (InterruptedException ignored) { |
| 1614 | Thread.currentThread().interrupt(); |
| 1615 | } |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 1616 | totalWaitDurationMs += retryIntervalMs; |
| 1617 | retryIntervalMs = Math.min(retryIntervalMs * 2, MAX_RETRY_INTERVAL_MS); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 1618 | } |
| 1619 | // Check for a new power state now, before going around the loop again |
| 1620 | if (!mPendingPowerStates.isEmpty()) { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1621 | Slogf.i(TAG, "Terminating the attempt to Suspend to RAM"); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 1622 | return false; |
| 1623 | } |
| 1624 | } |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1625 | } |
| 1626 | // Too many failures trying to suspend. Shut down. |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 1627 | Slogf.w(TAG, "Could not Suspend to RAM after %dms long trial. Shutting down.", |
| 1628 | totalWaitDurationMs); |
Jim Kaye | ee122fc | 2020-05-29 09:37:05 -0700 | [diff] [blame] | 1629 | mSystemInterface.shutdown(); |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1633 | private static class CpmsState { |
Yuncheol Heo | e6e0347 | 2019-05-08 18:24:45 -0700 | [diff] [blame] | 1634 | // NOTE: When modifying states below, make sure to update CarPowerStateChanged.State in |
Jeffrey Huang | b8ecf01 | 2020-12-04 12:02:30 -0800 | [diff] [blame] | 1635 | // frameworks/proto_logging/stats/atoms.proto also. |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1636 | public static final int WAIT_FOR_VHAL = 0; |
| 1637 | public static final int ON = 1; |
| 1638 | public static final int SHUTDOWN_PREPARE = 2; |
| 1639 | public static final int WAIT_FOR_FINISH = 3; |
| 1640 | public static final int SUSPEND = 4; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1641 | public static final int SIMULATE_SLEEP = 5; |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1642 | |
| 1643 | /* Config values from AP_POWER_STATE_REQ */ |
| 1644 | public final boolean mCanPostpone; |
| 1645 | public final boolean mCanSleep; |
| 1646 | /* Message sent to CarPowerStateListener in response to this state */ |
| 1647 | public final int mCarPowerStateListenerState; |
| 1648 | /* One of the above state variables */ |
| 1649 | public final int mState; |
| 1650 | |
| 1651 | /** |
| 1652 | * This constructor takes a PowerHalService.PowerState object and creates the corresponding |
| 1653 | * CPMS state from it. |
| 1654 | */ |
| 1655 | CpmsState(PowerState halPowerState) { |
| 1656 | switch (halPowerState.mState) { |
| 1657 | case VehicleApPowerStateReq.ON: |
| 1658 | this.mCanPostpone = false; |
| 1659 | this.mCanSleep = false; |
| 1660 | this.mCarPowerStateListenerState = cpmsStateToPowerStateListenerState(ON); |
| 1661 | this.mState = ON; |
| 1662 | break; |
| 1663 | case VehicleApPowerStateReq.SHUTDOWN_PREPARE: |
| 1664 | this.mCanPostpone = halPowerState.canPostponeShutdown(); |
| 1665 | this.mCanSleep = halPowerState.canEnterDeepSleep(); |
| 1666 | this.mCarPowerStateListenerState = cpmsStateToPowerStateListenerState( |
| 1667 | SHUTDOWN_PREPARE); |
| 1668 | this.mState = SHUTDOWN_PREPARE; |
| 1669 | break; |
| 1670 | case VehicleApPowerStateReq.CANCEL_SHUTDOWN: |
| 1671 | this.mCanPostpone = false; |
| 1672 | this.mCanSleep = false; |
| 1673 | this.mCarPowerStateListenerState = CarPowerStateListener.SHUTDOWN_CANCELLED; |
| 1674 | this.mState = WAIT_FOR_VHAL; |
| 1675 | break; |
| 1676 | case VehicleApPowerStateReq.FINISHED: |
| 1677 | this.mCanPostpone = false; |
| 1678 | this.mCanSleep = false; |
| 1679 | this.mCarPowerStateListenerState = cpmsStateToPowerStateListenerState(SUSPEND); |
| 1680 | this.mState = SUSPEND; |
| 1681 | break; |
| 1682 | default: |
| 1683 | // Illegal state from PowerState. Throw an exception? |
| 1684 | this.mCanPostpone = false; |
| 1685 | this.mCanSleep = false; |
| 1686 | this.mCarPowerStateListenerState = 0; |
| 1687 | this.mState = 0; |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
| 1691 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1692 | CpmsState(int state, int carPowerStateListenerState) { |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1693 | this.mCanPostpone = (state == SIMULATE_SLEEP); |
| 1694 | this.mCanSleep = (state == SIMULATE_SLEEP); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1695 | this.mCarPowerStateListenerState = carPowerStateListenerState; |
| 1696 | this.mState = state; |
| 1697 | } |
| 1698 | |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1699 | public String name() { |
| 1700 | String baseName; |
| 1701 | switch(mState) { |
| 1702 | case WAIT_FOR_VHAL: baseName = "WAIT_FOR_VHAL"; break; |
| 1703 | case ON: baseName = "ON"; break; |
| 1704 | case SHUTDOWN_PREPARE: baseName = "SHUTDOWN_PREPARE"; break; |
| 1705 | case WAIT_FOR_FINISH: baseName = "WAIT_FOR_FINISH"; break; |
| 1706 | case SUSPEND: baseName = "SUSPEND"; break; |
| 1707 | case SIMULATE_SLEEP: baseName = "SIMULATE_SLEEP"; break; |
| 1708 | default: baseName = "<unknown>"; break; |
| 1709 | } |
| 1710 | return baseName + "(" + mState + ")"; |
| 1711 | } |
| 1712 | |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1713 | private static int cpmsStateToPowerStateListenerState(int state) { |
| 1714 | int powerStateListenerState = 0; |
| 1715 | |
| 1716 | // Set the CarPowerStateListenerState based on current state |
| 1717 | switch (state) { |
| 1718 | case ON: |
| 1719 | powerStateListenerState = CarPowerStateListener.ON; |
| 1720 | break; |
| 1721 | case SHUTDOWN_PREPARE: |
| 1722 | powerStateListenerState = CarPowerStateListener.SHUTDOWN_PREPARE; |
| 1723 | break; |
| 1724 | case SUSPEND: |
| 1725 | powerStateListenerState = CarPowerStateListener.SUSPEND_ENTER; |
| 1726 | break; |
| 1727 | case WAIT_FOR_VHAL: |
| 1728 | case WAIT_FOR_FINISH: |
| 1729 | default: |
| 1730 | // Illegal state for this constructor. Throw an exception? |
| 1731 | break; |
| 1732 | } |
| 1733 | return powerStateListenerState; |
| 1734 | } |
| 1735 | |
| 1736 | @Override |
| 1737 | public boolean equals(Object o) { |
| 1738 | if (this == o) { |
| 1739 | return true; |
| 1740 | } |
| 1741 | if (!(o instanceof CpmsState)) { |
| 1742 | return false; |
| 1743 | } |
| 1744 | CpmsState that = (CpmsState) o; |
| 1745 | return this.mState == that.mState |
| 1746 | && this.mCanSleep == that.mCanSleep |
| 1747 | && this.mCanPostpone == that.mCanPostpone |
| 1748 | && this.mCarPowerStateListenerState == that.mCarPowerStateListenerState; |
| 1749 | } |
| 1750 | |
| 1751 | @Override |
| 1752 | public String toString() { |
| 1753 | return "CpmsState canSleep:" + mCanSleep + ", canPostpone=" + mCanPostpone |
| 1754 | + ", carPowerStateListenerState=" + mCarPowerStateListenerState |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1755 | + ", CpmsState=" + this.name(); |
Steve Paik | 07db5ed | 2018-09-24 16:48:52 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1759 | /** |
| 1760 | * Resume after a manually-invoked suspend. |
| 1761 | * Invoked using "adb shell dumpsys activity service com.android.car resume". |
| 1762 | */ |
| 1763 | public void forceSimulatedResume() { |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 1764 | PowerHandler handler; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1765 | synchronized (mLock) { |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 1766 | // Cancel Garage Mode in case it's running |
| 1767 | mPendingPowerStates.addFirst(new CpmsState(CpmsState.WAIT_FOR_VHAL, |
| 1768 | CarPowerStateListener.SHUTDOWN_CANCELLED)); |
Jim Kaye | a94bb8b | 2020-07-28 09:42:35 -0700 | [diff] [blame] | 1769 | mLock.notify(); |
Jim Kaye | 0ffbf52 | 2019-07-01 16:10:40 -0700 | [diff] [blame] | 1770 | handler = mHandler; |
| 1771 | } |
| 1772 | handler.handlePowerStateChange(); |
| 1773 | |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1774 | synchronized (mSimulationWaitObject) { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 1775 | mWakeFromSimulatedSleep = true; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1776 | mSimulationWaitObject.notify(); |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /** |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 1781 | * Manually enter simulated suspend (Deep Sleep) mode, trigging Garage mode. |
| 1782 | * If the parameter is 'true', reboot the system when Garage Mode completes. |
| 1783 | * |
| 1784 | * Invoked using "adb shell dumpsys activity service com.android.car suspend" or |
| 1785 | * "adb shell dumpsys activity service com.android.car garage-mode reboot". |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1786 | * This is similar to 'onApPowerStateChange()' except that it needs to create a CpmsState |
| 1787 | * that is not directly derived from a VehicleApPowerStateReq. |
| 1788 | */ |
Eric Jeong | bc351e2 | 2020-07-31 13:54:17 -0700 | [diff] [blame] | 1789 | public void forceSuspendAndMaybeReboot(boolean shouldReboot) { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1790 | synchronized (mSimulationWaitObject) { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 1791 | mInSimulatedDeepSleepMode = true; |
| 1792 | mWakeFromSimulatedSleep = false; |
Jim Kaye | 9889f28 | 2020-05-28 09:17:17 -0700 | [diff] [blame] | 1793 | mGarageModeShouldExitImmediately = false; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1794 | } |
| 1795 | PowerHandler handler; |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 1796 | synchronized (mLock) { |
Jim Kaye | 9a834ea | 2020-02-07 15:50:53 -0800 | [diff] [blame] | 1797 | mRebootAfterGarageMode = shouldReboot; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 1798 | mPendingPowerStates.addFirst(new CpmsState(CpmsState.SIMULATE_SLEEP, |
| 1799 | CarPowerStateListener.SHUTDOWN_PREPARE)); |
| 1800 | handler = mHandler; |
| 1801 | } |
| 1802 | handler.handlePowerStateChange(); |
| 1803 | } |
| 1804 | |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1805 | /** |
| 1806 | * Manually defines a power policy. |
| 1807 | * |
| 1808 | * <p>If the given ID already exists or specified power components are invalid, it fails. |
| 1809 | * |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1810 | * @return {@code true}, if successful. Otherwise, {@code false}. |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1811 | */ |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1812 | public boolean definePowerPolicyFromCommand(String[] args, IndentingPrintWriter writer) { |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1813 | if (args.length < 2) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1814 | writer.println("Too few arguments"); |
| 1815 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1816 | } |
| 1817 | String powerPolicyId = args[1]; |
| 1818 | int index = 2; |
| 1819 | String[] enabledComponents = new String[0]; |
| 1820 | String[] disabledComponents = new String[0]; |
| 1821 | while (index < args.length) { |
| 1822 | switch (args[index]) { |
| 1823 | case "--enable": |
| 1824 | if (index == args.length - 1) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1825 | writer.println("No components for --enable"); |
| 1826 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1827 | } |
| 1828 | enabledComponents = args[index + 1].split(","); |
| 1829 | break; |
| 1830 | case "--disable": |
| 1831 | if (index == args.length - 1) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1832 | writer.println("No components for --disabled"); |
| 1833 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1834 | } |
| 1835 | disabledComponents = args[index + 1].split(","); |
| 1836 | break; |
| 1837 | default: |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1838 | writer.printf("Unrecognized argument: %s\n", args[index]); |
| 1839 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1840 | } |
| 1841 | index += 2; |
| 1842 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1843 | int status = definePowerPolicy(powerPolicyId, enabledComponents, disabledComponents); |
| 1844 | if (status != PolicyOperationStatus.OK) { |
| 1845 | writer.println(PolicyOperationStatus.errorCodeToString(status)); |
| 1846 | return false; |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1847 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1848 | writer.printf("Power policy(%s) is successfully defined.\n", powerPolicyId); |
| 1849 | return true; |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * Defines a power policy with the given id and components. |
| 1854 | * |
| 1855 | * <p> A policy defined with this method is valid until the system is rebooted/restarted. |
| 1856 | */ |
| 1857 | @VisibleForTesting |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1858 | @PolicyOperationStatus.ErrorCode |
| 1859 | public int definePowerPolicy(String powerPolicyId, String[] enabledComponents, |
Eric Jeong | b700381 | 2020-12-08 11:37:55 -0800 | [diff] [blame] | 1860 | String[] disabledComponents) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1861 | int status = mPolicyReader.definePowerPolicy(powerPolicyId, |
| 1862 | enabledComponents, disabledComponents); |
| 1863 | if (status != PolicyOperationStatus.OK) { |
| 1864 | int error = PolicyOperationStatus.ERROR_DEFINE_POWER_POLICY; |
| 1865 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(error)); |
| 1866 | return error; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1867 | } |
| 1868 | ICarPowerPolicySystemNotification daemon; |
| 1869 | synchronized (mLock) { |
| 1870 | daemon = mCarPowerPolicyDaemon; |
| 1871 | } |
| 1872 | try { |
| 1873 | daemon.notifyPowerPolicyDefinition(powerPolicyId, enabledComponents, |
| 1874 | disabledComponents); |
| 1875 | } catch (RemoteException e) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1876 | int error = PolicyOperationStatus.ERROR_DEFINE_POWER_POLICY; |
| 1877 | Slogf.w(TAG, PolicyOperationStatus.errorCodeToString(error)); |
| 1878 | return error; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1879 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1880 | return PolicyOperationStatus.OK; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * Manually applies a power policy. |
| 1885 | * |
| 1886 | * <p>If the given ID is not defined, it fails. |
| 1887 | * |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1888 | * @return {@code true}, if successful. Otherwise, {@code false}. |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1889 | */ |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1890 | public boolean applyPowerPolicyFromCommand(String[] args, IndentingPrintWriter writer) { |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1891 | if (args.length != 2) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1892 | writer.println("Power policy ID should be given"); |
| 1893 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1894 | } |
| 1895 | String powerPolicyId = args[1]; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1896 | if (powerPolicyId == null) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1897 | writer.println("Policy ID cannot be null"); |
| 1898 | return false; |
Eric Jeong | 4b69cf7 | 2021-01-07 21:23:16 -0800 | [diff] [blame] | 1899 | } |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1900 | boolean isPreemptive = mPolicyReader.isPreemptivePowerPolicy(powerPolicyId); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1901 | int status = isPreemptive ? applyPreemptivePowerPolicy(powerPolicyId) |
Eric Jeong | 36f61d4 | 2021-02-19 22:10:45 -0800 | [diff] [blame] | 1902 | : applyPowerPolicy(powerPolicyId, true); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1903 | if (status != PolicyOperationStatus.OK) { |
| 1904 | writer.println(PolicyOperationStatus.errorCodeToString(status)); |
| 1905 | return false; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1906 | } |
| 1907 | writer.printf("Power policy(%s) is successfully applied.\n", powerPolicyId); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1908 | return true; |
Eric Jeong | a293463 | 2020-11-08 23:14:30 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Eric Jeong | e701e0d | 2020-12-16 17:54:00 -0800 | [diff] [blame] | 1911 | /** |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1912 | * Manually defines a power policy group. |
| 1913 | * |
| 1914 | * <p>If the given ID already exists, a wrong power state is given, or specified power policy ID |
| 1915 | * doesn't exist, it fails. |
| 1916 | * |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1917 | * @return {@code true}, if successful. Otherwise, {@code false}. |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1918 | */ |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1919 | public boolean definePowerPolicyGroupFromCommand(String[] args, IndentingPrintWriter writer) { |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1920 | if (args.length < 3 || args.length > 4) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1921 | writer.println("Invalid syntax"); |
| 1922 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1923 | } |
| 1924 | String policyGroupId = args[1]; |
| 1925 | int index = 2; |
| 1926 | SparseArray<String> defaultPolicyPerState = new SparseArray<>(); |
| 1927 | while (index < args.length) { |
| 1928 | String[] tokens = args[index].split(":"); |
| 1929 | if (tokens.length != 2) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1930 | writer.println("Invalid syntax"); |
| 1931 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1932 | } |
| 1933 | int state = PolicyReader.toPowerState(tokens[0]); |
| 1934 | if (state == PolicyReader.INVALID_POWER_STATE) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1935 | writer.printf("Invalid power state: %s\n", tokens[0]); |
| 1936 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1937 | } |
| 1938 | defaultPolicyPerState.put(state, tokens[1]); |
| 1939 | index++; |
| 1940 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1941 | int status = mPolicyReader.definePowerPolicyGroup(policyGroupId, |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1942 | defaultPolicyPerState); |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1943 | if (status != PolicyOperationStatus.OK) { |
| 1944 | writer.println(PolicyOperationStatus.errorCodeToString(status)); |
| 1945 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1946 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1947 | writer.printf("Power policy group(%s) is successfully defined.\n", policyGroupId); |
| 1948 | return true; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /** |
| 1952 | * Manually sets a power policy group. |
| 1953 | * |
| 1954 | * <p>If the given ID is not defined, it fails. |
| 1955 | * |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1956 | * @return {@code true}, if successful. Otherwise, {@code false}. |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1957 | */ |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1958 | public boolean setPowerPolicyGroupFromCommand(String[] args, IndentingPrintWriter writer) { |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1959 | if (args.length != 2) { |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1960 | writer.println("Power policy group ID should be given"); |
| 1961 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1962 | } |
| 1963 | String policyGroupId = args[1]; |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1964 | int status = setCurrentPowerPolicyGroup(policyGroupId); |
| 1965 | if (status != PolicyOperationStatus.OK) { |
| 1966 | writer.println(PolicyOperationStatus.errorCodeToString(status)); |
| 1967 | return false; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1968 | } |
Eric Jeong | 83225c7 | 2021-05-13 13:22:45 -0700 | [diff] [blame^] | 1969 | writer.printf("Setting power policy group(%s) is successful.\n", policyGroupId); |
| 1970 | return true; |
Eric Jeong | 110c963 | 2021-04-09 10:59:50 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /** |
Eric Jeong | e701e0d | 2020-12-16 17:54:00 -0800 | [diff] [blame] | 1974 | * Powers off the device, considering the given options. |
| 1975 | * |
| 1976 | * <p>The final state can be "suspend-to-RAM" or "shutdown". Attempting to go to suspend-to-RAM |
| 1977 | * on devices which do not support it may lead to an unexpected system state. |
| 1978 | */ |
| 1979 | public void powerOffFromCommand(boolean skipGarageMode, boolean shutdown) { |
| 1980 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
| 1981 | int param = 0; |
| 1982 | if (shutdown) { |
| 1983 | param = skipGarageMode ? VehicleApPowerStateShutdownParam.SHUTDOWN_IMMEDIATELY |
| 1984 | : VehicleApPowerStateShutdownParam.SHUTDOWN_ONLY; |
| 1985 | } else { |
| 1986 | param = skipGarageMode ? VehicleApPowerStateShutdownParam.SLEEP_IMMEDIATELY |
| 1987 | : VehicleApPowerStateShutdownParam.CAN_SLEEP; |
| 1988 | } |
| 1989 | PowerState state = new PowerState(VehicleApPowerStateReq.SHUTDOWN_PREPARE, param); |
| 1990 | synchronized (mLock) { |
| 1991 | mRebootAfterGarageMode = false; |
| 1992 | mPendingPowerStates.addFirst(new CpmsState(state)); |
| 1993 | mLock.notify(); |
| 1994 | } |
| 1995 | mHandler.handlePowerStateChange(); |
| 1996 | } |
| 1997 | |
Eric Jeong | c1f53b2 | 2021-02-18 00:40:36 -0800 | [diff] [blame] | 1998 | /** |
| 1999 | * Changes Silent Mode to the given mode. |
| 2000 | */ |
| 2001 | public void setSilentMode(String silentMode) { |
| 2002 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CAR_POWER); |
| 2003 | mSilentModeHandler.setSilentMode(silentMode); |
| 2004 | } |
| 2005 | |
| 2006 | /** |
| 2007 | * Dumps the current Silent Mode. |
| 2008 | */ |
| 2009 | public void dumpSilentMode(IndentingPrintWriter writer) { |
| 2010 | mSilentModeHandler.dump(writer); |
| 2011 | } |
| 2012 | |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2013 | // In a real Deep Sleep, the hardware removes power from the CPU (but retains power |
| 2014 | // on the RAM). This puts the processor to sleep. Upon some external signal, power |
| 2015 | // is re-applied to the CPU, and processing resumes right where it left off. |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 2016 | // We simulate this behavior by calling wait(). |
| 2017 | // We continue from wait() when forceSimulatedResume() is called. |
| 2018 | private void simulateSleepByWaiting() { |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 2019 | Slogf.i(TAG, "Starting to simulate Deep Sleep by waiting"); |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 2020 | synchronized (mSimulationWaitObject) { |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 2021 | while (!mWakeFromSimulatedSleep) { |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2022 | try { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 2023 | mSimulationWaitObject.wait(); |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2024 | } catch (InterruptedException ignored) { |
Jim Kaye | d22f1d6 | 2019-12-03 16:08:00 -0800 | [diff] [blame] | 2025 | Thread.currentThread().interrupt(); // Restore interrupted status |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2026 | } |
| 2027 | } |
Jim Kaye | 4ae10b4 | 2019-04-02 11:42:50 -0700 | [diff] [blame] | 2028 | mInSimulatedDeepSleepMode = false; |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2029 | } |
Eric Jeong | d37b78b | 2021-04-29 00:22:36 -0700 | [diff] [blame] | 2030 | Slogf.i(TAG, "Exit Deep Sleep simulation"); |
Jim Kaye | d76c274 | 2019-04-02 11:33:27 -0700 | [diff] [blame] | 2031 | } |
Eric Jeong | decc425 | 2020-11-19 22:05:41 -0800 | [diff] [blame] | 2032 | |
| 2033 | private int getMaxSuspendWaitDurationConfig() { |
| 2034 | return mContext.getResources().getInteger(R.integer.config_maxSuspendWaitDuration); |
| 2035 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 2036 | } |