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