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 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 19 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT; |
| 20 | |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Makoto Onuki | bf03b1b | 2020-01-15 11:16:19 -0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
| 25 | import android.annotation.SystemApi.Client; |
| 26 | import android.annotation.SystemApi.Process; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 27 | import android.annotation.UserIdInt; |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 28 | import android.app.ActivityThread; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
| 32 | import android.os.ServiceManager; |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 33 | import android.os.UserHandle; |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 34 | import android.os.UserManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 35 | |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 36 | import com.android.server.pm.UserManagerService; |
| 37 | |
Felipe Leme | e87735b | 2019-12-17 09:58:40 -0800 | [diff] [blame] | 38 | import java.io.PrintWriter; |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Felipe Leme | e87735b | 2019-12-17 09:58:40 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 44 | /** |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 45 | * The base class for services running in the system process. Override and implement |
| 46 | * the lifecycle event callback methods as needed. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 47 | * <p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 48 | * The lifecycle of a SystemService: |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 49 | * </p><ul> |
| 50 | * <li>The constructor is called and provided with the system {@link Context} |
| 51 | * to initialize the system service. |
| 52 | * <li>{@link #onStart()} is called to get the service running. The service should |
| 53 | * publish its binder interface at this point using |
| 54 | * {@link #publishBinderService(String, IBinder)}. It may also publish additional |
| 55 | * local interfaces that other services within the system server may use to access |
| 56 | * privileged internal functions. |
| 57 | * <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] | 58 | * 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] | 59 | * is an opportunity to do special work, like acquiring optional service dependencies, |
| 60 | * waiting to see if SafeMode is enabled, or registering with a service that gets |
| 61 | * started after this one. |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 62 | * </ul><p> |
| 63 | * NOTE: All lifecycle methods are called from the system server's main looper thread. |
| 64 | * </p> |
Adam Lesinski | b102b2c | 2013-12-20 11:46:14 -0800 | [diff] [blame] | 65 | * |
| 66 | * {@hide} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 67 | */ |
Makoto Onuki | bf03b1b | 2020-01-15 11:16:19 -0800 | [diff] [blame] | 68 | @SystemApi(client = Client.MODULE_LIBRARIES, process = Process.SYSTEM_SERVER) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 69 | public abstract class SystemService { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 70 | |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 71 | /** @hide */ |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 72 | // TODO(b/133242016) STOPSHIP: change to false before R ships |
| 73 | protected static final boolean DEBUG_USER = true; |
| 74 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 75 | /* |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 76 | * The earliest boot phase the system send to system services on boot. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 77 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 78 | public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * After receiving this boot phase, services can obtain lock settings data. |
| 82 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 83 | public static final int PHASE_LOCK_SETTINGS_READY = 480; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * After receiving this boot phase, services can safely call into core system services |
| 87 | * such as the PowerManager or PackageManager. |
| 88 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 89 | public static final int PHASE_SYSTEM_SERVICES_READY = 500; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
Daichi Hirono | edfcb00 | 2017-10-10 17:22:58 +0900 | [diff] [blame] | 92 | * After receiving this boot phase, services can safely call into device specific services. |
| 93 | */ |
| 94 | public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520; |
| 95 | |
| 96 | /** |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 97 | * After receiving this boot phase, services can broadcast Intents. |
| 98 | */ |
| 99 | public static final int PHASE_ACTIVITY_MANAGER_READY = 550; |
| 100 | |
| 101 | /** |
| 102 | * After receiving this boot phase, services can start/bind to third party apps. |
| 103 | * Apps will be able to make Binder calls into services at this point. |
| 104 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 105 | public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600; |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 108 | * After receiving this boot phase, services can allow user interaction with the device. |
| 109 | * This phase occurs when boot has completed and the home application has started. |
| 110 | * System services may prefer to listen to this phase rather than registering a |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 111 | * broadcast receiver for {@link android.content.Intent#ACTION_LOCKED_BOOT_COMPLETED} |
| 112 | * to reduce overall latency. |
Adam Lesinski | 2cb6c60 | 2014-02-14 17:19:56 -0800 | [diff] [blame] | 113 | */ |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 114 | public static final int PHASE_BOOT_COMPLETED = 1000; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 115 | |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 116 | /** @hide */ |
| 117 | @IntDef(flag = true, prefix = { "PHASE_" }, value = { |
| 118 | PHASE_WAIT_FOR_DEFAULT_DISPLAY, |
| 119 | PHASE_LOCK_SETTINGS_READY, |
| 120 | PHASE_SYSTEM_SERVICES_READY, |
| 121 | PHASE_DEVICE_SPECIFIC_SERVICES_READY, |
| 122 | PHASE_ACTIVITY_MANAGER_READY, |
| 123 | PHASE_THIRD_PARTY_APPS_CAN_START, |
| 124 | PHASE_BOOT_COMPLETED |
| 125 | }) |
| 126 | @Retention(RetentionPolicy.SOURCE) |
| 127 | public @interface BootPhase {} |
| 128 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 129 | private final Context mContext; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 130 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 131 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 132 | * Class representing user in question in the lifecycle callbacks. |
| 133 | * @hide |
| 134 | */ |
Makoto Onuki | bf03b1b | 2020-01-15 11:16:19 -0800 | [diff] [blame] | 135 | @SystemApi(client = Client.MODULE_LIBRARIES, process = Process.SYSTEM_SERVER) |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 136 | public static final class TargetUser { |
| 137 | @NonNull |
| 138 | private final UserInfo mUserInfo; |
| 139 | |
| 140 | /** @hide */ |
| 141 | public TargetUser(@NonNull UserInfo userInfo) { |
| 142 | mUserInfo = userInfo; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return The information about the user. <b>NOTE: </b> this is a "live" object |
| 147 | * referenced by {@link UserManagerService} and hence should not be modified. |
| 148 | * |
| 149 | * @hide |
| 150 | */ |
| 151 | @NonNull |
| 152 | public UserInfo getUserInfo() { |
| 153 | return mUserInfo; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return the target {@link UserHandle}. |
| 158 | */ |
| 159 | @NonNull |
| 160 | public UserHandle getUserHandle() { |
| 161 | return mUserInfo.getUserHandle(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @return the integer user id |
| 166 | * |
| 167 | * @hide |
| 168 | */ |
| 169 | public int getUserIdentifier() { |
| 170 | return mUserInfo.id; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 175 | * Initializes the system service. |
| 176 | * <p> |
| 177 | * Subclasses must define a single argument constructor that accepts the context |
| 178 | * and passes it to super. |
| 179 | * </p> |
| 180 | * |
| 181 | * @param context The system server context. |
| 182 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 183 | public SystemService(@NonNull Context context) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 184 | mContext = context; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 187 | /** |
| 188 | * Gets the system context. |
| 189 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 190 | @NonNull |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 191 | public final Context getContext() { |
| 192 | return mContext; |
| 193 | } |
| 194 | |
| 195 | /** |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 196 | * Get the system UI context. This context is to be used for displaying UI. It is themable, |
| 197 | * which means resources can be overridden at runtime. Do not use to retrieve properties that |
| 198 | * configure the behavior of the device that is not UX related. |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 199 | * |
| 200 | * @hide |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 201 | */ |
| 202 | public final Context getUiContext() { |
| 203 | // This has already been set up by the time any SystemServices are created. |
| 204 | return ActivityThread.currentActivityThread().getSystemUiContext(); |
| 205 | } |
| 206 | |
| 207 | /** |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 208 | * Returns true if the system is running in safe mode. |
| 209 | * TODO: we should define in which phase this becomes valid |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 210 | * |
| 211 | * @hide |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 212 | */ |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 213 | public final boolean isSafeMode() { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 214 | return getManager().isSafeMode(); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 217 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 218 | * Called when the system service should publish a binder service using |
| 219 | * {@link #publishBinderService(String, IBinder).} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 220 | */ |
| 221 | public abstract void onStart(); |
| 222 | |
| 223 | /** |
| 224 | * Called on each phase of the boot process. Phases before the service's start phase |
| 225 | * (as defined in the @Service annotation) are never received. |
| 226 | * |
| 227 | * @param phase The current boot phase. |
| 228 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 229 | public void onBootPhase(@BootPhase int phase) {} |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 232 | * Checks if the service should be available for the given user. |
| 233 | * |
| 234 | * <p>By default returns {@code true}, but subclasses should extend for optimization, if they |
| 235 | * don't support some types (like headless system user). |
| 236 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 237 | public boolean isSupportedUser(@NonNull TargetUser user) { |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 242 | * Helper method used to dump which users are {@link #onStartUser(TargetUser) supported}. |
| 243 | * |
| 244 | * @hide |
Felipe Leme | e87735b | 2019-12-17 09:58:40 -0800 | [diff] [blame] | 245 | */ |
| 246 | protected void dumpSupportedUsers(@NonNull PrintWriter pw, @NonNull String prefix) { |
| 247 | final List<UserInfo> allUsers = UserManager.get(mContext).getUsers(); |
| 248 | final List<Integer> supportedUsers = new ArrayList<>(allUsers.size()); |
| 249 | for (UserInfo user : allUsers) { |
| 250 | supportedUsers.add(user.id); |
| 251 | } |
| 252 | if (allUsers.isEmpty()) { |
| 253 | pw.print(prefix); pw.println("No supported users"); |
| 254 | } else { |
| 255 | final int size = supportedUsers.size(); |
| 256 | pw.print(prefix); pw.print(size); pw.print(" supported user"); |
| 257 | if (size > 1) pw.print("s"); |
| 258 | pw.print(": "); pw.println(supportedUsers); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 263 | * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead |
| 264 | * (which by default calls this method). |
| 265 | * |
| 266 | * @hide |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 267 | */ |
| 268 | @Deprecated |
Bookatz | f56f258 | 2019-09-04 16:06:41 -0700 | [diff] [blame] | 269 | public void onStartUser(@UserIdInt int userId) {} |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 270 | |
| 271 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 272 | * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead |
| 273 | * (which by default calls this method). |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 274 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 275 | * @hide |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 276 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 277 | @Deprecated |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 278 | public void onStartUser(@NonNull UserInfo userInfo) { |
| 279 | onStartUser(userInfo.id); |
| 280 | } |
| 281 | |
| 282 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 283 | * Called when a new user is starting, for system services to initialize any per-user |
| 284 | * state they maintain for running users. |
| 285 | * |
| 286 | * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} |
| 287 | * this user. |
| 288 | * |
| 289 | * @param user target user |
| 290 | */ |
| 291 | public void onStartUser(@NonNull TargetUser user) { |
| 292 | onStartUser(user.getUserInfo()); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 297 | * default calls this method). |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 298 | * |
| 299 | * @hide |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 300 | */ |
| 301 | @Deprecated |
Bookatz | f56f258 | 2019-09-04 16:06:41 -0700 | [diff] [blame] | 302 | public void onUnlockUser(@UserIdInt int userId) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 305 | * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by |
| 306 | * default calls this method). |
| 307 | * |
| 308 | * @hide |
| 309 | */ |
| 310 | @Deprecated |
| 311 | public void onUnlockUser(@NonNull UserInfo userInfo) { |
| 312 | onUnlockUser(userInfo.id); |
| 313 | } |
| 314 | |
| 315 | /** |
Jeff Sharkey | bb4988a | 2017-02-23 17:31:39 -0700 | [diff] [blame] | 316 | * Called when an existing user is in the process of being unlocked. This |
| 317 | * means the credential-encrypted storage for that user is now available, |
| 318 | * and encryption-aware component filtering is no longer in effect. |
| 319 | * <p> |
| 320 | * While dispatching this event to services, the user is in the |
| 321 | * {@code STATE_RUNNING_UNLOCKING} state, and once dispatching is finished |
| 322 | * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state. |
| 323 | * Code written inside system services should use |
| 324 | * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of |
| 325 | * these states. |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 326 | * <p> |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 327 | * This method is only called when the service {@link #isSupportedUser(TargetUser) supports} |
| 328 | * this user. |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 329 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 330 | * @param user target user |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 331 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 332 | public void onUnlockUser(@NonNull TargetUser user) { |
| 333 | onUnlockUser(user.getUserInfo()); |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 337 | * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 338 | * (which by default calls this method). |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 339 | * |
| 340 | * @hide |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 341 | */ |
| 342 | @Deprecated |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 343 | public void onSwitchUser(@UserIdInt int toUserId) {} |
| 344 | |
| 345 | /** |
| 346 | * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead |
| 347 | * (which by default calls this method). |
| 348 | * |
| 349 | * @hide |
| 350 | */ |
| 351 | @Deprecated |
| 352 | public void onSwitchUser(@Nullable UserInfo from, @NonNull UserInfo to) { |
| 353 | onSwitchUser(to.id); |
| 354 | } |
Jeff Sharkey | bedbaa9 | 2015-12-02 16:42:25 -0700 | [diff] [blame] | 355 | |
| 356 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 357 | * Called when switching to a different foreground user, for system services that have |
| 358 | * special behavior for whichever user is currently in the foreground. This is called |
| 359 | * before any application processes are aware of the new user. |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 360 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 361 | * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} |
| 362 | * either of the users ({@code from} or {@code to}). |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 363 | * |
| 364 | * <b>NOTE: </b> both {@code from} and {@code to} are "live" objects |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 365 | * referenced by {@link UserManagerService} and hence should not be modified. |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 366 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 367 | * @param from the user switching from |
| 368 | * @param to the user switching to |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 369 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 370 | public void onSwitchUser(@Nullable TargetUser from, @NonNull TargetUser to) { |
| 371 | onSwitchUser((from == null ? null : from.getUserInfo()), to.getUserInfo()); |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 375 | * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead |
| 376 | * (which by default calls this method). |
| 377 | * |
| 378 | * @hide |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 379 | */ |
| 380 | @Deprecated |
Bookatz | f56f258 | 2019-09-04 16:06:41 -0700 | [diff] [blame] | 381 | public void onStopUser(@UserIdInt int userId) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 382 | |
| 383 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 384 | * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead |
| 385 | * (which by default calls this method). |
| 386 | * |
| 387 | * @hide |
| 388 | */ |
| 389 | @Deprecated |
| 390 | public void onStopUser(@NonNull UserInfo user) { |
| 391 | onStopUser(user.id); |
| 392 | |
| 393 | } |
| 394 | |
| 395 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 396 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 397 | * state they maintain for running users. This is called prior to sending the SHUTDOWN |
| 398 | * broadcast to the user; it is a good place to stop making use of any resources of that |
| 399 | * user (such as binding to a service running in the user). |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 400 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 401 | * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} |
| 402 | * this user. |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 403 | * |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 404 | * <p>NOTE: This is the last callback where the callee may access the target user's CE storage. |
| 405 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 406 | * @param user target user |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 407 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 408 | public void onStopUser(@NonNull TargetUser user) { |
| 409 | onStopUser(user.getUserInfo()); |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 413 | * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 414 | * default calls this method). |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 415 | * |
| 416 | * @hide |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 417 | */ |
| 418 | @Deprecated |
Bookatz | f56f258 | 2019-09-04 16:06:41 -0700 | [diff] [blame] | 419 | public void onCleanupUser(@UserIdInt int userId) {} |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 420 | |
| 421 | /** |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 422 | * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by |
| 423 | * default calls this method). |
| 424 | * |
| 425 | * @hide |
| 426 | */ |
| 427 | @Deprecated |
| 428 | public void onCleanupUser(@NonNull UserInfo user) { |
| 429 | onCleanupUser(user.id); |
| 430 | } |
| 431 | |
| 432 | /** |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 433 | * Called when an existing user is stopping, for system services to finalize any per-user |
| 434 | * state they maintain for running users. This is called after all application process |
| 435 | * teardown of the user is complete. |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 436 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 437 | * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} |
| 438 | * this user. |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 439 | * |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 440 | * <p>NOTE: When this callback is called, the CE storage for the target user may not be |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 441 | * accessible already. Use {@link #onStopUser(TargetUser)} instead if you need to access the CE |
Felipe Leme | 501a514 | 2019-08-15 16:23:47 -0700 | [diff] [blame] | 442 | * storage. |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 443 | * |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 444 | * @param user target user |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 445 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 446 | public void onCleanupUser(@NonNull TargetUser user) { |
| 447 | onCleanupUser(user.getUserInfo()); |
Felipe Leme | e5434c3 | 2019-08-13 09:28:33 -0700 | [diff] [blame] | 448 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 449 | |
| 450 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 451 | * Publish the service so it is accessible to other services and apps. |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 452 | * |
| 453 | * @param name the name of the new service |
| 454 | * @param service the service object |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 455 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 456 | protected final void publishBinderService(@NonNull String name, @NonNull IBinder service) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 457 | publishBinderService(name, service, false); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Publish the service so it is accessible to other services and apps. |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 462 | * |
| 463 | * @param name the name of the new service |
| 464 | * @param service the service object |
| 465 | * @param allowIsolated set to true to allow isolated sandboxed processes |
| 466 | * to access this service |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 467 | */ |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 468 | protected final void publishBinderService(@NonNull String name, @NonNull IBinder service, |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 469 | boolean allowIsolated) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 470 | publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Publish the service so it is accessible to other services and apps. |
| 475 | * |
| 476 | * @param name the name of the new service |
| 477 | * @param service the service object |
| 478 | * @param allowIsolated set to true to allow isolated sandboxed processes |
| 479 | * to access this service |
| 480 | * @param dumpPriority supported dump priority levels as a bitmask |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 481 | * |
| 482 | * @hide |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 483 | */ |
| 484 | protected final void publishBinderService(String name, IBinder service, |
| 485 | boolean allowIsolated, int dumpPriority) { |
| 486 | ServiceManager.addService(name, service, allowIsolated, dumpPriority); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Get a binder service by its name. |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 491 | * |
| 492 | * @hide |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 493 | */ |
| 494 | protected final IBinder getBinderService(String name) { |
| 495 | return ServiceManager.getService(name); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Publish the service so it is only accessible to the system process. |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 500 | * |
| 501 | * @hide |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 502 | */ |
| 503 | protected final <T> void publishLocalService(Class<T> type, T service) { |
| 504 | LocalServices.addService(type, service); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Get a local service by interface. |
Makoto Onuki | c881590 | 2020-01-08 13:55:25 -0800 | [diff] [blame] | 509 | * |
| 510 | * @hide |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 511 | */ |
| 512 | protected final <T> T getLocalService(Class<T> type) { |
| 513 | return LocalServices.getService(type); |
| 514 | } |
| 515 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 516 | private SystemServiceManager getManager() { |
| 517 | return LocalServices.getService(SystemServiceManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 518 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 519 | } |