blob: fb8a8154e13e455b8966fb21e5b67432a6d530cc [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;
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090020import android.os.Trace;
Adam Lesinski182f73f2013-12-05 16:48:06 -080021import android.util.Slog;
22
Jeff Brownb880d882014-02-10 19:47:07 -080023import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
Adam Lesinski182f73f2013-12-05 16:48:06 -080025import java.util.ArrayList;
26
27/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080028 * Manages creating, starting, and other lifecycle events of
Jeff Brownb880d882014-02-10 19:47:07 -080029 * {@link com.android.server.SystemService system services}.
Adam Lesinskib102b2c2013-12-20 11:46:14 -080030 *
31 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080032 */
33public class SystemServiceManager {
34 private static final String TAG = "SystemServiceManager";
35
36 private final Context mContext;
Amith Yamasani91588252013-11-22 08:25:26 -080037 private boolean mSafeMode;
Adam Lesinski182f73f2013-12-05 16:48:06 -080038
39 // Services that should receive lifecycle events.
40 private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
41
42 private int mCurrentPhase = -1;
43
44 public SystemServiceManager(Context context) {
45 mContext = context;
46 }
47
Jeff Brown6f357d32014-01-15 20:40:55 -080048 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080049 * Starts a service by class name.
Jeff Brown6f357d32014-01-15 20:40:55 -080050 *
Adam Lesinski2cb6c602014-02-14 17:19:56 -080051 * @return The service instance.
Jeff Brown6f357d32014-01-15 20:40:55 -080052 */
53 @SuppressWarnings("unchecked")
Jeff Brown2c43c332014-06-12 22:38:59 -070054 public SystemService startService(String className) {
55 final Class<SystemService> serviceClass;
56 try {
57 serviceClass = (Class<SystemService>)Class.forName(className);
58 } catch (ClassNotFoundException ex) {
59 Slog.i(TAG, "Starting " + className);
60 throw new RuntimeException("Failed to create service " + className
61 + ": service class not found, usually indicates that the caller should "
62 + "have called PackageManager.hasSystemFeature() to check whether the "
63 + "feature is available on this device before trying to start the "
64 + "services that implement it", ex);
65 }
66 return startService(serviceClass);
Amith Yamasani91588252013-11-22 08:25:26 -080067 }
68
Adam Lesinski182f73f2013-12-05 16:48:06 -080069 /**
70 * Creates and starts a system service. The class must be a subclass of
71 * {@link com.android.server.SystemService}.
72 *
73 * @param serviceClass A Java class that implements the SystemService interface.
Jeff Brown6f357d32014-01-15 20:40:55 -080074 * @return The service instance, never null.
Adam Lesinski182f73f2013-12-05 16:48:06 -080075 * @throws RuntimeException if the service fails to start.
76 */
Jeff Brown6f357d32014-01-15 20:40:55 -080077 @SuppressWarnings("unchecked")
78 public <T extends SystemService> T startService(Class<T> serviceClass) {
Adam Lesinski182f73f2013-12-05 16:48:06 -080079 try {
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090080 final String name = serviceClass.getName();
81 Slog.i(TAG, "Starting " + name);
82 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
Adam Lesinski182f73f2013-12-05 16:48:06 -080083
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090084 // Create the service.
85 if (!SystemService.class.isAssignableFrom(serviceClass)) {
86 throw new RuntimeException("Failed to create " + name
87 + ": service must extend " + SystemService.class.getName());
88 }
89 final T service;
90 try {
91 Constructor<T> constructor = serviceClass.getConstructor(Context.class);
92 service = constructor.newInstance(mContext);
93 } catch (InstantiationException ex) {
94 throw new RuntimeException("Failed to create service " + name
95 + ": service could not be instantiated", ex);
96 } catch (IllegalAccessException ex) {
97 throw new RuntimeException("Failed to create service " + name
98 + ": service must have a public constructor with a Context argument", ex);
99 } catch (NoSuchMethodException ex) {
100 throw new RuntimeException("Failed to create service " + name
101 + ": service must have a public constructor with a Context argument", ex);
102 } catch (InvocationTargetException ex) {
103 throw new RuntimeException("Failed to create service " + name
104 + ": service constructor threw an exception", ex);
105 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800106
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900107 // Register it.
108 mServices.add(service);
109
110 // Start it.
111 try {
112 service.onStart();
113 } catch (RuntimeException ex) {
114 throw new RuntimeException("Failed to start service " + name
115 + ": onStart threw an exception", ex);
116 }
117 return service;
118 } finally {
119 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800120 }
121 }
122
123 /**
124 * Starts the specified boot phase for all system services that have been started up to
125 * this point.
126 *
127 * @param phase The boot phase to start.
128 */
129 public void startBootPhase(final int phase) {
130 if (phase <= mCurrentPhase) {
131 throw new IllegalArgumentException("Next phase must be larger than previous");
132 }
133 mCurrentPhase = phase;
134
135 Slog.i(TAG, "Starting phase " + mCurrentPhase);
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900136 try {
137 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase " + phase);
138 final int serviceLen = mServices.size();
139 for (int i = 0; i < serviceLen; i++) {
140 final SystemService service = mServices.get(i);
Philip Cuadra4345c0d2016-08-17 18:02:24 -0700141 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, service.getClass().getName());
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900142 try {
143 service.onBootPhase(mCurrentPhase);
144 } catch (Exception ex) {
145 throw new RuntimeException("Failed to boot service "
146 + service.getClass().getName()
147 + ": onBootPhase threw an exception during phase "
148 + mCurrentPhase, ex);
149 }
Philip Cuadra4345c0d2016-08-17 18:02:24 -0700150 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800151 }
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900152 } finally {
153 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800154 }
155 }
156
Dianne Hackborn91097de2014-04-04 18:02:06 -0700157 public void startUser(final int userHandle) {
158 final int serviceLen = mServices.size();
159 for (int i = 0; i < serviceLen; i++) {
160 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700161 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStartUser "
162 + service.getClass().getName());
Dianne Hackborn91097de2014-04-04 18:02:06 -0700163 try {
164 service.onStartUser(userHandle);
165 } catch (Exception ex) {
166 Slog.wtf(TAG, "Failure reporting start of user " + userHandle
167 + " to service " + service.getClass().getName(), ex);
168 }
Philip Cuadraca3329c2016-06-24 12:02:06 -0700169 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700170 }
171 }
172
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700173 public void unlockUser(final int userHandle) {
174 final int serviceLen = mServices.size();
175 for (int i = 0; i < serviceLen; i++) {
176 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700177 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onUnlockUser "
178 + service.getClass().getName());
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700179 try {
180 service.onUnlockUser(userHandle);
181 } catch (Exception ex) {
182 Slog.wtf(TAG, "Failure reporting unlock of user " + userHandle
183 + " to service " + service.getClass().getName(), ex);
184 }
Philip Cuadraca3329c2016-06-24 12:02:06 -0700185 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700186 }
187 }
188
Dianne Hackborn91097de2014-04-04 18:02:06 -0700189 public void switchUser(final int userHandle) {
190 final int serviceLen = mServices.size();
191 for (int i = 0; i < serviceLen; i++) {
192 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700193 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onSwitchUser "
194 + service.getClass().getName());
Dianne Hackborn91097de2014-04-04 18:02:06 -0700195 try {
196 service.onSwitchUser(userHandle);
197 } catch (Exception ex) {
198 Slog.wtf(TAG, "Failure reporting switch of user " + userHandle
199 + " to service " + service.getClass().getName(), ex);
200 }
Philip Cuadraca3329c2016-06-24 12:02:06 -0700201 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700202 }
203 }
204
205 public void stopUser(final int userHandle) {
206 final int serviceLen = mServices.size();
207 for (int i = 0; i < serviceLen; i++) {
208 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700209 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStopUser "
210 + service.getClass().getName());
Dianne Hackborn91097de2014-04-04 18:02:06 -0700211 try {
212 service.onStopUser(userHandle);
213 } catch (Exception ex) {
214 Slog.wtf(TAG, "Failure reporting stop of user " + userHandle
215 + " to service " + service.getClass().getName(), ex);
216 }
Philip Cuadraca3329c2016-06-24 12:02:06 -0700217 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700218 }
219 }
220
221 public void cleanupUser(final int userHandle) {
222 final int serviceLen = mServices.size();
223 for (int i = 0; i < serviceLen; i++) {
224 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700225 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onCleanupUser "
226 + service.getClass().getName());
Dianne Hackborn91097de2014-04-04 18:02:06 -0700227 try {
228 service.onCleanupUser(userHandle);
229 } catch (Exception ex) {
230 Slog.wtf(TAG, "Failure reporting cleanup of user " + userHandle
231 + " to service " + service.getClass().getName(), ex);
232 }
Philip Cuadraca3329c2016-06-24 12:02:06 -0700233 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700234 }
235 }
236
Amith Yamasani91588252013-11-22 08:25:26 -0800237 /** Sets the safe mode flag for services to query. */
238 public void setSafeMode(boolean safeMode) {
239 mSafeMode = safeMode;
240 }
241
242 /**
243 * Returns whether we are booting into safe mode.
244 * @return safe mode flag
245 */
246 public boolean isSafeMode() {
247 return mSafeMode;
248 }
249
Adam Lesinski182f73f2013-12-05 16:48:06 -0800250 /**
251 * Outputs the state of this manager to the System log.
252 */
253 public void dump() {
254 StringBuilder builder = new StringBuilder();
255 builder.append("Current phase: ").append(mCurrentPhase).append("\n");
256 builder.append("Services:\n");
257 final int startedLen = mServices.size();
258 for (int i = 0; i < startedLen; i++) {
259 final SystemService service = mServices.get(i);
260 builder.append("\t")
261 .append(service.getClass().getSimpleName())
262 .append("\n");
263 }
264
265 Slog.e(TAG, builder.toString());
266 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800267}