blob: 4151c726993468115f65be33a29a53d78d058b31 [file] [log] [blame]
Adam Lesinski182f73f2013-12-05 16:48:06 -08001/*
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
17package com.android.server;
18
Vishnu Naire3e4d252018-03-01 11:26:57 -080019import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT;
20
Felipe Lemee5434c32019-08-13 09:28:33 -070021import android.annotation.NonNull;
22import android.annotation.UserIdInt;
Adam Lesinskia82b6262017-03-21 16:56:17 -070023import android.app.ActivityThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080024import android.content.Context;
Felipe Lemee5434c32019-08-13 09:28:33 -070025import android.content.pm.UserInfo;
Adam Lesinski182f73f2013-12-05 16:48:06 -080026import android.os.IBinder;
27import android.os.ServiceManager;
Jeff Sharkeybb4988a2017-02-23 17:31:39 -070028import android.os.UserManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -080029
Felipe Lemee5434c32019-08-13 09:28:33 -070030import com.android.server.pm.UserManagerService;
31
Adam Lesinski182f73f2013-12-05 16:48:06 -080032/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080033 * The base class for services running in the system process. Override and implement
34 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080035 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080036 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080037 * </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 Hackborna750a632015-06-16 17:18:23 -070046 * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
Adam Lesinskib102b2c2013-12-20 11:46:14 -080047 * 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 Brownb880d882014-02-10 19:47:07 -080050 * </ul><p>
51 * NOTE: All lifecycle methods are called from the system server's main looper thread.
52 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080053 *
54 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080055 */
56public abstract class SystemService {
Felipe Leme501a5142019-08-15 16:23:47 -070057
58 // TODO(b/133242016) STOPSHIP: change to false before R ships
59 protected static final boolean DEBUG_USER = true;
60
Adam Lesinski182f73f2013-12-05 16:48:06 -080061 /*
62 * Boot Phases
63 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080064 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
Adam Lesinski2cb6c602014-02-14 17:19:56 -080065
66 /**
67 * After receiving this boot phase, services can obtain lock settings data.
68 */
Amith Yamasani91588252013-11-22 08:25:26 -080069 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080070
71 /**
72 * After receiving this boot phase, services can safely call into core system services
73 * such as the PowerManager or PackageManager.
74 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080075 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080076
77 /**
Daichi Hironoedfcb002017-10-10 17:22:58 +090078 * After receiving this boot phase, services can safely call into device specific services.
79 */
80 public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
81
82 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080083 * After receiving this boot phase, services can broadcast Intents.
84 */
85 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
86
87 /**
88 * After receiving this boot phase, services can start/bind to third party apps.
89 * Apps will be able to make Binder calls into services at this point.
90 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080091 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080092
93 /**
Jeff Brown6d2a9492014-08-07 19:06:49 -070094 * After receiving this boot phase, services can allow user interaction with the device.
95 * This phase occurs when boot has completed and the home application has started.
96 * System services may prefer to listen to this phase rather than registering a
97 * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
Adam Lesinski2cb6c602014-02-14 17:19:56 -080098 */
Jeff Brown6d2a9492014-08-07 19:06:49 -070099 public static final int PHASE_BOOT_COMPLETED = 1000;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800100
Jeff Brownb880d882014-02-10 19:47:07 -0800101 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102
Jeff Brownb880d882014-02-10 19:47:07 -0800103 /**
104 * Initializes the system service.
105 * <p>
106 * Subclasses must define a single argument constructor that accepts the context
107 * and passes it to super.
108 * </p>
109 *
110 * @param context The system server context.
111 */
112 public SystemService(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800113 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800114 }
115
Jeff Brownb880d882014-02-10 19:47:07 -0800116 /**
117 * Gets the system context.
118 */
119 public final Context getContext() {
120 return mContext;
121 }
122
123 /**
Adam Lesinskia82b6262017-03-21 16:56:17 -0700124 * Get the system UI context. This context is to be used for displaying UI. It is themable,
125 * which means resources can be overridden at runtime. Do not use to retrieve properties that
126 * configure the behavior of the device that is not UX related.
127 */
128 public final Context getUiContext() {
129 // This has already been set up by the time any SystemServices are created.
130 return ActivityThread.currentActivityThread().getSystemUiContext();
131 }
132
133 /**
Jeff Brownb880d882014-02-10 19:47:07 -0800134 * Returns true if the system is running in safe mode.
135 * TODO: we should define in which phase this becomes valid
136 */
Amith Yamasani91588252013-11-22 08:25:26 -0800137 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800138 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800139 }
140
Adam Lesinski182f73f2013-12-05 16:48:06 -0800141 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800142 * Called when the dependencies listed in the @Service class-annotation are available
143 * and after the chosen start phase.
144 * When this method returns, the service should be published.
145 */
146 public abstract void onStart();
147
148 /**
149 * Called on each phase of the boot process. Phases before the service's start phase
150 * (as defined in the @Service annotation) are never received.
151 *
152 * @param phase The current boot phase.
153 */
154 public void onBootPhase(int phase) {}
155
156 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700157 * Checks if the service should be available for the given user.
158 *
159 * <p>By default returns {@code true}, but subclasses should extend for optimization, if they
160 * don't support some types (like headless system user).
161 */
162 public boolean isSupported(@NonNull UserInfo userInfo) {
163 return true;
164 }
165
166 /**
167 * @deprecated subclasses should extend {@link #onStartUser(UserInfo)} instead (which by default
Felipe Lemee5434c32019-08-13 09:28:33 -0700168 * calls this method).
169 */
170 @Deprecated
171 public void onStartUser(@UserIdInt int userHandle) {}
172
173 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700174 * Called when a new user is starting, for system services to initialize any per-user
175 * state they maintain for running users.
Felipe Lemee5434c32019-08-13 09:28:33 -0700176 *
Felipe Leme501a5142019-08-15 16:23:47 -0700177 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
178 * user.
179 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700180 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
181 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700182 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700183 public void onStartUser(@NonNull UserInfo userInfo) {
184 onStartUser(userInfo.id);
185 }
186
187 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700188 * @deprecated subclasses should extend {@link #onUnlockUser(UserInfo)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700189 * default calls this method).
190 */
191 @Deprecated
192 public void onUnlockUser(@UserIdInt int userHandle) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700193
194 /**
Jeff Sharkeybb4988a2017-02-23 17:31:39 -0700195 * Called when an existing user is in the process of being unlocked. This
196 * means the credential-encrypted storage for that user is now available,
197 * and encryption-aware component filtering is no longer in effect.
198 * <p>
199 * While dispatching this event to services, the user is in the
200 * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished
201 * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state.
202 * Code written inside system services should use
203 * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of
204 * these states.
Felipe Leme501a5142019-08-15 16:23:47 -0700205 * <p>
206 * This method is only called when the service {@link #isSupported(UserInfo) supports} this
207 * user.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700208 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700209 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
210 * referenced by {@link UserManagerService} and hence should not be modified.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700211 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700212 public void onUnlockUser(@NonNull UserInfo userInfo) {
213 onUnlockUser(userInfo.id);
214 }
215
216 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700217 * @deprecated subclasses should extend {@link #onSwitchUser(UserInfo, UserInfo)} instead
218 * (which by default calls this method).
Felipe Lemee5434c32019-08-13 09:28:33 -0700219 */
220 @Deprecated
221 public void onSwitchUser(@UserIdInt int userHandle) {}
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700222
223 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700224 * Called when switching to a different foreground user, for system services that have
225 * special behavior for whichever user is currently in the foreground. This is called
226 * before any application processes are aware of the new user.
Felipe Lemee5434c32019-08-13 09:28:33 -0700227 *
Felipe Leme501a5142019-08-15 16:23:47 -0700228 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} either
229 * of the users ({@code from} or {@code to}).
230 *
231 * <b>NOTE: </b> both {@code from} and {@code to} are "live" objects
Felipe Lemee5434c32019-08-13 09:28:33 -0700232 * referenced by {@link UserManagerService} and hence should not be modified.
Felipe Leme501a5142019-08-15 16:23:47 -0700233 *
234 * @param from The information about the user being switched from.
235 * @param to The information about the user being switched from to.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700236 */
Felipe Leme501a5142019-08-15 16:23:47 -0700237 public void onSwitchUser(@NonNull UserInfo from, @NonNull UserInfo to) {
238 onSwitchUser(to.id);
Felipe Lemee5434c32019-08-13 09:28:33 -0700239 }
240
241 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700242 * @deprecated subclasses should extend {@link #onStopUser(UserInfo)} instead (which by default
Felipe Lemee5434c32019-08-13 09:28:33 -0700243 * calls this method).
244 */
245 @Deprecated
246 public void onStopUser(@UserIdInt int userHandle) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700247
248 /**
249 * Called when an existing user is stopping, for system services to finalize any per-user
250 * state they maintain for running users. This is called prior to sending the SHUTDOWN
251 * broadcast to the user; it is a good place to stop making use of any resources of that
252 * user (such as binding to a service running in the user).
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700253 *
Felipe Leme501a5142019-08-15 16:23:47 -0700254 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
255 * user.
256 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700257 * <p>NOTE: This is the last callback where the callee may access the target user's CE storage.
258 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700259 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
260 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700261 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700262 public void onStopUser(@NonNull UserInfo userInfo) {
263 onStopUser(userInfo.id);
264 }
265
266 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700267 * @deprecated subclasses should extend {@link #onCleanupUser(UserInfo)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700268 * default calls this method).
269 */
270 @Deprecated
271 public void onCleanupUser(@UserIdInt int userHandle) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700272
273 /**
274 * Called when an existing user is stopping, for system services to finalize any per-user
275 * state they maintain for running users. This is called after all application process
276 * teardown of the user is complete.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700277 *
Felipe Leme501a5142019-08-15 16:23:47 -0700278 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
279 * user.
280 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700281 * <p>NOTE: When this callback is called, the CE storage for the target user may not be
Felipe Leme501a5142019-08-15 16:23:47 -0700282 * accessible already. Use {@link #onStopUser(UserInfo)} instead if you need to access the CE
283 * storage.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700284 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700285 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
286 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700287 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700288 public void onCleanupUser(@NonNull UserInfo userInfo) {
289 onCleanupUser(userInfo.id);
290 }
Dianne Hackborn91097de2014-04-04 18:02:06 -0700291
292 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800293 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800294 *
295 * @param name the name of the new service
296 * @param service the service object
Adam Lesinski182f73f2013-12-05 16:48:06 -0800297 */
298 protected final void publishBinderService(String name, IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800299 publishBinderService(name, service, false);
300 }
301
302 /**
303 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800304 *
305 * @param name the name of the new service
306 * @param service the service object
307 * @param allowIsolated set to true to allow isolated sandboxed processes
308 * to access this service
Jeff Brown4ccb8232014-01-16 22:16:42 -0800309 */
310 protected final void publishBinderService(String name, IBinder service,
311 boolean allowIsolated) {
Vishnu Naire3e4d252018-03-01 11:26:57 -0800312 publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
313 }
314
315 /**
316 * Publish the service so it is accessible to other services and apps.
317 *
318 * @param name the name of the new service
319 * @param service the service object
320 * @param allowIsolated set to true to allow isolated sandboxed processes
321 * to access this service
322 * @param dumpPriority supported dump priority levels as a bitmask
323 */
324 protected final void publishBinderService(String name, IBinder service,
325 boolean allowIsolated, int dumpPriority) {
326 ServiceManager.addService(name, service, allowIsolated, dumpPriority);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800327 }
328
329 /**
330 * Get a binder service by its name.
331 */
332 protected final IBinder getBinderService(String name) {
333 return ServiceManager.getService(name);
334 }
335
336 /**
337 * Publish the service so it is only accessible to the system process.
338 */
339 protected final <T> void publishLocalService(Class<T> type, T service) {
340 LocalServices.addService(type, service);
341 }
342
343 /**
344 * Get a local service by interface.
345 */
346 protected final <T> T getLocalService(Class<T> type) {
347 return LocalServices.getService(type);
348 }
349
Jeff Brownb880d882014-02-10 19:47:07 -0800350 private SystemServiceManager getManager() {
351 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800352 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800353}