blob: c5409f85bde3c78d58a7a9fb13b61a81cd2ef49b [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
Felipe Lemee87735b2019-12-17 09:58:40 -080032import java.io.PrintWriter;
33import java.util.ArrayList;
34import java.util.List;
35
Adam Lesinski182f73f2013-12-05 16:48:06 -080036/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080037 * The base class for services running in the system process. Override and implement
38 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080039 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080040 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080041 * </p><ul>
42 * <li>The constructor is called and provided with the system {@link Context}
43 * to initialize the system service.
44 * <li>{@link #onStart()} is called to get the service running. The service should
45 * publish its binder interface at this point using
46 * {@link #publishBinderService(String, IBinder)}. It may also publish additional
47 * local interfaces that other services within the system server may use to access
48 * privileged internal functions.
49 * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
Dianne Hackborna750a632015-06-16 17:18:23 -070050 * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
Adam Lesinskib102b2c2013-12-20 11:46:14 -080051 * is an opportunity to do special work, like acquiring optional service dependencies,
52 * waiting to see if SafeMode is enabled, or registering with a service that gets
53 * started after this one.
Jeff Brownb880d882014-02-10 19:47:07 -080054 * </ul><p>
55 * NOTE: All lifecycle methods are called from the system server's main looper thread.
56 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080057 *
58 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080059 */
60public abstract class SystemService {
Felipe Leme501a5142019-08-15 16:23:47 -070061
62 // TODO(b/133242016) STOPSHIP: change to false before R ships
63 protected static final boolean DEBUG_USER = true;
64
Adam Lesinski182f73f2013-12-05 16:48:06 -080065 /*
66 * Boot Phases
67 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080068 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
Adam Lesinski2cb6c602014-02-14 17:19:56 -080069
70 /**
71 * After receiving this boot phase, services can obtain lock settings data.
72 */
Amith Yamasani91588252013-11-22 08:25:26 -080073 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080074
75 /**
76 * After receiving this boot phase, services can safely call into core system services
77 * such as the PowerManager or PackageManager.
78 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080079 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080080
81 /**
Daichi Hironoedfcb002017-10-10 17:22:58 +090082 * After receiving this boot phase, services can safely call into device specific services.
83 */
84 public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
85
86 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080087 * After receiving this boot phase, services can broadcast Intents.
88 */
89 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
90
91 /**
92 * After receiving this boot phase, services can start/bind to third party apps.
93 * Apps will be able to make Binder calls into services at this point.
94 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080095 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080096
97 /**
Jeff Brown6d2a9492014-08-07 19:06:49 -070098 * After receiving this boot phase, services can allow user interaction with the device.
99 * This phase occurs when boot has completed and the home application has started.
100 * System services may prefer to listen to this phase rather than registering a
101 * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
Adam Lesinski2cb6c602014-02-14 17:19:56 -0800102 */
Jeff Brown6d2a9492014-08-07 19:06:49 -0700103 public static final int PHASE_BOOT_COMPLETED = 1000;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800104
Jeff Brownb880d882014-02-10 19:47:07 -0800105 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800106
Jeff Brownb880d882014-02-10 19:47:07 -0800107 /**
108 * Initializes the system service.
109 * <p>
110 * Subclasses must define a single argument constructor that accepts the context
111 * and passes it to super.
112 * </p>
113 *
114 * @param context The system server context.
115 */
116 public SystemService(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800117 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800118 }
119
Jeff Brownb880d882014-02-10 19:47:07 -0800120 /**
121 * Gets the system context.
122 */
123 public final Context getContext() {
124 return mContext;
125 }
126
127 /**
Adam Lesinskia82b6262017-03-21 16:56:17 -0700128 * Get the system UI context. This context is to be used for displaying UI. It is themable,
129 * which means resources can be overridden at runtime. Do not use to retrieve properties that
130 * configure the behavior of the device that is not UX related.
131 */
132 public final Context getUiContext() {
133 // This has already been set up by the time any SystemServices are created.
134 return ActivityThread.currentActivityThread().getSystemUiContext();
135 }
136
137 /**
Jeff Brownb880d882014-02-10 19:47:07 -0800138 * Returns true if the system is running in safe mode.
139 * TODO: we should define in which phase this becomes valid
140 */
Amith Yamasani91588252013-11-22 08:25:26 -0800141 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800142 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800143 }
144
Adam Lesinski182f73f2013-12-05 16:48:06 -0800145 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800146 * Called when the dependencies listed in the @Service class-annotation are available
147 * and after the chosen start phase.
148 * When this method returns, the service should be published.
149 */
150 public abstract void onStart();
151
152 /**
153 * Called on each phase of the boot process. Phases before the service's start phase
154 * (as defined in the @Service annotation) are never received.
155 *
156 * @param phase The current boot phase.
157 */
158 public void onBootPhase(int phase) {}
159
160 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700161 * Checks if the service should be available for the given user.
162 *
163 * <p>By default returns {@code true}, but subclasses should extend for optimization, if they
164 * don't support some types (like headless system user).
165 */
166 public boolean isSupported(@NonNull UserInfo userInfo) {
167 return true;
168 }
169
170 /**
Felipe Lemee87735b2019-12-17 09:58:40 -0800171 * Helper method used to dump which users are {@link #onStartUser(UserInfo) supported}.
172 */
173 protected void dumpSupportedUsers(@NonNull PrintWriter pw, @NonNull String prefix) {
174 final List<UserInfo> allUsers = UserManager.get(mContext).getUsers();
175 final List<Integer> supportedUsers = new ArrayList<>(allUsers.size());
176 for (UserInfo user : allUsers) {
177 supportedUsers.add(user.id);
178 }
179 if (allUsers.isEmpty()) {
180 pw.print(prefix); pw.println("No supported users");
181 } else {
182 final int size = supportedUsers.size();
183 pw.print(prefix); pw.print(size); pw.print(" supported user");
184 if (size > 1) pw.print("s");
185 pw.print(": "); pw.println(supportedUsers);
186 }
187 }
188
189 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700190 * @deprecated subclasses should extend {@link #onStartUser(UserInfo)} instead (which by default
Felipe Lemee5434c32019-08-13 09:28:33 -0700191 * calls this method).
192 */
193 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700194 public void onStartUser(@UserIdInt int userId) {}
Felipe Lemee5434c32019-08-13 09:28:33 -0700195
196 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700197 * Called when a new user is starting, for system services to initialize any per-user
198 * state they maintain for running users.
Felipe Lemee5434c32019-08-13 09:28:33 -0700199 *
Felipe Leme501a5142019-08-15 16:23:47 -0700200 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
201 * user.
202 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700203 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
204 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700205 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700206 public void onStartUser(@NonNull UserInfo userInfo) {
207 onStartUser(userInfo.id);
208 }
209
210 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700211 * @deprecated subclasses should extend {@link #onUnlockUser(UserInfo)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700212 * default calls this method).
213 */
214 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700215 public void onUnlockUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700216
217 /**
Jeff Sharkeybb4988a2017-02-23 17:31:39 -0700218 * Called when an existing user is in the process of being unlocked. This
219 * means the credential-encrypted storage for that user is now available,
220 * and encryption-aware component filtering is no longer in effect.
221 * <p>
222 * While dispatching this event to services, the user is in the
223 * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished
224 * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state.
225 * Code written inside system services should use
226 * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of
227 * these states.
Felipe Leme501a5142019-08-15 16:23:47 -0700228 * <p>
229 * This method is only called when the service {@link #isSupported(UserInfo) supports} this
230 * user.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700231 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700232 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
233 * referenced by {@link UserManagerService} and hence should not be modified.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700234 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700235 public void onUnlockUser(@NonNull UserInfo userInfo) {
236 onUnlockUser(userInfo.id);
237 }
238
239 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700240 * @deprecated subclasses should extend {@link #onSwitchUser(UserInfo, UserInfo)} instead
241 * (which by default calls this method).
Felipe Lemee5434c32019-08-13 09:28:33 -0700242 */
243 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700244 public void onSwitchUser(@UserIdInt int userId) {}
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700245
246 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700247 * Called when switching to a different foreground user, for system services that have
248 * special behavior for whichever user is currently in the foreground. This is called
249 * before any application processes are aware of the new user.
Felipe Lemee5434c32019-08-13 09:28:33 -0700250 *
Felipe Leme501a5142019-08-15 16:23:47 -0700251 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} either
252 * of the users ({@code from} or {@code to}).
253 *
254 * <b>NOTE: </b> both {@code from} and {@code to} are "live" objects
Felipe Lemee5434c32019-08-13 09:28:33 -0700255 * referenced by {@link UserManagerService} and hence should not be modified.
Felipe Leme501a5142019-08-15 16:23:47 -0700256 *
257 * @param from The information about the user being switched from.
258 * @param to The information about the user being switched from to.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700259 */
Felipe Leme501a5142019-08-15 16:23:47 -0700260 public void onSwitchUser(@NonNull UserInfo from, @NonNull UserInfo to) {
261 onSwitchUser(to.id);
Felipe Lemee5434c32019-08-13 09:28:33 -0700262 }
263
264 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700265 * @deprecated subclasses should extend {@link #onStopUser(UserInfo)} instead (which by default
Felipe Lemee5434c32019-08-13 09:28:33 -0700266 * calls this method).
267 */
268 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700269 public void onStopUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700270
271 /**
272 * Called when an existing user is stopping, for system services to finalize any per-user
273 * state they maintain for running users. This is called prior to sending the SHUTDOWN
274 * broadcast to the user; it is a good place to stop making use of any resources of that
275 * user (such as binding to a service running in the user).
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700276 *
Felipe Leme501a5142019-08-15 16:23:47 -0700277 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
278 * user.
279 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700280 * <p>NOTE: This is the last callback where the callee may access the target user's CE storage.
281 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700282 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
283 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700284 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700285 public void onStopUser(@NonNull UserInfo userInfo) {
286 onStopUser(userInfo.id);
287 }
288
289 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700290 * @deprecated subclasses should extend {@link #onCleanupUser(UserInfo)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700291 * default calls this method).
292 */
293 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700294 public void onCleanupUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700295
296 /**
297 * Called when an existing user is stopping, for system services to finalize any per-user
298 * state they maintain for running users. This is called after all application process
299 * teardown of the user is complete.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700300 *
Felipe Leme501a5142019-08-15 16:23:47 -0700301 * <p>This method is only called when the service {@link #isSupported(UserInfo) supports} this
302 * user.
303 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700304 * <p>NOTE: When this callback is called, the CE storage for the target user may not be
Felipe Leme501a5142019-08-15 16:23:47 -0700305 * accessible already. Use {@link #onStopUser(UserInfo)} instead if you need to access the CE
306 * storage.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700307 *
Felipe Lemee5434c32019-08-13 09:28:33 -0700308 * @param userInfo The information about the user. <b>NOTE: </b> this is a "live" object
309 * referenced by {@link UserManagerService} and hence should not be modified.
Dianne Hackborn91097de2014-04-04 18:02:06 -0700310 */
Felipe Lemee5434c32019-08-13 09:28:33 -0700311 public void onCleanupUser(@NonNull UserInfo userInfo) {
312 onCleanupUser(userInfo.id);
313 }
Dianne Hackborn91097de2014-04-04 18:02:06 -0700314
315 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800316 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800317 *
318 * @param name the name of the new service
319 * @param service the service object
Adam Lesinski182f73f2013-12-05 16:48:06 -0800320 */
321 protected final void publishBinderService(String name, IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800322 publishBinderService(name, service, false);
323 }
324
325 /**
326 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800327 *
328 * @param name the name of the new service
329 * @param service the service object
330 * @param allowIsolated set to true to allow isolated sandboxed processes
331 * to access this service
Jeff Brown4ccb8232014-01-16 22:16:42 -0800332 */
333 protected final void publishBinderService(String name, IBinder service,
334 boolean allowIsolated) {
Vishnu Naire3e4d252018-03-01 11:26:57 -0800335 publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
336 }
337
338 /**
339 * Publish the service so it is accessible to other services and apps.
340 *
341 * @param name the name of the new service
342 * @param service the service object
343 * @param allowIsolated set to true to allow isolated sandboxed processes
344 * to access this service
345 * @param dumpPriority supported dump priority levels as a bitmask
346 */
347 protected final void publishBinderService(String name, IBinder service,
348 boolean allowIsolated, int dumpPriority) {
349 ServiceManager.addService(name, service, allowIsolated, dumpPriority);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800350 }
351
352 /**
353 * Get a binder service by its name.
354 */
355 protected final IBinder getBinderService(String name) {
356 return ServiceManager.getService(name);
357 }
358
359 /**
360 * Publish the service so it is only accessible to the system process.
361 */
362 protected final <T> void publishLocalService(Class<T> type, T service) {
363 LocalServices.addService(type, service);
364 }
365
366 /**
367 * Get a local service by interface.
368 */
369 protected final <T> T getLocalService(Class<T> type) {
370 return LocalServices.getService(type);
371 }
372
Jeff Brownb880d882014-02-10 19:47:07 -0800373 private SystemServiceManager getManager() {
374 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800375 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800376}