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