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