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