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