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