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? |
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 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 57 | private final Context mContext; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 58 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 59 | /** |
| 60 | * Initializes the system service. |
| 61 | * <p> |
| 62 | * Subclasses must define a single argument constructor that accepts the context |
| 63 | * and passes it to super. |
| 64 | * </p> |
| 65 | * |
| 66 | * @param context The system server context. |
| 67 | */ |
| 68 | public SystemService(Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 69 | mContext = context; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 72 | /** |
| 73 | * Gets the system context. |
| 74 | */ |
| 75 | public final Context getContext() { |
| 76 | return mContext; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns true if the system is running in safe mode. |
| 81 | * TODO: we should define in which phase this becomes valid |
| 82 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 83 | public final boolean isSafeMode() { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 84 | return getManager().isSafeMode(); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Services are not yet available. This is a good place to do setup work that does |
| 89 | * not require other services. |
| 90 | * |
| 91 | * @param context The system context. |
| 92 | */ |
| 93 | public void onCreate(Context context) {} |
| 94 | |
| 95 | /** |
| 96 | * Called when the dependencies listed in the @Service class-annotation are available |
| 97 | * and after the chosen start phase. |
| 98 | * When this method returns, the service should be published. |
| 99 | */ |
| 100 | public abstract void onStart(); |
| 101 | |
| 102 | /** |
| 103 | * Called on each phase of the boot process. Phases before the service's start phase |
| 104 | * (as defined in the @Service annotation) are never received. |
| 105 | * |
| 106 | * @param phase The current boot phase. |
| 107 | */ |
| 108 | public void onBootPhase(int phase) {} |
| 109 | |
| 110 | /** |
| 111 | * Publish the service so it is accessible to other services and apps. |
| 112 | */ |
| 113 | protected final void publishBinderService(String name, IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 114 | publishBinderService(name, service, false); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Publish the service so it is accessible to other services and apps. |
| 119 | */ |
| 120 | protected final void publishBinderService(String name, IBinder service, |
| 121 | boolean allowIsolated) { |
| 122 | ServiceManager.addService(name, service, allowIsolated); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get a binder service by its name. |
| 127 | */ |
| 128 | protected final IBinder getBinderService(String name) { |
| 129 | return ServiceManager.getService(name); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Publish the service so it is only accessible to the system process. |
| 134 | */ |
| 135 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 136 | LocalServices.addService(type, service); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get a local service by interface. |
| 141 | */ |
| 142 | protected final <T> T getLocalService(Class<T> type) { |
| 143 | return LocalServices.getService(type); |
| 144 | } |
| 145 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 146 | private SystemServiceManager getManager() { |
| 147 | return LocalServices.getService(SystemServiceManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // /** |
| 151 | // * Called when a new user has been created. If your service deals with multiple users, this |
| 152 | // * method should be overridden. |
| 153 | // * |
| 154 | // * @param userHandle The user that was created. |
| 155 | // */ |
| 156 | // public void onUserCreated(int userHandle) { |
| 157 | // } |
| 158 | // |
| 159 | // /** |
| 160 | // * Called when an existing user has started a new session. If your service deals with multiple |
| 161 | // * users, this method should be overridden. |
| 162 | // * |
| 163 | // * @param userHandle The user who started a new session. |
| 164 | // */ |
| 165 | // public void onUserStarted(int userHandle) { |
| 166 | // } |
| 167 | // |
| 168 | // /** |
| 169 | // * Called when a background user session has entered the foreground. If your service deals with |
| 170 | // * multiple users, this method should be overridden. |
| 171 | // * |
| 172 | // * @param userHandle The user who's session entered the foreground. |
| 173 | // */ |
| 174 | // public void onUserForeground(int userHandle) { |
| 175 | // } |
| 176 | // |
| 177 | // /** |
| 178 | // * Called when a foreground user session has entered the background. If your service deals with |
| 179 | // * multiple users, this method should be overridden; |
| 180 | // * |
| 181 | // * @param userHandle The user who's session entered the background. |
| 182 | // */ |
| 183 | // public void onUserBackground(int userHandle) { |
| 184 | // } |
| 185 | // |
| 186 | // /** |
| 187 | // * Called when a user's active session has stopped. If your service deals with multiple users, |
| 188 | // * this method should be overridden. |
| 189 | // * |
| 190 | // * @param userHandle The user who's session has stopped. |
| 191 | // */ |
| 192 | // public void onUserStopped(int userHandle) { |
| 193 | // } |
| 194 | // |
| 195 | // /** |
| 196 | // * Called when a user has been removed from the system. If your service deals with multiple |
| 197 | // * users, this method should be overridden. |
| 198 | // * |
| 199 | // * @param userHandle The user who has been removed. |
| 200 | // */ |
| 201 | // public void onUserRemoved(int userHandle) { |
| 202 | // } |
| 203 | } |