blob: 421d5a6ab964b7561ee3e2ada78f3e7928e25a1d [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
19import android.content.Context;
20import android.os.IBinder;
21import android.os.ServiceManager;
Jeff Sharkeybb4988a2017-02-23 17:31:39 -070022import android.os.UserManager;
Adam Lesinski182f73f2013-12-05 16:48:06 -080023
24/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080025 * The base class for services running in the system process. Override and implement
26 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080027 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080028 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080029 * </p><ul>
30 * <li>The constructor is called and provided with the system {@link Context}
31 * to initialize the system service.
32 * <li>{@link #onStart()} is called to get the service running. The service should
33 * publish its binder interface at this point using
34 * {@link #publishBinderService(String, IBinder)}. It may also publish additional
35 * local interfaces that other services within the system server may use to access
36 * privileged internal functions.
37 * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
Dianne Hackborna750a632015-06-16 17:18:23 -070038 * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
Adam Lesinskib102b2c2013-12-20 11:46:14 -080039 * is an opportunity to do special work, like acquiring optional service dependencies,
40 * waiting to see if SafeMode is enabled, or registering with a service that gets
41 * started after this one.
Jeff Brownb880d882014-02-10 19:47:07 -080042 * </ul><p>
43 * NOTE: All lifecycle methods are called from the system server's main looper thread.
44 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080045 *
46 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080047 */
48public abstract class SystemService {
49 /*
50 * Boot Phases
51 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080052 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
Adam Lesinski2cb6c602014-02-14 17:19:56 -080053
54 /**
55 * After receiving this boot phase, services can obtain lock settings data.
56 */
Amith Yamasani91588252013-11-22 08:25:26 -080057 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080058
59 /**
60 * After receiving this boot phase, services can safely call into core system services
61 * such as the PowerManager or PackageManager.
62 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080063 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080064
65 /**
66 * After receiving this boot phase, services can broadcast Intents.
67 */
68 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
69
70 /**
71 * After receiving this boot phase, services can start/bind to third party apps.
72 * Apps will be able to make Binder calls into services at this point.
73 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080074 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080075
76 /**
Jeff Brown6d2a9492014-08-07 19:06:49 -070077 * After receiving this boot phase, services can allow user interaction with the device.
78 * This phase occurs when boot has completed and the home application has started.
79 * System services may prefer to listen to this phase rather than registering a
80 * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
Adam Lesinski2cb6c602014-02-14 17:19:56 -080081 */
Jeff Brown6d2a9492014-08-07 19:06:49 -070082 public static final int PHASE_BOOT_COMPLETED = 1000;
Adam Lesinski182f73f2013-12-05 16:48:06 -080083
Jeff Brownb880d882014-02-10 19:47:07 -080084 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -080085
Jeff Brownb880d882014-02-10 19:47:07 -080086 /**
87 * Initializes the system service.
88 * <p>
89 * Subclasses must define a single argument constructor that accepts the context
90 * and passes it to super.
91 * </p>
92 *
93 * @param context The system server context.
94 */
95 public SystemService(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -080096 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -080097 }
98
Jeff Brownb880d882014-02-10 19:47:07 -080099 /**
100 * Gets the system context.
101 */
102 public final Context getContext() {
103 return mContext;
104 }
105
106 /**
107 * Returns true if the system is running in safe mode.
108 * TODO: we should define in which phase this becomes valid
109 */
Amith Yamasani91588252013-11-22 08:25:26 -0800110 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800111 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800112 }
113
Adam Lesinski182f73f2013-12-05 16:48:06 -0800114 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800115 * Called when the dependencies listed in the @Service class-annotation are available
116 * and after the chosen start phase.
117 * When this method returns, the service should be published.
118 */
119 public abstract void onStart();
120
121 /**
122 * Called on each phase of the boot process. Phases before the service's start phase
123 * (as defined in the @Service annotation) are never received.
124 *
125 * @param phase The current boot phase.
126 */
127 public void onBootPhase(int phase) {}
128
129 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700130 * Called when a new user is starting, for system services to initialize any per-user
131 * state they maintain for running users.
132 * @param userHandle The identifier of the user.
133 */
134 public void onStartUser(int userHandle) {}
135
136 /**
Jeff Sharkeybb4988a2017-02-23 17:31:39 -0700137 * Called when an existing user is in the process of being unlocked. This
138 * means the credential-encrypted storage for that user is now available,
139 * and encryption-aware component filtering is no longer in effect.
140 * <p>
141 * While dispatching this event to services, the user is in the
142 * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished
143 * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state.
144 * Code written inside system services should use
145 * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of
146 * these states.
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700147 *
148 * @param userHandle The identifier of the user.
149 */
150 public void onUnlockUser(int userHandle) {}
151
152 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700153 * Called when switching to a different foreground user, for system services that have
154 * special behavior for whichever user is currently in the foreground. This is called
155 * before any application processes are aware of the new user.
156 * @param userHandle The identifier of the user.
157 */
158 public void onSwitchUser(int userHandle) {}
159
160 /**
161 * Called when an existing user is stopping, for system services to finalize any per-user
162 * state they maintain for running users. This is called prior to sending the SHUTDOWN
163 * broadcast to the user; it is a good place to stop making use of any resources of that
164 * user (such as binding to a service running in the user).
165 * @param userHandle The identifier of the user.
166 */
167 public void onStopUser(int userHandle) {}
168
169 /**
170 * Called when an existing user is stopping, for system services to finalize any per-user
171 * state they maintain for running users. This is called after all application process
172 * teardown of the user is complete.
173 * @param userHandle The identifier of the user.
174 */
175 public void onCleanupUser(int userHandle) {}
176
177 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800178 * Publish the service so it is accessible to other services and apps.
179 */
180 protected final void publishBinderService(String name, IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800181 publishBinderService(name, service, false);
182 }
183
184 /**
185 * Publish the service so it is accessible to other services and apps.
186 */
187 protected final void publishBinderService(String name, IBinder service,
188 boolean allowIsolated) {
189 ServiceManager.addService(name, service, allowIsolated);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800190 }
191
192 /**
193 * Get a binder service by its name.
194 */
195 protected final IBinder getBinderService(String name) {
196 return ServiceManager.getService(name);
197 }
198
199 /**
200 * Publish the service so it is only accessible to the system process.
201 */
202 protected final <T> void publishLocalService(Class<T> type, T service) {
203 LocalServices.addService(type, service);
204 }
205
206 /**
207 * Get a local service by interface.
208 */
209 protected final <T> T getLocalService(Class<T> type) {
210 return LocalServices.getService(type);
211 }
212
Jeff Brownb880d882014-02-10 19:47:07 -0800213 private SystemServiceManager getManager() {
214 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800215 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800216}