blob: f46b9ae3e8fb199d53ded2096ac5317af6f74cd3 [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
Makoto Onukic8815902020-01-08 13:55:25 -080021import android.annotation.IntDef;
Felipe Lemee5434c32019-08-13 09:28:33 -070022import android.annotation.NonNull;
Makoto Onukic8815902020-01-08 13:55:25 -080023import android.annotation.Nullable;
Felipe Lemee5434c32019-08-13 09:28:33 -070024import android.annotation.UserIdInt;
Adam Lesinskia82b6262017-03-21 16:56:17 -070025import android.app.ActivityThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080026import android.content.Context;
Felipe Lemee5434c32019-08-13 09:28:33 -070027import android.content.pm.UserInfo;
Adam Lesinski182f73f2013-12-05 16:48:06 -080028import android.os.IBinder;
29import android.os.ServiceManager;
Makoto Onukic8815902020-01-08 13:55:25 -080030import android.os.UserHandle;
Jeff Sharkeybb4988a2017-02-23 17:31:39 -070031import android.os.UserManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -080032
Felipe Lemee5434c32019-08-13 09:28:33 -070033import com.android.server.pm.UserManagerService;
34
Felipe Lemee87735b2019-12-17 09:58:40 -080035import java.io.PrintWriter;
Makoto Onukic8815902020-01-08 13:55:25 -080036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Felipe Lemee87735b2019-12-17 09:58:40 -080038import java.util.ArrayList;
39import java.util.List;
40
Adam Lesinski182f73f2013-12-05 16:48:06 -080041/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080042 * The base class for services running in the system process. Override and implement
43 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080044 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080045 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080046 * </p><ul>
47 * <li>The constructor is called and provided with the system {@link Context}
48 * to initialize the system service.
49 * <li>{@link #onStart()} is called to get the service running. The service should
50 * publish its binder interface at this point using
51 * {@link #publishBinderService(String, IBinder)}. It may also publish additional
52 * local interfaces that other services within the system server may use to access
53 * privileged internal functions.
54 * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
Dianne Hackborna750a632015-06-16 17:18:23 -070055 * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
Adam Lesinskib102b2c2013-12-20 11:46:14 -080056 * is an opportunity to do special work, like acquiring optional service dependencies,
57 * waiting to see if SafeMode is enabled, or registering with a service that gets
58 * started after this one.
Jeff Brownb880d882014-02-10 19:47:07 -080059 * </ul><p>
60 * NOTE: All lifecycle methods are called from the system server's main looper thread.
61 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080062 *
63 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080064 */
Makoto Onukic8815902020-01-08 13:55:25 -080065//@SystemApi(client = Client.MODULE_LIBRARIES, process = Process.SYSTEM_SERVER)
Adam Lesinski182f73f2013-12-05 16:48:06 -080066public abstract class SystemService {
Felipe Leme501a5142019-08-15 16:23:47 -070067
Makoto Onukic8815902020-01-08 13:55:25 -080068 /** @hide */
Felipe Leme501a5142019-08-15 16:23:47 -070069 // TODO(b/133242016) STOPSHIP: change to false before R ships
70 protected static final boolean DEBUG_USER = true;
71
Adam Lesinski182f73f2013-12-05 16:48:06 -080072 /*
Makoto Onukic8815902020-01-08 13:55:25 -080073 * The earliest boot phase the system send to system services on boot.
Adam Lesinski182f73f2013-12-05 16:48:06 -080074 */
Makoto Onukic8815902020-01-08 13:55:25 -080075 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080076
77 /**
78 * After receiving this boot phase, services can obtain lock settings data.
79 */
Amith Yamasani91588252013-11-22 08:25:26 -080080 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080081
82 /**
83 * After receiving this boot phase, services can safely call into core system services
84 * such as the PowerManager or PackageManager.
85 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080086 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080087
88 /**
Daichi Hironoedfcb002017-10-10 17:22:58 +090089 * After receiving this boot phase, services can safely call into device specific services.
90 */
91 public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
92
93 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080094 * After receiving this boot phase, services can broadcast Intents.
95 */
96 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
97
98 /**
99 * After receiving this boot phase, services can start/bind to third party apps.
100 * Apps will be able to make Binder calls into services at this point.
101 */
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -0800103
104 /**
Jeff Brown6d2a9492014-08-07 19:06:49 -0700105 * After receiving this boot phase, services can allow user interaction with the device.
106 * This phase occurs when boot has completed and the home application has started.
107 * System services may prefer to listen to this phase rather than registering a
Makoto Onukic8815902020-01-08 13:55:25 -0800108 * broadcast receiver for {@link android.content.Intent#ACTION_LOCKED_BOOT_COMPLETED}
109 * to reduce overall latency.
Adam Lesinski2cb6c602014-02-14 17:19:56 -0800110 */
Jeff Brown6d2a9492014-08-07 19:06:49 -0700111 public static final int PHASE_BOOT_COMPLETED = 1000;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800112
Makoto Onukic8815902020-01-08 13:55:25 -0800113 /** @hide */
114 @IntDef(flag = true, prefix = { "PHASE_" }, value = {
115 PHASE_WAIT_FOR_DEFAULT_DISPLAY,
116 PHASE_LOCK_SETTINGS_READY,
117 PHASE_SYSTEM_SERVICES_READY,
118 PHASE_DEVICE_SPECIFIC_SERVICES_READY,
119 PHASE_ACTIVITY_MANAGER_READY,
120 PHASE_THIRD_PARTY_APPS_CAN_START,
121 PHASE_BOOT_COMPLETED
122 })
123 @Retention(RetentionPolicy.SOURCE)
124 public @interface BootPhase {}
125
Jeff Brownb880d882014-02-10 19:47:07 -0800126 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800127
Jeff Brownb880d882014-02-10 19:47:07 -0800128 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800129 * Class representing user in question in the lifecycle callbacks.
130 * @hide
131 */
132 //@SystemApi(client = Client.MODULE_LIBRARIES, process = Process.SYSTEM_SERVER)
133 public static final class TargetUser {
134 @NonNull
135 private final UserInfo mUserInfo;
136
137 /** @hide */
138 public TargetUser(@NonNull UserInfo userInfo) {
139 mUserInfo = userInfo;
140 }
141
142 /**
143 * @return The information about the user. <b>NOTE: </b> this is a "live" object
144 * referenced by {@link UserManagerService} and hence should not be modified.
145 *
146 * @hide
147 */
148 @NonNull
149 public UserInfo getUserInfo() {
150 return mUserInfo;
151 }
152
153 /**
154 * @return the target {@link UserHandle}.
155 */
156 @NonNull
157 public UserHandle getUserHandle() {
158 return mUserInfo.getUserHandle();
159 }
160
161 /**
162 * @return the integer user id
163 *
164 * @hide
165 */
166 public int getUserIdentifier() {
167 return mUserInfo.id;
168 }
169 }
170
171 /**
Jeff Brownb880d882014-02-10 19:47:07 -0800172 * Initializes the system service.
173 * <p>
174 * Subclasses must define a single argument constructor that accepts the context
175 * and passes it to super.
176 * </p>
177 *
178 * @param context The system server context.
179 */
Makoto Onukic8815902020-01-08 13:55:25 -0800180 public SystemService(@NonNull Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800181 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800182 }
183
Jeff Brownb880d882014-02-10 19:47:07 -0800184 /**
185 * Gets the system context.
186 */
Makoto Onukic8815902020-01-08 13:55:25 -0800187 @NonNull
Jeff Brownb880d882014-02-10 19:47:07 -0800188 public final Context getContext() {
189 return mContext;
190 }
191
192 /**
Adam Lesinskia82b6262017-03-21 16:56:17 -0700193 * Get the system UI context. This context is to be used for displaying UI. It is themable,
194 * which means resources can be overridden at runtime. Do not use to retrieve properties that
195 * configure the behavior of the device that is not UX related.
Makoto Onukic8815902020-01-08 13:55:25 -0800196 *
197 * @hide
Adam Lesinskia82b6262017-03-21 16:56:17 -0700198 */
199 public final Context getUiContext() {
200 // This has already been set up by the time any SystemServices are created.
201 return ActivityThread.currentActivityThread().getSystemUiContext();
202 }
203
204 /**
Jeff Brownb880d882014-02-10 19:47:07 -0800205 * Returns true if the system is running in safe mode.
206 * TODO: we should define in which phase this becomes valid
Makoto Onukic8815902020-01-08 13:55:25 -0800207 *
208 * @hide
Jeff Brownb880d882014-02-10 19:47:07 -0800209 */
Amith Yamasani91588252013-11-22 08:25:26 -0800210 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800211 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800212 }
213
Adam Lesinski182f73f2013-12-05 16:48:06 -0800214 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800215 * Called when the system service should publish a binder service using
216 * {@link #publishBinderService(String, IBinder).}
Adam Lesinski182f73f2013-12-05 16:48:06 -0800217 */
218 public abstract void onStart();
219
220 /**
221 * Called on each phase of the boot process. Phases before the service's start phase
222 * (as defined in the @Service annotation) are never received.
223 *
224 * @param phase The current boot phase.
225 */
Makoto Onukic8815902020-01-08 13:55:25 -0800226 public void onBootPhase(@BootPhase int phase) {}
Adam Lesinski182f73f2013-12-05 16:48:06 -0800227
228 /**
Felipe Leme501a5142019-08-15 16:23:47 -0700229 * Checks if the service should be available for the given user.
230 *
231 * <p>By default returns {@code true}, but subclasses should extend for optimization, if they
232 * don't support some types (like headless system user).
233 */
Makoto Onukic8815902020-01-08 13:55:25 -0800234 public boolean isSupportedUser(@NonNull TargetUser user) {
Felipe Leme501a5142019-08-15 16:23:47 -0700235 return true;
236 }
237
238 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800239 * Helper method used to dump which users are {@link #onStartUser(TargetUser) supported}.
240 *
241 * @hide
Felipe Lemee87735b2019-12-17 09:58:40 -0800242 */
243 protected void dumpSupportedUsers(@NonNull PrintWriter pw, @NonNull String prefix) {
244 final List<UserInfo> allUsers = UserManager.get(mContext).getUsers();
245 final List<Integer> supportedUsers = new ArrayList<>(allUsers.size());
246 for (UserInfo user : allUsers) {
247 supportedUsers.add(user.id);
248 }
249 if (allUsers.isEmpty()) {
250 pw.print(prefix); pw.println("No supported users");
251 } else {
252 final int size = supportedUsers.size();
253 pw.print(prefix); pw.print(size); pw.print(" supported user");
254 if (size > 1) pw.print("s");
255 pw.print(": "); pw.println(supportedUsers);
256 }
257 }
258
259 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800260 * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead
261 * (which by default calls this method).
262 *
263 * @hide
Felipe Lemee5434c32019-08-13 09:28:33 -0700264 */
265 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700266 public void onStartUser(@UserIdInt int userId) {}
Felipe Lemee5434c32019-08-13 09:28:33 -0700267
268 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800269 * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead
270 * (which by default calls this method).
Felipe Lemee5434c32019-08-13 09:28:33 -0700271 *
Makoto Onukic8815902020-01-08 13:55:25 -0800272 * @hide
Dianne Hackborn91097de2014-04-04 18:02:06 -0700273 */
Makoto Onukic8815902020-01-08 13:55:25 -0800274 @Deprecated
Felipe Lemee5434c32019-08-13 09:28:33 -0700275 public void onStartUser(@NonNull UserInfo userInfo) {
276 onStartUser(userInfo.id);
277 }
278
279 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800280 * Called when a new user is starting, for system services to initialize any per-user
281 * state they maintain for running users.
282 *
283 * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports}
284 * this user.
285 *
286 * @param user target user
287 */
288 public void onStartUser(@NonNull TargetUser user) {
289 onStartUser(user.getUserInfo());
290 }
291
292 /**
293 * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700294 * default calls this method).
Makoto Onukic8815902020-01-08 13:55:25 -0800295 *
296 * @hide
Felipe Lemee5434c32019-08-13 09:28:33 -0700297 */
298 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700299 public void onUnlockUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700300
301 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800302 * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by
303 * default calls this method).
304 *
305 * @hide
306 */
307 @Deprecated
308 public void onUnlockUser(@NonNull UserInfo userInfo) {
309 onUnlockUser(userInfo.id);
310 }
311
312 /**
Jeff Sharkeybb4988a2017-02-23 17:31:39 -0700313 * Called when an existing user is in the process of being unlocked. This
314 * means the credential-encrypted storage for that user is now available,
315 * and encryption-aware component filtering is no longer in effect.
316 * <p>
317 * While dispatching this event to services, the user is in the
318 * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished
319 * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state.
320 * Code written inside system services should use
321 * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of
322 * these states.
Felipe Leme501a5142019-08-15 16:23:47 -0700323 * <p>
Makoto Onukic8815902020-01-08 13:55:25 -0800324 * This method is only called when the service {@link #isSupportedUser(TargetUser) supports}
325 * this user.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700326 *
Makoto Onukic8815902020-01-08 13:55:25 -0800327 * @param user target user
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700328 */
Makoto Onukic8815902020-01-08 13:55:25 -0800329 public void onUnlockUser(@NonNull TargetUser user) {
330 onUnlockUser(user.getUserInfo());
Felipe Lemee5434c32019-08-13 09:28:33 -0700331 }
332
333 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800334 * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead
Felipe Leme501a5142019-08-15 16:23:47 -0700335 * (which by default calls this method).
Makoto Onukic8815902020-01-08 13:55:25 -0800336 *
337 * @hide
Felipe Lemee5434c32019-08-13 09:28:33 -0700338 */
339 @Deprecated
Makoto Onukic8815902020-01-08 13:55:25 -0800340 public void onSwitchUser(@UserIdInt int toUserId) {}
341
342 /**
343 * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead
344 * (which by default calls this method).
345 *
346 * @hide
347 */
348 @Deprecated
349 public void onSwitchUser(@Nullable UserInfo from, @NonNull UserInfo to) {
350 onSwitchUser(to.id);
351 }
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700352
353 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700354 * Called when switching to a different foreground user, for system services that have
355 * special behavior for whichever user is currently in the foreground. This is called
356 * before any application processes are aware of the new user.
Felipe Lemee5434c32019-08-13 09:28:33 -0700357 *
Makoto Onukic8815902020-01-08 13:55:25 -0800358 * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports}
359 * either of the users ({@code from} or {@code to}).
Felipe Leme501a5142019-08-15 16:23:47 -0700360 *
361 * <b>NOTE: </b> both {@code from} and {@code to} are "live" objects
Felipe Lemee5434c32019-08-13 09:28:33 -0700362 * referenced by {@link UserManagerService} and hence should not be modified.
Felipe Leme501a5142019-08-15 16:23:47 -0700363 *
Makoto Onukic8815902020-01-08 13:55:25 -0800364 * @param from the user switching from
365 * @param to the user switching to
Dianne Hackborn91097de2014-04-04 18:02:06 -0700366 */
Makoto Onukic8815902020-01-08 13:55:25 -0800367 public void onSwitchUser(@Nullable TargetUser from, @NonNull TargetUser to) {
368 onSwitchUser((from == null ? null : from.getUserInfo()), to.getUserInfo());
Felipe Lemee5434c32019-08-13 09:28:33 -0700369 }
370
371 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800372 * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead
373 * (which by default calls this method).
374 *
375 * @hide
Felipe Lemee5434c32019-08-13 09:28:33 -0700376 */
377 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700378 public void onStopUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700379
380 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800381 * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead
382 * (which by default calls this method).
383 *
384 * @hide
385 */
386 @Deprecated
387 public void onStopUser(@NonNull UserInfo user) {
388 onStopUser(user.id);
389
390 }
391
392 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700393 * Called when an existing user is stopping, for system services to finalize any per-user
394 * state they maintain for running users. This is called prior to sending the SHUTDOWN
395 * broadcast to the user; it is a good place to stop making use of any resources of that
396 * user (such as binding to a service running in the user).
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700397 *
Makoto Onukic8815902020-01-08 13:55:25 -0800398 * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports}
399 * this user.
Felipe Leme501a5142019-08-15 16:23:47 -0700400 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700401 * <p>NOTE: This is the last callback where the callee may access the target user's CE storage.
402 *
Makoto Onukic8815902020-01-08 13:55:25 -0800403 * @param user target user
Dianne Hackborn91097de2014-04-04 18:02:06 -0700404 */
Makoto Onukic8815902020-01-08 13:55:25 -0800405 public void onStopUser(@NonNull TargetUser user) {
406 onStopUser(user.getUserInfo());
Felipe Lemee5434c32019-08-13 09:28:33 -0700407 }
408
409 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800410 * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by
Felipe Lemee5434c32019-08-13 09:28:33 -0700411 * default calls this method).
Makoto Onukic8815902020-01-08 13:55:25 -0800412 *
413 * @hide
Felipe Lemee5434c32019-08-13 09:28:33 -0700414 */
415 @Deprecated
Bookatzf56f2582019-09-04 16:06:41 -0700416 public void onCleanupUser(@UserIdInt int userId) {}
Dianne Hackborn91097de2014-04-04 18:02:06 -0700417
418 /**
Makoto Onukic8815902020-01-08 13:55:25 -0800419 * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by
420 * default calls this method).
421 *
422 * @hide
423 */
424 @Deprecated
425 public void onCleanupUser(@NonNull UserInfo user) {
426 onCleanupUser(user.id);
427 }
428
429 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700430 * Called when an existing user is stopping, for system services to finalize any per-user
431 * state they maintain for running users. This is called after all application process
432 * teardown of the user is complete.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700433 *
Makoto Onukic8815902020-01-08 13:55:25 -0800434 * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports}
435 * this user.
Felipe Leme501a5142019-08-15 16:23:47 -0700436 *
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700437 * <p>NOTE: When this callback is called, the CE storage for the target user may not be
Makoto Onukic8815902020-01-08 13:55:25 -0800438 * accessible already. Use {@link #onStopUser(TargetUser)} instead if you need to access the CE
Felipe Leme501a5142019-08-15 16:23:47 -0700439 * storage.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700440 *
Makoto Onukic8815902020-01-08 13:55:25 -0800441 * @param user target user
Dianne Hackborn91097de2014-04-04 18:02:06 -0700442 */
Makoto Onukic8815902020-01-08 13:55:25 -0800443 public void onCleanupUser(@NonNull TargetUser user) {
444 onCleanupUser(user.getUserInfo());
Felipe Lemee5434c32019-08-13 09:28:33 -0700445 }
Dianne Hackborn91097de2014-04-04 18:02:06 -0700446
447 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800448 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800449 *
450 * @param name the name of the new service
451 * @param service the service object
Adam Lesinski182f73f2013-12-05 16:48:06 -0800452 */
Makoto Onukic8815902020-01-08 13:55:25 -0800453 protected final void publishBinderService(@NonNull String name, @NonNull IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800454 publishBinderService(name, service, false);
455 }
456
457 /**
458 * Publish the service so it is accessible to other services and apps.
Vishnu Naire3e4d252018-03-01 11:26:57 -0800459 *
460 * @param name the name of the new service
461 * @param service the service object
462 * @param allowIsolated set to true to allow isolated sandboxed processes
463 * to access this service
Jeff Brown4ccb8232014-01-16 22:16:42 -0800464 */
Makoto Onukic8815902020-01-08 13:55:25 -0800465 protected final void publishBinderService(@NonNull String name, @NonNull IBinder service,
Jeff Brown4ccb8232014-01-16 22:16:42 -0800466 boolean allowIsolated) {
Vishnu Naire3e4d252018-03-01 11:26:57 -0800467 publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
468 }
469
470 /**
471 * Publish the service so it is accessible to other services and apps.
472 *
473 * @param name the name of the new service
474 * @param service the service object
475 * @param allowIsolated set to true to allow isolated sandboxed processes
476 * to access this service
477 * @param dumpPriority supported dump priority levels as a bitmask
Makoto Onukic8815902020-01-08 13:55:25 -0800478 *
479 * @hide
Vishnu Naire3e4d252018-03-01 11:26:57 -0800480 */
481 protected final void publishBinderService(String name, IBinder service,
482 boolean allowIsolated, int dumpPriority) {
483 ServiceManager.addService(name, service, allowIsolated, dumpPriority);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800484 }
485
486 /**
487 * Get a binder service by its name.
Makoto Onukic8815902020-01-08 13:55:25 -0800488 *
489 * @hide
Adam Lesinski182f73f2013-12-05 16:48:06 -0800490 */
491 protected final IBinder getBinderService(String name) {
492 return ServiceManager.getService(name);
493 }
494
495 /**
496 * Publish the service so it is only accessible to the system process.
Makoto Onukic8815902020-01-08 13:55:25 -0800497 *
498 * @hide
Adam Lesinski182f73f2013-12-05 16:48:06 -0800499 */
500 protected final <T> void publishLocalService(Class<T> type, T service) {
501 LocalServices.addService(type, service);
502 }
503
504 /**
505 * Get a local service by interface.
Makoto Onukic8815902020-01-08 13:55:25 -0800506 *
507 * @hide
Adam Lesinski182f73f2013-12-05 16:48:06 -0800508 */
509 protected final <T> T getLocalService(Class<T> type) {
510 return LocalServices.getService(type);
511 }
512
Jeff Brownb880d882014-02-10 19:47:07 -0800513 private SystemServiceManager getManager() {
514 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800515 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800516}