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 | |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 19 | import android.app.ActivityThread; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.ServiceManager; |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 23 | import android.os.UserManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 26 | * The base class for services running in the system process. Override and implement |
| 27 | * the lifecycle event callback methods as needed. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 28 | * <p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 29 | * The lifecycle of a SystemService: |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 30 | * </p><ul> |
| 31 | * <li>The constructor is called and provided with the system {@link Context} |
| 32 | * to initialize the system service. |
| 33 | * <li>{@link #onStart()} is called to get the service running. The service should |
| 34 | * publish its binder interface at this point using |
| 35 | * {@link #publishBinderService(String, IBinder)}. It may also publish additional |
| 36 | * local interfaces that other services within the system server may use to access |
| 37 | * privileged internal functions. |
| 38 | * <li>Then {@link #onBootPhase(int)} is called as many times as there are boot phases |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 39 | * until {@link #PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 40 | * is an opportunity to do special work, like acquiring optional service dependencies, |
| 41 | * waiting to see if SafeMode is enabled, or registering with a service that gets |
| 42 | * started after this one. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 43 | * </ul><p> |
| 44 | * NOTE: All lifecycle methods are called from the system server's main looper thread. |
| 45 | * </p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 46 | * |
| 47 | * {@hide} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 48 | */ |
| 49 | public abstract class SystemService { |
| 50 | /* |
| 51 | * Boot Phases |
| 52 | */ |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 53 | 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] | 54 | |
| 55 | /** |
| 56 | * After receiving this boot phase, services can obtain lock settings data. |
| 57 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 58 | public static final int PHASE_LOCK_SETTINGS_READY = 480; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * After receiving this boot phase, services can safely call into core system services |
| 62 | * such as the PowerManager or PackageManager. |
| 63 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 64 | public static final int PHASE_SYSTEM_SERVICES_READY = 500; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * After receiving this boot phase, services can broadcast Intents. |
| 68 | */ |
| 69 | public static final int PHASE_ACTIVITY_MANAGER_READY = 550; |
| 70 | |
| 71 | /** |
| 72 | * After receiving this boot phase, services can start/bind to third party apps. |
| 73 | * Apps will be able to make Binder calls into services at this point. |
| 74 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 75 | public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 76 | |
| 77 | /** |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 78 | * After receiving this boot phase, services can allow user interaction with the device. |
| 79 | * This phase occurs when boot has completed and the home application has started. |
| 80 | * System services may prefer to listen to this phase rather than registering a |
| 81 | * broadcast receiver for ACTION_BOOT_COMPLETED to reduce overall latency. |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 82 | */ |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 83 | public static final int PHASE_BOOT_COMPLETED = 1000; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 84 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 85 | private final Context mContext; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 86 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Initializes the system service. |
| 89 | * <p> |
| 90 | * Subclasses must define a single argument constructor that accepts the context |
| 91 | * and passes it to super. |
| 92 | * </p> |
| 93 | * |
| 94 | * @param context The system server context. |
| 95 | */ |
| 96 | public SystemService(Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 97 | mContext = context; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Gets the system context. |
| 102 | */ |
| 103 | public final Context getContext() { |
| 104 | return mContext; |
| 105 | } |
| 106 | |
| 107 | /** |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 108 | * Get the system UI context. This context is to be used for displaying UI. It is themable, |
| 109 | * which means resources can be overridden at runtime. Do not use to retrieve properties that |
| 110 | * configure the behavior of the device that is not UX related. |
| 111 | */ |
| 112 | public final Context getUiContext() { |
| 113 | // This has already been set up by the time any SystemServices are created. |
| 114 | return ActivityThread.currentActivityThread().getSystemUiContext(); |
| 115 | } |
| 116 | |
| 117 | /** |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 118 | * Returns true if the system is running in safe mode. |
| 119 | * TODO: we should define in which phase this becomes valid |
| 120 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 121 | public final boolean isSafeMode() { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 122 | return getManager().isSafeMode(); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 125 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 126 | * Called when the dependencies listed in the @Service class-annotation are available |
| 127 | * and after the chosen start phase. |
| 128 | * When this method returns, the service should be published. |
| 129 | */ |
| 130 | public abstract void onStart(); |
| 131 | |
| 132 | /** |
| 133 | * Called on each phase of the boot process. Phases before the service's start phase |
| 134 | * (as defined in the @Service annotation) are never received. |
| 135 | * |
| 136 | * @param phase The current boot phase. |
| 137 | */ |
| 138 | public void onBootPhase(int phase) {} |
| 139 | |
| 140 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 141 | * Called when a new user is starting, for system services to initialize any per-user |
| 142 | * state they maintain for running users. |
| 143 | * @param userHandle The identifier of the user. |
| 144 | */ |
| 145 | public void onStartUser(int userHandle) {} |
| 146 | |
| 147 | /** |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 148 | * Called when an existing user is in the process of being unlocked. This |
| 149 | * means the credential-encrypted storage for that user is now available, |
| 150 | * and encryption-aware component filtering is no longer in effect. |
| 151 | * <p> |
| 152 | * While dispatching this event to services, the user is in the |
| 153 | * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished |
| 154 | * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state. |
| 155 | * Code written inside system services should use |
| 156 | * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of |
| 157 | * these states. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 158 | * |
| 159 | * @param userHandle The identifier of the user. |
| 160 | */ |
| 161 | public void onUnlockUser(int userHandle) {} |
| 162 | |
| 163 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 164 | * Called when switching to a different foreground user, for system services that have |
| 165 | * special behavior for whichever user is currently in the foreground. This is called |
| 166 | * before any application processes are aware of the new user. |
| 167 | * @param userHandle The identifier of the user. |
| 168 | */ |
| 169 | public void onSwitchUser(int userHandle) {} |
| 170 | |
| 171 | /** |
| 172 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 173 | * state they maintain for running users. This is called prior to sending the SHUTDOWN |
| 174 | * broadcast to the user; it is a good place to stop making use of any resources of that |
| 175 | * user (such as binding to a service running in the user). |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 176 | * |
| 177 | * <p>NOTE: This is the last callback where the callee may access the target user's CE storage. |
| 178 | * |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 179 | * @param userHandle The identifier of the user. |
| 180 | */ |
| 181 | public void onStopUser(int userHandle) {} |
| 182 | |
| 183 | /** |
| 184 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 185 | * state they maintain for running users. This is called after all application process |
| 186 | * teardown of the user is complete. |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 187 | * |
| 188 | * <p>NOTE: When this callback is called, the CE storage for the target user may not be |
| 189 | * accessible already. Use {@link #onStopUser} instead if you need to access the CE storage. |
| 190 | * |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 191 | * @param userHandle The identifier of the user. |
| 192 | */ |
| 193 | public void onCleanupUser(int userHandle) {} |
| 194 | |
| 195 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 196 | * Publish the service so it is accessible to other services and apps. |
| 197 | */ |
| 198 | protected final void publishBinderService(String name, IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 199 | publishBinderService(name, service, false); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Publish the service so it is accessible to other services and apps. |
| 204 | */ |
| 205 | protected final void publishBinderService(String name, IBinder service, |
| 206 | boolean allowIsolated) { |
| 207 | ServiceManager.addService(name, service, allowIsolated); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get a binder service by its name. |
| 212 | */ |
| 213 | protected final IBinder getBinderService(String name) { |
| 214 | return ServiceManager.getService(name); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Publish the service so it is only accessible to the system process. |
| 219 | */ |
| 220 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 221 | LocalServices.addService(type, service); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get a local service by interface. |
| 226 | */ |
| 227 | protected final <T> T getLocalService(Class<T> type) { |
| 228 | return LocalServices.getService(type); |
| 229 | } |
| 230 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 231 | private SystemServiceManager getManager() { |
| 232 | return LocalServices.getService(SystemServiceManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 233 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 234 | } |