Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.pm; |
| 18 | |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 19 | import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS; |
| 20 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
| 21 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 22 | import android.Manifest; |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 25 | import android.annotation.UserIdInt; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 27 | import android.app.ActivityManager; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 28 | import android.app.ActivityManagerInternal; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 29 | import android.app.ActivityManagerNative; |
Sudheer Shanka | 38c67d3 | 2016-08-02 22:13:17 +0000 | [diff] [blame] | 30 | import android.app.AppGlobals; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 31 | import android.app.IActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 32 | import android.app.IStopUserCallback; |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 33 | import android.app.KeyguardManager; |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 34 | import android.app.PendingIntent; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 38 | import android.content.IntentFilter; |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 39 | import android.content.IntentSender; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 42 | import android.content.pm.UserInfo; |
Lenka Trochtova | 02fee15 | 2015-12-22 14:26:18 +0100 | [diff] [blame] | 43 | import android.content.res.Resources; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 44 | import android.graphics.Bitmap; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 45 | import android.os.Binder; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 46 | import android.os.Build; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 47 | import android.os.Bundle; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 48 | import android.os.Debug; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 49 | import android.os.Environment; |
| 50 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 51 | import android.os.Handler; |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 52 | import android.os.IBinder; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 53 | import android.os.IUserManager; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 54 | import android.os.Message; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 55 | import android.os.ParcelFileDescriptor; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 56 | import android.os.Parcelable; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 57 | import android.os.PersistableBundle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 58 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 59 | import android.os.RemoteException; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 60 | import android.os.ResultReceiver; |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 61 | import android.os.SELinux; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 63 | import android.os.ShellCallback; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 64 | import android.os.ShellCommand; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 65 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 66 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 67 | import android.os.UserManager; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 68 | import android.os.UserManager.EnforcingUser; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 69 | import android.os.UserManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 70 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 71 | import android.os.storage.StorageManager; |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 72 | import android.security.GateKeeper; |
| 73 | import android.service.gatekeeper.IGateKeeperService; |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 74 | import android.system.ErrnoException; |
| 75 | import android.system.Os; |
| 76 | import android.system.OsConstants; |
Amith Yamasani | eb437d4 | 2016-04-29 09:31:25 -0700 | [diff] [blame] | 77 | import android.text.TextUtils; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 78 | import android.util.AtomicFile; |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 79 | import android.util.IntArray; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 80 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 81 | import android.util.Slog; |
| 82 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 83 | import android.util.SparseBooleanArray; |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 84 | import android.util.SparseIntArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 85 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 86 | import android.util.Xml; |
| 87 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 88 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 89 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 90 | import com.android.internal.app.IAppOpsService; |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 91 | import com.android.internal.logging.MetricsLogger; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 92 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 93 | import com.android.internal.util.FastXmlSerializer; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 94 | import com.android.internal.util.Preconditions; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 95 | import com.android.internal.util.XmlUtils; |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 96 | import com.android.internal.widget.LockPatternUtils; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 97 | import com.android.server.LocalServices; |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 98 | import com.android.server.LockGuard; |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 99 | import com.android.server.SystemService; |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 100 | import com.android.server.am.UserState; |
Suprabh Shukla | c5e057c | 2016-08-08 16:22:44 -0700 | [diff] [blame] | 101 | import com.android.server.storage.DeviceStorageMonitorInternal; |
Tony Mak | 6dc428f | 2016-10-10 15:48:27 +0100 | [diff] [blame] | 102 | |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 103 | import libcore.io.IoUtils; |
| 104 | import libcore.util.Objects; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 105 | |
| 106 | import org.xmlpull.v1.XmlPullParser; |
| 107 | import org.xmlpull.v1.XmlPullParserException; |
| 108 | import org.xmlpull.v1.XmlSerializer; |
| 109 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 110 | import java.io.BufferedOutputStream; |
| 111 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 112 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 113 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 114 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 115 | import java.io.FileOutputStream; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 116 | import java.io.InputStream; |
| 117 | import java.io.OutputStream; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 118 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 119 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 120 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 121 | import java.util.ArrayList; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 122 | import java.util.Collections; |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 123 | import java.util.LinkedList; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 124 | import java.util.List; |
| 125 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Service for {@link UserManager}. |
| 128 | * |
| 129 | * Method naming convention: |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 130 | * <ul> |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 131 | * <li> Methods suffixed with "LAr" should be called within the {@link #mAppRestrictionsLock} lock. |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 132 | * <li> Methods suffixed with "LP" should be called within the {@link #mPackagesLock} lock. |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 133 | * <li> Methods suffixed with "LR" should be called within the {@link #mRestrictionsLock} lock. |
| 134 | * <li> Methods suffixed with "LU" should be called within the {@link #mUsersLock} lock. |
| 135 | * </ul> |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 136 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 137 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 138 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 139 | private static final String LOG_TAG = "UserManagerService"; |
Makoto Onuki | 81c61ea | 2016-01-22 11:22:26 -0800 | [diff] [blame] | 140 | static final boolean DBG = false; // DO NOT SUBMIT WITH TRUE |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 141 | private static final boolean DBG_WITH_STACKTRACE = false; // DO NOT SUBMIT WITH TRUE |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 142 | // Can be used for manual testing of id recycling |
| 143 | private static final boolean RELEASE_DELETED_USER_ID = false; // DO NOT SUBMIT WITH TRUE |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 144 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 145 | private static final String TAG_NAME = "name"; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 146 | private static final String TAG_ACCOUNT = "account"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 147 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 148 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 149 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 150 | private static final String ATTR_CREATION_TIME = "created"; |
| 151 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 152 | private static final String ATTR_LAST_LOGGED_IN_FINGERPRINT = "lastLoggedInFingerprint"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 153 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 154 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 155 | private static final String ATTR_PARTIAL = "partial"; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 156 | private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 157 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 158 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 159 | private static final String ATTR_PROFILE_BADGE = "profileBadge"; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 160 | private static final String ATTR_RESTRICTED_PROFILE_PARENT_ID = "restrictedProfileParentId"; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 161 | private static final String ATTR_SEED_ACCOUNT_NAME = "seedAccountName"; |
| 162 | private static final String ATTR_SEED_ACCOUNT_TYPE = "seedAccountType"; |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 163 | private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 164 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 165 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 166 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 167 | private static final String TAG_DEVICE_POLICY_RESTRICTIONS = "device_policy_restrictions"; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 168 | private static final String TAG_DEVICE_POLICY_GLOBAL_RESTRICTIONS = |
| 169 | "device_policy_global_restrictions"; |
| 170 | /** Legacy name for device owner id tag. */ |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 171 | private static final String TAG_GLOBAL_RESTRICTION_OWNER_ID = "globalRestrictionOwnerUserId"; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 172 | private static final String TAG_DEVICE_OWNER_USER_ID = "deviceOwnerUserId"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 173 | private static final String TAG_ENTRY = "entry"; |
| 174 | private static final String TAG_VALUE = "value"; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 175 | private static final String TAG_SEED_ACCOUNT_OPTIONS = "seedAccountOptions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 176 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 177 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 178 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 179 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 180 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 181 | private static final String ATTR_TYPE_STRING = "s"; |
| 182 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
Amith Yamasani | 5b5aa40 | 2014-06-01 20:10:14 -0700 | [diff] [blame] | 183 | private static final String ATTR_TYPE_INTEGER = "i"; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 184 | private static final String ATTR_TYPE_BUNDLE = "B"; |
| 185 | private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 186 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 187 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 188 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 189 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 190 | private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 191 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 192 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 193 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 194 | |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 195 | private static final int ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION = |
| 196 | UserInfo.FLAG_MANAGED_PROFILE |
| 197 | | UserInfo.FLAG_EPHEMERAL |
| 198 | | UserInfo.FLAG_RESTRICTED |
Sudheer Shanka | 234d1af | 2016-08-26 15:42:53 -0700 | [diff] [blame] | 199 | | UserInfo.FLAG_GUEST |
| 200 | | UserInfo.FLAG_DEMO; |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 201 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 202 | @VisibleForTesting |
| 203 | static final int MIN_USER_ID = 10; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 204 | // We need to keep process uid within Integer.MAX_VALUE. |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 205 | @VisibleForTesting |
| 206 | static final int MAX_USER_ID = Integer.MAX_VALUE / UserHandle.PER_USER_RANGE; |
| 207 | |
| 208 | // Max size of the queue of recently removed users |
| 209 | @VisibleForTesting |
| 210 | static final int MAX_RECENTLY_REMOVED_IDS_SIZE = 100; |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 211 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 212 | private static final int USER_VERSION = 7; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 213 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 214 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 215 | |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 216 | // Maximum number of managed profiles permitted per user is 1. This cannot be increased |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 217 | // without first making sure that the rest of the framework is prepared for it. |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 218 | @VisibleForTesting |
| 219 | static final int MAX_MANAGED_PROFILES = 1; |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 220 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 221 | static final int WRITE_USER_MSG = 1; |
| 222 | static final int WRITE_USER_DELAY = 2*1000; // 2 seconds |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 223 | |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 224 | // Tron counters |
| 225 | private static final String TRON_GUEST_CREATED = "users_guest_created"; |
| 226 | private static final String TRON_USER_CREATED = "users_user_created"; |
Christine Franks | 8822056 | 2017-05-24 11:11:21 -0700 | [diff] [blame] | 227 | private static final String TRON_DEMO_CREATED = "users_demo_created"; |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 228 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 229 | private final Context mContext; |
| 230 | private final PackageManagerService mPm; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 231 | private final Object mPackagesLock; |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 232 | private final UserDataPreparer mUserDataPreparer; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 233 | // Short-term lock for internal state, when interaction/sync with PM is not required |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 234 | private final Object mUsersLock = LockGuard.installNewLock(LockGuard.INDEX_USER); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 235 | private final Object mRestrictionsLock = new Object(); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 236 | // Used for serializing access to app restriction files |
| 237 | private final Object mAppRestrictionsLock = new Object(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 239 | private final Handler mHandler; |
| 240 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 241 | private final File mUsersDir; |
| 242 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 243 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 244 | private static final IBinder mUserRestriconToken = new Binder(); |
| 245 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 246 | /** |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 247 | * User-related information that is used for persisting to flash. Only UserInfo is |
| 248 | * directly exposed to other system apps. |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 249 | */ |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 250 | @VisibleForTesting |
| 251 | static class UserData { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 252 | // Basic user information and properties |
| 253 | UserInfo info; |
| 254 | // Account name used when there is a strong association between a user and an account |
| 255 | String account; |
| 256 | // Account information for seeding into a newly created user. This could also be |
| 257 | // used for login validation for an existing user, for updating their credentials. |
| 258 | // In the latter case, data may not need to be persisted as it is only valid for the |
| 259 | // current login session. |
| 260 | String seedAccountName; |
| 261 | String seedAccountType; |
| 262 | PersistableBundle seedAccountOptions; |
| 263 | // Whether to perist the seed account information to be available after a boot |
| 264 | boolean persistSeedData; |
| 265 | |
| 266 | void clearSeedAccountData() { |
| 267 | seedAccountName = null; |
| 268 | seedAccountType = null; |
| 269 | seedAccountOptions = null; |
| 270 | persistSeedData = false; |
| 271 | } |
| 272 | } |
| 273 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 274 | @GuardedBy("mUsersLock") |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 275 | private final SparseArray<UserData> mUsers = new SparseArray<>(); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 276 | |
| 277 | /** |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 278 | * User restrictions set via UserManager. This doesn't include restrictions set by |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 279 | * device owner / profile owners. Only non-empty restriction bundles are stored. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 280 | * |
| 281 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 282 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 283 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 284 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 285 | * (Otherwise we won't be able to detect what restrictions have changed in |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 286 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 287 | */ |
| 288 | @GuardedBy("mRestrictionsLock") |
| 289 | private final SparseArray<Bundle> mBaseUserRestrictions = new SparseArray<>(); |
| 290 | |
| 291 | /** |
| 292 | * Cached user restrictions that are in effect -- i.e. {@link #mBaseUserRestrictions} combined |
| 293 | * with device / profile owner restrictions. We'll initialize it lazily; use |
| 294 | * {@link #getEffectiveUserRestrictions} to access it. |
| 295 | * |
| 296 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 297 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 298 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 299 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 300 | * (Otherwise we won't be able to detect what restrictions have changed in |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 301 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 302 | */ |
| 303 | @GuardedBy("mRestrictionsLock") |
| 304 | private final SparseArray<Bundle> mCachedEffectiveUserRestrictions = new SparseArray<>(); |
| 305 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 306 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 307 | * User restrictions that have already been applied in |
| 308 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)}. We use it to detect restrictions |
| 309 | * that have changed since the last |
| 310 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)} call. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 311 | */ |
| 312 | @GuardedBy("mRestrictionsLock") |
| 313 | private final SparseArray<Bundle> mAppliedUserRestrictions = new SparseArray<>(); |
| 314 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 315 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 316 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 317 | * that should be applied to all users, including guests. Only non-empty restriction bundles are |
| 318 | * stored. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 319 | */ |
| 320 | @GuardedBy("mRestrictionsLock") |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 321 | private final SparseArray<Bundle> mDevicePolicyGlobalUserRestrictions = new SparseArray<>(); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 322 | |
| 323 | /** |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 324 | * Id of the user that set global restrictions. |
| 325 | */ |
| 326 | @GuardedBy("mRestrictionsLock") |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 327 | private int mDeviceOwnerUserId = UserHandle.USER_NULL; |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 328 | |
| 329 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 330 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 331 | * for each user. Only non-empty restriction bundles are stored. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 332 | */ |
| 333 | @GuardedBy("mRestrictionsLock") |
| 334 | private final SparseArray<Bundle> mDevicePolicyLocalUserRestrictions = new SparseArray<>(); |
| 335 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 336 | @GuardedBy("mGuestRestrictions") |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 337 | private final Bundle mGuestRestrictions = new Bundle(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 341 | * for several seconds to work around a VFS caching issue. |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 342 | * Use {@link #addRemovingUserIdLocked(int)} to add elements to this array |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 343 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 344 | @GuardedBy("mUsersLock") |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 345 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 346 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Queue of recently removed userIds. Used for recycling of userIds |
| 349 | */ |
| 350 | @GuardedBy("mUsersLock") |
| 351 | private final LinkedList<Integer> mRecentlyRemovedIds = new LinkedList<>(); |
| 352 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 353 | @GuardedBy("mUsersLock") |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 354 | private int[] mUserIds; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 355 | @GuardedBy("mPackagesLock") |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 356 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 357 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 358 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 359 | private IAppOpsService mAppOpsService; |
| 360 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 361 | private final LocalService mLocalService; |
| 362 | |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 363 | @GuardedBy("mUsersLock") |
| 364 | private boolean mIsDeviceManaged; |
| 365 | |
| 366 | @GuardedBy("mUsersLock") |
| 367 | private final SparseBooleanArray mIsUserManaged = new SparseBooleanArray(); |
| 368 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 369 | @GuardedBy("mUserRestrictionsListeners") |
| 370 | private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners = |
| 371 | new ArrayList<>(); |
| 372 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 373 | private final LockPatternUtils mLockPatternUtils; |
| 374 | |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 375 | private final String ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK = |
| 376 | "com.android.server.pm.DISABLE_QUIET_MODE_AFTER_UNLOCK"; |
| 377 | |
| 378 | private final BroadcastReceiver mDisableQuietModeCallback = new BroadcastReceiver() { |
| 379 | @Override |
| 380 | public void onReceive(Context context, Intent intent) { |
| 381 | if (ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK.equals(intent.getAction())) { |
| 382 | final IntentSender target = intent.getParcelableExtra(Intent.EXTRA_INTENT); |
| 383 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_ID, 0); |
| 384 | setQuietModeEnabled(userHandle, false); |
| 385 | if (target != null) { |
| 386 | try { |
| 387 | mContext.startIntentSender(target, null, 0, 0, 0); |
| 388 | } catch (IntentSender.SendIntentException e) { |
| 389 | /* ignore */ |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | }; |
| 395 | |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 396 | /** |
| 397 | * Whether all users should be created ephemeral. |
| 398 | */ |
| 399 | @GuardedBy("mUsersLock") |
| 400 | private boolean mForceEphemeralUsers; |
| 401 | |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 402 | @GuardedBy("mUserStates") |
| 403 | private final SparseIntArray mUserStates = new SparseIntArray(); |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 404 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 405 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 406 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 407 | public static UserManagerService getInstance() { |
| 408 | synchronized (UserManagerService.class) { |
| 409 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 410 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 411 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 412 | |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 413 | public static class LifeCycle extends SystemService { |
| 414 | |
| 415 | private UserManagerService mUms; |
| 416 | |
| 417 | /** |
| 418 | * @param context |
| 419 | */ |
| 420 | public LifeCycle(Context context) { |
| 421 | super(context); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onStart() { |
| 426 | mUms = UserManagerService.getInstance(); |
| 427 | publishBinderService(Context.USER_SERVICE, mUms); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void onBootPhase(int phase) { |
| 432 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 433 | mUms.cleanupPartialUsers(); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 438 | // TODO b/28848102 Add support for test dependencies injection |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 439 | @VisibleForTesting |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 440 | UserManagerService(Context context) { |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 441 | this(context, null, null, new Object(), context.getCacheDir()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Called by package manager to create the service. This is closely |
| 446 | * associated with the package manager, and the given lock is the |
| 447 | * package manager's own lock. |
| 448 | */ |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 449 | UserManagerService(Context context, PackageManagerService pm, UserDataPreparer userDataPreparer, |
| 450 | Object packagesLock) { |
| 451 | this(context, pm, userDataPreparer, packagesLock, Environment.getDataDirectory()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 454 | private UserManagerService(Context context, PackageManagerService pm, |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 455 | UserDataPreparer userDataPreparer, Object packagesLock, File dataDir) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 456 | mContext = context; |
| 457 | mPm = pm; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 458 | mPackagesLock = packagesLock; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 459 | mHandler = new MainHandler(); |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 460 | mUserDataPreparer = userDataPreparer; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 461 | synchronized (mPackagesLock) { |
| 462 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 463 | mUsersDir.mkdirs(); |
| 464 | // Make zeroth user directory, for services to migrate their files to that location |
| 465 | File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM)); |
| 466 | userZeroDir.mkdirs(); |
| 467 | FileUtils.setPermissions(mUsersDir.toString(), |
| 468 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, |
| 469 | -1, -1); |
| 470 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 471 | initDefaultGuestRestrictions(); |
| 472 | readUserListLP(); |
| 473 | sInstance = this; |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 474 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 475 | mLocalService = new LocalService(); |
| 476 | LocalServices.addService(UserManagerInternal.class, mLocalService); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 477 | mLockPatternUtils = new LockPatternUtils(mContext); |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 478 | mUserStates.put(UserHandle.USER_SYSTEM, UserState.STATE_BOOTING); |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void systemReady() { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 482 | mAppOpsService = IAppOpsService.Stub.asInterface( |
| 483 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 484 | |
| 485 | synchronized (mRestrictionsLock) { |
| 486 | applyUserRestrictionsLR(UserHandle.USER_SYSTEM); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 487 | } |
Samuel Tan | d9453b8 | 2016-03-14 15:57:02 -0700 | [diff] [blame] | 488 | |
| 489 | UserInfo currentGuestUser = findCurrentGuestUser(); |
| 490 | if (currentGuestUser != null && !hasUserRestriction( |
| 491 | UserManager.DISALLOW_CONFIG_WIFI, currentGuestUser.id)) { |
| 492 | // If a guest user currently exists, apply the DISALLOW_CONFIG_WIFI option |
| 493 | // to it, in case this guest was created in a previous version where this |
| 494 | // user restriction was not a default guest restriction. |
| 495 | setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true, currentGuestUser.id); |
| 496 | } |
Amith Yamasani | eb437d4 | 2016-04-29 09:31:25 -0700 | [diff] [blame] | 497 | |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 498 | mContext.registerReceiver(mDisableQuietModeCallback, |
| 499 | new IntentFilter(ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK), |
| 500 | null, mHandler); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 503 | void cleanupPartialUsers() { |
| 504 | // Prune out any partially created, partially removed and ephemeral users. |
| 505 | ArrayList<UserInfo> partials = new ArrayList<>(); |
| 506 | synchronized (mUsersLock) { |
| 507 | final int userSize = mUsers.size(); |
| 508 | for (int i = 0; i < userSize; i++) { |
| 509 | UserInfo ui = mUsers.valueAt(i).info; |
| 510 | if ((ui.partial || ui.guestToRemove || ui.isEphemeral()) && i != 0) { |
| 511 | partials.add(ui); |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 512 | addRemovingUserIdLocked(ui.id); |
Amith Yamasani | ae26189 | 2016-06-27 10:40:09 -0700 | [diff] [blame] | 513 | ui.partial = true; |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
| 517 | final int partialsSize = partials.size(); |
| 518 | for (int i = 0; i < partialsSize; i++) { |
| 519 | UserInfo ui = partials.get(i); |
| 520 | Slog.w(LOG_TAG, "Removing partially created user " + ui.id |
| 521 | + " (name=" + ui.name + ")"); |
| 522 | removeUserState(ui.id); |
| 523 | } |
| 524 | } |
| 525 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 526 | @Override |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 527 | public String getUserAccount(int userId) { |
| 528 | checkManageUserAndAcrossUsersFullPermission("get user account"); |
| 529 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 530 | return mUsers.get(userId).account; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void setUserAccount(int userId, String accountName) { |
| 536 | checkManageUserAndAcrossUsersFullPermission("set user account"); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 537 | UserData userToUpdate = null; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 538 | synchronized (mPackagesLock) { |
| 539 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 540 | final UserData userData = mUsers.get(userId); |
| 541 | if (userData == null) { |
| 542 | Slog.e(LOG_TAG, "User not found for setting user account: u" + userId); |
| 543 | return; |
| 544 | } |
| 545 | String currentAccount = userData.account; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 546 | if (!Objects.equal(currentAccount, accountName)) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 547 | userData.account = accountName; |
| 548 | userToUpdate = userData; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | if (userToUpdate != null) { |
| 553 | writeUserLP(userToUpdate); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | @Override |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 559 | public UserInfo getPrimaryUser() { |
| 560 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 561 | synchronized (mUsersLock) { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 562 | final int userSize = mUsers.size(); |
| 563 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 564 | UserInfo ui = mUsers.valueAt(i).info; |
Xiaohui Chen | d3e9e18 | 2015-11-18 13:37:32 -0800 | [diff] [blame] | 565 | if (ui.isPrimary() && !mRemovingUserIds.get(ui.id)) { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 566 | return ui; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | return null; |
| 571 | } |
| 572 | |
| 573 | @Override |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 574 | public @NonNull List<UserInfo> getUsers(boolean excludeDying) { |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 575 | checkManageOrCreateUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 576 | synchronized (mUsersLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 577 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 578 | final int userSize = mUsers.size(); |
| 579 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 580 | UserInfo ui = mUsers.valueAt(i).info; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 581 | if (ui.partial) { |
| 582 | continue; |
| 583 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 584 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Amith Yamasani | 6f48d6e | 2016-03-23 14:28:25 -0700 | [diff] [blame] | 585 | users.add(userWithName(ui)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 586 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 587 | } |
| 588 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 589 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 592 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 593 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Fyodor Kupolov | c533b56 | 2016-04-01 14:37:07 -0700 | [diff] [blame] | 594 | boolean returnFullInfo = true; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 595 | if (userId != UserHandle.getCallingUserId()) { |
Sudheer Shanka | 7468091 | 2016-07-29 11:03:37 -0700 | [diff] [blame] | 596 | checkManageOrCreateUsersPermission("getting profiles related to user " + userId); |
Fyodor Kupolov | c533b56 | 2016-04-01 14:37:07 -0700 | [diff] [blame] | 597 | } else { |
| 598 | returnFullInfo = hasManageUsersPermission(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 599 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 600 | final long ident = Binder.clearCallingIdentity(); |
| 601 | try { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 602 | synchronized (mUsersLock) { |
Fyodor Kupolov | c533b56 | 2016-04-01 14:37:07 -0700 | [diff] [blame] | 603 | return getProfilesLU(userId, enabledOnly, returnFullInfo); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 604 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 605 | } finally { |
| 606 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 610 | @Override |
| 611 | public int[] getProfileIds(int userId, boolean enabledOnly) { |
| 612 | if (userId != UserHandle.getCallingUserId()) { |
bohu | 12d23a1 | 2016-09-26 16:20:07 -0700 | [diff] [blame] | 613 | checkManageOrCreateUsersPermission("getting profiles related to user " + userId); |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 614 | } |
| 615 | final long ident = Binder.clearCallingIdentity(); |
| 616 | try { |
| 617 | synchronized (mUsersLock) { |
| 618 | return getProfileIdsLU(userId, enabledOnly).toArray(); |
| 619 | } |
| 620 | } finally { |
| 621 | Binder.restoreCallingIdentity(ident); |
| 622 | } |
| 623 | } |
| 624 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 625 | /** Assume permissions already checked and caller's identity cleared */ |
Fyodor Kupolov | c533b56 | 2016-04-01 14:37:07 -0700 | [diff] [blame] | 626 | private List<UserInfo> getProfilesLU(int userId, boolean enabledOnly, boolean fullInfo) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 627 | IntArray profileIds = getProfileIdsLU(userId, enabledOnly); |
| 628 | ArrayList<UserInfo> users = new ArrayList<>(profileIds.size()); |
| 629 | for (int i = 0; i < profileIds.size(); i++) { |
| 630 | int profileId = profileIds.get(i); |
| 631 | UserInfo userInfo = mUsers.get(profileId).info; |
| 632 | // If full info is not required - clear PII data to prevent 3P apps from reading it |
| 633 | if (!fullInfo) { |
| 634 | userInfo = new UserInfo(userInfo); |
| 635 | userInfo.name = null; |
| 636 | userInfo.iconPath = null; |
| 637 | } else { |
| 638 | userInfo = userWithName(userInfo); |
| 639 | } |
| 640 | users.add(userInfo); |
| 641 | } |
| 642 | return users; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Assume permissions already checked and caller's identity cleared |
| 647 | */ |
| 648 | private IntArray getProfileIdsLU(int userId, boolean enabledOnly) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 649 | UserInfo user = getUserInfoLU(userId); |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 650 | IntArray result = new IntArray(mUsers.size()); |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 651 | if (user == null) { |
| 652 | // Probably a dying user |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 653 | return result; |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 654 | } |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 655 | final int userSize = mUsers.size(); |
| 656 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 657 | UserInfo profile = mUsers.valueAt(i).info; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 658 | if (!isProfileOf(user, profile)) { |
| 659 | continue; |
| 660 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 661 | if (enabledOnly && !profile.isEnabled()) { |
| 662 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 663 | } |
Amith Yamasani | 70fcf0c | 2014-07-11 08:40:19 -0700 | [diff] [blame] | 664 | if (mRemovingUserIds.get(profile.id)) { |
| 665 | continue; |
| 666 | } |
Tony Mak | 80189cd | 2016-04-05 17:21:42 +0100 | [diff] [blame] | 667 | if (profile.partial) { |
| 668 | continue; |
| 669 | } |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 670 | result.add(profile.id); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 671 | } |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 672 | return result; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 675 | @Override |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 676 | public int getCredentialOwnerProfile(int userHandle) { |
| 677 | checkManageUsersPermission("get the credential owner"); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 678 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 679 | synchronized (mUsersLock) { |
| 680 | UserInfo profileParent = getProfileParentLU(userHandle); |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 681 | if (profileParent != null) { |
| 682 | return profileParent.id; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return userHandle; |
| 688 | } |
| 689 | |
| 690 | @Override |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 691 | public boolean isSameProfileGroup(int userId, int otherUserId) { |
| 692 | if (userId == otherUserId) return true; |
| 693 | checkManageUsersPermission("check if in the same profile group"); |
Fyodor Kupolov | 0fb772e | 2016-10-13 16:27:34 -0700 | [diff] [blame] | 694 | return isSameProfileGroupNoChecks(userId, otherUserId); |
| 695 | } |
| 696 | |
| 697 | private boolean isSameProfileGroupNoChecks(int userId, int otherUserId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 698 | synchronized (mUsersLock) { |
| 699 | UserInfo userInfo = getUserInfoLU(userId); |
| 700 | if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 701 | return false; |
| 702 | } |
| 703 | UserInfo otherUserInfo = getUserInfoLU(otherUserId); |
| 704 | if (otherUserInfo == null |
| 705 | || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 706 | return false; |
| 707 | } |
| 708 | return userInfo.profileGroupId == otherUserInfo.profileGroupId; |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 709 | } |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | @Override |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 713 | public UserInfo getProfileParent(int userHandle) { |
| 714 | checkManageUsersPermission("get the profile parent"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 715 | synchronized (mUsersLock) { |
| 716 | return getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 720 | private UserInfo getProfileParentLU(int userHandle) { |
| 721 | UserInfo profile = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 722 | if (profile == null) { |
| 723 | return null; |
| 724 | } |
| 725 | int parentUserId = profile.profileGroupId; |
Amith Yamasani | 801e342 | 2017-01-25 11:35:44 -0800 | [diff] [blame] | 726 | if (parentUserId == userHandle || parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 727 | return null; |
| 728 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 729 | return getUserInfoLU(parentUserId); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 733 | private static boolean isProfileOf(UserInfo user, UserInfo profile) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 734 | return user.id == profile.id || |
| 735 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 736 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 739 | private void broadcastProfileAvailabilityChanges(UserHandle profileHandle, |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 740 | UserHandle parentHandle, boolean inQuietMode) { |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 741 | Intent intent = new Intent(); |
| 742 | if (inQuietMode) { |
| 743 | intent.setAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 744 | } else { |
| 745 | intent.setAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 746 | } |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 747 | intent.putExtra(Intent.EXTRA_QUIET_MODE, inQuietMode); |
| 748 | intent.putExtra(Intent.EXTRA_USER, profileHandle); |
| 749 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileHandle.getIdentifier()); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 750 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 751 | mContext.sendBroadcastAsUser(intent, parentHandle); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public void setQuietModeEnabled(int userHandle, boolean enableQuietMode) { |
| 756 | checkManageUsersPermission("silence profile"); |
| 757 | boolean changed = false; |
| 758 | UserInfo profile, parent; |
| 759 | synchronized (mPackagesLock) { |
| 760 | synchronized (mUsersLock) { |
| 761 | profile = getUserInfoLU(userHandle); |
| 762 | parent = getProfileParentLU(userHandle); |
| 763 | |
| 764 | } |
| 765 | if (profile == null || !profile.isManagedProfile()) { |
| 766 | throw new IllegalArgumentException("User " + userHandle + " is not a profile"); |
| 767 | } |
| 768 | if (profile.isQuietModeEnabled() != enableQuietMode) { |
| 769 | profile.flags ^= UserInfo.FLAG_QUIET_MODE; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 770 | writeUserLP(getUserDataLU(profile.id)); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 771 | changed = true; |
| 772 | } |
| 773 | } |
| 774 | if (changed) { |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 775 | long identity = Binder.clearCallingIdentity(); |
| 776 | try { |
| 777 | if (enableQuietMode) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 778 | ActivityManager.getService().stopUser(userHandle, /* force */true, null); |
Rubin Xu | f8451b9 | 2016-04-01 15:50:58 +0100 | [diff] [blame] | 779 | LocalServices.getService(ActivityManagerInternal.class) |
| 780 | .killForegroundAppsForUser(userHandle); |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 781 | } else { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 782 | ActivityManager.getService().startUserInBackground(userHandle); |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 783 | } |
| 784 | } catch (RemoteException e) { |
| 785 | Slog.e(LOG_TAG, "fail to start/stop user for quiet mode", e); |
| 786 | } finally { |
| 787 | Binder.restoreCallingIdentity(identity); |
| 788 | } |
| 789 | |
| 790 | broadcastProfileAvailabilityChanges(profile.getUserHandle(), parent.getUserHandle(), |
| 791 | enableQuietMode); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public boolean isQuietModeEnabled(int userHandle) { |
| 797 | synchronized (mPackagesLock) { |
| 798 | UserInfo info; |
| 799 | synchronized (mUsersLock) { |
| 800 | info = getUserInfoLU(userHandle); |
| 801 | } |
| 802 | if (info == null || !info.isManagedProfile()) { |
Rubin Xu | f13c980 | 2016-01-21 18:06:00 +0000 | [diff] [blame] | 803 | return false; |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 804 | } |
| 805 | return info.isQuietModeEnabled(); |
| 806 | } |
| 807 | } |
| 808 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 809 | @Override |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 810 | public boolean trySetQuietModeDisabled(int userHandle, IntentSender target) { |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 811 | checkManageUsersPermission("silence profile"); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 812 | if (StorageManager.isUserKeyUnlocked(userHandle) |
Ricky Wai | 9cc7ad6 | 2016-05-11 18:00:20 +0100 | [diff] [blame] | 813 | || !mLockPatternUtils.isSecure(userHandle)) { |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 814 | // if the user is already unlocked, no need to show a profile challenge |
| 815 | setQuietModeEnabled(userHandle, false); |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | long identity = Binder.clearCallingIdentity(); |
| 820 | try { |
| 821 | // otherwise, we show a profile challenge to trigger decryption of the user |
| 822 | final KeyguardManager km = (KeyguardManager) mContext.getSystemService( |
| 823 | Context.KEYGUARD_SERVICE); |
Ricky Wai | 7881cf8 | 2016-04-15 17:20:12 +0100 | [diff] [blame] | 824 | // We should use userHandle not credentialOwnerUserId here, as even if it is unified |
| 825 | // lock, confirm screenlock page will know and show personal challenge, and unlock |
| 826 | // work profile when personal challenge is correct |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 827 | final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, |
| 828 | userHandle); |
| 829 | if (unlockIntent == null) { |
| 830 | return false; |
| 831 | } |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 832 | final Intent callBackIntent = new Intent( |
| 833 | ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK); |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 834 | if (target != null) { |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 835 | callBackIntent.putExtra(Intent.EXTRA_INTENT, target); |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 836 | } |
Ricky Wai | b1dd80b | 2016-06-07 18:00:55 +0100 | [diff] [blame] | 837 | callBackIntent.putExtra(Intent.EXTRA_USER_ID, userHandle); |
| 838 | callBackIntent.setPackage(mContext.getPackageName()); |
| 839 | callBackIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 840 | final PendingIntent pendingIntent = PendingIntent.getBroadcast( |
| 841 | mContext, |
| 842 | 0, |
| 843 | callBackIntent, |
| 844 | PendingIntent.FLAG_CANCEL_CURRENT | |
| 845 | PendingIntent.FLAG_ONE_SHOT | |
| 846 | PendingIntent.FLAG_IMMUTABLE); |
| 847 | // After unlocking the challenge, it will disable quiet mode and run the original |
| 848 | // intentSender |
| 849 | unlockIntent.putExtra(Intent.EXTRA_INTENT, pendingIntent.getIntentSender()); |
Benjamin Franz | f02420c | 2016-04-04 18:52:21 +0100 | [diff] [blame] | 850 | unlockIntent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 851 | mContext.startActivity(unlockIntent); |
| 852 | } finally { |
| 853 | Binder.restoreCallingIdentity(identity); |
| 854 | } |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 859 | public void setUserEnabled(int userId) { |
| 860 | checkManageUsersPermission("enable user"); |
| 861 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 862 | UserInfo info; |
| 863 | synchronized (mUsersLock) { |
| 864 | info = getUserInfoLU(userId); |
| 865 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 866 | if (info != null && !info.isEnabled()) { |
| 867 | info.flags ^= UserInfo.FLAG_DISABLED; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 868 | writeUserLP(getUserDataLU(info.id)); |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 873 | /** |
| 874 | * Evicts a user's CE key by stopping and restarting the user. |
| 875 | * |
| 876 | * The key is evicted automatically by the user controller when the user has stopped. |
| 877 | */ |
| 878 | @Override |
| 879 | public void evictCredentialEncryptionKey(@UserIdInt int userId) { |
| 880 | checkManageUsersPermission("evict CE key"); |
| 881 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 882 | final long identity = Binder.clearCallingIdentity(); |
| 883 | try { |
| 884 | am.restartUserInBackground(userId); |
| 885 | } catch (RemoteException re) { |
| 886 | throw re.rethrowAsRuntimeException(); |
| 887 | } finally { |
| 888 | Binder.restoreCallingIdentity(identity); |
| 889 | } |
| 890 | } |
| 891 | |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 892 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 893 | public UserInfo getUserInfo(int userId) { |
Sudheer Shanka | accaa08 | 2016-06-14 16:22:57 -0700 | [diff] [blame] | 894 | checkManageOrCreateUsersPermission("query user"); |
Tony Mak | 8673b28 | 2016-03-21 21:10:59 +0000 | [diff] [blame] | 895 | synchronized (mUsersLock) { |
Amith Yamasani | 6f48d6e | 2016-03-23 14:28:25 -0700 | [diff] [blame] | 896 | return userWithName(getUserInfoLU(userId)); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Returns a UserInfo object with the name filled in, for Owner, or the original |
| 902 | * if the name is already set. |
| 903 | */ |
| 904 | private UserInfo userWithName(UserInfo orig) { |
| 905 | if (orig != null && orig.name == null && orig.id == UserHandle.USER_SYSTEM) { |
| 906 | UserInfo withName = new UserInfo(orig); |
| 907 | withName.name = getOwnerName(); |
| 908 | return withName; |
| 909 | } else { |
| 910 | return orig; |
Tony Mak | 8673b28 | 2016-03-21 21:10:59 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | @Override |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 915 | public int getManagedProfileBadge(@UserIdInt int userId) { |
| 916 | int callingUserId = UserHandle.getCallingUserId(); |
| 917 | if (callingUserId != userId && !hasManageUsersPermission()) { |
| 918 | if (!isSameProfileGroupNoChecks(callingUserId, userId)) { |
| 919 | throw new SecurityException( |
| 920 | "You need MANAGE_USERS permission to: check if specified user a " + |
| 921 | "managed profile outside your profile group"); |
| 922 | } |
| 923 | } |
| 924 | synchronized (mUsersLock) { |
| 925 | UserInfo userInfo = getUserInfoLU(userId); |
| 926 | return userInfo != null ? userInfo.profileBadge : 0; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | @Override |
Tony Mak | 8673b28 | 2016-03-21 21:10:59 +0000 | [diff] [blame] | 931 | public boolean isManagedProfile(int userId) { |
Tony Mak | b531d08 | 2016-03-16 14:49:52 +0000 | [diff] [blame] | 932 | int callingUserId = UserHandle.getCallingUserId(); |
| 933 | if (callingUserId != userId && !hasManageUsersPermission()) { |
Fyodor Kupolov | 0fb772e | 2016-10-13 16:27:34 -0700 | [diff] [blame] | 934 | if (!isSameProfileGroupNoChecks(callingUserId, userId)) { |
Fyodor Kupolov | c413f70 | 2016-10-06 17:11:14 -0700 | [diff] [blame] | 935 | throw new SecurityException( |
| 936 | "You need MANAGE_USERS permission to: check if specified user a " + |
| 937 | "managed profile outside your profile group"); |
Tony Mak | b531d08 | 2016-03-16 14:49:52 +0000 | [diff] [blame] | 938 | } |
Tony Mak | 4dc008c | 2016-03-16 10:46:49 +0000 | [diff] [blame] | 939 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 940 | synchronized (mUsersLock) { |
Amith Yamasani | 1c41dc8 | 2016-06-28 16:13:15 -0700 | [diff] [blame] | 941 | UserInfo userInfo = getUserInfoLU(userId); |
Tony Mak | 8673b28 | 2016-03-21 21:10:59 +0000 | [diff] [blame] | 942 | return userInfo != null && userInfo.isManagedProfile(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 946 | @Override |
Fyodor Kupolov | c413f70 | 2016-10-06 17:11:14 -0700 | [diff] [blame] | 947 | public boolean isUserUnlockingOrUnlocked(int userId) { |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 948 | checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserUnlockingOrUnlocked"); |
Fyodor Kupolov | c413f70 | 2016-10-06 17:11:14 -0700 | [diff] [blame] | 949 | return mLocalService.isUserUnlockingOrUnlocked(userId); |
| 950 | } |
| 951 | |
| 952 | @Override |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 953 | public boolean isUserUnlocked(int userId) { |
| 954 | checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserUnlocked"); |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 955 | return mLocalService.isUserUnlocked(userId); |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | @Override |
| 959 | public boolean isUserRunning(int userId) { |
| 960 | checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserRunning"); |
| 961 | return mLocalService.isUserRunning(userId); |
| 962 | } |
| 963 | |
| 964 | private void checkManageOrInteractPermIfCallerInOtherProfileGroup(int userId, String name) { |
| 965 | int callingUserId = UserHandle.getCallingUserId(); |
| 966 | if (callingUserId == userId || isSameProfileGroupNoChecks(callingUserId, userId) || |
| 967 | hasManageUsersPermission()) { |
| 968 | return; |
| 969 | } |
| 970 | if (ActivityManager.checkComponentPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 971 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 972 | throw new SecurityException("You need INTERACT_ACROSS_USERS or MANAGE_USERS permission " |
| 973 | + "to: check " + name); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | @Override |
Amith Yamasani | 1c41dc8 | 2016-06-28 16:13:15 -0700 | [diff] [blame] | 978 | public boolean isDemoUser(int userId) { |
| 979 | int callingUserId = UserHandle.getCallingUserId(); |
| 980 | if (callingUserId != userId && !hasManageUsersPermission()) { |
| 981 | throw new SecurityException("You need MANAGE_USERS permission to query if u=" + userId |
| 982 | + " is a demo user"); |
| 983 | } |
| 984 | synchronized (mUsersLock) { |
| 985 | UserInfo userInfo = getUserInfoLU(userId); |
| 986 | return userInfo != null && userInfo.isDemo(); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | @Override |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 991 | public boolean isRestricted() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 992 | synchronized (mUsersLock) { |
| 993 | return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 997 | @Override |
| 998 | public boolean canHaveRestrictedProfile(int userId) { |
| 999 | checkManageUsersPermission("canHaveRestrictedProfile"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1000 | synchronized (mUsersLock) { |
| 1001 | final UserInfo userInfo = getUserInfoLU(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1002 | if (userInfo == null || !userInfo.canHaveProfile()) { |
| 1003 | return false; |
| 1004 | } |
| 1005 | if (!userInfo.isAdmin()) { |
| 1006 | return false; |
| 1007 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 1008 | // restricted profile can be created if there is no DO set and the admin user has no PO; |
| 1009 | return !mIsDeviceManaged && !mIsUserManaged.get(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1010 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * Should be locked on mUsers before calling this. |
| 1015 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1016 | private UserInfo getUserInfoLU(int userId) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1017 | final UserData userData = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1018 | // If it is partial and not in the process of being removed, return as unknown user. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1019 | if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1020 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 1021 | return null; |
| 1022 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1023 | return userData != null ? userData.info : null; |
| 1024 | } |
| 1025 | |
| 1026 | private UserData getUserDataLU(int userId) { |
| 1027 | final UserData userData = mUsers.get(userId); |
| 1028 | // If it is partial and not in the process of being removed, return as unknown user. |
| 1029 | if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) { |
| 1030 | return null; |
| 1031 | } |
| 1032 | return userData; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1035 | /** |
| 1036 | * Obtains {@link #mUsersLock} and return UserInfo from mUsers. |
| 1037 | * <p>No permissions checking or any addition checks are made</p> |
| 1038 | */ |
| 1039 | private UserInfo getUserInfoNoChecks(int userId) { |
| 1040 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1041 | final UserData userData = mUsers.get(userId); |
| 1042 | return userData != null ? userData.info : null; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * Obtains {@link #mUsersLock} and return UserData from mUsers. |
| 1048 | * <p>No permissions checking or any addition checks are made</p> |
| 1049 | */ |
| 1050 | private UserData getUserDataNoChecks(int userId) { |
| 1051 | synchronized (mUsersLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1052 | return mUsers.get(userId); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Amith Yamasani | 236b2b5 | 2015-08-18 14:32:14 -0700 | [diff] [blame] | 1056 | /** Called by PackageManagerService */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1057 | public boolean exists(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1058 | return getUserInfoNoChecks(userId) != null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1061 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1062 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1063 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1064 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1065 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1066 | UserData userData = getUserDataNoChecks(userId); |
| 1067 | if (userData == null || userData.info.partial) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1068 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 1069 | return; |
| 1070 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1071 | if (name != null && !name.equals(userData.info.name)) { |
| 1072 | userData.info.name = name; |
| 1073 | writeUserLP(userData); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1074 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1077 | if (changed) { |
| 1078 | sendUserInfoChangedBroadcast(userId); |
| 1079 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1082 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1083 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1084 | checkManageUsersPermission("update users"); |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 1085 | if (hasUserRestriction(UserManager.DISALLOW_SET_USER_ICON, userId)) { |
| 1086 | Log.w(LOG_TAG, "Cannot set user icon. DISALLOW_SET_USER_ICON is enabled."); |
| 1087 | return; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1088 | } |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 1089 | mLocalService.setUserIcon(userId, bitmap); |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 1092 | |
| 1093 | |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1094 | private void sendUserInfoChangedBroadcast(int userId) { |
| 1095 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 1096 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1097 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 1098 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1101 | @Override |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1102 | public ParcelFileDescriptor getUserIcon(int targetUserId) { |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1103 | String iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1104 | synchronized (mPackagesLock) { |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1105 | UserInfo targetUserInfo = getUserInfoNoChecks(targetUserId); |
| 1106 | if (targetUserInfo == null || targetUserInfo.partial) { |
| 1107 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + targetUserId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1108 | return null; |
| 1109 | } |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1110 | |
| 1111 | final int callingUserId = UserHandle.getCallingUserId(); |
| 1112 | final int callingGroupId = getUserInfoNoChecks(callingUserId).profileGroupId; |
| 1113 | final int targetGroupId = targetUserInfo.profileGroupId; |
| 1114 | final boolean sameGroup = (callingGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 1115 | && callingGroupId == targetGroupId); |
| 1116 | if ((callingUserId != targetUserId) && !sameGroup) { |
Nicolas Prevot | 88cc346 | 2014-05-14 14:51:48 +0100 | [diff] [blame] | 1117 | checkManageUsersPermission("get the icon of a user who is not related"); |
| 1118 | } |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1119 | |
| 1120 | if (targetUserInfo.iconPath == null) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1121 | return null; |
| 1122 | } |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1123 | iconPath = targetUserInfo.iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1124 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1125 | |
| 1126 | try { |
| 1127 | return ParcelFileDescriptor.open( |
| 1128 | new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY); |
| 1129 | } catch (FileNotFoundException e) { |
| 1130 | Log.e(LOG_TAG, "Couldn't find icon file", e); |
| 1131 | } |
| 1132 | return null; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1135 | public void makeInitialized(int userId) { |
| 1136 | checkManageUsersPermission("makeInitialized"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1137 | boolean scheduleWriteUser = false; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1138 | UserData userData; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1139 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1140 | userData = mUsers.get(userId); |
| 1141 | if (userData == null || userData.info.partial) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1142 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1143 | return; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1144 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1145 | if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 1146 | userData.info.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1147 | scheduleWriteUser = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1150 | if (scheduleWriteUser) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1151 | scheduleWriteUser(userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1152 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1155 | /** |
| 1156 | * If default guest restrictions haven't been initialized yet, add the basic |
| 1157 | * restrictions. |
| 1158 | */ |
| 1159 | private void initDefaultGuestRestrictions() { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1160 | synchronized (mGuestRestrictions) { |
| 1161 | if (mGuestRestrictions.isEmpty()) { |
Samuel Tan | d9453b8 | 2016-03-14 15:57:02 -0700 | [diff] [blame] | 1162 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, true); |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 1163 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1164 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
| 1165 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
| 1166 | } |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1170 | @Override |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1171 | public Bundle getDefaultGuestRestrictions() { |
| 1172 | checkManageUsersPermission("getDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1173 | synchronized (mGuestRestrictions) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1174 | return new Bundle(mGuestRestrictions); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | @Override |
| 1179 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 1180 | checkManageUsersPermission("setDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1181 | synchronized (mGuestRestrictions) { |
| 1182 | mGuestRestrictions.clear(); |
| 1183 | mGuestRestrictions.putAll(restrictions); |
| 1184 | } |
| 1185 | synchronized (mPackagesLock) { |
| 1186 | writeUserListLP(); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1190 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1191 | * See {@link UserManagerInternal#setDevicePolicyUserRestrictions} |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1192 | */ |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1193 | private void setDevicePolicyUserRestrictionsInner(int userId, @Nullable Bundle restrictions, |
| 1194 | boolean isDeviceOwner, int cameraRestrictionScope) { |
| 1195 | final Bundle global = new Bundle(); |
| 1196 | final Bundle local = new Bundle(); |
| 1197 | |
| 1198 | // Sort restrictions into local and global ensuring they don't overlap. |
| 1199 | UserRestrictionsUtils.sortToGlobalAndLocal(restrictions, isDeviceOwner, |
| 1200 | cameraRestrictionScope, global, local); |
| 1201 | |
| 1202 | boolean globalChanged, localChanged; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1203 | synchronized (mRestrictionsLock) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1204 | // Update global and local restrictions if they were changed. |
| 1205 | globalChanged = updateRestrictionsIfNeededLR( |
| 1206 | userId, global, mDevicePolicyGlobalUserRestrictions); |
| 1207 | localChanged = updateRestrictionsIfNeededLR( |
| 1208 | userId, local, mDevicePolicyLocalUserRestrictions); |
| 1209 | |
| 1210 | if (isDeviceOwner) { |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1211 | // Remember the global restriction owner userId to be able to make a distinction |
| 1212 | // in getUserRestrictionSource on who set local policies. |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1213 | mDeviceOwnerUserId = userId; |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1214 | } else { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1215 | if (mDeviceOwnerUserId == userId) { |
Zoltan Szatmary-Ban | 8a5536d | 2016-05-27 17:56:44 +0100 | [diff] [blame] | 1216 | // When profile owner sets restrictions it passes null global bundle and we |
| 1217 | // reset global restriction owner userId. |
| 1218 | // This means this user used to have DO, but now the DO is gone and the user |
| 1219 | // instead has PO. |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1220 | mDeviceOwnerUserId = UserHandle.USER_NULL; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | if (DBG) { |
| 1225 | Log.d(LOG_TAG, "setDevicePolicyUserRestrictions: userId=" + userId |
| 1226 | + " global=" + global + (globalChanged ? " (changed)" : "") |
| 1227 | + " local=" + local + (localChanged ? " (changed)" : "") |
| 1228 | ); |
| 1229 | } |
| 1230 | // Don't call them within the mRestrictionsLock. |
| 1231 | synchronized (mPackagesLock) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1232 | if (localChanged || globalChanged) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1233 | writeUserLP(getUserDataNoChecks(userId)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | synchronized (mRestrictionsLock) { |
| 1238 | if (globalChanged) { |
| 1239 | applyUserRestrictionsForAllUsersLR(); |
| 1240 | } else if (localChanged) { |
| 1241 | applyUserRestrictionsLR(userId); |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1246 | /** |
| 1247 | * Updates restriction bundle for a given user in a given restriction array. If new bundle is |
| 1248 | * empty, record is removed from the array. |
| 1249 | * @return whether restrictions bundle is different from the old one. |
| 1250 | */ |
| 1251 | private boolean updateRestrictionsIfNeededLR(int userId, @Nullable Bundle restrictions, |
| 1252 | SparseArray<Bundle> restrictionsArray) { |
| 1253 | final boolean changed = |
| 1254 | !UserRestrictionsUtils.areEqual(restrictionsArray.get(userId), restrictions); |
| 1255 | if (changed) { |
| 1256 | if (!UserRestrictionsUtils.isEmpty(restrictions)) { |
| 1257 | restrictionsArray.put(userId, restrictions); |
| 1258 | } else { |
| 1259 | restrictionsArray.delete(userId); |
| 1260 | } |
| 1261 | } |
| 1262 | return changed; |
| 1263 | } |
| 1264 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1265 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1266 | private Bundle computeEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1267 | final Bundle baseRestrictions = |
| 1268 | UserRestrictionsUtils.nonNull(mBaseUserRestrictions.get(userId)); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1269 | final Bundle global = UserRestrictionsUtils.mergeAll(mDevicePolicyGlobalUserRestrictions); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1270 | final Bundle local = mDevicePolicyLocalUserRestrictions.get(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1271 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1272 | if (UserRestrictionsUtils.isEmpty(global) && UserRestrictionsUtils.isEmpty(local)) { |
| 1273 | // Common case first. |
| 1274 | return baseRestrictions; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1275 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1276 | final Bundle effective = UserRestrictionsUtils.clone(baseRestrictions); |
| 1277 | UserRestrictionsUtils.merge(effective, global); |
| 1278 | UserRestrictionsUtils.merge(effective, local); |
| 1279 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1280 | return effective; |
| 1281 | } |
| 1282 | |
| 1283 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1284 | private void invalidateEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1285 | if (DBG) { |
| 1286 | Log.d(LOG_TAG, "invalidateEffectiveUserRestrictions userId=" + userId); |
| 1287 | } |
| 1288 | mCachedEffectiveUserRestrictions.remove(userId); |
| 1289 | } |
| 1290 | |
| 1291 | private Bundle getEffectiveUserRestrictions(int userId) { |
| 1292 | synchronized (mRestrictionsLock) { |
| 1293 | Bundle restrictions = mCachedEffectiveUserRestrictions.get(userId); |
| 1294 | if (restrictions == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1295 | restrictions = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1296 | mCachedEffectiveUserRestrictions.put(userId, restrictions); |
| 1297 | } |
| 1298 | return restrictions; |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1302 | /** @return a specific user restriction that's in effect currently. */ |
| 1303 | @Override |
| 1304 | public boolean hasUserRestriction(String restrictionKey, int userId) { |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 1305 | if (!UserRestrictionsUtils.isValidRestriction(restrictionKey)) { |
| 1306 | return false; |
| 1307 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1308 | Bundle restrictions = getEffectiveUserRestrictions(userId); |
| 1309 | return restrictions != null && restrictions.getBoolean(restrictionKey); |
| 1310 | } |
| 1311 | |
| 1312 | /** |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1313 | * @hide |
| 1314 | * |
| 1315 | * Returns who set a user restriction on a user. |
| 1316 | * Requires {@link android.Manifest.permission#MANAGE_USERS} permission. |
| 1317 | * @param restrictionKey the string key representing the restriction |
| 1318 | * @param userId the id of the user for whom to retrieve the restrictions. |
| 1319 | * @return The source of user restriction. Any combination of |
| 1320 | * {@link UserManager#RESTRICTION_NOT_SET}, |
| 1321 | * {@link UserManager#RESTRICTION_SOURCE_SYSTEM}, |
| 1322 | * {@link UserManager#RESTRICTION_SOURCE_DEVICE_OWNER} |
| 1323 | * and {@link UserManager#RESTRICTION_SOURCE_PROFILE_OWNER} |
| 1324 | */ |
| 1325 | @Override |
| 1326 | public int getUserRestrictionSource(String restrictionKey, int userId) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1327 | List<EnforcingUser> enforcingUsers = getUserRestrictionSources(restrictionKey, userId); |
| 1328 | // Get "bitwise or" of restriction sources for all enforcing users. |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1329 | int result = UserManager.RESTRICTION_NOT_SET; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1330 | for (int i = enforcingUsers.size() - 1; i >= 0; i--) { |
| 1331 | result |= enforcingUsers.get(i).getUserRestrictionSource(); |
| 1332 | } |
| 1333 | return result; |
| 1334 | } |
| 1335 | |
| 1336 | @Override |
| 1337 | public List<EnforcingUser> getUserRestrictionSources( |
| 1338 | String restrictionKey, @UserIdInt int userId) { |
| 1339 | checkManageUsersPermission("getUserRestrictionSource"); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1340 | |
| 1341 | // Shortcut for the most common case |
| 1342 | if (!hasUserRestriction(restrictionKey, userId)) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1343 | return Collections.emptyList(); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1346 | final List<EnforcingUser> result = new ArrayList<>(); |
| 1347 | |
| 1348 | // Check if it is base restriction. |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1349 | if (hasBaseUserRestriction(restrictionKey, userId)) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1350 | result.add(new EnforcingUser( |
| 1351 | UserHandle.USER_NULL, UserManager.RESTRICTION_SOURCE_SYSTEM)); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1354 | synchronized (mRestrictionsLock) { |
| 1355 | // Check if it is set by profile owner. |
| 1356 | Bundle profileOwnerRestrictions = mDevicePolicyLocalUserRestrictions.get(userId); |
| 1357 | if (UserRestrictionsUtils.contains(profileOwnerRestrictions, restrictionKey)) { |
| 1358 | result.add(getEnforcingUserLocked(userId)); |
| 1359 | } |
| 1360 | |
| 1361 | // Iterate over all users who enforce global restrictions. |
| 1362 | for (int i = mDevicePolicyGlobalUserRestrictions.size() - 1; i >= 0; i--) { |
| 1363 | Bundle globalRestrictions = mDevicePolicyGlobalUserRestrictions.valueAt(i); |
| 1364 | int profileUserId = mDevicePolicyGlobalUserRestrictions.keyAt(i); |
| 1365 | if (UserRestrictionsUtils.contains(globalRestrictions, restrictionKey)) { |
| 1366 | result.add(getEnforcingUserLocked(profileUserId)); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1367 | } |
| 1368 | } |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1369 | } |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1370 | return result; |
| 1371 | } |
| 1372 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1373 | private EnforcingUser getEnforcingUserLocked(@UserIdInt int userId) { |
| 1374 | int source = mDeviceOwnerUserId == userId ? UserManager.RESTRICTION_SOURCE_DEVICE_OWNER |
| 1375 | : UserManager.RESTRICTION_SOURCE_PROFILE_OWNER; |
| 1376 | return new EnforcingUser(userId, source); |
| 1377 | } |
| 1378 | |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1379 | /** |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1380 | * @return UserRestrictions that are in effect currently. This always returns a new |
| 1381 | * {@link Bundle}. |
| 1382 | */ |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 1383 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1384 | public Bundle getUserRestrictions(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1385 | return UserRestrictionsUtils.clone(getEffectiveUserRestrictions(userId)); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | @Override |
Zoltan Szatmary-Ban | 3bbcedd | 2015-11-26 13:45:51 +0000 | [diff] [blame] | 1389 | public boolean hasBaseUserRestriction(String restrictionKey, int userId) { |
| 1390 | checkManageUsersPermission("hasBaseUserRestriction"); |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 1391 | if (!UserRestrictionsUtils.isValidRestriction(restrictionKey)) { |
| 1392 | return false; |
| 1393 | } |
Zoltan Szatmary-Ban | 3bbcedd | 2015-11-26 13:45:51 +0000 | [diff] [blame] | 1394 | synchronized (mRestrictionsLock) { |
| 1395 | Bundle bundle = mBaseUserRestrictions.get(userId); |
| 1396 | return (bundle != null && bundle.getBoolean(restrictionKey, false)); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | @Override |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1401 | public void setUserRestriction(String key, boolean value, int userId) { |
Fyodor Kupolov | d4b2604 | 2015-07-27 14:30:59 -0700 | [diff] [blame] | 1402 | checkManageUsersPermission("setUserRestriction"); |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 1403 | if (!UserRestrictionsUtils.isValidRestriction(key)) { |
| 1404 | return; |
| 1405 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1406 | synchronized (mRestrictionsLock) { |
| 1407 | // Note we can't modify Bundles stored in mBaseUserRestrictions directly, so create |
| 1408 | // a copy. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1409 | final Bundle newRestrictions = UserRestrictionsUtils.clone( |
| 1410 | mBaseUserRestrictions.get(userId)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1411 | newRestrictions.putBoolean(key, value); |
| 1412 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1413 | updateUserRestrictionsInternalLR(newRestrictions, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1417 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1418 | * Optionally updating user restrictions, calculate the effective user restrictions and also |
| 1419 | * propagate to other services and system settings. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1420 | * |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1421 | * @param newBaseRestrictions User restrictions to set. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1422 | * If null, will not update user restrictions and only does the propagation. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1423 | * @param userId target user ID. |
| 1424 | */ |
| 1425 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1426 | private void updateUserRestrictionsInternalLR( |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1427 | @Nullable Bundle newBaseRestrictions, int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1428 | final Bundle prevAppliedRestrictions = UserRestrictionsUtils.nonNull( |
| 1429 | mAppliedUserRestrictions.get(userId)); |
| 1430 | |
| 1431 | // Update base restrictions. |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1432 | if (newBaseRestrictions != null) { |
| 1433 | // If newBaseRestrictions == the current one, it's probably a bug. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1434 | final Bundle prevBaseRestrictions = mBaseUserRestrictions.get(userId); |
| 1435 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1436 | Preconditions.checkState(prevBaseRestrictions != newBaseRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1437 | Preconditions.checkState(mCachedEffectiveUserRestrictions.get(userId) |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1438 | != newBaseRestrictions); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1439 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1440 | if (updateRestrictionsIfNeededLR(userId, newBaseRestrictions, mBaseUserRestrictions)) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1441 | scheduleWriteUser(getUserDataNoChecks(userId)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1442 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1445 | final Bundle effective = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1446 | |
Makoto Onuki | 759a763 | 2015-10-28 16:43:10 -0700 | [diff] [blame] | 1447 | mCachedEffectiveUserRestrictions.put(userId, effective); |
| 1448 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1449 | // Apply the new restrictions. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1450 | if (DBG) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1451 | debug("Applying user restrictions: userId=" + userId |
| 1452 | + " new=" + effective + " prev=" + prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1455 | if (mAppOpsService != null) { // We skip it until system-ready. |
Fyodor Kupolov | ec30ca3 | 2016-06-16 15:09:29 -0700 | [diff] [blame] | 1456 | mHandler.post(new Runnable() { |
| 1457 | @Override |
| 1458 | public void run() { |
| 1459 | try { |
| 1460 | mAppOpsService.setUserRestrictions(effective, mUserRestriconToken, userId); |
| 1461 | } catch (RemoteException e) { |
| 1462 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 1463 | } |
| 1464 | } |
| 1465 | }); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1466 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1467 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1468 | propagateUserRestrictionsLR(userId, effective, prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1469 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1470 | mAppliedUserRestrictions.put(userId, new Bundle(effective)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1473 | private void propagateUserRestrictionsLR(final int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1474 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1475 | // Note this method doesn't touch any state, meaning it doesn't require mRestrictionsLock |
| 1476 | // actually, but we still need some kind of synchronization otherwise we might end up |
| 1477 | // calling listeners out-of-order, thus "LR". |
| 1478 | |
| 1479 | if (UserRestrictionsUtils.areEqual(newRestrictions, prevRestrictions)) { |
| 1480 | return; |
| 1481 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1482 | |
| 1483 | final Bundle newRestrictionsFinal = new Bundle(newRestrictions); |
| 1484 | final Bundle prevRestrictionsFinal = new Bundle(prevRestrictions); |
| 1485 | |
| 1486 | mHandler.post(new Runnable() { |
| 1487 | @Override |
| 1488 | public void run() { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1489 | UserRestrictionsUtils.applyUserRestrictions( |
| 1490 | mContext, userId, newRestrictionsFinal, prevRestrictionsFinal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1491 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1492 | final UserRestrictionsListener[] listeners; |
| 1493 | synchronized (mUserRestrictionsListeners) { |
| 1494 | listeners = new UserRestrictionsListener[mUserRestrictionsListeners.size()]; |
| 1495 | mUserRestrictionsListeners.toArray(listeners); |
| 1496 | } |
| 1497 | for (int i = 0; i < listeners.length; i++) { |
| 1498 | listeners[i].onUserRestrictionsChanged(userId, |
| 1499 | newRestrictionsFinal, prevRestrictionsFinal); |
| 1500 | } |
Makoto Onuki | e72f81b | 2017-03-16 14:08:19 -0700 | [diff] [blame] | 1501 | |
| 1502 | final Intent broadcast = new Intent(UserManager.ACTION_USER_RESTRICTIONS_CHANGED) |
| 1503 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1504 | mContext.sendBroadcastAsUser(broadcast, UserHandle.of(userId)); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1505 | } |
| 1506 | }); |
| 1507 | } |
| 1508 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1509 | // Package private for the inner class. |
| 1510 | void applyUserRestrictionsLR(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1511 | updateUserRestrictionsInternalLR(null, userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | @GuardedBy("mRestrictionsLock") |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1515 | // Package private for the inner class. |
| 1516 | void applyUserRestrictionsForAllUsersLR() { |
| 1517 | if (DBG) { |
| 1518 | debug("applyUserRestrictionsForAllUsersLR"); |
| 1519 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1520 | // First, invalidate all cached values. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1521 | mCachedEffectiveUserRestrictions.clear(); |
| 1522 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1523 | // We don't want to call into ActivityManagerService while taking a lock, so we'll call |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1524 | // it on a handler. |
| 1525 | final Runnable r = new Runnable() { |
| 1526 | @Override |
| 1527 | public void run() { |
| 1528 | // Then get the list of running users. |
| 1529 | final int[] runningUsers; |
| 1530 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1531 | runningUsers = ActivityManager.getService().getRunningUserIds(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1532 | } catch (RemoteException e) { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1533 | Log.w(LOG_TAG, "Unable to access ActivityManagerService"); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1534 | return; |
| 1535 | } |
| 1536 | // Then re-calculate the effective restrictions and apply, only for running users. |
| 1537 | // It's okay if a new user has started after the getRunningUserIds() call, |
| 1538 | // because we'll do the same thing (re-calculate the restrictions and apply) |
| 1539 | // when we start a user. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1540 | synchronized (mRestrictionsLock) { |
| 1541 | for (int i = 0; i < runningUsers.length; i++) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1542 | applyUserRestrictionsLR(runningUsers[i]); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | }; |
| 1547 | mHandler.post(r); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1550 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1551 | * Check if we've hit the limit of how many users can be created. |
| 1552 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1553 | private boolean isUserLimitReached() { |
| 1554 | int count; |
| 1555 | synchronized (mUsersLock) { |
| 1556 | count = getAliveUsersExcludingGuestsCountLU(); |
| 1557 | } |
| 1558 | return count >= UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | @Override |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1562 | public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1563 | checkManageUsersPermission("check if more managed profiles can be added."); |
| 1564 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1565 | return false; |
| 1566 | } |
Fyodor Kupolov | b615799 | 2015-06-05 15:32:28 -0700 | [diff] [blame] | 1567 | if (!mContext.getPackageManager().hasSystemFeature( |
| 1568 | PackageManager.FEATURE_MANAGED_USERS)) { |
| 1569 | return false; |
| 1570 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1571 | // Limit number of managed profiles that can be created |
Benjamin Franz | c877615 | 2017-01-06 14:16:41 +0000 | [diff] [blame] | 1572 | final int managedProfilesCount = getProfiles(userId, false).size() - 1; |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1573 | final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 1574 | if (managedProfilesCount - profilesRemovedCount >= getMaxManagedProfiles()) { |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1577 | synchronized(mUsersLock) { |
| 1578 | UserInfo userInfo = getUserInfoLU(userId); |
yuanhao | 47f9f7c | 2017-01-18 09:54:45 +0800 | [diff] [blame] | 1579 | if (userInfo == null || !userInfo.canHaveProfile()) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1580 | return false; |
| 1581 | } |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1582 | int usersCountAfterRemoving = getAliveUsersExcludingGuestsCountLU() |
| 1583 | - profilesRemovedCount; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1584 | // We allow creating a managed profile in the special case where there is only one user. |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1585 | return usersCountAfterRemoving == 1 |
| 1586 | || usersCountAfterRemoving < UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1590 | private int getAliveUsersExcludingGuestsCountLU() { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1591 | int aliveUserCount = 0; |
| 1592 | final int totalUserCount = mUsers.size(); |
| 1593 | // Skip over users being removed |
| 1594 | for (int i = 0; i < totalUserCount; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1595 | UserInfo user = mUsers.valueAt(i).info; |
Fyodor Kupolov | 6e99d3f | 2016-11-01 17:18:41 -0700 | [diff] [blame] | 1596 | if (!mRemovingUserIds.get(user.id) && !user.isGuest()) { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1597 | aliveUserCount++; |
| 1598 | } |
| 1599 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1600 | return aliveUserCount; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1604 | * Enforces that only the system UID or root's UID or apps that have the |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1605 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} and |
| 1606 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL INTERACT_ACROSS_USERS_FULL} |
| 1607 | * permissions can make certain calls to the UserManager. |
| 1608 | * |
| 1609 | * @param message used as message if SecurityException is thrown |
| 1610 | * @throws SecurityException if the caller does not have enough privilege. |
| 1611 | */ |
| 1612 | private static final void checkManageUserAndAcrossUsersFullPermission(String message) { |
| 1613 | final int uid = Binder.getCallingUid(); |
| 1614 | if (uid != Process.SYSTEM_UID && uid != 0 |
| 1615 | && ActivityManager.checkComponentPermission( |
| 1616 | Manifest.permission.MANAGE_USERS, |
| 1617 | uid, -1, true) != PackageManager.PERMISSION_GRANTED |
| 1618 | && ActivityManager.checkComponentPermission( |
| 1619 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1620 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1621 | throw new SecurityException( |
| 1622 | "You need MANAGE_USERS and INTERACT_ACROSS_USERS_FULL permission to: " |
| 1623 | + message); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * Enforces that only the system UID or root's UID or apps that have the |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1629 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1630 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1631 | * |
| 1632 | * @param message used as message if SecurityException is thrown |
| 1633 | * @throws SecurityException if the caller is not system or root |
Tony Mak | 4dc008c | 2016-03-16 10:46:49 +0000 | [diff] [blame] | 1634 | * @see #hasManageUsersPermission() |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1635 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1636 | private static final void checkManageUsersPermission(String message) { |
Tony Mak | 4dc008c | 2016-03-16 10:46:49 +0000 | [diff] [blame] | 1637 | if (!hasManageUsersPermission()) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1638 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 1639 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 1640 | } |
| 1641 | |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1642 | /** |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 1643 | * Enforces that only the system UID or root's UID or apps that have the |
| 1644 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} or |
| 1645 | * {@link android.Manifest.permission#CREATE_USERS CREATE_USERS} |
| 1646 | * can make certain calls to the UserManager. |
| 1647 | * |
| 1648 | * @param message used as message if SecurityException is thrown |
| 1649 | * @throws SecurityException if the caller is not system or root |
| 1650 | * @see #hasManageOrCreateUsersPermission() |
| 1651 | */ |
| 1652 | private static final void checkManageOrCreateUsersPermission(String message) { |
| 1653 | if (!hasManageOrCreateUsersPermission()) { |
| 1654 | throw new SecurityException( |
| 1655 | "You either need MANAGE_USERS or CREATE_USERS permission to: " + message); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * Similar to {@link #checkManageOrCreateUsersPermission(String)} but when the caller is tries |
| 1661 | * to create user/profiles other than what is allowed for |
| 1662 | * {@link android.Manifest.permission#CREATE_USERS CREATE_USERS} permission, then it will only |
| 1663 | * allow callers with {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} permission. |
| 1664 | */ |
| 1665 | private static final void checkManageOrCreateUsersPermission(int creationFlags) { |
| 1666 | if ((creationFlags & ~ALLOWED_FLAGS_FOR_CREATE_USERS_PERMISSION) == 0) { |
| 1667 | if (!hasManageOrCreateUsersPermission()) { |
| 1668 | throw new SecurityException("You either need MANAGE_USERS or CREATE_USERS " |
| 1669 | + "permission to create an user with flags: " + creationFlags); |
| 1670 | } |
| 1671 | } else if (!hasManageUsersPermission()) { |
| 1672 | throw new SecurityException("You need MANAGE_USERS permission to create an user " |
| 1673 | + " with flags: " + creationFlags); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /** |
Tony Mak | 4dc008c | 2016-03-16 10:46:49 +0000 | [diff] [blame] | 1678 | * @return whether the calling UID is system UID or root's UID or the calling app has the |
| 1679 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}. |
| 1680 | */ |
| 1681 | private static final boolean hasManageUsersPermission() { |
| 1682 | final int callingUid = Binder.getCallingUid(); |
| 1683 | return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 1684 | || callingUid == Process.ROOT_UID |
| 1685 | || ActivityManager.checkComponentPermission( |
| 1686 | android.Manifest.permission.MANAGE_USERS, |
| 1687 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 1691 | * @return whether the calling UID is system UID or root's UID or the calling app has the |
| 1692 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} or |
| 1693 | * {@link android.Manifest.permission#CREATE_USERS CREATE_USERS}. |
| 1694 | */ |
| 1695 | private static final boolean hasManageOrCreateUsersPermission() { |
| 1696 | final int callingUid = Binder.getCallingUid(); |
| 1697 | return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 1698 | || callingUid == Process.ROOT_UID |
| 1699 | || ActivityManager.checkComponentPermission( |
| 1700 | android.Manifest.permission.MANAGE_USERS, |
| 1701 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED |
| 1702 | || ActivityManager.checkComponentPermission( |
| 1703 | android.Manifest.permission.CREATE_USERS, |
| 1704 | callingUid, -1, true) == PackageManager.PERMISSION_GRANTED; |
| 1705 | } |
| 1706 | |
| 1707 | /** |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1708 | * Enforces that only the system UID or root's UID (on any user) can make certain calls to the |
| 1709 | * UserManager. |
| 1710 | * |
| 1711 | * @param message used as message if SecurityException is thrown |
| 1712 | * @throws SecurityException if the caller is not system or root |
| 1713 | */ |
| 1714 | private static void checkSystemOrRoot(String message) { |
| 1715 | final int uid = Binder.getCallingUid(); |
| 1716 | if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID) { |
| 1717 | throw new SecurityException("Only system may: " + message); |
| 1718 | } |
| 1719 | } |
| 1720 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1721 | private void writeBitmapLP(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1722 | try { |
| 1723 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 1724 | File file = new File(dir, USER_PHOTO_FILENAME); |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1725 | File tmp = new File(dir, USER_PHOTO_FILENAME_TMP); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1726 | if (!dir.exists()) { |
| 1727 | dir.mkdir(); |
| 1728 | FileUtils.setPermissions( |
| 1729 | dir.getPath(), |
| 1730 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1731 | -1, -1); |
| 1732 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1733 | FileOutputStream os; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1734 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp)) |
Oleksandr Peletskyi | cd6fa30 | 2016-02-25 16:28:39 +0100 | [diff] [blame] | 1735 | && tmp.renameTo(file) && SELinux.restorecon(file)) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1736 | info.iconPath = file.getAbsolutePath(); |
| 1737 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1738 | try { |
| 1739 | os.close(); |
| 1740 | } catch (IOException ioe) { |
| 1741 | // What the ... ! |
| 1742 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1743 | tmp.delete(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1744 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1745 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1746 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1749 | /** |
| 1750 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 1751 | * cache it elsewhere. |
| 1752 | * @return the array of user ids. |
| 1753 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 1754 | public int[] getUserIds() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1755 | synchronized (mUsersLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1756 | return mUserIds; |
| 1757 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1760 | private void readUserListLP() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1761 | if (!mUserListFile.exists()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1762 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1763 | return; |
| 1764 | } |
| 1765 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1766 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1767 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1768 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1769 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1770 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1771 | int type; |
| 1772 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1773 | && type != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1774 | // Skip |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1778 | Slog.e(LOG_TAG, "Unable to read user list"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1779 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1780 | return; |
| 1781 | } |
| 1782 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1783 | mNextSerialNumber = -1; |
| 1784 | if (parser.getName().equals(TAG_USERS)) { |
| 1785 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 1786 | if (lastSerialNumber != null) { |
| 1787 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 1788 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1789 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 1790 | if (versionNumber != null) { |
| 1791 | mUserVersion = Integer.parseInt(versionNumber); |
| 1792 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1795 | // Pre-O global user restriction were stored as a single bundle (as opposed to per-user |
| 1796 | // currently), take care of it in case of upgrade. |
| 1797 | Bundle oldDevicePolicyGlobalUserRestrictions = null; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1798 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1799 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1800 | if (type == XmlPullParser.START_TAG) { |
| 1801 | final String name = parser.getName(); |
| 1802 | if (name.equals(TAG_USER)) { |
| 1803 | String id = parser.getAttributeValue(null, ATTR_ID); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1804 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1805 | UserData userData = readUserLP(Integer.parseInt(id)); |
| 1806 | |
| 1807 | if (userData != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1808 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1809 | mUsers.put(userData.info.id, userData); |
| 1810 | if (mNextSerialNumber < 0 |
| 1811 | || mNextSerialNumber <= userData.info.id) { |
| 1812 | mNextSerialNumber = userData.info.id + 1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1813 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1814 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1815 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1816 | } else if (name.equals(TAG_GUEST_RESTRICTIONS)) { |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1817 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1818 | && type != XmlPullParser.END_TAG) { |
| 1819 | if (type == XmlPullParser.START_TAG) { |
| 1820 | if (parser.getName().equals(TAG_RESTRICTIONS)) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1821 | synchronized (mGuestRestrictions) { |
Fyodor Kupolov | eafee02 | 2017-03-15 17:09:04 -0700 | [diff] [blame] | 1822 | UserRestrictionsUtils |
| 1823 | .readRestrictions(parser, mGuestRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1824 | } |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1825 | } |
| 1826 | break; |
| 1827 | } |
| 1828 | } |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1829 | } else if (name.equals(TAG_DEVICE_OWNER_USER_ID) |
| 1830 | // Legacy name, should only be encountered when upgrading from pre-O. |
| 1831 | || name.equals(TAG_GLOBAL_RESTRICTION_OWNER_ID)) { |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1832 | String ownerUserId = parser.getAttributeValue(null, ATTR_ID); |
| 1833 | if (ownerUserId != null) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1834 | mDeviceOwnerUserId = Integer.parseInt(ownerUserId); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 1835 | } |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1836 | } else if (name.equals(TAG_DEVICE_POLICY_RESTRICTIONS)) { |
| 1837 | // Should only happen when upgrading from pre-O (version < 7). |
| 1838 | oldDevicePolicyGlobalUserRestrictions = |
| 1839 | UserRestrictionsUtils.readRestrictions(parser); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1840 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1841 | } |
| 1842 | } |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1843 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1844 | updateUserIds(); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1845 | upgradeIfNecessaryLP(oldDevicePolicyGlobalUserRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1846 | } catch (IOException | XmlPullParserException e) { |
| 1847 | fallbackToSingleUserLP(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1848 | } finally { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1849 | IoUtils.closeQuietly(fis); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1853 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1854 | * Upgrade steps between versions, either for fixing bugs or changing the data format. |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1855 | * @param oldGlobalUserRestrictions Pre-O global device policy restrictions. |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1856 | */ |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1857 | private void upgradeIfNecessaryLP(Bundle oldGlobalUserRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1858 | final int originalVersion = mUserVersion; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1859 | int userVersion = mUserVersion; |
| 1860 | if (userVersion < 1) { |
| 1861 | // Assign a proper name for the owner, if not initialized correctly before |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1862 | UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM); |
| 1863 | if ("Primary".equals(userData.info.name)) { |
| 1864 | userData.info.name = |
| 1865 | mContext.getResources().getString(com.android.internal.R.string.owner_name); |
| 1866 | scheduleWriteUser(userData); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1867 | } |
| 1868 | userVersion = 1; |
| 1869 | } |
| 1870 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1871 | if (userVersion < 2) { |
| 1872 | // Owner should be marked as initialized |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1873 | UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM); |
| 1874 | if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 1875 | userData.info.flags |= UserInfo.FLAG_INITIALIZED; |
| 1876 | scheduleWriteUser(userData); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1877 | } |
| 1878 | userVersion = 2; |
| 1879 | } |
| 1880 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1881 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1882 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1883 | userVersion = 4; |
| 1884 | } |
| 1885 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1886 | if (userVersion < 5) { |
| 1887 | initDefaultGuestRestrictions(); |
| 1888 | userVersion = 5; |
| 1889 | } |
| 1890 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1891 | if (userVersion < 6) { |
| 1892 | final boolean splitSystemUser = UserManager.isSplitSystemUser(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1893 | synchronized (mUsersLock) { |
| 1894 | for (int i = 0; i < mUsers.size(); i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1895 | UserData userData = mUsers.valueAt(i); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1896 | // In non-split mode, only user 0 can have restricted profiles |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1897 | if (!splitSystemUser && userData.info.isRestricted() |
| 1898 | && (userData.info.restrictedProfileParentId |
| 1899 | == UserInfo.NO_PROFILE_GROUP_ID)) { |
| 1900 | userData.info.restrictedProfileParentId = UserHandle.USER_SYSTEM; |
| 1901 | scheduleWriteUser(userData); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1902 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | userVersion = 6; |
| 1906 | } |
| 1907 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1908 | if (userVersion < 7) { |
| 1909 | // Previously only one user could enforce global restrictions, now it is per-user. |
| 1910 | synchronized (mRestrictionsLock) { |
| 1911 | if (!UserRestrictionsUtils.isEmpty(oldGlobalUserRestrictions) |
| 1912 | && mDeviceOwnerUserId != UserHandle.USER_NULL) { |
| 1913 | mDevicePolicyGlobalUserRestrictions.put( |
| 1914 | mDeviceOwnerUserId, oldGlobalUserRestrictions); |
| 1915 | } |
| 1916 | // ENSURE_VERIFY_APPS is now enforced globally even if put by profile owner, so move |
| 1917 | // it from local to global bundle for all users who set it. |
| 1918 | UserRestrictionsUtils.moveRestriction(UserManager.ENSURE_VERIFY_APPS, |
| 1919 | mDevicePolicyLocalUserRestrictions, mDevicePolicyGlobalUserRestrictions |
| 1920 | ); |
| 1921 | } |
| 1922 | userVersion = 7; |
| 1923 | } |
| 1924 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1925 | if (userVersion < USER_VERSION) { |
| 1926 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 1927 | + USER_VERSION); |
| 1928 | } else { |
| 1929 | mUserVersion = userVersion; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1930 | |
| 1931 | if (originalVersion < mUserVersion) { |
| 1932 | writeUserListLP(); |
| 1933 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1937 | private void fallbackToSingleUserLP() { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1938 | int flags = UserInfo.FLAG_INITIALIZED; |
| 1939 | // In split system user mode, the admin and primary flags are assigned to the first human |
| 1940 | // user. |
| 1941 | if (!UserManager.isSplitSystemUser()) { |
| 1942 | flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY; |
| 1943 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1944 | // Create the system user |
Amith Yamasani | 6f48d6e | 2016-03-23 14:28:25 -0700 | [diff] [blame] | 1945 | UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, null, null, flags); |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 1946 | UserData userData = putUserInfo(system); |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1947 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 1948 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1949 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1950 | Bundle restrictions = new Bundle(); |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 1951 | try { |
| 1952 | final String[] defaultFirstUserRestrictions = mContext.getResources().getStringArray( |
| 1953 | com.android.internal.R.array.config_defaultFirstUserRestrictions); |
| 1954 | for (String userRestriction : defaultFirstUserRestrictions) { |
| 1955 | if (UserRestrictionsUtils.isValidRestriction(userRestriction)) { |
| 1956 | restrictions.putBoolean(userRestriction, true); |
| 1957 | } |
| 1958 | } |
| 1959 | } catch (Resources.NotFoundException e) { |
| 1960 | Log.e(LOG_TAG, "Couldn't find resource: config_defaultFirstUserRestrictions", e); |
| 1961 | } |
| 1962 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1963 | if (!restrictions.isEmpty()) { |
| 1964 | synchronized (mRestrictionsLock) { |
| 1965 | mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions); |
| 1966 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1967 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1968 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1969 | updateUserIds(); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1970 | initDefaultGuestRestrictions(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1971 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1972 | writeUserLP(userData); |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 1973 | writeUserListLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Amith Yamasani | 6f48d6e | 2016-03-23 14:28:25 -0700 | [diff] [blame] | 1976 | private String getOwnerName() { |
| 1977 | return mContext.getResources().getString(com.android.internal.R.string.owner_name); |
| 1978 | } |
| 1979 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1980 | private void scheduleWriteUser(UserData UserData) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1981 | if (DBG) { |
| 1982 | debug("scheduleWriteUser"); |
| 1983 | } |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1984 | // No need to wrap it within a lock -- worst case, we'll just post the same message |
| 1985 | // twice. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1986 | if (!mHandler.hasMessages(WRITE_USER_MSG, UserData)) { |
| 1987 | Message msg = mHandler.obtainMessage(WRITE_USER_MSG, UserData); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1988 | mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY); |
| 1989 | } |
| 1990 | } |
| 1991 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1992 | private void writeUserLP(UserData userData) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1993 | if (DBG) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1994 | debug("writeUserLP " + userData); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1995 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1996 | FileOutputStream fos = null; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1997 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userData.info.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1998 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1999 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2000 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2001 | writeUserLP(userData, bos); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2002 | userFile.finishWrite(fos); |
| 2003 | } catch (Exception ioe) { |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 2004 | Slog.e(LOG_TAG, "Error writing user info " + userData.info.id, ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2005 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | /* |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2010 | * Writes the user file in this format: |
| 2011 | * |
| 2012 | * <user flags="20039023" id="0"> |
| 2013 | * <name>Primary</name> |
| 2014 | * </user> |
| 2015 | */ |
| 2016 | @VisibleForTesting |
| 2017 | void writeUserLP(UserData userData, OutputStream os) |
| 2018 | throws IOException, XmlPullParserException { |
| 2019 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 2020 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 2021 | serializer.setOutput(os, StandardCharsets.UTF_8.name()); |
| 2022 | serializer.startDocument(null, true); |
| 2023 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 2024 | |
| 2025 | final UserInfo userInfo = userData.info; |
| 2026 | serializer.startTag(null, TAG_USER); |
| 2027 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
| 2028 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
| 2029 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
| 2030 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 2031 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 2032 | Long.toString(userInfo.lastLoggedInTime)); |
| 2033 | if (userInfo.lastLoggedInFingerprint != null) { |
| 2034 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_FINGERPRINT, |
| 2035 | userInfo.lastLoggedInFingerprint); |
| 2036 | } |
| 2037 | if (userInfo.iconPath != null) { |
| 2038 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 2039 | } |
| 2040 | if (userInfo.partial) { |
| 2041 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 2042 | } |
| 2043 | if (userInfo.guestToRemove) { |
| 2044 | serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true"); |
| 2045 | } |
| 2046 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 2047 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 2048 | Integer.toString(userInfo.profileGroupId)); |
| 2049 | } |
| 2050 | serializer.attribute(null, ATTR_PROFILE_BADGE, |
| 2051 | Integer.toString(userInfo.profileBadge)); |
| 2052 | if (userInfo.restrictedProfileParentId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 2053 | serializer.attribute(null, ATTR_RESTRICTED_PROFILE_PARENT_ID, |
| 2054 | Integer.toString(userInfo.restrictedProfileParentId)); |
| 2055 | } |
| 2056 | // Write seed data |
| 2057 | if (userData.persistSeedData) { |
| 2058 | if (userData.seedAccountName != null) { |
| 2059 | serializer.attribute(null, ATTR_SEED_ACCOUNT_NAME, userData.seedAccountName); |
| 2060 | } |
| 2061 | if (userData.seedAccountType != null) { |
| 2062 | serializer.attribute(null, ATTR_SEED_ACCOUNT_TYPE, userData.seedAccountType); |
| 2063 | } |
| 2064 | } |
| 2065 | if (userInfo.name != null) { |
| 2066 | serializer.startTag(null, TAG_NAME); |
| 2067 | serializer.text(userInfo.name); |
| 2068 | serializer.endTag(null, TAG_NAME); |
| 2069 | } |
| 2070 | synchronized (mRestrictionsLock) { |
| 2071 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 2072 | mBaseUserRestrictions.get(userInfo.id), TAG_RESTRICTIONS); |
| 2073 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 2074 | mDevicePolicyLocalUserRestrictions.get(userInfo.id), |
| 2075 | TAG_DEVICE_POLICY_RESTRICTIONS); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2076 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 2077 | mDevicePolicyGlobalUserRestrictions.get(userInfo.id), |
| 2078 | TAG_DEVICE_POLICY_GLOBAL_RESTRICTIONS); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | if (userData.account != null) { |
| 2082 | serializer.startTag(null, TAG_ACCOUNT); |
| 2083 | serializer.text(userData.account); |
| 2084 | serializer.endTag(null, TAG_ACCOUNT); |
| 2085 | } |
| 2086 | |
| 2087 | if (userData.persistSeedData && userData.seedAccountOptions != null) { |
| 2088 | serializer.startTag(null, TAG_SEED_ACCOUNT_OPTIONS); |
| 2089 | userData.seedAccountOptions.saveToXml(serializer); |
| 2090 | serializer.endTag(null, TAG_SEED_ACCOUNT_OPTIONS); |
| 2091 | } |
| 2092 | |
| 2093 | serializer.endTag(null, TAG_USER); |
| 2094 | |
| 2095 | serializer.endDocument(); |
| 2096 | } |
| 2097 | |
| 2098 | /* |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2099 | * Writes the user list file in this format: |
| 2100 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2101 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2102 | * <user id="0"></user> |
| 2103 | * <user id="2"></user> |
| 2104 | * </users> |
| 2105 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2106 | private void writeUserListLP() { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2107 | if (DBG) { |
| 2108 | debug("writeUserList"); |
| 2109 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2110 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2111 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2112 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2113 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2114 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 2115 | |
| 2116 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 2117 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2118 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2119 | serializer.startDocument(null, true); |
| 2120 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 2121 | |
| 2122 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2123 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 2124 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2125 | |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2126 | serializer.startTag(null, TAG_GUEST_RESTRICTIONS); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2127 | synchronized (mGuestRestrictions) { |
| 2128 | UserRestrictionsUtils |
| 2129 | .writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS); |
| 2130 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 2131 | serializer.endTag(null, TAG_GUEST_RESTRICTIONS); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2132 | serializer.startTag(null, TAG_DEVICE_OWNER_USER_ID); |
| 2133 | serializer.attribute(null, ATTR_ID, Integer.toString(mDeviceOwnerUserId)); |
| 2134 | serializer.endTag(null, TAG_DEVICE_OWNER_USER_ID); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2135 | int[] userIdsToWrite; |
| 2136 | synchronized (mUsersLock) { |
| 2137 | userIdsToWrite = new int[mUsers.size()]; |
| 2138 | for (int i = 0; i < userIdsToWrite.length; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2139 | UserInfo user = mUsers.valueAt(i).info; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2140 | userIdsToWrite[i] = user.id; |
| 2141 | } |
| 2142 | } |
| 2143 | for (int id : userIdsToWrite) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2144 | serializer.startTag(null, TAG_USER); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2145 | serializer.attribute(null, ATTR_ID, Integer.toString(id)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2146 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | serializer.endTag(null, TAG_USERS); |
| 2150 | |
| 2151 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2152 | userListFile.finishWrite(fos); |
| 2153 | } catch (Exception e) { |
| 2154 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2155 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2159 | private UserData readUserLP(int id) { |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2160 | FileInputStream fis = null; |
| 2161 | try { |
| 2162 | AtomicFile userFile = |
| 2163 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
| 2164 | fis = userFile.openRead(); |
| 2165 | return readUserLP(id, fis); |
| 2166 | } catch (IOException ioe) { |
| 2167 | Slog.e(LOG_TAG, "Error reading user list"); |
| 2168 | } catch (XmlPullParserException pe) { |
| 2169 | Slog.e(LOG_TAG, "Error reading user list"); |
| 2170 | } finally { |
| 2171 | IoUtils.closeQuietly(fis); |
| 2172 | } |
| 2173 | return null; |
| 2174 | } |
| 2175 | |
| 2176 | @VisibleForTesting |
| 2177 | UserData readUserLP(int id, InputStream is) throws IOException, |
| 2178 | XmlPullParserException { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2179 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2180 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2181 | String name = null; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 2182 | String account = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 2183 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2184 | long creationTime = 0L; |
| 2185 | long lastLoggedInTime = 0L; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 2186 | String lastLoggedInFingerprint = null; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 2187 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2188 | int profileBadge = 0; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2189 | int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2190 | boolean partial = false; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2191 | boolean guestToRemove = false; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2192 | boolean persistSeedData = false; |
| 2193 | String seedAccountName = null; |
| 2194 | String seedAccountType = null; |
| 2195 | PersistableBundle seedAccountOptions = null; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2196 | Bundle baseRestrictions = null; |
| 2197 | Bundle localRestrictions = null; |
| 2198 | Bundle globalRestrictions = null; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2199 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2200 | XmlPullParser parser = Xml.newPullParser(); |
| 2201 | parser.setInput(is, StandardCharsets.UTF_8.name()); |
| 2202 | int type; |
| 2203 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 2204 | && type != XmlPullParser.END_DOCUMENT) { |
| 2205 | // Skip |
| 2206 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2207 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2208 | if (type != XmlPullParser.START_TAG) { |
| 2209 | Slog.e(LOG_TAG, "Unable to read user " + id); |
| 2210 | return null; |
| 2211 | } |
| 2212 | |
| 2213 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 2214 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 2215 | if (storedId != id) { |
| 2216 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2217 | return null; |
| 2218 | } |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2219 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 2220 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
| 2221 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
| 2222 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 2223 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
| 2224 | lastLoggedInFingerprint = parser.getAttributeValue(null, |
| 2225 | ATTR_LAST_LOGGED_IN_FINGERPRINT); |
| 2226 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 2227 | UserInfo.NO_PROFILE_GROUP_ID); |
| 2228 | profileBadge = readIntAttribute(parser, ATTR_PROFILE_BADGE, 0); |
| 2229 | restrictedProfileParentId = readIntAttribute(parser, |
| 2230 | ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID); |
| 2231 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 2232 | if ("true".equals(valueString)) { |
| 2233 | partial = true; |
| 2234 | } |
| 2235 | valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE); |
| 2236 | if ("true".equals(valueString)) { |
| 2237 | guestToRemove = true; |
| 2238 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2239 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2240 | seedAccountName = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_NAME); |
| 2241 | seedAccountType = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_TYPE); |
| 2242 | if (seedAccountName != null || seedAccountType != null) { |
| 2243 | persistSeedData = true; |
| 2244 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2245 | |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2246 | int outerDepth = parser.getDepth(); |
| 2247 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2248 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2249 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2250 | continue; |
| 2251 | } |
| 2252 | String tag = parser.getName(); |
| 2253 | if (TAG_NAME.equals(tag)) { |
| 2254 | type = parser.next(); |
| 2255 | if (type == XmlPullParser.TEXT) { |
| 2256 | name = parser.getText(); |
| 2257 | } |
| 2258 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2259 | baseRestrictions = UserRestrictionsUtils.readRestrictions(parser); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2260 | } else if (TAG_DEVICE_POLICY_RESTRICTIONS.equals(tag)) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2261 | localRestrictions = UserRestrictionsUtils.readRestrictions(parser); |
| 2262 | } else if (TAG_DEVICE_POLICY_GLOBAL_RESTRICTIONS.equals(tag)) { |
| 2263 | globalRestrictions = UserRestrictionsUtils.readRestrictions(parser); |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2264 | } else if (TAG_ACCOUNT.equals(tag)) { |
| 2265 | type = parser.next(); |
| 2266 | if (type == XmlPullParser.TEXT) { |
| 2267 | account = parser.getText(); |
| 2268 | } |
| 2269 | } else if (TAG_SEED_ACCOUNT_OPTIONS.equals(tag)) { |
| 2270 | seedAccountOptions = PersistableBundle.restoreFromXml(parser); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2271 | persistSeedData = true; |
| 2272 | } |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 2273 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2274 | } |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2275 | |
| 2276 | // Create the UserInfo object that gets passed around |
| 2277 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
| 2278 | userInfo.serialNumber = serialNumber; |
| 2279 | userInfo.creationTime = creationTime; |
| 2280 | userInfo.lastLoggedInTime = lastLoggedInTime; |
| 2281 | userInfo.lastLoggedInFingerprint = lastLoggedInFingerprint; |
| 2282 | userInfo.partial = partial; |
| 2283 | userInfo.guestToRemove = guestToRemove; |
| 2284 | userInfo.profileGroupId = profileGroupId; |
| 2285 | userInfo.profileBadge = profileBadge; |
| 2286 | userInfo.restrictedProfileParentId = restrictedProfileParentId; |
| 2287 | |
| 2288 | // Create the UserData object that's internal to this class |
| 2289 | UserData userData = new UserData(); |
| 2290 | userData.info = userInfo; |
| 2291 | userData.account = account; |
| 2292 | userData.seedAccountName = seedAccountName; |
| 2293 | userData.seedAccountType = seedAccountType; |
| 2294 | userData.persistSeedData = persistSeedData; |
| 2295 | userData.seedAccountOptions = seedAccountOptions; |
| 2296 | |
| 2297 | synchronized (mRestrictionsLock) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2298 | if (baseRestrictions != null) { |
| 2299 | mBaseUserRestrictions.put(id, baseRestrictions); |
| 2300 | } |
| 2301 | if (localRestrictions != null) { |
| 2302 | mDevicePolicyLocalUserRestrictions.put(id, localRestrictions); |
| 2303 | } |
| 2304 | if (globalRestrictions != null) { |
| 2305 | mDevicePolicyGlobalUserRestrictions.put(id, globalRestrictions); |
| 2306 | } |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2307 | } |
| 2308 | return userData; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2311 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 2312 | String valueString = parser.getAttributeValue(null, attr); |
| 2313 | if (valueString == null) return defaultValue; |
| 2314 | try { |
| 2315 | return Integer.parseInt(valueString); |
| 2316 | } catch (NumberFormatException nfe) { |
| 2317 | return defaultValue; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 2322 | String valueString = parser.getAttributeValue(null, attr); |
| 2323 | if (valueString == null) return defaultValue; |
| 2324 | try { |
| 2325 | return Long.parseLong(valueString); |
| 2326 | } catch (NumberFormatException nfe) { |
| 2327 | return defaultValue; |
| 2328 | } |
| 2329 | } |
| 2330 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 2331 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2332 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 2333 | */ |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2334 | private static void cleanAppRestrictionsForPackageLAr(String pkg, int userId) { |
| 2335 | File dir = Environment.getUserSystemDirectory(userId); |
| 2336 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
| 2337 | if (resFile.exists()) { |
| 2338 | resFile.delete(); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 2342 | @Override |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 2343 | public UserInfo createProfileForUser(String name, int flags, int userId, |
| 2344 | String[] disallowedPackages) { |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 2345 | checkManageOrCreateUsersPermission(flags); |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 2346 | return createUserInternal(name, flags, userId, disallowedPackages); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 2349 | @Override |
Tony Mak | 6dc428f | 2016-10-10 15:48:27 +0100 | [diff] [blame] | 2350 | public UserInfo createProfileForUserEvenWhenDisallowed(String name, int flags, int userId, |
| 2351 | String[] disallowedPackages) { |
| 2352 | checkManageOrCreateUsersPermission(flags); |
| 2353 | return createUserInternalUnchecked(name, flags, userId, disallowedPackages); |
| 2354 | } |
| 2355 | |
| 2356 | @Override |
Nicolas Prevot | d37c4a9 | 2017-01-23 11:56:00 +0000 | [diff] [blame] | 2357 | public boolean removeUserEvenWhenDisallowed(@UserIdInt int userHandle) { |
| 2358 | checkManageOrCreateUsersPermission("Only the system can remove users"); |
| 2359 | return removeUserUnchecked(userHandle); |
| 2360 | } |
| 2361 | |
| 2362 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2363 | public UserInfo createUser(String name, int flags) { |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 2364 | checkManageOrCreateUsersPermission(flags); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 2365 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 2366 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 2367 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 2368 | private UserInfo createUserInternal(String name, int flags, int parentId) { |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 2369 | return createUserInternal(name, flags, parentId, null); |
| 2370 | } |
| 2371 | |
| 2372 | private UserInfo createUserInternal(String name, int flags, int parentId, |
| 2373 | String[] disallowedPackages) { |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2374 | String restriction = ((flags & UserInfo.FLAG_MANAGED_PROFILE) != 0) |
| 2375 | ? UserManager.DISALLOW_ADD_MANAGED_PROFILE |
| 2376 | : UserManager.DISALLOW_ADD_USER; |
| 2377 | if (hasUserRestriction(restriction, UserHandle.getCallingUserId())) { |
| 2378 | Log.w(LOG_TAG, "Cannot add user. " + restriction + " is enabled."); |
Julia Reynolds | 7517502 | 2014-06-26 16:35:00 -0400 | [diff] [blame] | 2379 | return null; |
| 2380 | } |
Tony Mak | 6dc428f | 2016-10-10 15:48:27 +0100 | [diff] [blame] | 2381 | return createUserInternalUnchecked(name, flags, parentId, disallowedPackages); |
| 2382 | } |
| 2383 | |
| 2384 | private UserInfo createUserInternalUnchecked(String name, int flags, int parentId, |
| 2385 | String[] disallowedPackages) { |
Suprabh Shukla | c5e057c | 2016-08-08 16:22:44 -0700 | [diff] [blame] | 2386 | DeviceStorageMonitorInternal dsm = LocalServices |
| 2387 | .getService(DeviceStorageMonitorInternal.class); |
| 2388 | if (dsm.isMemoryLow()) { |
| 2389 | Log.w(LOG_TAG, "Cannot add user. Not enough space on disk."); |
| 2390 | return null; |
| 2391 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 2392 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 2393 | return null; |
| 2394 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 2395 | final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 2396 | final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2397 | final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0; |
Amith Yamasani | 52c21e3 | 2016-05-31 09:12:20 -0700 | [diff] [blame] | 2398 | final boolean isDemo = (flags & UserInfo.FLAG_DEMO) != 0; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2399 | final long ident = Binder.clearCallingIdentity(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2400 | UserInfo userInfo; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2401 | UserData userData; |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 2402 | final int userId; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2403 | try { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2404 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2405 | UserData parent = null; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2406 | if (parentId != UserHandle.USER_NULL) { |
| 2407 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2408 | parent = getUserDataLU(parentId); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 2409 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2410 | if (parent == null) return null; |
| 2411 | } |
| 2412 | if (isManagedProfile && !canAddMoreManagedProfiles(parentId, false)) { |
| 2413 | Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId); |
| 2414 | return null; |
| 2415 | } |
Amith Yamasani | 52c21e3 | 2016-05-31 09:12:20 -0700 | [diff] [blame] | 2416 | if (!isGuest && !isManagedProfile && !isDemo && isUserLimitReached()) { |
| 2417 | // If we're not adding a guest/demo user or a managed profile and the limit has |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2418 | // been reached, cannot add a user. |
| 2419 | return null; |
| 2420 | } |
| 2421 | // If we're adding a guest and there already exists one, bail. |
| 2422 | if (isGuest && findCurrentGuestUser() != null) { |
| 2423 | return null; |
| 2424 | } |
| 2425 | // In legacy mode, restricted profile's parent can only be the owner user |
| 2426 | if (isRestricted && !UserManager.isSplitSystemUser() |
| 2427 | && (parentId != UserHandle.USER_SYSTEM)) { |
| 2428 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be owner"); |
| 2429 | return null; |
| 2430 | } |
| 2431 | if (isRestricted && UserManager.isSplitSystemUser()) { |
| 2432 | if (parent == null) { |
| 2433 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be " |
| 2434 | + "specified"); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 2435 | return null; |
| 2436 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2437 | if (!parent.info.canHaveProfile()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2438 | Log.w(LOG_TAG, "Cannot add restricted profile - profiles cannot be " |
| 2439 | + "created for the specified parent user id " + parentId); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 2440 | return null; |
| 2441 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2442 | } |
Suprabh Shukla | cc30b0e7 | 2016-05-20 14:41:22 -0700 | [diff] [blame] | 2443 | if (!UserManager.isSplitSystemUser() && (flags & UserInfo.FLAG_EPHEMERAL) != 0 |
| 2444 | && (flags & UserInfo.FLAG_DEMO) == 0) { |
Lenka Trochtova | 024f979 | 2016-02-17 13:55:17 +0100 | [diff] [blame] | 2445 | Log.e(LOG_TAG, |
| 2446 | "Ephemeral users are supported on split-system-user systems only."); |
| 2447 | return null; |
| 2448 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2449 | // In split system user mode, we assign the first human user the primary flag. |
| 2450 | // And if there is no device owner, we also assign the admin flag to primary user. |
| 2451 | if (UserManager.isSplitSystemUser() |
| 2452 | && !isGuest && !isManagedProfile && getPrimaryUser() == null) { |
| 2453 | flags |= UserInfo.FLAG_PRIMARY; |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2454 | synchronized (mUsersLock) { |
| 2455 | if (!mIsDeviceManaged) { |
| 2456 | flags |= UserInfo.FLAG_ADMIN; |
| 2457 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 2458 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2459 | } |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2460 | |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 2461 | userId = getNextAvailableId(); |
| 2462 | Environment.getUserSystemDirectory(userId).mkdirs(); |
Lenka Trochtova | 02fee15 | 2015-12-22 14:26:18 +0100 | [diff] [blame] | 2463 | boolean ephemeralGuests = Resources.getSystem() |
| 2464 | .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral); |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 2465 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2466 | synchronized (mUsersLock) { |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 2467 | // Add ephemeral flag to guests/users if required. Also inherit it from parent. |
| 2468 | if ((isGuest && ephemeralGuests) || mForceEphemeralUsers |
| 2469 | || (parent != null && parent.info.isEphemeral())) { |
| 2470 | flags |= UserInfo.FLAG_EPHEMERAL; |
| 2471 | } |
| 2472 | |
| 2473 | userInfo = new UserInfo(userId, name, null, flags); |
| 2474 | userInfo.serialNumber = mNextSerialNumber++; |
| 2475 | long now = System.currentTimeMillis(); |
| 2476 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
| 2477 | userInfo.partial = true; |
Fyodor Kupolov | 83c2424 | 2016-03-31 13:30:42 -0700 | [diff] [blame] | 2478 | userInfo.lastLoggedInFingerprint = Build.FINGERPRINT; |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 2479 | if (isManagedProfile && parentId != UserHandle.USER_NULL) { |
| 2480 | userInfo.profileBadge = getFreeProfileBadgeLU(parentId); |
| 2481 | } |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 2482 | userData = new UserData(); |
| 2483 | userData.info = userInfo; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2484 | mUsers.put(userId, userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2485 | } |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 2486 | writeUserLP(userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2487 | writeUserListLP(); |
| 2488 | if (parent != null) { |
| 2489 | if (isManagedProfile) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2490 | if (parent.info.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 2491 | parent.info.profileGroupId = parent.info.id; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2492 | writeUserLP(parent); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 2493 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2494 | userInfo.profileGroupId = parent.info.profileGroupId; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2495 | } else if (isRestricted) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2496 | if (parent.info.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 2497 | parent.info.restrictedProfileParentId = parent.info.id; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2498 | writeUserLP(parent); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2499 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2500 | userInfo.restrictedProfileParentId = parent.info.restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2501 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2502 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 2503 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2504 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 2505 | storage.createUserKey(userId, userInfo.serialNumber, userInfo.isEphemeral()); |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 2506 | mUserDataPreparer.prepareUserData(userId, userInfo.serialNumber, |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 2507 | StorageManager.FLAG_STORAGE_DE | StorageManager.FLAG_STORAGE_CE); |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 2508 | mPm.createNewUser(userId, disallowedPackages); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2509 | userInfo.partial = false; |
| 2510 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2511 | writeUserLP(userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2512 | } |
| 2513 | updateUserIds(); |
| 2514 | Bundle restrictions = new Bundle(); |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 2515 | if (isGuest) { |
| 2516 | synchronized (mGuestRestrictions) { |
| 2517 | restrictions.putAll(mGuestRestrictions); |
| 2518 | } |
| 2519 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2520 | synchronized (mRestrictionsLock) { |
| 2521 | mBaseUserRestrictions.append(userId, restrictions); |
| 2522 | } |
Fyodor Kupolov | 5fd967d | 2016-07-12 14:46:19 -0700 | [diff] [blame] | 2523 | mPm.onNewUserCreated(userId); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2524 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 2525 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 2526 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 2527 | android.Manifest.permission.MANAGE_USERS); |
Christine Franks | 8822056 | 2017-05-24 11:11:21 -0700 | [diff] [blame] | 2528 | MetricsLogger.count(mContext, isGuest ? TRON_GUEST_CREATED |
| 2529 | : (isDemo ? TRON_DEMO_CREATED : TRON_USER_CREATED), 1); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2530 | } finally { |
| 2531 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 2532 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2533 | return userInfo; |
| 2534 | } |
| 2535 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 2536 | @VisibleForTesting |
| 2537 | UserData putUserInfo(UserInfo userInfo) { |
| 2538 | final UserData userData = new UserData(); |
| 2539 | userData.info = userInfo; |
| 2540 | synchronized (mUsers) { |
| 2541 | mUsers.put(userInfo.id, userData); |
| 2542 | } |
| 2543 | return userData; |
| 2544 | } |
| 2545 | |
| 2546 | @VisibleForTesting |
| 2547 | void removeUserInfo(int userId) { |
| 2548 | synchronized (mUsers) { |
| 2549 | mUsers.remove(userId); |
| 2550 | } |
| 2551 | } |
| 2552 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2553 | /** |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2554 | * @hide |
| 2555 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2556 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2557 | public UserInfo createRestrictedProfile(String name, int parentUserId) { |
Sudheer Shanka | 53d4147 | 2016-06-16 09:11:08 -0700 | [diff] [blame] | 2558 | checkManageOrCreateUsersPermission("setupRestrictedProfile"); |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 2559 | final UserInfo user = createProfileForUser( |
| 2560 | name, UserInfo.FLAG_RESTRICTED, parentUserId, null); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2561 | if (user == null) { |
| 2562 | return null; |
| 2563 | } |
Fyodor Kupolov | 9e912ba | 2016-02-09 18:29:43 -0800 | [diff] [blame] | 2564 | long identity = Binder.clearCallingIdentity(); |
| 2565 | try { |
| 2566 | setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user.id); |
| 2567 | // Change the setting before applying the DISALLOW_SHARE_LOCATION restriction, otherwise |
| 2568 | // the putIntForUser() will fail. |
| 2569 | android.provider.Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 2570 | android.provider.Settings.Secure.LOCATION_MODE, |
| 2571 | android.provider.Settings.Secure.LOCATION_MODE_OFF, user.id); |
| 2572 | setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user.id); |
| 2573 | } finally { |
| 2574 | Binder.restoreCallingIdentity(identity); |
| 2575 | } |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2576 | return user; |
| 2577 | } |
| 2578 | |
| 2579 | /** |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2580 | * Find the current guest user. If the Guest user is partial, |
| 2581 | * then do not include it in the results as it is about to die. |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2582 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2583 | private UserInfo findCurrentGuestUser() { |
| 2584 | synchronized (mUsersLock) { |
| 2585 | final int size = mUsers.size(); |
| 2586 | for (int i = 0; i < size; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2587 | final UserInfo user = mUsers.valueAt(i).info; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2588 | if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) { |
| 2589 | return user; |
| 2590 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2591 | } |
| 2592 | } |
| 2593 | return null; |
| 2594 | } |
| 2595 | |
| 2596 | /** |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2597 | * Mark this guest user for deletion to allow us to create another guest |
| 2598 | * and switch to that user before actually removing this guest. |
| 2599 | * @param userHandle the userid of the current guest |
| 2600 | * @return whether the user could be marked for deletion |
| 2601 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2602 | @Override |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2603 | public boolean markGuestForDeletion(int userHandle) { |
| 2604 | checkManageUsersPermission("Only the system can remove users"); |
| 2605 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 2606 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 2607 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 2608 | return false; |
| 2609 | } |
| 2610 | |
| 2611 | long ident = Binder.clearCallingIdentity(); |
| 2612 | try { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2613 | final UserData userData; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2614 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2615 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2616 | userData = mUsers.get(userHandle); |
| 2617 | if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2618 | return false; |
| 2619 | } |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2620 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2621 | if (!userData.info.isGuest()) { |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2622 | return false; |
| 2623 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 2624 | // We set this to a guest user that is to be removed. This is a temporary state |
| 2625 | // where we are allowed to add new Guest users, even if this one is still not |
| 2626 | // removed. This user will still show up in getUserInfo() calls. |
| 2627 | // If we don't get around to removing this Guest user, it will be purged on next |
| 2628 | // startup. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2629 | userData.info.guestToRemove = true; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2630 | // Mark it as disabled, so that it isn't returned any more when |
| 2631 | // profiles are queried. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2632 | userData.info.flags |= UserInfo.FLAG_DISABLED; |
| 2633 | writeUserLP(userData); |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } finally { |
| 2636 | Binder.restoreCallingIdentity(ident); |
| 2637 | } |
| 2638 | return true; |
| 2639 | } |
| 2640 | |
| 2641 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2642 | * Removes a user and all data directories created for that user. This method should be called |
| 2643 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 2644 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2645 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2646 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 2647 | public boolean removeUser(int userHandle) { |
Fyodor Kupolov | 4593e42 | 2016-10-27 11:00:29 -0700 | [diff] [blame] | 2648 | Slog.i(LOG_TAG, "removeUser u" + userHandle); |
Sudheer Shanka | f5cea03 | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 2649 | checkManageOrCreateUsersPermission("Only the system can remove users"); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2650 | |
| 2651 | final boolean isManagedProfile; |
| 2652 | synchronized (mUsersLock) { |
| 2653 | UserInfo userInfo = getUserInfoLU(userHandle); |
| 2654 | isManagedProfile = userInfo != null && userInfo.isManagedProfile(); |
| 2655 | } |
| 2656 | String restriction = isManagedProfile |
| 2657 | ? UserManager.DISALLOW_REMOVE_MANAGED_PROFILE : UserManager.DISALLOW_REMOVE_USER; |
| 2658 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(restriction, false)) { |
| 2659 | Log.w(LOG_TAG, "Cannot remove user. " + restriction + " is enabled."); |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 2660 | return false; |
| 2661 | } |
Nicolas Prevot | df1b87d | 2016-10-25 13:57:08 +0100 | [diff] [blame] | 2662 | return removeUserUnchecked(userHandle); |
| 2663 | } |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 2664 | |
Nicolas Prevot | df1b87d | 2016-10-25 13:57:08 +0100 | [diff] [blame] | 2665 | private boolean removeUserUnchecked(int userHandle) { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2666 | long ident = Binder.clearCallingIdentity(); |
| 2667 | try { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2668 | final UserData userData; |
Fyodor Kupolov | 0df68cd | 2015-10-01 13:54:22 -0700 | [diff] [blame] | 2669 | int currentUser = ActivityManager.getCurrentUser(); |
| 2670 | if (currentUser == userHandle) { |
| 2671 | Log.w(LOG_TAG, "Current user cannot be removed"); |
| 2672 | return false; |
| 2673 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2674 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2675 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2676 | userData = mUsers.get(userHandle); |
| 2677 | if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2678 | return false; |
| 2679 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 2680 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 2681 | addRemovingUserIdLocked(userHandle); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2682 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 2683 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2684 | // Set this to a partially created user, so that the user will be purged |
| 2685 | // on next startup, in case the runtime stops now before stopping and |
| 2686 | // removing the user completely. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2687 | userData.info.partial = true; |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2688 | // Mark it as disabled, so that it isn't returned any more when |
| 2689 | // profiles are queried. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2690 | userData.info.flags |= UserInfo.FLAG_DISABLED; |
| 2691 | writeUserLP(userData); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2692 | } |
Fyodor Kupolov | 6c96f7f | 2017-06-28 18:22:59 -0700 | [diff] [blame] | 2693 | try { |
| 2694 | mAppOpsService.removeUser(userHandle); |
| 2695 | } catch (RemoteException e) { |
| 2696 | Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e); |
| 2697 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2698 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2699 | if (userData.info.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 2700 | && userData.info.isManagedProfile()) { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2701 | // Send broadcast to notify system that the user removed was a |
| 2702 | // managed user. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2703 | sendProfileRemovedBroadcast(userData.info.profileGroupId, userData.info.id); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
| 2707 | int res; |
| 2708 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2709 | res = ActivityManager.getService().stopUser(userHandle, /* force= */ true, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 2710 | new IStopUserCallback.Stub() { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2711 | @Override |
| 2712 | public void userStopped(int userId) { |
| 2713 | finishRemoveUser(userId); |
| 2714 | } |
| 2715 | @Override |
| 2716 | public void userStopAborted(int userId) { |
| 2717 | } |
| 2718 | }); |
| 2719 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2720 | return false; |
| 2721 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2722 | return res == ActivityManager.USER_OP_SUCCESS; |
| 2723 | } finally { |
| 2724 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2725 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 2728 | @VisibleForTesting |
| 2729 | void addRemovingUserIdLocked(int userId) { |
| 2730 | // We remember deleted user IDs to prevent them from being |
| 2731 | // reused during the current boot; they can still be reused |
| 2732 | // after a reboot or recycling of userIds. |
| 2733 | mRemovingUserIds.put(userId, true); |
| 2734 | mRecentlyRemovedIds.add(userId); |
| 2735 | // Keep LRU queue of recently removed IDs for recycling |
| 2736 | if (mRecentlyRemovedIds.size() > MAX_RECENTLY_REMOVED_IDS_SIZE) { |
| 2737 | mRecentlyRemovedIds.removeFirst(); |
| 2738 | } |
| 2739 | } |
| 2740 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2741 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 2742 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2743 | // Let other services shutdown any activity and clean up their state before completely |
| 2744 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2745 | long ident = Binder.clearCallingIdentity(); |
| 2746 | try { |
| 2747 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 2748 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2749 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 2750 | android.Manifest.permission.MANAGE_USERS, |
| 2751 | |
| 2752 | new BroadcastReceiver() { |
| 2753 | @Override |
| 2754 | public void onReceive(Context context, Intent intent) { |
| 2755 | if (DBG) { |
| 2756 | Slog.i(LOG_TAG, |
| 2757 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 2758 | + userHandle); |
| 2759 | } |
| 2760 | new Thread() { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2761 | @Override |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2762 | public void run() { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 2763 | // Clean up any ActivityManager state |
| 2764 | LocalServices.getService(ActivityManagerInternal.class) |
| 2765 | .onUserRemoved(userHandle); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2766 | removeUserState(userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2767 | } |
| 2768 | }.start(); |
| 2769 | } |
| 2770 | }, |
| 2771 | |
| 2772 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2773 | } finally { |
| 2774 | Binder.restoreCallingIdentity(ident); |
| 2775 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2778 | private void removeUserState(final int userHandle) { |
Paul Crowley | 9129379 | 2016-03-25 15:23:07 -0700 | [diff] [blame] | 2779 | try { |
| 2780 | mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle); |
| 2781 | } catch (IllegalStateException e) { |
| 2782 | // This may be simply because the user was partially created. |
| 2783 | Slog.i(LOG_TAG, |
| 2784 | "Destroying key for user " + userHandle + " failed, continuing anyway", e); |
| 2785 | } |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 2786 | |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 2787 | // Cleanup gatekeeper secure user id |
| 2788 | try { |
| 2789 | final IGateKeeperService gk = GateKeeper.getService(); |
| 2790 | if (gk != null) { |
| 2791 | gk.clearSecureUserId(userHandle); |
| 2792 | } |
| 2793 | } catch (Exception ex) { |
| 2794 | Slog.w(LOG_TAG, "unable to clear GK secure user id"); |
| 2795 | } |
| 2796 | |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2797 | // Cleanup package manager settings |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2798 | mPm.cleanUpUser(this, userHandle); |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 2799 | |
| 2800 | // Clean up all data before removing metadata |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 2801 | mUserDataPreparer.destroyUserData(userHandle, |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 2802 | StorageManager.FLAG_STORAGE_DE | StorageManager.FLAG_STORAGE_CE); |
| 2803 | |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2804 | // Remove this user from the list |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2805 | synchronized (mUsersLock) { |
| 2806 | mUsers.remove(userHandle); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2807 | mIsUserManaged.delete(userHandle); |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 2808 | } |
| 2809 | synchronized (mUserStates) { |
| 2810 | mUserStates.delete(userHandle); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2811 | } |
| 2812 | synchronized (mRestrictionsLock) { |
| 2813 | mBaseUserRestrictions.remove(userHandle); |
| 2814 | mAppliedUserRestrictions.remove(userHandle); |
| 2815 | mCachedEffectiveUserRestrictions.remove(userHandle); |
| 2816 | mDevicePolicyLocalUserRestrictions.remove(userHandle); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 2817 | if (mDevicePolicyGlobalUserRestrictions.get(userHandle) != null) { |
| 2818 | mDevicePolicyGlobalUserRestrictions.remove(userHandle); |
| 2819 | applyUserRestrictionsForAllUsersLR(); |
| 2820 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2821 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2822 | // Update the user list |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2823 | synchronized (mPackagesLock) { |
| 2824 | writeUserListLP(); |
| 2825 | } |
Jeff Sharkey | cd57599 | 2016-03-29 14:12:49 -0600 | [diff] [blame] | 2826 | // Remove user file |
| 2827 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
| 2828 | userFile.delete(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2829 | updateUserIds(); |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 2830 | if (RELEASE_DELETED_USER_ID) { |
| 2831 | synchronized (mUsers) { |
| 2832 | mRemovingUserIds.delete(userHandle); |
| 2833 | } |
| 2834 | } |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2837 | private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2838 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Adam Connors | d4b584e | 2014-06-09 13:55:47 +0100 | [diff] [blame] | 2839 | managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 2840 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2841 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame] | 2842 | managedProfileIntent.putExtra(Intent.EXTRA_USER_HANDLE, removedUserId); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2843 | mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null); |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2846 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2847 | public Bundle getApplicationRestrictions(String packageName) { |
| 2848 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 2849 | } |
| 2850 | |
| 2851 | @Override |
| 2852 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2853 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2854 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 2855 | checkSystemOrRoot("get application restrictions for other user/app " + packageName); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2856 | } |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2857 | synchronized (mAppRestrictionsLock) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2858 | // Read the restrictions from XML |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2859 | return readApplicationRestrictionsLAr(packageName, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2864 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2865 | int userId) { |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 2866 | checkSystemOrRoot("set application restrictions"); |
Fyodor Kupolov | 0d88d54 | 2016-04-19 11:37:22 -0700 | [diff] [blame] | 2867 | if (restrictions != null) { |
| 2868 | restrictions.setDefusable(true); |
| 2869 | } |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2870 | synchronized (mAppRestrictionsLock) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2871 | if (restrictions == null || restrictions.isEmpty()) { |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2872 | cleanAppRestrictionsForPackageLAr(packageName, userId); |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2873 | } else { |
| 2874 | // Write the restrictions to XML |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2875 | writeApplicationRestrictionsLAr(packageName, restrictions, userId); |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2876 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2877 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 2878 | |
Fyodor Kupolov | d284612 | 2016-02-11 18:06:34 -0800 | [diff] [blame] | 2879 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 2880 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 2881 | changeIntent.setPackage(packageName); |
| 2882 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 2883 | mContext.sendBroadcastAsUser(changeIntent, UserHandle.of(userId)); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2887 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2888 | try { |
| 2889 | return mContext.getPackageManager().getApplicationInfo(packageName, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 2890 | PackageManager.MATCH_ANY_USER).uid; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2891 | } catch (NameNotFoundException nnfe) { |
| 2892 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2893 | } finally { |
| 2894 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2898 | @GuardedBy("mAppRestrictionsLock") |
| 2899 | private static Bundle readApplicationRestrictionsLAr(String packageName, int userId) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2900 | AtomicFile restrictionsFile = |
| 2901 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 2902 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2903 | return readApplicationRestrictionsLAr(restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | @VisibleForTesting |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2907 | @GuardedBy("mAppRestrictionsLock") |
| 2908 | static Bundle readApplicationRestrictionsLAr(AtomicFile restrictionsFile) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2909 | final Bundle restrictions = new Bundle(); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2910 | final ArrayList<String> values = new ArrayList<>(); |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2911 | if (!restrictionsFile.getBaseFile().exists()) { |
Fyodor Kupolov | f6ee224 | 2015-04-06 10:15:07 -0700 | [diff] [blame] | 2912 | return restrictions; |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2913 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2914 | |
| 2915 | FileInputStream fis = null; |
| 2916 | try { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2917 | fis = restrictionsFile.openRead(); |
| 2918 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2919 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2920 | XmlUtils.nextElement(parser); |
| 2921 | if (parser.getEventType() != XmlPullParser.START_TAG) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2922 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 2923 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2924 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2925 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2926 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 2927 | readEntry(restrictions, values, parser); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2928 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2929 | } catch (IOException|XmlPullParserException e) { |
| 2930 | Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2931 | } finally { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2932 | IoUtils.closeQuietly(fis); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2933 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2934 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2935 | } |
| 2936 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2937 | private static void readEntry(Bundle restrictions, ArrayList<String> values, |
| 2938 | XmlPullParser parser) throws XmlPullParserException, IOException { |
| 2939 | int type = parser.getEventType(); |
| 2940 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 2941 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 2942 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
| 2943 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 2944 | if (multiple != null) { |
| 2945 | values.clear(); |
| 2946 | int count = Integer.parseInt(multiple); |
| 2947 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 2948 | if (type == XmlPullParser.START_TAG |
| 2949 | && parser.getName().equals(TAG_VALUE)) { |
| 2950 | values.add(parser.nextText().trim()); |
| 2951 | count--; |
| 2952 | } |
| 2953 | } |
| 2954 | String [] valueStrings = new String[values.size()]; |
| 2955 | values.toArray(valueStrings); |
| 2956 | restrictions.putStringArray(key, valueStrings); |
| 2957 | } else if (ATTR_TYPE_BUNDLE.equals(valType)) { |
| 2958 | restrictions.putBundle(key, readBundleEntry(parser, values)); |
| 2959 | } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) { |
| 2960 | final int outerDepth = parser.getDepth(); |
| 2961 | ArrayList<Bundle> bundleList = new ArrayList<>(); |
| 2962 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2963 | Bundle childBundle = readBundleEntry(parser, values); |
| 2964 | bundleList.add(childBundle); |
| 2965 | } |
| 2966 | restrictions.putParcelableArray(key, |
| 2967 | bundleList.toArray(new Bundle[bundleList.size()])); |
| 2968 | } else { |
| 2969 | String value = parser.nextText().trim(); |
| 2970 | if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 2971 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 2972 | } else if (ATTR_TYPE_INTEGER.equals(valType)) { |
| 2973 | restrictions.putInt(key, Integer.parseInt(value)); |
| 2974 | } else { |
| 2975 | restrictions.putString(key, value); |
| 2976 | } |
| 2977 | } |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values) |
| 2982 | throws IOException, XmlPullParserException { |
| 2983 | Bundle childBundle = new Bundle(); |
| 2984 | final int outerDepth = parser.getDepth(); |
| 2985 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2986 | readEntry(childBundle, values, parser); |
| 2987 | } |
| 2988 | return childBundle; |
| 2989 | } |
| 2990 | |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2991 | @GuardedBy("mAppRestrictionsLock") |
| 2992 | private static void writeApplicationRestrictionsLAr(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2993 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2994 | AtomicFile restrictionsFile = new AtomicFile( |
| 2995 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2996 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 2997 | writeApplicationRestrictionsLAr(restrictions, restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | @VisibleForTesting |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 3001 | @GuardedBy("mAppRestrictionsLock") |
| 3002 | static void writeApplicationRestrictionsLAr(Bundle restrictions, AtomicFile restrictionsFile) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 3003 | FileOutputStream fos = null; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3004 | try { |
| 3005 | fos = restrictionsFile.startWrite(); |
| 3006 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 3007 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3008 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 3009 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3010 | serializer.startDocument(null, true); |
| 3011 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 3012 | |
| 3013 | serializer.startTag(null, TAG_RESTRICTIONS); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 3014 | writeBundle(restrictions, serializer); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3015 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 3016 | |
| 3017 | serializer.endDocument(); |
| 3018 | restrictionsFile.finishWrite(fos); |
| 3019 | } catch (Exception e) { |
| 3020 | restrictionsFile.failWrite(fos); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 3021 | Slog.e(LOG_TAG, "Error writing application restrictions list", e); |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | private static void writeBundle(Bundle restrictions, XmlSerializer serializer) |
| 3026 | throws IOException { |
| 3027 | for (String key : restrictions.keySet()) { |
| 3028 | Object value = restrictions.get(key); |
| 3029 | serializer.startTag(null, TAG_ENTRY); |
| 3030 | serializer.attribute(null, ATTR_KEY, key); |
| 3031 | |
| 3032 | if (value instanceof Boolean) { |
| 3033 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 3034 | serializer.text(value.toString()); |
| 3035 | } else if (value instanceof Integer) { |
| 3036 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER); |
| 3037 | serializer.text(value.toString()); |
| 3038 | } else if (value == null || value instanceof String) { |
| 3039 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 3040 | serializer.text(value != null ? (String) value : ""); |
| 3041 | } else if (value instanceof Bundle) { |
| 3042 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 3043 | writeBundle((Bundle) value, serializer); |
| 3044 | } else if (value instanceof Parcelable[]) { |
| 3045 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY); |
| 3046 | Parcelable[] array = (Parcelable[]) value; |
| 3047 | for (Parcelable parcelable : array) { |
| 3048 | if (!(parcelable instanceof Bundle)) { |
| 3049 | throw new IllegalArgumentException("bundle-array can only hold Bundles"); |
| 3050 | } |
| 3051 | serializer.startTag(null, TAG_ENTRY); |
| 3052 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 3053 | writeBundle((Bundle) parcelable, serializer); |
| 3054 | serializer.endTag(null, TAG_ENTRY); |
| 3055 | } |
| 3056 | } else { |
| 3057 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 3058 | String[] values = (String[]) value; |
| 3059 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
| 3060 | for (String choice : values) { |
| 3061 | serializer.startTag(null, TAG_VALUE); |
| 3062 | serializer.text(choice != null ? choice : ""); |
| 3063 | serializer.endTag(null, TAG_VALUE); |
| 3064 | } |
| 3065 | } |
| 3066 | serializer.endTag(null, TAG_ENTRY); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3071 | public int getUserSerialNumber(int userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3072 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3073 | if (!exists(userHandle)) return -1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3074 | return getUserInfoLU(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | @Override |
Fyodor Kupolov | 605b12a | 2017-05-10 15:58:09 -0700 | [diff] [blame] | 3079 | public boolean isUserNameSet(int userHandle) { |
| 3080 | synchronized (mUsersLock) { |
| 3081 | UserInfo userInfo = getUserInfoLU(userHandle); |
| 3082 | return userInfo != null && userInfo.name != null; |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3087 | public int getUserHandle(int userSerialNumber) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3088 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3089 | for (int userId : mUserIds) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3090 | UserInfo info = getUserInfoLU(userId); |
Kenny Guy | 945f883 | 2015-02-10 15:17:26 +0000 | [diff] [blame] | 3091 | if (info != null && info.serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 3092 | } |
| 3093 | // Not found |
| 3094 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 3098 | @Override |
| 3099 | public long getUserCreationTime(int userHandle) { |
| 3100 | int callingUserId = UserHandle.getCallingUserId(); |
| 3101 | UserInfo userInfo = null; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3102 | synchronized (mUsersLock) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 3103 | if (callingUserId == userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3104 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 3105 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3106 | UserInfo parent = getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 3107 | if (parent != null && parent.id == callingUserId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3108 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | if (userInfo == null) { |
| 3113 | throw new SecurityException("userHandle can only be the calling user or a managed " |
| 3114 | + "profile associated with this user"); |
| 3115 | } |
| 3116 | return userInfo.creationTime; |
| 3117 | } |
| 3118 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3119 | /** |
| 3120 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 3121 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3122 | private void updateUserIds() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3123 | int num = 0; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3124 | synchronized (mUsersLock) { |
| 3125 | final int userSize = mUsers.size(); |
| 3126 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3127 | if (!mUsers.valueAt(i).info.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3128 | num++; |
| 3129 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3130 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3131 | final int[] newUsers = new int[num]; |
| 3132 | int n = 0; |
| 3133 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3134 | if (!mUsers.valueAt(i).info.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3135 | newUsers[n++] = mUsers.keyAt(i); |
| 3136 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 3137 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3138 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /** |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 3143 | * Called right before a user is started. This gives us a chance to prepare |
| 3144 | * app storage and apply any user restrictions. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3145 | */ |
| 3146 | public void onBeforeStartUser(int userId) { |
Fyodor Kupolov | d80c362 | 2016-07-19 17:22:09 -0700 | [diff] [blame] | 3147 | UserInfo userInfo = getUserInfo(userId); |
| 3148 | if (userInfo == null) { |
| 3149 | return; |
| 3150 | } |
| 3151 | final int userSerial = userInfo.serialNumber; |
| 3152 | // Migrate only if build fingerprints mismatch |
| 3153 | boolean migrateAppsData = !Build.FINGERPRINT.equals(userInfo.lastLoggedInFingerprint); |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 3154 | mUserDataPreparer.prepareUserData(userId, userSerial, StorageManager.FLAG_STORAGE_DE); |
Fyodor Kupolov | d80c362 | 2016-07-19 17:22:09 -0700 | [diff] [blame] | 3155 | mPm.reconcileAppsData(userId, StorageManager.FLAG_STORAGE_DE, migrateAppsData); |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 3156 | |
| 3157 | if (userId != UserHandle.USER_SYSTEM) { |
| 3158 | synchronized (mRestrictionsLock) { |
| 3159 | applyUserRestrictionsLR(userId); |
| 3160 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | /** |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 3165 | * Called right before a user is unlocked. This gives us a chance to prepare |
| 3166 | * app storage. |
| 3167 | */ |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 3168 | public void onBeforeUnlockUser(@UserIdInt int userId) { |
Fyodor Kupolov | d80c362 | 2016-07-19 17:22:09 -0700 | [diff] [blame] | 3169 | UserInfo userInfo = getUserInfo(userId); |
| 3170 | if (userInfo == null) { |
| 3171 | return; |
| 3172 | } |
| 3173 | final int userSerial = userInfo.serialNumber; |
| 3174 | // Migrate only if build fingerprints mismatch |
| 3175 | boolean migrateAppsData = !Build.FINGERPRINT.equals(userInfo.lastLoggedInFingerprint); |
Fyodor Kupolov | 5c0ecfd | 2017-01-27 11:11:57 -0800 | [diff] [blame] | 3176 | mUserDataPreparer.prepareUserData(userId, userSerial, StorageManager.FLAG_STORAGE_CE); |
Fyodor Kupolov | d80c362 | 2016-07-19 17:22:09 -0700 | [diff] [blame] | 3177 | mPm.reconcileAppsData(userId, StorageManager.FLAG_STORAGE_CE, migrateAppsData); |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /** |
Fyodor Kupolov | 50979d1 | 2017-01-27 17:36:32 -0800 | [diff] [blame] | 3181 | * Examine all users present on given mounted volume, and destroy data |
| 3182 | * belonging to users that are no longer valid, or whose user ID has been |
| 3183 | * recycled. |
| 3184 | */ |
| 3185 | void reconcileUsers(String volumeUuid) { |
| 3186 | mUserDataPreparer.reconcileUsers(volumeUuid, getUsers(true /* excludeDying */)); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 3190 | * Make a note of the last started time of a user and do some cleanup. |
Amith Yamasani | eb437d4 | 2016-04-29 09:31:25 -0700 | [diff] [blame] | 3191 | * This is called with ActivityManagerService lock held. |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3192 | * @param userId the user that was just foregrounded |
| 3193 | */ |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 3194 | public void onUserLoggedIn(@UserIdInt int userId) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3195 | UserData userData = getUserDataNoChecks(userId); |
| 3196 | if (userData == null || userData.info.partial) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 3197 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 3198 | return; |
| 3199 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 3200 | |
| 3201 | final long now = System.currentTimeMillis(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 3202 | if (now > EPOCH_PLUS_30_YEARS) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3203 | userData.info.lastLoggedInTime = now; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3204 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 3205 | userData.info.lastLoggedInFingerprint = Build.FINGERPRINT; |
| 3206 | scheduleWriteUser(userData); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3210 | * Returns the next available user id, filling in any holes in the ids. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 3211 | */ |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 3212 | @VisibleForTesting |
| 3213 | int getNextAvailableId() { |
| 3214 | int nextId; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3215 | synchronized (mUsersLock) { |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 3216 | nextId = scanNextAvailableIdLocked(); |
| 3217 | if (nextId >= 0) { |
| 3218 | return nextId; |
| 3219 | } |
| 3220 | // All ids up to MAX_USER_ID were used. Remove all mRemovingUserIds, |
| 3221 | // except most recently removed |
| 3222 | if (mRemovingUserIds.size() > 0) { |
| 3223 | Slog.i(LOG_TAG, "All available IDs are used. Recycling LRU ids."); |
| 3224 | mRemovingUserIds.clear(); |
| 3225 | for (Integer recentlyRemovedId : mRecentlyRemovedIds) { |
| 3226 | mRemovingUserIds.put(recentlyRemovedId, true); |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 3227 | } |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 3228 | nextId = scanNextAvailableIdLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 3229 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 3230 | } |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 3231 | if (nextId < 0) { |
| 3232 | throw new IllegalStateException("No user id available!"); |
| 3233 | } |
| 3234 | return nextId; |
| 3235 | } |
| 3236 | |
| 3237 | private int scanNextAvailableIdLocked() { |
| 3238 | for (int i = MIN_USER_ID; i < MAX_USER_ID; i++) { |
| 3239 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
| 3240 | return i; |
| 3241 | } |
| 3242 | } |
| 3243 | return -1; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 3244 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3245 | |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 3246 | private static String packageToRestrictionsFileName(String packageName) { |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 3247 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 3248 | } |
| 3249 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3250 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3251 | public void setSeedAccountData(int userId, String accountName, String accountType, |
| 3252 | PersistableBundle accountOptions, boolean persist) { |
| 3253 | checkManageUsersPermission("Require MANAGE_USERS permission to set user seed data"); |
| 3254 | synchronized (mPackagesLock) { |
| 3255 | final UserData userData; |
| 3256 | synchronized (mUsersLock) { |
| 3257 | userData = getUserDataLU(userId); |
| 3258 | if (userData == null) { |
| 3259 | Slog.e(LOG_TAG, "No such user for settings seed data u=" + userId); |
| 3260 | return; |
| 3261 | } |
| 3262 | userData.seedAccountName = accountName; |
| 3263 | userData.seedAccountType = accountType; |
| 3264 | userData.seedAccountOptions = accountOptions; |
| 3265 | userData.persistSeedData = persist; |
| 3266 | } |
| 3267 | if (persist) { |
| 3268 | writeUserLP(userData); |
| 3269 | } |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | @Override |
| 3274 | public String getSeedAccountName() throws RemoteException { |
| 3275 | checkManageUsersPermission("Cannot get seed account information"); |
| 3276 | synchronized (mUsersLock) { |
| 3277 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 3278 | return userData.seedAccountName; |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | @Override |
| 3283 | public String getSeedAccountType() throws RemoteException { |
| 3284 | checkManageUsersPermission("Cannot get seed account information"); |
| 3285 | synchronized (mUsersLock) { |
| 3286 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 3287 | return userData.seedAccountType; |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | @Override |
| 3292 | public PersistableBundle getSeedAccountOptions() throws RemoteException { |
| 3293 | checkManageUsersPermission("Cannot get seed account information"); |
| 3294 | synchronized (mUsersLock) { |
| 3295 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 3296 | return userData.seedAccountOptions; |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | @Override |
| 3301 | public void clearSeedAccountData() throws RemoteException { |
| 3302 | checkManageUsersPermission("Cannot clear seed account information"); |
| 3303 | synchronized (mPackagesLock) { |
| 3304 | UserData userData; |
| 3305 | synchronized (mUsersLock) { |
| 3306 | userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 3307 | if (userData == null) return; |
| 3308 | userData.clearSeedAccountData(); |
| 3309 | } |
| 3310 | writeUserLP(userData); |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | @Override |
| 3315 | public boolean someUserHasSeedAccount(String accountName, String accountType) |
| 3316 | throws RemoteException { |
| 3317 | checkManageUsersPermission("Cannot check seed account information"); |
| 3318 | synchronized (mUsersLock) { |
| 3319 | final int userSize = mUsers.size(); |
| 3320 | for (int i = 0; i < userSize; i++) { |
| 3321 | final UserData data = mUsers.valueAt(i); |
| 3322 | if (data.info.isInitialized()) continue; |
| 3323 | if (data.seedAccountName == null || !data.seedAccountName.equals(accountName)) { |
| 3324 | continue; |
| 3325 | } |
| 3326 | if (data.seedAccountType == null || !data.seedAccountType.equals(accountType)) { |
| 3327 | continue; |
| 3328 | } |
| 3329 | return true; |
| 3330 | } |
| 3331 | } |
| 3332 | return false; |
| 3333 | } |
| 3334 | |
| 3335 | @Override |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 3336 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3337 | FileDescriptor err, String[] args, ShellCallback callback, |
| 3338 | ResultReceiver resultReceiver) { |
| 3339 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | int onShellCommand(Shell shell, String cmd) { |
| 3343 | if (cmd == null) { |
| 3344 | return shell.handleDefaultCommands(cmd); |
| 3345 | } |
| 3346 | |
| 3347 | final PrintWriter pw = shell.getOutPrintWriter(); |
| 3348 | try { |
| 3349 | switch(cmd) { |
| 3350 | case "list": |
| 3351 | return runList(pw); |
| 3352 | } |
| 3353 | } catch (RemoteException e) { |
| 3354 | pw.println("Remote exception: " + e); |
| 3355 | } |
| 3356 | return -1; |
| 3357 | } |
| 3358 | |
| 3359 | private int runList(PrintWriter pw) throws RemoteException { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 3360 | final IActivityManager am = ActivityManager.getService(); |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 3361 | final List<UserInfo> users = getUsers(false); |
| 3362 | if (users == null) { |
| 3363 | pw.println("Error: couldn't get users"); |
| 3364 | return 1; |
| 3365 | } else { |
| 3366 | pw.println("Users:"); |
| 3367 | for (int i = 0; i < users.size(); i++) { |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 3368 | String running = am.isUserRunning(users.get(i).id, 0) ? " running" : ""; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 3369 | pw.println("\t" + users.get(i).toString() + running); |
| 3370 | } |
| 3371 | return 0; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3376 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3377 | if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, pw)) return; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3378 | |
| 3379 | long now = System.currentTimeMillis(); |
| 3380 | StringBuilder sb = new StringBuilder(); |
| 3381 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3382 | synchronized (mUsersLock) { |
| 3383 | pw.println("Users:"); |
| 3384 | for (int i = 0; i < mUsers.size(); i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3385 | UserData userData = mUsers.valueAt(i); |
| 3386 | if (userData == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3387 | continue; |
| 3388 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3389 | UserInfo userInfo = userData.info; |
| 3390 | final int userId = userInfo.id; |
| 3391 | pw.print(" "); pw.print(userInfo); |
| 3392 | pw.print(" serialNo="); pw.print(userInfo.serialNumber); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 3393 | if (mRemovingUserIds.get(userId)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3394 | pw.print(" <removing> "); |
| 3395 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3396 | if (userInfo.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3397 | pw.print(" <partial>"); |
| 3398 | } |
| 3399 | pw.println(); |
Makoto Onuki | 88aef75 | 2017-03-29 16:52:03 -0700 | [diff] [blame] | 3400 | pw.print(" State: "); |
| 3401 | final int state; |
| 3402 | synchronized (mUserStates) { |
| 3403 | state = mUserStates.get(userId, -1); |
| 3404 | } |
| 3405 | pw.println(UserState.stateToString(state)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3406 | pw.print(" Created: "); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3407 | if (userInfo.creationTime == 0) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3408 | pw.println("<unknown>"); |
| 3409 | } else { |
| 3410 | sb.setLength(0); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3411 | TimeUtils.formatDuration(now - userInfo.creationTime, sb); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3412 | sb.append(" ago"); |
| 3413 | pw.println(sb); |
| 3414 | } |
| 3415 | pw.print(" Last logged in: "); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3416 | if (userInfo.lastLoggedInTime == 0) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3417 | pw.println("<unknown>"); |
| 3418 | } else { |
| 3419 | sb.setLength(0); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3420 | TimeUtils.formatDuration(now - userInfo.lastLoggedInTime, sb); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3421 | sb.append(" ago"); |
| 3422 | pw.println(sb); |
| 3423 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 3424 | pw.print(" Last logged in fingerprint: "); |
| 3425 | pw.println(userInfo.lastLoggedInFingerprint); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 3426 | pw.print(" Has profile owner: "); |
| 3427 | pw.println(mIsUserManaged.get(userId)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3428 | pw.println(" Restrictions:"); |
| 3429 | synchronized (mRestrictionsLock) { |
| 3430 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3431 | pw, " ", mBaseUserRestrictions.get(userInfo.id)); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 3432 | pw.println(" Device policy global restrictions:"); |
| 3433 | UserRestrictionsUtils.dumpRestrictions( |
| 3434 | pw, " ", mDevicePolicyGlobalUserRestrictions.get(userInfo.id)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3435 | pw.println(" Device policy local restrictions:"); |
| 3436 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3437 | pw, " ", mDevicePolicyLocalUserRestrictions.get(userInfo.id)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3438 | pw.println(" Effective restrictions:"); |
| 3439 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3440 | pw, " ", mCachedEffectiveUserRestrictions.get(userInfo.id)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 3441 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3442 | |
| 3443 | if (userData.account != null) { |
| 3444 | pw.print(" Account name: " + userData.account); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 3445 | pw.println(); |
| 3446 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3447 | |
| 3448 | if (userData.seedAccountName != null) { |
| 3449 | pw.print(" Seed account name: " + userData.seedAccountName); |
| 3450 | pw.println(); |
| 3451 | if (userData.seedAccountType != null) { |
| 3452 | pw.print(" account type: " + userData.seedAccountType); |
| 3453 | pw.println(); |
| 3454 | } |
| 3455 | if (userData.seedAccountOptions != null) { |
| 3456 | pw.print(" account options exist"); |
| 3457 | pw.println(); |
| 3458 | } |
| 3459 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3460 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3461 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3462 | pw.println(); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 3463 | pw.println(" Device owner id:" + mDeviceOwnerUserId); |
Zoltan Szatmary-Ban | e783460 | 2016-04-08 18:41:11 +0100 | [diff] [blame] | 3464 | pw.println(); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3465 | pw.println(" Guest restrictions:"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 3466 | synchronized (mGuestRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3467 | UserRestrictionsUtils.dumpRestrictions(pw, " ", mGuestRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 3468 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 3469 | synchronized (mUsersLock) { |
| 3470 | pw.println(); |
| 3471 | pw.println(" Device managed: " + mIsDeviceManaged); |
Fyodor Kupolov | 0f4533d | 2016-05-18 18:43:12 -0700 | [diff] [blame] | 3472 | if (mRemovingUserIds.size() > 0) { |
| 3473 | pw.println(); |
| 3474 | pw.println(" Recently removed userIds: " + mRecentlyRemovedIds); |
| 3475 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 3476 | } |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 3477 | synchronized (mUserStates) { |
| 3478 | pw.println(" Started users state: " + mUserStates); |
| 3479 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3480 | // Dump some capabilities |
| 3481 | pw.println(); |
| 3482 | pw.println(" Max users: " + UserManager.getMaxSupportedUsers()); |
| 3483 | pw.println(" Supports switchable users: " + UserManager.supportsMultipleUsers()); |
Lenka Trochtova | 02fee15 | 2015-12-22 14:26:18 +0100 | [diff] [blame] | 3484 | pw.println(" All guests ephemeral: " + Resources.getSystem().getBoolean( |
| 3485 | com.android.internal.R.bool.config_guestUserEphemeral)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 3486 | } |
| 3487 | } |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 3488 | |
| 3489 | final class MainHandler extends Handler { |
| 3490 | |
| 3491 | @Override |
| 3492 | public void handleMessage(Message msg) { |
| 3493 | switch (msg.what) { |
| 3494 | case WRITE_USER_MSG: |
| 3495 | removeMessages(WRITE_USER_MSG, msg.obj); |
| 3496 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3497 | int userId = ((UserData) msg.obj).info.id; |
| 3498 | UserData userData = getUserDataNoChecks(userId); |
| 3499 | if (userData != null) { |
| 3500 | writeUserLP(userData); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 3506 | |
| 3507 | /** |
| 3508 | * @param userId |
| 3509 | * @return whether the user has been initialized yet |
| 3510 | */ |
| 3511 | boolean isInitialized(int userId) { |
| 3512 | return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 3513 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3514 | |
| 3515 | private class LocalService extends UserManagerInternal { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3516 | @Override |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 3517 | public void setDevicePolicyUserRestrictions(int userId, @Nullable Bundle restrictions, |
| 3518 | boolean isDeviceOwner, int cameraRestrictionScope) { |
| 3519 | UserManagerService.this.setDevicePolicyUserRestrictionsInner(userId, restrictions, |
| 3520 | isDeviceOwner, cameraRestrictionScope); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | @Override |
| 3524 | public Bundle getBaseUserRestrictions(int userId) { |
| 3525 | synchronized (mRestrictionsLock) { |
| 3526 | return mBaseUserRestrictions.get(userId); |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | @Override |
| 3531 | public void setBaseUserRestrictionsByDpmsForMigration( |
| 3532 | int userId, Bundle baseRestrictions) { |
| 3533 | synchronized (mRestrictionsLock) { |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 3534 | if (updateRestrictionsIfNeededLR( |
| 3535 | userId, new Bundle(baseRestrictions), mBaseUserRestrictions)) { |
| 3536 | invalidateEffectiveUserRestrictionsLR(userId); |
| 3537 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3538 | } |
| 3539 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3540 | final UserData userData = getUserDataNoChecks(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3541 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3542 | if (userData != null) { |
| 3543 | writeUserLP(userData); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3544 | } else { |
| 3545 | Slog.w(LOG_TAG, "UserInfo not found for " + userId); |
| 3546 | } |
| 3547 | } |
| 3548 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 3549 | |
| 3550 | @Override |
| 3551 | public boolean getUserRestriction(int userId, String key) { |
| 3552 | return getUserRestrictions(userId).getBoolean(key); |
| 3553 | } |
| 3554 | |
| 3555 | @Override |
| 3556 | public void addUserRestrictionsListener(UserRestrictionsListener listener) { |
| 3557 | synchronized (mUserRestrictionsListeners) { |
| 3558 | mUserRestrictionsListeners.add(listener); |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | @Override |
| 3563 | public void removeUserRestrictionsListener(UserRestrictionsListener listener) { |
| 3564 | synchronized (mUserRestrictionsListeners) { |
| 3565 | mUserRestrictionsListeners.remove(listener); |
| 3566 | } |
| 3567 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 3568 | |
| 3569 | @Override |
| 3570 | public void setDeviceManaged(boolean isManaged) { |
| 3571 | synchronized (mUsersLock) { |
| 3572 | mIsDeviceManaged = isManaged; |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | @Override |
| 3577 | public void setUserManaged(int userId, boolean isManaged) { |
| 3578 | synchronized (mUsersLock) { |
| 3579 | mIsUserManaged.put(userId, isManaged); |
| 3580 | } |
| 3581 | } |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 3582 | |
| 3583 | @Override |
| 3584 | public void setUserIcon(int userId, Bitmap bitmap) { |
| 3585 | long ident = Binder.clearCallingIdentity(); |
| 3586 | try { |
| 3587 | synchronized (mPackagesLock) { |
| 3588 | UserData userData = getUserDataNoChecks(userId); |
| 3589 | if (userData == null || userData.info.partial) { |
| 3590 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 3591 | return; |
| 3592 | } |
| 3593 | writeBitmapLP(userData.info, bitmap); |
| 3594 | writeUserLP(userData); |
| 3595 | } |
| 3596 | sendUserInfoChangedBroadcast(userId); |
| 3597 | } finally { |
| 3598 | Binder.restoreCallingIdentity(ident); |
| 3599 | } |
| 3600 | } |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 3601 | |
| 3602 | @Override |
| 3603 | public void setForceEphemeralUsers(boolean forceEphemeralUsers) { |
| 3604 | synchronized (mUsersLock) { |
| 3605 | mForceEphemeralUsers = forceEphemeralUsers; |
| 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | @Override |
| 3610 | public void removeAllUsers() { |
| 3611 | if (UserHandle.USER_SYSTEM == ActivityManager.getCurrentUser()) { |
| 3612 | // Remove the non-system users straight away. |
| 3613 | removeNonSystemUsers(); |
| 3614 | } else { |
| 3615 | // Switch to the system user first and then remove the other users. |
| 3616 | BroadcastReceiver userSwitchedReceiver = new BroadcastReceiver() { |
| 3617 | @Override |
| 3618 | public void onReceive(Context context, Intent intent) { |
| 3619 | int userId = |
| 3620 | intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 3621 | if (userId != UserHandle.USER_SYSTEM) { |
| 3622 | return; |
| 3623 | } |
| 3624 | mContext.unregisterReceiver(this); |
| 3625 | removeNonSystemUsers(); |
| 3626 | } |
| 3627 | }; |
| 3628 | IntentFilter userSwitchedFilter = new IntentFilter(); |
| 3629 | userSwitchedFilter.addAction(Intent.ACTION_USER_SWITCHED); |
| 3630 | mContext.registerReceiver( |
| 3631 | userSwitchedReceiver, userSwitchedFilter, null, mHandler); |
| 3632 | |
| 3633 | // Switch to the system user. |
| 3634 | ActivityManager am = |
| 3635 | (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 3636 | am.switchUser(UserHandle.USER_SYSTEM); |
| 3637 | } |
| 3638 | } |
phweiss | e9c4406 | 2016-02-10 12:57:38 +0100 | [diff] [blame] | 3639 | |
| 3640 | @Override |
Lenka Trochtova | 1ddda47 | 2016-02-12 10:42:12 +0100 | [diff] [blame] | 3641 | public void onEphemeralUserStop(int userId) { |
| 3642 | synchronized (mUsersLock) { |
| 3643 | UserInfo userInfo = getUserInfoLU(userId); |
| 3644 | if (userInfo != null && userInfo.isEphemeral()) { |
| 3645 | // Do not allow switching back to the ephemeral user again as the user is going |
| 3646 | // to be deleted. |
| 3647 | userInfo.flags |= UserInfo.FLAG_DISABLED; |
| 3648 | if (userInfo.isGuest()) { |
| 3649 | // Indicate that the guest will be deleted after it stops. |
| 3650 | userInfo.guestToRemove = true; |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | @Override |
phweiss | e9c4406 | 2016-02-10 12:57:38 +0100 | [diff] [blame] | 3657 | public UserInfo createUserEvenWhenDisallowed(String name, int flags) { |
Sudheer Shanka | 7cb54a3 | 2016-09-16 12:59:05 -0700 | [diff] [blame] | 3658 | UserInfo user = createUserInternalUnchecked(name, flags, UserHandle.USER_NULL, null); |
phweiss | e9c4406 | 2016-02-10 12:57:38 +0100 | [diff] [blame] | 3659 | // Keep this in sync with UserManager.createUser |
Christine Franks | 97a5480 | 2017-08-09 10:06:43 -0700 | [diff] [blame] | 3660 | if (user != null && !user.isAdmin() && !user.isDemo()) { |
phweiss | e9c4406 | 2016-02-10 12:57:38 +0100 | [diff] [blame] | 3661 | setUserRestriction(UserManager.DISALLOW_SMS, true, user.id); |
| 3662 | setUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, true, user.id); |
| 3663 | } |
| 3664 | return user; |
| 3665 | } |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 3666 | |
| 3667 | @Override |
Nicolas Prevot | df1b87d | 2016-10-25 13:57:08 +0100 | [diff] [blame] | 3668 | public boolean removeUserEvenWhenDisallowed(int userId) { |
| 3669 | return removeUserUnchecked(userId); |
| 3670 | } |
| 3671 | |
| 3672 | @Override |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 3673 | public boolean isUserRunning(int userId) { |
| 3674 | synchronized (mUserStates) { |
| 3675 | return mUserStates.get(userId, -1) >= 0; |
| 3676 | } |
| 3677 | } |
| 3678 | |
| 3679 | @Override |
| 3680 | public void setUserState(int userId, int userState) { |
| 3681 | synchronized (mUserStates) { |
| 3682 | mUserStates.put(userId, userState); |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | @Override |
| 3687 | public void removeUserState(int userId) { |
| 3688 | synchronized (mUserStates) { |
| 3689 | mUserStates.delete(userId); |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | @Override |
Michael Wachenschwanz | 3f2b655 | 2017-05-15 13:53:09 -0700 | [diff] [blame] | 3694 | public int[] getUserIds() { |
| 3695 | return UserManagerService.this.getUserIds(); |
| 3696 | } |
| 3697 | |
| 3698 | @Override |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 3699 | public boolean isUserUnlockingOrUnlocked(int userId) { |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3700 | int state; |
Amith Yamasani | ea1b9d7 | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 3701 | synchronized (mUserStates) { |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3702 | state = mUserStates.get(userId, -1); |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 3703 | } |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3704 | // Special case, in the stopping/shutdown state user key can still be unlocked |
| 3705 | if (state == UserState.STATE_STOPPING || state == UserState.STATE_SHUTDOWN) { |
| 3706 | return StorageManager.isUserKeyUnlocked(userId); |
| 3707 | } |
| 3708 | return (state == UserState.STATE_RUNNING_UNLOCKING) |
| 3709 | || (state == UserState.STATE_RUNNING_UNLOCKED); |
Fyodor Kupolov | 6c915ea | 2016-05-09 19:10:53 -0700 | [diff] [blame] | 3710 | } |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 3711 | |
| 3712 | @Override |
| 3713 | public boolean isUserUnlocked(int userId) { |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3714 | int state; |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 3715 | synchronized (mUserStates) { |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3716 | state = mUserStates.get(userId, -1); |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 3717 | } |
Fyodor Kupolov | 0468ee9 | 2017-05-25 17:06:17 -0700 | [diff] [blame] | 3718 | // Special case, in the stopping/shutdown state user key can still be unlocked |
| 3719 | if (state == UserState.STATE_STOPPING || state == UserState.STATE_SHUTDOWN) { |
| 3720 | return StorageManager.isUserKeyUnlocked(userId); |
| 3721 | } |
| 3722 | return state == UserState.STATE_RUNNING_UNLOCKED; |
Fyodor Kupolov | 2e7e096 | 2016-12-01 18:09:17 -0800 | [diff] [blame] | 3723 | } |
Lenka Trochtova | f348e8e | 2016-01-07 17:20:34 +0100 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | /* Remove all the users except of the system one. */ |
| 3727 | private void removeNonSystemUsers() { |
| 3728 | ArrayList<UserInfo> usersToRemove = new ArrayList<>(); |
| 3729 | synchronized (mUsersLock) { |
| 3730 | final int userSize = mUsers.size(); |
| 3731 | for (int i = 0; i < userSize; i++) { |
| 3732 | UserInfo ui = mUsers.valueAt(i).info; |
| 3733 | if (ui.id != UserHandle.USER_SYSTEM) { |
| 3734 | usersToRemove.add(ui); |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | for (UserInfo ui: usersToRemove) { |
| 3739 | removeUser(ui.id); |
| 3740 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 3741 | } |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 3742 | |
| 3743 | private class Shell extends ShellCommand { |
| 3744 | @Override |
| 3745 | public int onCommand(String cmd) { |
| 3746 | return onShellCommand(this, cmd); |
| 3747 | } |
| 3748 | |
| 3749 | @Override |
| 3750 | public void onHelp() { |
| 3751 | final PrintWriter pw = getOutPrintWriter(); |
| 3752 | pw.println("User manager (user) commands:"); |
| 3753 | pw.println(" help"); |
| 3754 | pw.println(" Print this help text."); |
| 3755 | pw.println(""); |
| 3756 | pw.println(" list"); |
| 3757 | pw.println(" Prints all users on the system."); |
| 3758 | } |
| 3759 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 3760 | |
| 3761 | private static void debug(String message) { |
| 3762 | Log.d(LOG_TAG, message + |
| 3763 | (DBG_WITH_STACKTRACE ? " called at\n" + Debug.getCallers(10, " ") : "")); |
| 3764 | } |
Kenny Guy | 02c8990 | 2016-11-15 19:36:38 +0000 | [diff] [blame] | 3765 | |
| 3766 | @VisibleForTesting |
| 3767 | static int getMaxManagedProfiles() { |
| 3768 | // Allow overriding max managed profiles on debuggable builds for testing |
| 3769 | // of multiple profiles. |
| 3770 | if (!Build.IS_DEBUGGABLE) { |
| 3771 | return MAX_MANAGED_PROFILES; |
| 3772 | } else { |
| 3773 | return SystemProperties.getInt("persist.sys.max_profiles", |
| 3774 | MAX_MANAGED_PROFILES); |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | @VisibleForTesting |
| 3779 | int getFreeProfileBadgeLU(int parentUserId) { |
| 3780 | int maxManagedProfiles = getMaxManagedProfiles(); |
| 3781 | boolean[] usedBadges = new boolean[maxManagedProfiles]; |
| 3782 | final int userSize = mUsers.size(); |
| 3783 | for (int i = 0; i < userSize; i++) { |
| 3784 | UserInfo ui = mUsers.valueAt(i).info; |
| 3785 | // Check which badge indexes are already used by this profile group. |
| 3786 | if (ui.isManagedProfile() |
| 3787 | && ui.profileGroupId == parentUserId |
| 3788 | && !mRemovingUserIds.get(ui.id) |
| 3789 | && ui.profileBadge < maxManagedProfiles) { |
| 3790 | usedBadges[ui.profileBadge] = true; |
| 3791 | } |
| 3792 | } |
| 3793 | for (int i = 0; i < maxManagedProfiles; i++) { |
| 3794 | if (!usedBadges[i]) { |
| 3795 | return i; |
| 3796 | } |
| 3797 | } |
| 3798 | return 0; |
| 3799 | } |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 3800 | |
| 3801 | /** |
| 3802 | * Checks if the given user has a managed profile associated with it. |
| 3803 | * @param userId The parent user |
| 3804 | * @return |
| 3805 | */ |
| 3806 | boolean hasManagedProfile(int userId) { |
| 3807 | synchronized (mUsersLock) { |
| 3808 | UserInfo userInfo = getUserInfoLU(userId); |
| 3809 | final int userSize = mUsers.size(); |
| 3810 | for (int i = 0; i < userSize; i++) { |
| 3811 | UserInfo profile = mUsers.valueAt(i).info; |
| 3812 | if (userId != profile.id && isProfileOf(userInfo, profile)) { |
| 3813 | return true; |
| 3814 | } |
| 3815 | } |
| 3816 | return false; |
| 3817 | } |
| 3818 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 3819 | } |