blob: 58731d27d97c94efc52c00d155a2f7f7a3d5d1bd [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
Adam Lesinskia82b6262017-03-21 16:56:17 -070019import android.app.ActivityThread;
Adam Lesinski182f73f2013-12-05 16:48:06 -080020import android.content.Context;
21import android.os.IBinder;
22import android.os.ServiceManager;
Jeff Sharkeybb4988a2017-02-23 17:31:39 -070023import android.os.UserManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -080024
25/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080026 * The base class for services running in the system process. Override and implement
27 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080028 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080029 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080030 * </p><ul>
31 * <li>The constructor is called and provided with the system {@link Context}
32 * to initialize the system service.
33 * <li>{@link #onStart()} is called to get the service running. The service should
34 * publish its binder interface at this point using
35 * {@link #publishBinderService(String, IBinder)}. It may also publish additional
36 * local interfaces that other services within the system server may use to access
37 * privileged internal functions.
38 * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
Dianne Hackborna750a632015-06-16 17:18:23 -070039 * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
Adam Lesinskib102b2c2013-12-20 11:46:14 -080040 * is an opportunity to do special work, like acquiring optional service dependencies,
41 * waiting to see if SafeMode is enabled, or registering with a service that gets
42 * started after this one.
Jeff Brownb880d882014-02-10 19:47:07 -080043 * </ul><p>
44 * NOTE: All lifecycle methods are called from the system server's main looper thread.
45 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080046 *
47 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080048 */
49public abstract class SystemService {
50 /*
51 * Boot Phases
52 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080053 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
Adam Lesinski2cb6c602014-02-14 17:19:56 -080054
55 /**
56 * After receiving this boot phase, services can obtain lock settings data.
57 */
Amith Yamasani91588252013-11-22 08:25:26 -080058 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080059
60 /**
61 * After receiving this boot phase, services can safely call into core system services
62 * such as the PowerManager or PackageManager.
63 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080064 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080065
66 /**
Daichi Hironoedfcb002017-10-10 17:22:58 +090067 * After receiving this boot phase, services can safely call into device specific services.
68 */
69 public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
70
71 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080072 * After receiving this boot phase, services can broadcast Intents.
73 */
74 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
75
76 /**
77 * After receiving this boot phase, services can start/bind to third party apps.
78 * Apps will be able to make Binder calls into services at this point.
79 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080080 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080081
82 /**
Jeff Brown6d2a9492014-08-07 19:06:49 -070083 * After receiving this boot phase, services can allow user interaction with the device.
84 * This phase occurs when boot has completed and the home application has started.
85 * System services may prefer to listen to this phase rather than registering a
86 * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
Adam Lesinski2cb6c602014-02-14 17:19:56 -080087 */
Jeff Brown6d2a9492014-08-07 19:06:49 -070088 public static final int PHASE_BOOT_COMPLETED = 1000;
Adam Lesinski182f73f2013-12-05 16:48:06 -080089
Jeff Brownb880d882014-02-10 19:47:07 -080090 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -080091
Jeff Brownb880d882014-02-10 19:47:07 -080092 /**
93 * Initializes the system service.
94 * <p>
95 * Subclasses must define a single argument constructor that accepts the context
96 * and passes it to super.
97 * </p>
98 *
99 * @param context The system server context.
100 */
101 public SystemService(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -0800102 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -0800103 }
104
Jeff Brownb880d882014-02-10 19:47:07 -0800105 /**
106 * Gets the system context.
107 */
108 public final Context getContext() {
109 return mContext;
110 }
111
112 /**
Adam Lesinskia82b6262017-03-21 16:56:17 -0700113 * Get the system UI context. This context is to be used for displaying UI. It is themable,
114 * which means resources can be overridden at runtime. Do not use to retrieve properties that
115 * configure the behavior of the device that is not UX related.
116 */
117 public final Context getUiContext() {
118 // This has already been set up by the time any SystemServices are created.
119 return ActivityThread.currentActivityThread().getSystemUiContext();
120 }
121
122 /**
Jeff Brownb880d882014-02-10 19:47:07 -0800123 * Returns true if the system is running in safe mode.
124 * TODO: we should define in which phase this becomes valid
125 */
Amith Yamasani91588252013-11-22 08:25:26 -0800126 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800127 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800128 }
129
Adam Lesinski182f73f2013-12-05 16:48:06 -0800130 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800131 * Called when the dependencies listed in the @Service class-annotation are available
132 * and after the chosen start phase.
133 * When this method returns, the service should be published.
134 */
135 public abstract void onStart();
136
137 /**
138 * Called on each phase of the boot process. Phases before the service's start phase
139 * (as defined in the @Service annotation) are never received.
140 *
141 * @param phase The current boot phase.
142 */
143 public void onBootPhase(int phase) {}
144
145 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700146 * Called when a new user is starting, for system services to initialize any per-user
147 * state they maintain for running users.
148 * @param userHandle The identifier of the user.
149 */
150 public void onStartUser(int userHandle) {}
151
152 /**
Jeff Sharkeybb4988a2017-02-23 17:31:39 -0700153 * Called when an existing user is in the process of being unlocked. This
154 * means the credential-encrypted storage for that user is now available,
155 * and encryption-aware component filtering is no longer in effect.
156 * <p>
157 * While dispatching this event to services, the user is in the
158 * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished
159 * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state.
160 * Code written inside system services should use
161 * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of
162 * these states.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700163 *
164 * @param userHandle The identifier of the user.
165 */
166 public void onUnlockUser(int userHandle) {}
167
168 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700169 * Called when switching to a different foreground user, for system services that have
170 * special behavior for whichever user is currently in the foreground. This is called
171 * before any application processes are aware of the new user.
172 * @param userHandle The identifier of the user.
173 */
174 public void onSwitchUser(int userHandle) {}
175
176 /**
177 * Called when an existing user is stopping, for system services to finalize any per-user
178 * state they maintain for running users. This is called prior to sending the SHUTDOWN
179 * broadcast to the user; it is a good place to stop making use of any resources of that
180 * user (such as binding to a service running in the user).
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700181 *
182 * <p>NOTE: This is the last callback where the callee may access the target user's CE storage.
183 *
Dianne Hackborn91097de2014-04-04 18:02:06 -0700184 * @param userHandle The identifier of the user.
185 */
186 public void onStopUser(int userHandle) {}
187
188 /**
189 * Called when an existing user is stopping, for system services to finalize any per-user
190 * state they maintain for running users. This is called after all application process
191 * teardown of the user is complete.
Makoto Onuki01ce92b2017-04-28 12:24:16 -0700192 *
193 * <p>NOTE: When this callback is called, the CE storage for the target user may not be
194 * accessible already. Use {@link #onStopUser} instead if you need to access the CE storage.
195 *
Dianne Hackborn91097de2014-04-04 18:02:06 -0700196 * @param userHandle The identifier of the user.
197 */
198 public void onCleanupUser(int userHandle) {}
199
200 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800201 * Publish the service so it is accessible to other services and apps.
202 */
203 protected final void publishBinderService(String name, IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800204 publishBinderService(name, service, false);
205 }
206
207 /**
208 * Publish the service so it is accessible to other services and apps.
209 */
210 protected final void publishBinderService(String name, IBinder service,
211 boolean allowIsolated) {
212 ServiceManager.addService(name, service, allowIsolated);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800213 }
214
215 /**
216 * Get a binder service by its name.
217 */
218 protected final IBinder getBinderService(String name) {
219 return ServiceManager.getService(name);
220 }
221
222 /**
223 * Publish the service so it is only accessible to the system process.
224 */
225 protected final <T> void publishLocalService(Class<T> type, T service) {
226 LocalServices.addService(type, service);
227 }
228
229 /**
230 * Get a local service by interface.
231 */
232 protected final <T> T getLocalService(Class<T> type) {
233 return LocalServices.getService(type);
234 }
235
Jeff Brownb880d882014-02-10 19:47:07 -0800236 private SystemServiceManager getManager() {
237 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800238 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800239}