blob: 37afb4f875418cbe7d6ce7895657b2d0d90aa37d [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/**
24 * System services respond to lifecycle events that help the services know what
25 */
26public abstract class SystemService {
27 /*
28 * Boot Phases
29 */
30 public static final int PHASE_SYSTEM_SERVICES_READY = 500;
31 public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
32 public static final int PHASE_BOOT_COMPLETE = 1000;
33
34 private SystemServiceManager mManager;
35 private Context mContext;
36
37 final void init(Context context, SystemServiceManager manager) {
38 mContext = context;
39 mManager = manager;
40 onCreate(context);
41 }
42
43 /**
44 * Services are not yet available. This is a good place to do setup work that does
45 * not require other services.
46 *
47 * @param context The system context.
48 */
49 public void onCreate(Context context) {}
50
51 /**
52 * Called when the dependencies listed in the @Service class-annotation are available
53 * and after the chosen start phase.
54 * When this method returns, the service should be published.
55 */
56 public abstract void onStart();
57
58 /**
59 * Called on each phase of the boot process. Phases before the service's start phase
60 * (as defined in the @Service annotation) are never received.
61 *
62 * @param phase The current boot phase.
63 */
64 public void onBootPhase(int phase) {}
65
66 /**
67 * Publish the service so it is accessible to other services and apps.
68 */
69 protected final void publishBinderService(String name, IBinder service) {
70 ServiceManager.addService(name, service);
71 }
72
73 /**
74 * Get a binder service by its name.
75 */
76 protected final IBinder getBinderService(String name) {
77 return ServiceManager.getService(name);
78 }
79
80 /**
81 * Publish the service so it is only accessible to the system process.
82 */
83 protected final <T> void publishLocalService(Class<T> type, T service) {
84 LocalServices.addService(type, service);
85 }
86
87 /**
88 * Get a local service by interface.
89 */
90 protected final <T> T getLocalService(Class<T> type) {
91 return LocalServices.getService(type);
92 }
93
94 public final Context getContext() {
95 return mContext;
96 }
97
98// /**
99// * Called when a new user has been created. If your service deals with multiple users, this
100// * method should be overridden.
101// *
102// * @param userHandle The user that was created.
103// */
104// public void onUserCreated(int userHandle) {
105// }
106//
107// /**
108// * Called when an existing user has started a new session. If your service deals with multiple
109// * users, this method should be overridden.
110// *
111// * @param userHandle The user who started a new session.
112// */
113// public void onUserStarted(int userHandle) {
114// }
115//
116// /**
117// * Called when a background user session has entered the foreground. If your service deals with
118// * multiple users, this method should be overridden.
119// *
120// * @param userHandle The user who's session entered the foreground.
121// */
122// public void onUserForeground(int userHandle) {
123// }
124//
125// /**
126// * Called when a foreground user session has entered the background. If your service deals with
127// * multiple users, this method should be overridden;
128// *
129// * @param userHandle The user who's session entered the background.
130// */
131// public void onUserBackground(int userHandle) {
132// }
133//
134// /**
135// * Called when a user's active session has stopped. If your service deals with multiple users,
136// * this method should be overridden.
137// *
138// * @param userHandle The user who's session has stopped.
139// */
140// public void onUserStopped(int userHandle) {
141// }
142//
143// /**
144// * Called when a user has been removed from the system. If your service deals with multiple
145// * users, this method should be overridden.
146// *
147// * @param userHandle The user who has been removed.
148// */
149// public void onUserRemoved(int userHandle) {
150// }
151}