Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 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 | /** |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 24 | * The base class for services running in the system process. Override and implement |
| 25 | * the lifecycle event callback methods as needed. |
| 26 | * |
| 27 | * The lifecycle of a SystemService: |
| 28 | * |
| 29 | * {@link #onCreate(android.content.Context)} is called to initialize the |
| 30 | * service. |
| 31 | * |
| 32 | * {@link #onStart()} is called to get the service running. It is common |
| 33 | * for services to publish their Binder interface at this point. All required |
| 34 | * dependencies are also assumed to be ready to use. |
| 35 | * |
| 36 | * Then {@link #onBootPhase(int)} is called as many times as there are boot phases |
| 37 | * 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. |
| 41 | * |
| 42 | * NOTE: All lifecycle methods are called from the same thread that created the |
| 43 | * SystemService. |
| 44 | * |
| 45 | * {@hide} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 46 | */ |
| 47 | public abstract class SystemService { |
| 48 | /* |
| 49 | * Boot Phases |
| 50 | */ |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 51 | public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; // maybe should be a dependency? |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 52 | public static final int PHASE_LOCK_SETTINGS_READY = 480; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 53 | public static final int PHASE_SYSTEM_SERVICES_READY = 500; |
| 54 | public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600; |
| 55 | public static final int PHASE_BOOT_COMPLETE = 1000; |
| 56 | |
| 57 | private SystemServiceManager mManager; |
| 58 | private Context mContext; |
| 59 | |
| 60 | final void init(Context context, SystemServiceManager manager) { |
| 61 | mContext = context; |
| 62 | mManager = manager; |
| 63 | onCreate(context); |
| 64 | } |
| 65 | |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 66 | public final boolean isSafeMode() { |
| 67 | return mManager.isSafeMode(); |
| 68 | } |
| 69 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Services are not yet available. This is a good place to do setup work that does |
| 72 | * not require other services. |
| 73 | * |
| 74 | * @param context The system context. |
| 75 | */ |
| 76 | public void onCreate(Context context) {} |
| 77 | |
| 78 | /** |
| 79 | * Called when the dependencies listed in the @Service class-annotation are available |
| 80 | * and after the chosen start phase. |
| 81 | * When this method returns, the service should be published. |
| 82 | */ |
| 83 | public abstract void onStart(); |
| 84 | |
| 85 | /** |
| 86 | * Called on each phase of the boot process. Phases before the service's start phase |
| 87 | * (as defined in the @Service annotation) are never received. |
| 88 | * |
| 89 | * @param phase The current boot phase. |
| 90 | */ |
| 91 | public void onBootPhase(int phase) {} |
| 92 | |
| 93 | /** |
| 94 | * Publish the service so it is accessible to other services and apps. |
| 95 | */ |
| 96 | protected final void publishBinderService(String name, IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 97 | publishBinderService(name, service, false); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Publish the service so it is accessible to other services and apps. |
| 102 | */ |
| 103 | protected final void publishBinderService(String name, IBinder service, |
| 104 | boolean allowIsolated) { |
| 105 | ServiceManager.addService(name, service, allowIsolated); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get a binder service by its name. |
| 110 | */ |
| 111 | protected final IBinder getBinderService(String name) { |
| 112 | return ServiceManager.getService(name); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Publish the service so it is only accessible to the system process. |
| 117 | */ |
| 118 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 119 | LocalServices.addService(type, service); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get a local service by interface. |
| 124 | */ |
| 125 | protected final <T> T getLocalService(Class<T> type) { |
| 126 | return LocalServices.getService(type); |
| 127 | } |
| 128 | |
| 129 | public final Context getContext() { |
| 130 | return mContext; |
| 131 | } |
| 132 | |
| 133 | // /** |
| 134 | // * Called when a new user has been created. If your service deals with multiple users, this |
| 135 | // * method should be overridden. |
| 136 | // * |
| 137 | // * @param userHandle The user that was created. |
| 138 | // */ |
| 139 | // public void onUserCreated(int userHandle) { |
| 140 | // } |
| 141 | // |
| 142 | // /** |
| 143 | // * Called when an existing user has started a new session. If your service deals with multiple |
| 144 | // * users, this method should be overridden. |
| 145 | // * |
| 146 | // * @param userHandle The user who started a new session. |
| 147 | // */ |
| 148 | // public void onUserStarted(int userHandle) { |
| 149 | // } |
| 150 | // |
| 151 | // /** |
| 152 | // * Called when a background user session has entered the foreground. If your service deals with |
| 153 | // * multiple users, this method should be overridden. |
| 154 | // * |
| 155 | // * @param userHandle The user who's session entered the foreground. |
| 156 | // */ |
| 157 | // public void onUserForeground(int userHandle) { |
| 158 | // } |
| 159 | // |
| 160 | // /** |
| 161 | // * Called when a foreground user session has entered the background. If your service deals with |
| 162 | // * multiple users, this method should be overridden; |
| 163 | // * |
| 164 | // * @param userHandle The user who's session entered the background. |
| 165 | // */ |
| 166 | // public void onUserBackground(int userHandle) { |
| 167 | // } |
| 168 | // |
| 169 | // /** |
| 170 | // * Called when a user's active session has stopped. If your service deals with multiple users, |
| 171 | // * this method should be overridden. |
| 172 | // * |
| 173 | // * @param userHandle The user who's session has stopped. |
| 174 | // */ |
| 175 | // public void onUserStopped(int userHandle) { |
| 176 | // } |
| 177 | // |
| 178 | // /** |
| 179 | // * Called when a user has been removed from the system. If your service deals with multiple |
| 180 | // * users, this method should be overridden. |
| 181 | // * |
| 182 | // * @param userHandle The user who has been removed. |
| 183 | // */ |
| 184 | // public void onUserRemoved(int userHandle) { |
| 185 | // } |
| 186 | } |