Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
MÃ¥rten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 20 | import android.annotation.UserIdInt; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 22 | import android.content.pm.UserInfo; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 23 | import android.os.Environment; |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 24 | import android.os.SystemClock; |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 25 | import android.os.Trace; |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 27 | import android.os.UserManagerInternal; |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 28 | import android.util.ArrayMap; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 29 | import android.util.Slog; |
| 30 | |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 31 | import com.android.server.SystemService.TargetUser; |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 32 | import com.android.server.utils.TimingsTraceAndSlog; |
| 33 | |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 34 | import dalvik.system.PathClassLoader; |
| 35 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 36 | import java.io.File; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 37 | import java.lang.reflect.Constructor; |
| 38 | import java.lang.reflect.InvocationTargetException; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | |
| 41 | /** |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 42 | * Manages creating, starting, and other lifecycle events of |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 43 | * {@link com.android.server.SystemService system services}. |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 44 | * |
| 45 | * {@hide} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 46 | */ |
| 47 | public class SystemServiceManager { |
| 48 | private static final String TAG = "SystemServiceManager"; |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 49 | private static final boolean DEBUG = false; |
Fyodor Kupolov | 701a85f | 2017-04-25 12:52:08 -0700 | [diff] [blame] | 50 | private static final int SERVICE_CALL_WARN_TIME_MS = 50; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 51 | |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 52 | // Constants used on onUser(...) |
| 53 | private static final String START = "Start"; |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 54 | private static final String UNLOCKING = "Unlocking"; |
| 55 | private static final String UNLOCKED = "Unlocked"; |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 56 | private static final String SWITCH = "Switch"; |
| 57 | private static final String STOP = "Stop"; |
| 58 | private static final String CLEANUP = "Cleanup"; |
| 59 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 60 | private static File sSystemDir; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 61 | private final Context mContext; |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 62 | private boolean mSafeMode; |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 63 | private boolean mRuntimeRestarted; |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 64 | private long mRuntimeStartElapsedTime; |
| 65 | private long mRuntimeStartUptime; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 66 | |
| 67 | // Services that should receive lifecycle events. |
| 68 | private final ArrayList<SystemService> mServices = new ArrayList<SystemService>(); |
| 69 | |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 70 | // Map of paths to PathClassLoader, so we don't load the same path multiple times. |
| 71 | private final ArrayMap<String, PathClassLoader> mLoadedPaths = new ArrayMap<>(); |
| 72 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 73 | private int mCurrentPhase = -1; |
| 74 | |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 75 | private UserManagerInternal mUserManagerInternal; |
| 76 | |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 77 | SystemServiceManager(Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 78 | mContext = context; |
| 79 | } |
| 80 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 81 | /** |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 82 | * Starts a service by class name. |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 83 | * |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 84 | * @return The service instance. |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 85 | */ |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 86 | public SystemService startService(String className) { |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 87 | final Class<SystemService> serviceClass = loadClassFromLoader(className, |
| 88 | this.getClass().getClassLoader()); |
| 89 | return startService(serviceClass); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Starts a service by class name and a path that specifies the jar where the service lives. |
| 94 | * |
| 95 | * @return The service instance. |
| 96 | */ |
| 97 | public SystemService startServiceFromJar(String className, String path) { |
| 98 | PathClassLoader pathClassLoader = mLoadedPaths.get(path); |
| 99 | if (pathClassLoader == null) { |
| 100 | // NB: the parent class loader should always be the system server class loader. |
| 101 | // Changing it has implications that require discussion with the mainline team. |
| 102 | pathClassLoader = new PathClassLoader(path, this.getClass().getClassLoader()); |
| 103 | mLoadedPaths.put(path, pathClassLoader); |
| 104 | } |
| 105 | final Class<SystemService> serviceClass = loadClassFromLoader(className, pathClassLoader); |
| 106 | return startService(serviceClass); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Loads and initializes a class from the given classLoader. Returns the class. |
| 111 | */ |
| 112 | @SuppressWarnings("unchecked") |
| 113 | private static Class<SystemService> loadClassFromLoader(String className, |
| 114 | ClassLoader classLoader) { |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 115 | try { |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 116 | return (Class<SystemService>) Class.forName(className, true, classLoader); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 117 | } catch (ClassNotFoundException ex) { |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 118 | throw new RuntimeException("Failed to create service " + className |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 119 | + " from class loader " + classLoader.toString() + ": service class not " |
| 120 | + "found, usually indicates that the caller should " |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 121 | + "have called PackageManager.hasSystemFeature() to check whether the " |
| 122 | + "feature is available on this device before trying to start the " |
Tej Singh | b2d6884 | 2020-01-08 15:03:38 -0800 | [diff] [blame] | 123 | + "services that implement it. Also ensure that the correct path for the " |
| 124 | + "classloader is supplied, if applicable.", ex); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 125 | } |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Creates and starts a system service. The class must be a subclass of |
| 130 | * {@link com.android.server.SystemService}. |
| 131 | * |
| 132 | * @param serviceClass A Java class that implements the SystemService interface. |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 133 | * @return The service instance, never null. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 134 | * @throws RuntimeException if the service fails to start. |
| 135 | */ |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 136 | public <T extends SystemService> T startService(Class<T> serviceClass) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 137 | try { |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 138 | final String name = serviceClass.getName(); |
| 139 | Slog.i(TAG, "Starting " + name); |
| 140 | Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 141 | |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 142 | // Create the service. |
| 143 | if (!SystemService.class.isAssignableFrom(serviceClass)) { |
| 144 | throw new RuntimeException("Failed to create " + name |
| 145 | + ": service must extend " + SystemService.class.getName()); |
| 146 | } |
| 147 | final T service; |
| 148 | try { |
| 149 | Constructor<T> constructor = serviceClass.getConstructor(Context.class); |
| 150 | service = constructor.newInstance(mContext); |
| 151 | } catch (InstantiationException ex) { |
| 152 | throw new RuntimeException("Failed to create service " + name |
| 153 | + ": service could not be instantiated", ex); |
| 154 | } catch (IllegalAccessException ex) { |
| 155 | throw new RuntimeException("Failed to create service " + name |
| 156 | + ": service must have a public constructor with a Context argument", ex); |
| 157 | } catch (NoSuchMethodException ex) { |
| 158 | throw new RuntimeException("Failed to create service " + name |
| 159 | + ": service must have a public constructor with a Context argument", ex); |
| 160 | } catch (InvocationTargetException ex) { |
| 161 | throw new RuntimeException("Failed to create service " + name |
| 162 | + ": service constructor threw an exception", ex); |
| 163 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 164 | |
MÃ¥rten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 165 | startService(service); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 166 | return service; |
| 167 | } finally { |
| 168 | Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
MÃ¥rten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 172 | public void startService(@NonNull final SystemService service) { |
| 173 | // Register it. |
| 174 | mServices.add(service); |
| 175 | // Start it. |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 176 | long time = SystemClock.elapsedRealtime(); |
MÃ¥rten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 177 | try { |
| 178 | service.onStart(); |
| 179 | } catch (RuntimeException ex) { |
| 180 | throw new RuntimeException("Failed to start service " + service.getClass().getName() |
| 181 | + ": onStart threw an exception", ex); |
| 182 | } |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 183 | warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart"); |
MÃ¥rten Kongstad | 2e0d0f3 | 2016-06-02 09:35:31 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 186 | /** |
| 187 | * Starts the specified boot phase for all system services that have been started up to |
| 188 | * this point. |
| 189 | * |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 190 | * @param t trace logger |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 191 | * @param phase The boot phase to start. |
| 192 | */ |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 193 | public void startBootPhase(@NonNull TimingsTraceAndSlog t, int phase) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 194 | if (phase <= mCurrentPhase) { |
| 195 | throw new IllegalArgumentException("Next phase must be larger than previous"); |
| 196 | } |
| 197 | mCurrentPhase = phase; |
| 198 | |
| 199 | Slog.i(TAG, "Starting phase " + mCurrentPhase); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 200 | try { |
Wei Wang | a62b4a7 | 2020-06-01 13:53:48 -0700 | [diff] [blame] | 201 | t.traceBegin("OnBootPhase_" + phase); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 202 | final int serviceLen = mServices.size(); |
| 203 | for (int i = 0; i < serviceLen; i++) { |
| 204 | final SystemService service = mServices.get(i); |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 205 | long time = SystemClock.elapsedRealtime(); |
Wei Wang | a62b4a7 | 2020-06-01 13:53:48 -0700 | [diff] [blame] | 206 | t.traceBegin("OnBootPhase_" + phase + "_" + service.getClass().getName()); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 207 | try { |
| 208 | service.onBootPhase(mCurrentPhase); |
| 209 | } catch (Exception ex) { |
| 210 | throw new RuntimeException("Failed to boot service " |
| 211 | + service.getClass().getName() |
| 212 | + ": onBootPhase threw an exception during phase " |
| 213 | + mCurrentPhase, ex); |
| 214 | } |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 215 | warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onBootPhase"); |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 216 | t.traceEnd(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 217 | } |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 218 | } finally { |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 219 | t.traceEnd(); |
| 220 | } |
| 221 | |
| 222 | if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
| 223 | final long totalBootTime = SystemClock.uptimeMillis() - mRuntimeStartUptime; |
| 224 | t.logDuration("TotalBootTime", totalBootTime); |
felipeal | fc53040 | 2020-05-11 10:20:43 -0700 | [diff] [blame] | 225 | SystemServerInitThreadPool.shutdown(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Keun-young Park | 4136d2d | 2017-05-08 14:51:59 -0700 | [diff] [blame] | 229 | /** |
| 230 | * @return true if system has completed the boot; false otherwise. |
| 231 | */ |
| 232 | public boolean isBootCompleted() { |
| 233 | return mCurrentPhase >= SystemService.PHASE_BOOT_COMPLETED; |
| 234 | } |
| 235 | |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 236 | /** |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 237 | * Called at the beginning of {@code ActivityManagerService.systemReady()}. |
| 238 | */ |
| 239 | public void preSystemReady() { |
| 240 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
| 241 | } |
| 242 | |
| 243 | private @NonNull UserInfo getUserInfo(@UserIdInt int userHandle) { |
| 244 | if (mUserManagerInternal == null) { |
| 245 | throw new IllegalStateException("mUserManagerInternal not set yet"); |
| 246 | } |
| 247 | final UserInfo userInfo = mUserManagerInternal.getUserInfo(userHandle); |
| 248 | if (userInfo == null) { |
| 249 | throw new IllegalStateException("No UserInfo for " + userHandle); |
| 250 | } |
| 251 | return userInfo; |
| 252 | } |
| 253 | |
| 254 | /** |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 255 | * Starts the given user. |
| 256 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 257 | public void startUser(final @NonNull TimingsTraceAndSlog t, final @UserIdInt int userHandle) { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 258 | onUser(t, START, userHandle); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Unlocks the given user. |
| 263 | */ |
| 264 | public void unlockUser(final @UserIdInt int userHandle) { |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 265 | onUser(UNLOCKING, userHandle); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Called after the user was unlocked. |
| 270 | */ |
| 271 | public void onUserUnlocked(final @UserIdInt int userHandle) { |
| 272 | onUser(UNLOCKED, userHandle); |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 275 | /** |
| 276 | * Switches to the given user. |
| 277 | */ |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 278 | public void switchUser(final @UserIdInt int from, final @UserIdInt int to) { |
| 279 | onUser(TimingsTraceAndSlog.newAsyncLog(), SWITCH, to, from); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Stops the given user. |
| 284 | */ |
| 285 | public void stopUser(final @UserIdInt int userHandle) { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 286 | onUser(STOP, userHandle); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Cleans up the given user. |
| 291 | */ |
| 292 | public void cleanupUser(final @UserIdInt int userHandle) { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 293 | onUser(CLEANUP, userHandle); |
Felipe Leme | 3e99d40 | 2019-08-15 14:42:32 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 296 | private void onUser(@NonNull String onWhat, @UserIdInt int userHandle) { |
| 297 | onUser(TimingsTraceAndSlog.newAsyncLog(), onWhat, userHandle); |
Felipe Leme | 3e99d40 | 2019-08-15 14:42:32 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | private void onUser(@NonNull TimingsTraceAndSlog t, @NonNull String onWhat, |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 301 | @UserIdInt int userHandle) { |
| 302 | onUser(t, onWhat, userHandle, UserHandle.USER_NULL); |
| 303 | } |
| 304 | |
| 305 | private void onUser(@NonNull TimingsTraceAndSlog t, @NonNull String onWhat, |
| 306 | @UserIdInt int curUserId, @UserIdInt int prevUserId) { |
| 307 | t.traceBegin("ssm." + onWhat + "User-" + curUserId); |
| 308 | Slog.i(TAG, "Calling on" + onWhat + "User " + curUserId); |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 309 | final TargetUser curUser = new TargetUser(getUserInfo(curUserId)); |
| 310 | final TargetUser prevUser = prevUserId == UserHandle.USER_NULL ? null |
| 311 | : new TargetUser(getUserInfo(prevUserId)); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 312 | final int serviceLen = mServices.size(); |
| 313 | for (int i = 0; i < serviceLen; i++) { |
| 314 | final SystemService service = mServices.get(i); |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 315 | final String serviceName = service.getClass().getName(); |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 316 | boolean supported = service.isUserSupported(curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 317 | |
| 318 | // Must check if either curUser or prevUser is supported (for example, if switching from |
| 319 | // unsupported to supported, we still need to notify the services) |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 320 | if (!supported && prevUser != null) { |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 321 | supported = service.isUserSupported(prevUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (!supported) { |
| 325 | if (DEBUG) { |
| 326 | Slog.d(TAG, "Skipping " + onWhat + "User-" + curUserId + " on service " |
| 327 | + serviceName + " because it's not supported (curUser: " |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 328 | + curUser + ", prevUser:" + prevUser + ")"); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 329 | } else { |
| 330 | Slog.i(TAG, "Skipping " + onWhat + "User-" + curUserId + " on " |
| 331 | + serviceName); |
| 332 | } |
| 333 | continue; |
| 334 | } |
Wei Wang | a62b4a7 | 2020-06-01 13:53:48 -0700 | [diff] [blame] | 335 | t.traceBegin("ssm.on" + onWhat + "User-" + curUserId + "_" + serviceName); |
Wei Wang | c3a76d0 | 2017-07-14 11:39:30 -0700 | [diff] [blame] | 336 | long time = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 337 | try { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 338 | switch (onWhat) { |
| 339 | case SWITCH: |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 340 | service.onUserSwitching(prevUser, curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 341 | break; |
| 342 | case START: |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 343 | service.onUserStarting(curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 344 | break; |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 345 | case UNLOCKING: |
| 346 | service.onUserUnlocking(curUser); |
| 347 | break; |
| 348 | case UNLOCKED: |
| 349 | service.onUserUnlocked(curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 350 | break; |
| 351 | case STOP: |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 352 | service.onUserStopping(curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 353 | break; |
| 354 | case CLEANUP: |
Felipe Leme | 339b714 | 2020-02-21 11:05:49 -0800 | [diff] [blame] | 355 | service.onUserStopped(curUser); |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 356 | break; |
| 357 | default: |
| 358 | throw new IllegalArgumentException(onWhat + " what?"); |
| 359 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 360 | } catch (Exception ex) { |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 361 | Slog.wtf(TAG, "Failure reporting " + onWhat + " of user " + curUser |
Felipe Leme | 137e781 | 2019-06-10 11:38:05 -0700 | [diff] [blame] | 362 | + " to service " + serviceName, ex); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 363 | } |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 364 | warnIfTooLong(SystemClock.elapsedRealtime() - time, service, |
| 365 | "on" + onWhat + "User-" + curUserId); |
Felipe Leme | 3e99d40 | 2019-08-15 14:42:32 -0700 | [diff] [blame] | 366 | t.traceEnd(); // what on service |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 367 | } |
Felipe Leme | 3e99d40 | 2019-08-15 14:42:32 -0700 | [diff] [blame] | 368 | t.traceEnd(); // main entry |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 371 | /** Sets the safe mode flag for services to query. */ |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 372 | void setSafeMode(boolean safeMode) { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 373 | mSafeMode = safeMode; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns whether we are booting into safe mode. |
| 378 | * @return safe mode flag |
| 379 | */ |
| 380 | public boolean isSafeMode() { |
| 381 | return mSafeMode; |
| 382 | } |
| 383 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 384 | /** |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 385 | * @return true if runtime was restarted, false if it's normal boot |
| 386 | */ |
| 387 | public boolean isRuntimeRestarted() { |
| 388 | return mRuntimeRestarted; |
| 389 | } |
| 390 | |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 391 | /** |
| 392 | * @return Time when SystemServer was started, in elapsed realtime. |
| 393 | */ |
| 394 | public long getRuntimeStartElapsedTime() { |
| 395 | return mRuntimeStartElapsedTime; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @return Time when SystemServer was started, in uptime. |
| 400 | */ |
| 401 | public long getRuntimeStartUptime() { |
| 402 | return mRuntimeStartUptime; |
| 403 | } |
| 404 | |
| 405 | void setStartInfo(boolean runtimeRestarted, |
| 406 | long runtimeStartElapsedTime, long runtimeStartUptime) { |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 407 | mRuntimeRestarted = runtimeRestarted; |
Makoto Onuki | e8edbcf | 2018-03-02 16:49:29 -0800 | [diff] [blame] | 408 | mRuntimeStartElapsedTime = runtimeStartElapsedTime; |
| 409 | mRuntimeStartUptime = runtimeStartUptime; |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Fyodor Kupolov | 701a85f | 2017-04-25 12:52:08 -0700 | [diff] [blame] | 412 | private void warnIfTooLong(long duration, SystemService service, String operation) { |
| 413 | if (duration > SERVICE_CALL_WARN_TIME_MS) { |
| 414 | Slog.w(TAG, "Service " + service.getClass().getName() + " took " + duration + " ms in " |
| 415 | + operation); |
| 416 | } |
| 417 | } |
Fyodor Kupolov | 1d87e40 | 2017-01-10 18:34:10 -0800 | [diff] [blame] | 418 | |
| 419 | /** |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 420 | * Ensures that the system directory exist creating one if needed. |
| 421 | * @deprecated Use {@link Environment#getDataSystemCeDirectory()} |
| 422 | * or {@link Environment#getDataSystemDeDirectory()} instead. |
| 423 | * @return The system directory. |
| 424 | */ |
| 425 | @Deprecated |
| 426 | public static File ensureSystemDir() { |
| 427 | if (sSystemDir == null) { |
| 428 | File dataDir = Environment.getDataDirectory(); |
| 429 | sSystemDir = new File(dataDir, "system"); |
| 430 | sSystemDir.mkdirs(); |
| 431 | } |
| 432 | return sSystemDir; |
| 433 | } |
| 434 | |
| 435 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 436 | * Outputs the state of this manager to the System log. |
| 437 | */ |
| 438 | public void dump() { |
| 439 | StringBuilder builder = new StringBuilder(); |
| 440 | builder.append("Current phase: ").append(mCurrentPhase).append("\n"); |
| 441 | builder.append("Services:\n"); |
| 442 | final int startedLen = mServices.size(); |
| 443 | for (int i = 0; i < startedLen; i++) { |
| 444 | final SystemService service = mServices.get(i); |
| 445 | builder.append("\t") |
| 446 | .append(service.getClass().getSimpleName()) |
| 447 | .append("\n"); |
| 448 | } |
| 449 | |
| 450 | Slog.e(TAG, builder.toString()); |
| 451 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 452 | } |