1 /*
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 
17 package com.android.server;
18 
19 import android.content.Context;
20 import android.os.IBinder;
21 import android.os.ServiceManager;
22 
23 /**
24  * The base class for services running in the system process. Override and implement
25  * the lifecycle event callback methods as needed.
26  * <p>
27  * The lifecycle of a SystemService:
28  * </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
37  * until {@link #PHASE_BOOT_COMPLETED} 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.
41  * </ul><p>
42  * NOTE: All lifecycle methods are called from the system server's main looper thread.
43  * </p>
44  *
45  * {@hide}
46  */
47 public abstract class SystemService {
48     /*
49      * Boot Phases
50      */
51     public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency?
52 
53     /**
54      * After receiving this boot phase, services can obtain lock settings data.
55      */
56     public static final int PHASE_LOCK_SETTINGS_READY = 480;
57 
58     /**
59      * After receiving this boot phase, services can safely call into core system services
60      * such as the PowerManager or PackageManager.
61      */
62     public static final int PHASE_SYSTEM_SERVICES_READY = 500;
63 
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      */
73     public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
74 
75     /**
76      * After receiving this boot phase, services can allow user interaction with the device.
77      * This phase occurs when boot has completed and the home application has started.
78      * System services may prefer to listen to this phase rather than registering a
79      * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency.
80      */
81     public static final int PHASE_BOOT_COMPLETED = 1000;
82 
83     private final Context mContext;
84 
85     /**
86      * Initializes the system service.
87      * <p>
88      * Subclasses must define a single argument constructor that accepts the context
89      * and passes it to super.
90      * </p>
91      *
92      * @param context The system server context.
93      */
SystemService(Context context)94     public SystemService(Context context) {
95         mContext = context;
96     }
97 
98     /**
99      * Gets the system context.
100      */
getContext()101     public final Context getContext() {
102         return mContext;
103     }
104 
105     /**
106      * Returns true if the system is running in safe mode.
107      * TODO: we should define in which phase this becomes valid
108      */
isSafeMode()109     public final boolean isSafeMode() {
110         return getManager().isSafeMode();
111     }
112 
113     /**
114      * Called when the dependencies listed in the @Service class-annotation are available
115      * and after the chosen start phase.
116      * When this method returns, the service should be published.
117      */
onStart()118     public abstract void onStart();
119 
120     /**
121      * Called on each phase of the boot process. Phases before the service's start phase
122      * (as defined in the @Service annotation) are never received.
123      *
124      * @param phase The current boot phase.
125      */
onBootPhase(int phase)126     public void onBootPhase(int phase) {}
127 
128     /**
129      * Called when a new user is starting, for system services to initialize any per-user
130      * state they maintain for running users.
131      * @param userHandle The identifier of the user.
132      */
onStartUser(int userHandle)133     public void onStartUser(int userHandle) {}
134 
135     /**
136      * Called when an existing user is unlocked. This means the
137      * credential-encrypted storage for that user is now available, and
138      * encryption-aware component filtering is no longer in effect.
139      *
140      * @param userHandle The identifier of the user.
141      */
onUnlockUser(int userHandle)142     public void onUnlockUser(int userHandle) {}
143 
144     /**
145      * Called when switching to a different foreground user, for system services that have
146      * special behavior for whichever user is currently in the foreground.  This is called
147      * before any application processes are aware of the new user.
148      * @param userHandle The identifier of the user.
149      */
onSwitchUser(int userHandle)150     public void onSwitchUser(int userHandle) {}
151 
152     /**
153      * Called when an existing user is stopping, for system services to finalize any per-user
154      * state they maintain for running users.  This is called prior to sending the SHUTDOWN
155      * broadcast to the user; it is a good place to stop making use of any resources of that
156      * user (such as binding to a service running in the user).
157      * @param userHandle The identifier of the user.
158      */
onStopUser(int userHandle)159     public void onStopUser(int userHandle) {}
160 
161     /**
162      * Called when an existing user is stopping, for system services to finalize any per-user
163      * state they maintain for running users.  This is called after all application process
164      * teardown of the user is complete.
165      * @param userHandle The identifier of the user.
166      */
onCleanupUser(int userHandle)167     public void onCleanupUser(int userHandle) {}
168 
169     /**
170      * Publish the service so it is accessible to other services and apps.
171      */
publishBinderService(String name, IBinder service)172     protected final void publishBinderService(String name, IBinder service) {
173         publishBinderService(name, service, false);
174     }
175 
176     /**
177      * Publish the service so it is accessible to other services and apps.
178      */
publishBinderService(String name, IBinder service, boolean allowIsolated)179     protected final void publishBinderService(String name, IBinder service,
180             boolean allowIsolated) {
181         ServiceManager.addService(name, service, allowIsolated);
182     }
183 
184     /**
185      * Get a binder service by its name.
186      */
getBinderService(String name)187     protected final IBinder getBinderService(String name) {
188         return ServiceManager.getService(name);
189     }
190 
191     /**
192      * Publish the service so it is only accessible to the system process.
193      */
publishLocalService(Class<T> type, T service)194     protected final <T> void publishLocalService(Class<T> type, T service) {
195         LocalServices.addService(type, service);
196     }
197 
198     /**
199      * Get a local service by interface.
200      */
getLocalService(Class<T> type)201     protected final <T> T getLocalService(Class<T> type) {
202         return LocalServices.getService(type);
203     }
204 
getManager()205     private SystemServiceManager getManager() {
206         return LocalServices.getService(SystemServiceManager.class);
207     }
208 }
209