blob: 43a05d0402a09c9e6aa78d41f66452d32e6b060c [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;
22
23/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080024 * The base class for services running in the system process. Override and implement
25 * the lifecycle event callback methods as needed.
Jeff Brownb880d882014-02-10 19:47:07 -080026 * <p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080027 * The lifecycle of a SystemService:
Jeff Brownb880d882014-02-10 19:47:07 -080028 * </p><ul>
29 * <li>The constructor is called and provided with the system {@link Context}
30 * to initialize the system service.
31 * <li>{@link #onStart()} is called to get the service running. The service should
32 * publish its binder interface at this point using
33 * {@link #publishBinderService(String, IBinder)}. It may also publish additional
34 * local interfaces that other services within the system server may use to access
35 * privileged internal functions.
36 * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases
Adam Lesinskib102b2c2013-12-20 11:46:14 -080037 * until {@link #PHASE_BOOT_COMPLETE} is sent, which is the last boot phase. Each phase
38 * is an opportunity to do special work, like acquiring optional service dependencies,
39 * waiting to see if SafeMode is enabled, or registering with a service that gets
40 * started after this one.
Jeff Brownb880d882014-02-10 19:47:07 -080041 * </ul><p>
42 * NOTE: All lifecycle methods are called from the system server's main looper thread.
43 * </p>
Adam Lesinskib102b2c2013-12-20 11:46:14 -080044 *
45 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080046 */
47public abstract class SystemService {
48 /*
49 * Boot Phases
50 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080051 public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
Adam Lesinski2cb6c602014-02-14 17:19:56 -080052
53 /**
54 * After receiving this boot phase, services can obtain lock settings data.
55 */
Amith Yamasani91588252013-11-22 08:25:26 -080056 public static final int PHASE_LOCK_SETTINGS_READY = 480;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080057
58 /**
59 * After receiving this boot phase, services can safely call into core system services
60 * such as the PowerManager or PackageManager.
61 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080062 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080063
64 /**
65 * After receiving this boot phase, services can broadcast Intents.
66 */
67 public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
68
69 /**
70 * After receiving this boot phase, services can start/bind to third party apps.
71 * Apps will be able to make Binder calls into services at this point.
72 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080073 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
Adam Lesinski2cb6c602014-02-14 17:19:56 -080074
75 /**
76 * After receiving this boot phase, services must have finished all boot-related work.
77 */
Adam Lesinski182f73f2013-12-05 16:48:06 -080078 public static final int PHASE_BOOT_COMPLETE = 1000;
79
Jeff Brownb880d882014-02-10 19:47:07 -080080 private final Context mContext;
Adam Lesinski182f73f2013-12-05 16:48:06 -080081
Jeff Brownb880d882014-02-10 19:47:07 -080082 /**
83 * Initializes the system service.
84 * <p>
85 * Subclasses must define a single argument constructor that accepts the context
86 * and passes it to super.
87 * </p>
88 *
89 * @param context The system server context.
90 */
91 public SystemService(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -080092 mContext = context;
Adam Lesinski182f73f2013-12-05 16:48:06 -080093 }
94
Jeff Brownb880d882014-02-10 19:47:07 -080095 /**
96 * Gets the system context.
97 */
98 public final Context getContext() {
99 return mContext;
100 }
101
102 /**
103 * Returns true if the system is running in safe mode.
104 * TODO: we should define in which phase this becomes valid
105 */
Amith Yamasani91588252013-11-22 08:25:26 -0800106 public final boolean isSafeMode() {
Jeff Brownb880d882014-02-10 19:47:07 -0800107 return getManager().isSafeMode();
Amith Yamasani91588252013-11-22 08:25:26 -0800108 }
109
Adam Lesinski182f73f2013-12-05 16:48:06 -0800110 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800111 * Called when the dependencies listed in the @Service class-annotation are available
112 * and after the chosen start phase.
113 * When this method returns, the service should be published.
114 */
115 public abstract void onStart();
116
117 /**
118 * Called on each phase of the boot process. Phases before the service's start phase
119 * (as defined in the @Service annotation) are never received.
120 *
121 * @param phase The current boot phase.
122 */
123 public void onBootPhase(int phase) {}
124
125 /**
Dianne Hackborn91097de2014-04-04 18:02:06 -0700126 * Called when a new user is starting, for system services to initialize any per-user
127 * state they maintain for running users.
128 * @param userHandle The identifier of the user.
129 */
130 public void onStartUser(int userHandle) {}
131
132 /**
133 * Called when switching to a different foreground user, for system services that have
134 * special behavior for whichever user is currently in the foreground. This is called
135 * before any application processes are aware of the new user.
136 * @param userHandle The identifier of the user.
137 */
138 public void onSwitchUser(int userHandle) {}
139
140 /**
141 * Called when an existing user is stopping, for system services to finalize any per-user
142 * state they maintain for running users. This is called prior to sending the SHUTDOWN
143 * broadcast to the user; it is a good place to stop making use of any resources of that
144 * user (such as binding to a service running in the user).
145 * @param userHandle The identifier of the user.
146 */
147 public void onStopUser(int userHandle) {}
148
149 /**
150 * Called when an existing user is stopping, for system services to finalize any per-user
151 * state they maintain for running users. This is called after all application process
152 * teardown of the user is complete.
153 * @param userHandle The identifier of the user.
154 */
155 public void onCleanupUser(int userHandle) {}
156
157 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800158 * Publish the service so it is accessible to other services and apps.
159 */
160 protected final void publishBinderService(String name, IBinder service) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800161 publishBinderService(name, service, false);
162 }
163
164 /**
165 * Publish the service so it is accessible to other services and apps.
166 */
167 protected final void publishBinderService(String name, IBinder service,
168 boolean allowIsolated) {
169 ServiceManager.addService(name, service, allowIsolated);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800170 }
171
172 /**
173 * Get a binder service by its name.
174 */
175 protected final IBinder getBinderService(String name) {
176 return ServiceManager.getService(name);
177 }
178
179 /**
180 * Publish the service so it is only accessible to the system process.
181 */
182 protected final <T> void publishLocalService(Class<T> type, T service) {
183 LocalServices.addService(type, service);
184 }
185
186 /**
187 * Get a local service by interface.
188 */
189 protected final <T> T getLocalService(Class<T> type) {
190 return LocalServices.getService(type);
191 }
192
Jeff Brownb880d882014-02-10 19:47:07 -0800193 private SystemServiceManager getManager() {
194 return LocalServices.getService(SystemServiceManager.class);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800195 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800196}