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