1 /* 2 * Copyright (C) 2016 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 package com.android.server.pm; 17 18 import android.annotation.IntDef; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.UserIdInt; 22 import android.content.Context; 23 import android.content.pm.UserInfo; 24 import android.graphics.Bitmap; 25 import android.os.Bundle; 26 import android.os.UserManager; 27 28 import java.lang.annotation.Retention; 29 import java.lang.annotation.RetentionPolicy; 30 import java.util.List; 31 32 /** 33 * @hide Only for use within the system server. 34 */ 35 public abstract class UserManagerInternal { 36 37 public static final int OWNER_TYPE_DEVICE_OWNER = 0; 38 public static final int OWNER_TYPE_PROFILE_OWNER = 1; 39 public static final int OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE = 2; 40 public static final int OWNER_TYPE_NO_OWNER = 3; 41 42 @Retention(RetentionPolicy.SOURCE) 43 @IntDef(value = {OWNER_TYPE_DEVICE_OWNER, OWNER_TYPE_PROFILE_OWNER, 44 OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE, OWNER_TYPE_NO_OWNER}) 45 public @interface OwnerType { 46 } 47 48 public interface UserRestrictionsListener { 49 /** 50 * Called when a user restriction changes. 51 * 52 * @param userId target user id 53 * @param newRestrictions new user restrictions 54 * @param prevRestrictions user restrictions that were previously set 55 */ onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions)56 void onUserRestrictionsChanged(int userId, Bundle newRestrictions, Bundle prevRestrictions); 57 } 58 59 /** 60 * Listener for user lifecycle events. 61 * 62 * <p><b>NOTE: </b>implementations MUST not block the current thread. 63 */ 64 public interface UserLifecycleListener { 65 66 /** 67 * Called when a new user is created. 68 * 69 * @param user new user. 70 * @param token token passed to the method that created the user. 71 */ onUserCreated(UserInfo user, @Nullable Object token)72 default void onUserCreated(UserInfo user, @Nullable Object token) {} 73 74 /** Called when an existing user is removed. */ onUserRemoved(UserInfo user)75 default void onUserRemoved(UserInfo user) {} 76 } 77 78 /** 79 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to set 80 * restrictions enforced by the user. 81 * 82 * @param originatingUserId user id of the user where the restrictions originated. 83 * @param global a bundle of global user restrictions. Global restrictions are 84 * restrictions that apply device-wide: to the managed profile, 85 * primary profile and secondary users and any profile created in 86 * any secondary user. 87 * @param local a restriction set of local user restrictions. The key is the user 88 * id of the user whom the restrictions are targeting. 89 * @param isDeviceOwner whether {@code originatingUserId} corresponds to device owner 90 * user id. 91 */ setDevicePolicyUserRestrictions(int originatingUserId, @Nullable Bundle global, @Nullable RestrictionsSet local, boolean isDeviceOwner)92 public abstract void setDevicePolicyUserRestrictions(int originatingUserId, 93 @Nullable Bundle global, @Nullable RestrictionsSet local, boolean isDeviceOwner); 94 95 /** 96 * Returns the "base" user restrictions. 97 * 98 * Used by {@link com.android.server.devicepolicy.DevicePolicyManagerService} for upgrading 99 * from MNC. 100 */ getBaseUserRestrictions(int userId)101 public abstract Bundle getBaseUserRestrictions(int userId); 102 103 /** 104 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} for upgrading 105 * from MNC. 106 */ setBaseUserRestrictionsByDpmsForMigration(int userId, Bundle baseRestrictions)107 public abstract void setBaseUserRestrictionsByDpmsForMigration(int userId, 108 Bundle baseRestrictions); 109 110 /** Return a user restriction. */ getUserRestriction(int userId, String key)111 public abstract boolean getUserRestriction(int userId, String key); 112 113 /** Adds a listener to user restriction changes. */ addUserRestrictionsListener(UserRestrictionsListener listener)114 public abstract void addUserRestrictionsListener(UserRestrictionsListener listener); 115 116 /** Remove a {@link UserRestrictionsListener}. */ removeUserRestrictionsListener(UserRestrictionsListener listener)117 public abstract void removeUserRestrictionsListener(UserRestrictionsListener listener); 118 119 /** Adds a {@link UserLifecycleListener}. */ addUserLifecycleListener(UserLifecycleListener listener)120 public abstract void addUserLifecycleListener(UserLifecycleListener listener); 121 122 /** Removes a {@link UserLifecycleListener}. */ removeUserLifecycleListener(UserLifecycleListener listener)123 public abstract void removeUserLifecycleListener(UserLifecycleListener listener); 124 125 /** 126 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update 127 * whether the device is managed by device owner. 128 */ setDeviceManaged(boolean isManaged)129 public abstract void setDeviceManaged(boolean isManaged); 130 131 /** 132 * Returns whether the device is managed by device owner. 133 */ isDeviceManaged()134 public abstract boolean isDeviceManaged(); 135 136 /** 137 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to update 138 * whether the user is managed by profile owner. 139 */ setUserManaged(int userId, boolean isManaged)140 public abstract void setUserManaged(int userId, boolean isManaged); 141 142 /** 143 * whether a profile owner manages this user. 144 */ isUserManaged(int userId)145 public abstract boolean isUserManaged(int userId); 146 147 /** 148 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to omit 149 * restriction check, because DevicePolicyManager must always be able to set user icon 150 * regardless of any restriction. 151 * Also called by {@link com.android.server.pm.UserManagerService} because the logic of setting 152 * the icon is in this method. 153 */ setUserIcon(int userId, Bitmap bitmap)154 public abstract void setUserIcon(int userId, Bitmap bitmap); 155 156 /** 157 * Called by {@link com.android.server.devicepolicy.DevicePolicyManagerService} to inform the 158 * user manager whether all users should be created ephemeral. 159 */ setForceEphemeralUsers(boolean forceEphemeralUsers)160 public abstract void setForceEphemeralUsers(boolean forceEphemeralUsers); 161 162 /** 163 * Switches to the system user and deletes all other users. 164 * 165 * <p>Called by the {@link com.android.server.devicepolicy.DevicePolicyManagerService} when 166 * the force-ephemeral-users policy is toggled on to make sure there are no pre-existing 167 * non-ephemeral users left. 168 */ removeAllUsers()169 public abstract void removeAllUsers(); 170 171 /** 172 * Called by the activity manager when the ephemeral user goes to background and its removal 173 * starts as a result. 174 * 175 * <p>It marks the ephemeral user as disabled in order to prevent it from being re-entered 176 * before its removal finishes. 177 * 178 * @param userId the ID of the ephemeral user. 179 */ onEphemeralUserStop(int userId)180 public abstract void onEphemeralUserStop(int userId); 181 182 /** 183 * Same as UserManager.createUser(), but bypasses the check for 184 * {@link UserManager#DISALLOW_ADD_USER} and {@link UserManager#DISALLOW_ADD_MANAGED_PROFILE} 185 * 186 * <p>Called by the {@link com.android.server.devicepolicy.DevicePolicyManagerService} when 187 * createAndManageUser is called by the device owner; it uses {@code token} to block until 188 * the user is created (as it will be passed back to it through 189 * {@link UserLifecycleListener#onUserCreated(UserInfo, Object)}); 190 */ createUserEvenWhenDisallowed(String name, String userType, int flags, String[] disallowedPackages, @Nullable Object token)191 public abstract UserInfo createUserEvenWhenDisallowed(String name, String userType, 192 int flags, String[] disallowedPackages, @Nullable Object token) 193 throws UserManager.CheckedUserOperationException; 194 195 /** 196 * Same as {@link UserManager#removeUser(int userId)}, but bypasses the check for 197 * {@link UserManager#DISALLOW_REMOVE_USER} and 198 * {@link UserManager#DISALLOW_REMOVE_MANAGED_PROFILE} and does not require the 199 * {@link android.Manifest.permission#MANAGE_USERS} permission. 200 */ removeUserEvenWhenDisallowed(int userId)201 public abstract boolean removeUserEvenWhenDisallowed(int userId); 202 203 /** 204 * Return whether the given user is running in an 205 * {@code UserState.STATE_RUNNING_UNLOCKING} or 206 * {@code UserState.STATE_RUNNING_UNLOCKED} state. 207 */ isUserUnlockingOrUnlocked(int userId)208 public abstract boolean isUserUnlockingOrUnlocked(int userId); 209 210 /** 211 * Return whether the given user is running in an 212 * {@code UserState.STATE_RUNNING_UNLOCKED} state. 213 */ isUserUnlocked(int userId)214 public abstract boolean isUserUnlocked(int userId); 215 216 /** 217 * Returns whether the given user is running 218 */ isUserRunning(int userId)219 public abstract boolean isUserRunning(int userId); 220 221 /** 222 * Returns whether the given user is initialized 223 */ isUserInitialized(int userId)224 public abstract boolean isUserInitialized(int userId); 225 226 /** 227 * Returns whether the given user exists 228 */ exists(int userId)229 public abstract boolean exists(int userId); 230 231 /** 232 * Set user's running state 233 */ setUserState(int userId, int userState)234 public abstract void setUserState(int userId, int userState); 235 236 /** 237 * Remove user's running state 238 */ removeUserState(int userId)239 public abstract void removeUserState(int userId); 240 241 /** 242 * Returns an array of user ids. This array is cached in UserManagerService and passed as a 243 * reference, so do not modify the returned array. 244 * 245 * @return the array of user ids. 246 */ getUserIds()247 public abstract int[] getUserIds(); 248 249 /** 250 * Internal implementation of getUsers does not check permissions. 251 * This improves performance for calls from inside system server which already have permissions 252 * checked. 253 */ getUsers(boolean excludeDying)254 public abstract @NonNull List<UserInfo> getUsers(boolean excludeDying); 255 256 /** 257 * Internal implementation of getUsers does not check permissions. 258 * This improves performance for calls from inside system server which already have permissions 259 * checked. 260 */ getUsers(boolean excludePartial, boolean excludeDying, boolean excludePreCreated)261 public abstract @NonNull List<UserInfo> getUsers(boolean excludePartial, boolean excludeDying, 262 boolean excludePreCreated); 263 264 /** 265 * Checks if the {@code callingUserId} and {@code targetUserId} are same or in same group 266 * and that the {@code callingUserId} is not a profile and {@code targetUserId} is enabled. 267 * 268 * @return TRUE if the {@code callingUserId} can access {@code targetUserId}. FALSE 269 * otherwise 270 * 271 * @throws SecurityException if the calling user and {@code targetUser} are not in the same 272 * group and {@code throwSecurityException} is true, otherwise if will simply return false. 273 */ isProfileAccessible(int callingUserId, int targetUserId, String debugMsg, boolean throwSecurityException)274 public abstract boolean isProfileAccessible(int callingUserId, int targetUserId, 275 String debugMsg, boolean throwSecurityException); 276 277 /** 278 * If {@code userId} is of a profile, return the parent user ID. Otherwise return itself. 279 */ getProfileParentId(int userId)280 public abstract int getProfileParentId(int userId); 281 282 /** 283 * Checks whether changing a setting to a value is prohibited by the corresponding user 284 * restriction. 285 * 286 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction( 287 * Context, int, String, boolean)}, which should be in sync with this method. 288 * 289 * @return {@code true} if the change is prohibited, {@code false} if the change is allowed. 290 * 291 * @hide 292 */ isSettingRestrictedForUser(String setting, int userId, String value, int callingUid)293 public abstract boolean isSettingRestrictedForUser(String setting, int userId, String value, 294 int callingUid); 295 296 /** @return a specific user restriction that's in effect currently. */ hasUserRestriction(String restriction, int userId)297 public abstract boolean hasUserRestriction(String restriction, int userId); 298 299 /** 300 * Gets a {@link UserInfo} for the given {@code userId}, or {@code null} if not found. 301 */ getUserInfo(@serIdInt int userId)302 public abstract @Nullable UserInfo getUserInfo(@UserIdInt int userId); 303 304 /** 305 * Gets all {@link UserInfo UserInfos}. 306 */ getUserInfos()307 public abstract @NonNull UserInfo[] getUserInfos(); 308 309 /** 310 * Sets all default cross profile intent filters between {@code parentUserId} and 311 * {@code profileUserId}. 312 */ setDefaultCrossProfileIntentFilters( @serIdInt int parentUserId, @UserIdInt int profileUserId)313 public abstract void setDefaultCrossProfileIntentFilters( 314 @UserIdInt int parentUserId, @UserIdInt int profileUserId); 315 } 316