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