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