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 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 19 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT; |
| 20 | |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.UserIdInt; |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 23 | import android.app.ActivityThread; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 25 | import android.content.pm.UserInfo; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.ServiceManager; |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 28 | import android.os.UserManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 29 | |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 30 | import com.android.server.pm.UserManagerService; |
| 31 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 32 | /** |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 33 | * The base class for services running in the system process. Override and implement |
| 34 | * the lifecycle event callback methods as needed. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 35 | * <p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 36 | * The lifecycle of a SystemService: |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 37 | * </p><ul> |
| 38 | * <li>The constructor is called and provided with the system {@link Context} |
| 39 | * to initialize the system service. |
| 40 | * <li>{@link #onStart()} is called to get the service running. The service should |
| 41 | * publish its binder interface at this point using |
| 42 | * {@link #publishBinderService(String, IBinder)}. It may also publish additional |
| 43 | * local interfaces that other services within the system server may use to access |
| 44 | * privileged internal functions. |
| 45 | * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 46 | * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 47 | * is an opportunity to do special work, like acquiring optional service dependencies, |
| 48 | * waiting to see if SafeMode is enabled, or registering with a service that gets |
| 49 | * started after this one. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 50 | * </ul><p> |
| 51 | * NOTE: All lifecycle methods are called from the system server's main looper thread. |
| 52 | * </p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 53 | * |
| 54 | * {@hide} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 55 | */ |
| 56 | public abstract class SystemService { |
| 57 | /* |
| 58 | * Boot Phases |
| 59 | */ |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 60 | public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency? |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * After receiving this boot phase, services can obtain lock settings data. |
| 64 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 65 | public static final int PHASE_LOCK_SETTINGS_READY = 480; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * After receiving this boot phase, services can safely call into core system services |
| 69 | * such as the PowerManager or PackageManager. |
| 70 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 71 | public static final int PHASE_SYSTEM_SERVICES_READY = 500; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Daichi Hirono | edfcb00 | 2017-10-10 17:22:58 +0900 | [diff] [blame] | 74 | * After receiving this boot phase, services can safely call into device specific services. |
| 75 | */ |
| 76 | public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520; |
| 77 | |
| 78 | /** |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 79 | * After receiving this boot phase, services can broadcast Intents. |
| 80 | */ |
| 81 | public static final int PHASE_ACTIVITY_MANAGER_READY = 550; |
| 82 | |
| 83 | /** |
| 84 | * After receiving this boot phase, services can start/bind to third party apps. |
| 85 | * Apps will be able to make Binder calls into services at this point. |
| 86 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 87 | public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 90 | * After receiving this boot phase, services can allow user interaction with the device. |
| 91 | * This phase occurs when boot has completed and the home application has started. |
| 92 | * System services may prefer to listen to this phase rather than registering a |
| 93 | * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency. |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 94 | */ |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 95 | public static final int PHASE_BOOT_COMPLETED = 1000; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 96 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 97 | private final Context mContext; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 98 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Initializes the system service. |
| 101 | * <p> |
| 102 | * Subclasses must define a single argument constructor that accepts the context |
| 103 | * and passes it to super. |
| 104 | * </p> |
| 105 | * |
| 106 | * @param context The system server context. |
| 107 | */ |
| 108 | public SystemService(Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 109 | mContext = context; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Gets the system context. |
| 114 | */ |
| 115 | public final Context getContext() { |
| 116 | return mContext; |
| 117 | } |
| 118 | |
| 119 | /** |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 120 | * Get the system UI context. This context is to be used for displaying UI. It is themable, |
| 121 | * which means resources can be overridden at runtime. Do not use to retrieve properties that |
| 122 | * configure the behavior of the device that is not UX related. |
| 123 | */ |
| 124 | public final Context getUiContext() { |
| 125 | // This has already been set up by the time any SystemServices are created. |
| 126 | return ActivityThread.currentActivityThread().getSystemUiContext(); |
| 127 | } |
| 128 | |
| 129 | /** |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 130 | * Returns true if the system is running in safe mode. |
| 131 | * TODO: we should define in which phase this becomes valid |
| 132 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 133 | public final boolean isSafeMode() { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 134 | return getManager().isSafeMode(); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 137 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 138 | * Called when the dependencies listed in the @Service class-annotation are available |
| 139 | * and after the chosen start phase. |
| 140 | * When this method returns, the service should be published. |
| 141 | */ |
| 142 | public abstract void onStart(); |
| 143 | |
| 144 | /** |
| 145 | * Called on each phase of the boot process. Phases before the service's start phase |
| 146 | * (as defined in the @Service annotation) are never received. |
| 147 | * |
| 148 | * @param phase The current boot phase. |
| 149 | */ |
| 150 | public void onBootPhase(int phase) {} |
| 151 | |
| 152 | /** |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 153 | * @deprecated subclasses should extend {@link #onStartUser(int, int)} instead (which by default |
| 154 | * calls this method). |
| 155 | */ |
| 156 | @Deprecated |
| 157 | public void onStartUser(@UserIdInt int userHandle) {} |
| 158 | |
| 159 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 160 | * Called when a new user is starting, for system services to initialize any per-user |
| 161 | * state they maintain for running users. |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 162 | * |
| 163 | * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object |
| 164 | * referenced by {@link UserManagerService} and hence should not be modified. |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 165 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 166 | public void onStartUser(@NonNull UserInfo userInfo) { |
| 167 | onStartUser(userInfo.id); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @deprecated subclasses should extend {@link #onUnlockUser(int, int)} instead (which by |
| 172 | * default calls this method). |
| 173 | */ |
| 174 | @Deprecated |
| 175 | public void onUnlockUser(@UserIdInt int userHandle) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 178 | * Called when an existing user is in the process of being unlocked. This |
| 179 | * means the credential-encrypted storage for that user is now available, |
| 180 | * and encryption-aware component filtering is no longer in effect. |
| 181 | * <p> |
| 182 | * While dispatching this event to services, the user is in the |
| 183 | * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished |
| 184 | * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state. |
| 185 | * Code written inside system services should use |
| 186 | * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of |
| 187 | * these states. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 188 | * |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 189 | * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object |
| 190 | * referenced by {@link UserManagerService} and hence should not be modified. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 191 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 192 | public void onUnlockUser(@NonNull UserInfo userInfo) { |
| 193 | onUnlockUser(userInfo.id); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @deprecated subclasses should extend {@link #onSwitchUser(int, int)} instead (which by |
| 198 | * default calls this method). |
| 199 | */ |
| 200 | @Deprecated |
| 201 | public void onSwitchUser(@UserIdInt int userHandle) {} |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 204 | * Called when switching to a different foreground user, for system services that have |
| 205 | * special behavior for whichever user is currently in the foreground. This is called |
| 206 | * before any application processes are aware of the new user. |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 207 | * |
| 208 | * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object |
| 209 | * referenced by {@link UserManagerService} and hence should not be modified. |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 210 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 211 | public void onSwitchUser(@NonNull UserInfo userInfo) { |
| 212 | onSwitchUser(userInfo.id); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @deprecated subclasses should extend {@link #onStopUser(int, int)} instead (which by default |
| 217 | * calls this method). |
| 218 | */ |
| 219 | @Deprecated |
| 220 | public void onStopUser(@UserIdInt int userHandle) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 224 | * state they maintain for running users. This is called prior to sending the SHUTDOWN |
| 225 | * broadcast to the user; it is a good place to stop making use of any resources of that |
| 226 | * user (such as binding to a service running in the user). |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 227 | * |
| 228 | * <p>NOTE: This is the last callback where the callee may access the target user's CE storage. |
| 229 | * |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 230 | * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object |
| 231 | * referenced by {@link UserManagerService} and hence should not be modified. |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 232 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 233 | public void onStopUser(@NonNull UserInfo userInfo) { |
| 234 | onStopUser(userInfo.id); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @deprecated subclasses should extend {@link #onCleanupUser(int, int)} instead (which by |
| 239 | * default calls this method). |
| 240 | */ |
| 241 | @Deprecated |
| 242 | public void onCleanupUser(@UserIdInt int userHandle) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 246 | * state they maintain for running users. This is called after all application process |
| 247 | * teardown of the user is complete. |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 248 | * |
| 249 | * <p>NOTE: When this callback is called, the CE storage for the target user may not be |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 250 | * accessible already. Use {@link #onCleanupUser} instead if you need to access the CE storage. |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 251 | * |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 252 | * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object |
| 253 | * referenced by {@link UserManagerService} and hence should not be modified. |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 254 | */ |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame^] | 255 | public void onCleanupUser(@NonNull UserInfo userInfo) { |
| 256 | onCleanupUser(userInfo.id); |
| 257 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 260 | * Publish the service so it is accessible to other services and apps. |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 261 | * |
| 262 | * @param name the name of the new service |
| 263 | * @param service the service object |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 264 | */ |
| 265 | protected final void publishBinderService(String name, IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 266 | publishBinderService(name, service, false); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Publish the service so it is accessible to other services and apps. |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 271 | * |
| 272 | * @param name the name of the new service |
| 273 | * @param service the service object |
| 274 | * @param allowIsolated set to true to allow isolated sandboxed processes |
| 275 | * to access this service |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 276 | */ |
| 277 | protected final void publishBinderService(String name, IBinder service, |
| 278 | boolean allowIsolated) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 279 | publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Publish the service so it is accessible to other services and apps. |
| 284 | * |
| 285 | * @param name the name of the new service |
| 286 | * @param service the service object |
| 287 | * @param allowIsolated set to true to allow isolated sandboxed processes |
| 288 | * to access this service |
| 289 | * @param dumpPriority supported dump priority levels as a bitmask |
| 290 | */ |
| 291 | protected final void publishBinderService(String name, IBinder service, |
| 292 | boolean allowIsolated, int dumpPriority) { |
| 293 | ServiceManager.addService(name, service, allowIsolated, dumpPriority); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get a binder service by its name. |
| 298 | */ |
| 299 | protected final IBinder getBinderService(String name) { |
| 300 | return ServiceManager.getService(name); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Publish the service so it is only accessible to the system process. |
| 305 | */ |
| 306 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 307 | LocalServices.addService(type, service); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get a local service by interface. |
| 312 | */ |
| 313 | protected final <T> T getLocalService(Class<T> type) { |
| 314 | return LocalServices.getService(type); |
| 315 | } |
| 316 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 317 | private SystemServiceManager getManager() { |
| 318 | return LocalServices.getService(SystemServiceManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 319 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 320 | } |