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. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 26 | * <p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 27 | * The lifecycle of a SystemService: |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 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 |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 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. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 41 | * </ul><p> |
| 42 | * NOTE: All lifecycle methods are called from the system server's main looper thread. |
| 43 | * </p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 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? |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * After receiving this boot phase, services can obtain lock settings data. |
| 55 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 56 | public static final int PHASE_LOCK_SETTINGS_READY = 480; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * After receiving this boot phase, services can safely call into core system services |
| 60 | * such as the PowerManager or PackageManager. |
| 61 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 62 | public static final int PHASE_SYSTEM_SERVICES_READY = 500; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 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 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 73 | public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * After receiving this boot phase, services must have finished all boot-related work. |
| 77 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 78 | public static final int PHASE_BOOT_COMPLETE = 1000; |
| 79 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 80 | private final Context mContext; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 81 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Initializes the system service. |
| 84 | * <p> |
| 85 | * Subclasses must define a single argument constructor that accepts the context |
| 86 | * and passes it to super. |
| 87 | * </p> |
| 88 | * |
| 89 | * @param context The system server context. |
| 90 | */ |
| 91 | public SystemService(Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 92 | mContext = context; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Gets the system context. |
| 97 | */ |
| 98 | public final Context getContext() { |
| 99 | return mContext; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns true if the system is running in safe mode. |
| 104 | * TODO: we should define in which phase this becomes valid |
| 105 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 106 | public final boolean isSafeMode() { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 107 | return getManager().isSafeMode(); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 110 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 111 | * Called when the dependencies listed in the @Service class-annotation are available |
| 112 | * and after the chosen start phase. |
| 113 | * When this method returns, the service should be published. |
| 114 | */ |
| 115 | public abstract void onStart(); |
| 116 | |
| 117 | /** |
| 118 | * Called on each phase of the boot process. Phases before the service's start phase |
| 119 | * (as defined in the @Service annotation) are never received. |
| 120 | * |
| 121 | * @param phase The current boot phase. |
| 122 | */ |
| 123 | public void onBootPhase(int phase) {} |
| 124 | |
| 125 | /** |
| 126 | * Publish the service so it is accessible to other services and apps. |
| 127 | */ |
| 128 | protected final void publishBinderService(String name, IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 129 | publishBinderService(name, service, false); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Publish the service so it is accessible to other services and apps. |
| 134 | */ |
| 135 | protected final void publishBinderService(String name, IBinder service, |
| 136 | boolean allowIsolated) { |
| 137 | ServiceManager.addService(name, service, allowIsolated); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get a binder service by its name. |
| 142 | */ |
| 143 | protected final IBinder getBinderService(String name) { |
| 144 | return ServiceManager.getService(name); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Publish the service so it is only accessible to the system process. |
| 149 | */ |
| 150 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 151 | LocalServices.addService(type, service); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get a local service by interface. |
| 156 | */ |
| 157 | protected final <T> T getLocalService(Class<T> type) { |
| 158 | return LocalServices.getService(type); |
| 159 | } |
| 160 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 161 | private SystemServiceManager getManager() { |
| 162 | return LocalServices.getService(SystemServiceManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // /** |
| 166 | // * Called when a new user has been created. If your service deals with multiple users, this |
| 167 | // * method should be overridden. |
| 168 | // * |
| 169 | // * @param userHandle The user that was created. |
| 170 | // */ |
| 171 | // public void onUserCreated(int userHandle) { |
| 172 | // } |
| 173 | // |
| 174 | // /** |
| 175 | // * Called when an existing user has started a new session. If your service deals with multiple |
| 176 | // * users, this method should be overridden. |
| 177 | // * |
| 178 | // * @param userHandle The user who started a new session. |
| 179 | // */ |
| 180 | // public void onUserStarted(int userHandle) { |
| 181 | // } |
| 182 | // |
| 183 | // /** |
| 184 | // * Called when a background user session has entered the foreground. If your service deals with |
| 185 | // * multiple users, this method should be overridden. |
| 186 | // * |
| 187 | // * @param userHandle The user who's session entered the foreground. |
| 188 | // */ |
| 189 | // public void onUserForeground(int userHandle) { |
| 190 | // } |
| 191 | // |
| 192 | // /** |
| 193 | // * Called when a foreground user session has entered the background. If your service deals with |
| 194 | // * multiple users, this method should be overridden; |
| 195 | // * |
| 196 | // * @param userHandle The user who's session entered the background. |
| 197 | // */ |
| 198 | // public void onUserBackground(int userHandle) { |
| 199 | // } |
| 200 | // |
| 201 | // /** |
| 202 | // * Called when a user's active session has stopped. If your service deals with multiple users, |
| 203 | // * this method should be overridden. |
| 204 | // * |
| 205 | // * @param userHandle The user who's session has stopped. |
| 206 | // */ |
| 207 | // public void onUserStopped(int userHandle) { |
| 208 | // } |
| 209 | // |
| 210 | // /** |
| 211 | // * Called when a user has been removed from the system. If your service deals with multiple |
| 212 | // * users, this method should be overridden. |
| 213 | // * |
| 214 | // * @param userHandle The user who has been removed. |
| 215 | // */ |
| 216 | // public void onUserRemoved(int userHandle) { |
| 217 | // } |
| 218 | } |