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 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 24 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 25 | import android.app.ActivityManagerNative; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 26 | import android.app.IActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 27 | import android.app.IStopUserCallback; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 33 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 34 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 36 | import android.os.Binder; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 37 | import android.os.Bundle; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 38 | import android.os.Debug; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 39 | import android.os.Environment; |
| 40 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 41 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 42 | import android.os.IUserManager; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 43 | import android.os.Message; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 44 | import android.os.ParcelFileDescriptor; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 45 | import android.os.Parcelable; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 46 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 48 | import android.os.ResultReceiver; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 49 | import android.os.ServiceManager; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 50 | import android.os.ShellCommand; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 51 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 52 | import android.os.UserManager; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 53 | import android.os.UserManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 54 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 55 | import android.os.storage.StorageManager; |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 56 | import android.os.storage.VolumeInfo; |
| 57 | import android.system.ErrnoException; |
| 58 | import android.system.Os; |
| 59 | import android.system.OsConstants; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 60 | import android.util.AtomicFile; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 61 | import android.util.Log; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 62 | import android.util.Pair; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 63 | import android.util.Slog; |
| 64 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 65 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 66 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 67 | import android.util.Xml; |
| 68 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 69 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 70 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 71 | import com.android.internal.app.IAppOpsService; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 72 | import com.android.internal.util.FastXmlSerializer; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 73 | import com.android.internal.util.Preconditions; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 74 | import com.android.internal.util.XmlUtils; |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 75 | import com.android.internal.widget.LockPatternUtils; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 76 | import com.android.server.LocalServices; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 77 | |
| 78 | import org.xmlpull.v1.XmlPullParser; |
| 79 | import org.xmlpull.v1.XmlPullParserException; |
| 80 | import org.xmlpull.v1.XmlSerializer; |
| 81 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 82 | import java.io.BufferedOutputStream; |
| 83 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 84 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 85 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 86 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 87 | import java.io.FileOutputStream; |
| 88 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 89 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 90 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 91 | import java.util.ArrayList; |
| 92 | import java.util.List; |
| 93 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 94 | import libcore.io.IoUtils; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 95 | import libcore.util.Objects; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 96 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Service for {@link UserManager}. |
| 99 | * |
| 100 | * Method naming convention: |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 101 | * <ul> |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 102 | * <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] | 103 | * <li> Methods suffixed with "LR" should be called within the {@link #mRestrictionsLock} lock. |
| 104 | * <li> Methods suffixed with "LU" should be called within the {@link #mUsersLock} lock. |
| 105 | * </ul> |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 106 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 107 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 108 | private static final String LOG_TAG = "UserManagerService"; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 109 | static final boolean DBG = false; // DO NOT SUBMIT WITH TRUE |
| 110 | private static final boolean DBG_WITH_STACKTRACE = false; // DO NOT SUBMIT WITH TRUE |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 111 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 112 | private static final String TAG_NAME = "name"; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 113 | private static final String TAG_ACCOUNT = "account"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 114 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 115 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 116 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 117 | private static final String ATTR_CREATION_TIME = "created"; |
| 118 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 119 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 120 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 121 | private static final String ATTR_PARTIAL = "partial"; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 122 | private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 123 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 124 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 125 | private static final String ATTR_RESTRICTED_PROFILE_PARENT_ID = "restrictedProfileParentId"; |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 126 | private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 127 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 128 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 129 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 130 | private static final String TAG_DEVICE_POLICY_RESTRICTIONS = "device_policy_restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 131 | private static final String TAG_ENTRY = "entry"; |
| 132 | private static final String TAG_VALUE = "value"; |
| 133 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 134 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 135 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 136 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 137 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 138 | private static final String ATTR_TYPE_STRING = "s"; |
| 139 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
Amith Yamasani | 5b5aa40 | 2014-06-01 20:10:14 -0700 | [diff] [blame] | 140 | private static final String ATTR_TYPE_INTEGER = "i"; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 141 | private static final String ATTR_TYPE_BUNDLE = "B"; |
| 142 | private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 143 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 144 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 145 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 146 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 147 | private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 148 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 149 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 150 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 151 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 152 | private static final int MIN_USER_ID = 10; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 153 | // We need to keep process uid within Integer.MAX_VALUE. |
| 154 | private static final int MAX_USER_ID = Integer.MAX_VALUE / UserHandle.PER_USER_RANGE; |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 155 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 156 | private static final int USER_VERSION = 6; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 157 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 158 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 159 | |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 160 | // 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] | 161 | // without first making sure that the rest of the framework is prepared for it. |
| 162 | private static final int MAX_MANAGED_PROFILES = 1; |
| 163 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 164 | static final int WRITE_USER_MSG = 1; |
| 165 | static final int WRITE_USER_DELAY = 2*1000; // 2 seconds |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 166 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 167 | private static final String XATTR_SERIAL = "user.serial"; |
| 168 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 169 | private final Context mContext; |
| 170 | private final PackageManagerService mPm; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 171 | private final Object mPackagesLock; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 172 | // Short-term lock for internal state, when interaction/sync with PM is not required |
| 173 | private final Object mUsersLock = new Object(); |
| 174 | private final Object mRestrictionsLock = new Object(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 175 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 176 | private final Handler mHandler; |
| 177 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 178 | private final File mUsersDir; |
| 179 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 180 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 181 | @GuardedBy("mUsersLock") |
| 182 | private final SparseArray<UserInfo> mUsers = new SparseArray<>(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 185 | * This collection contains each user's account name if the user chose to set one up |
| 186 | * during the initial user creation process. Keeping this information separate from mUsers |
| 187 | * to avoid accidentally leak it. |
| 188 | */ |
| 189 | @GuardedBy("mUsersLock") |
| 190 | private final SparseArray<String> mUserAccounts = new SparseArray<>(); |
| 191 | |
| 192 | /** |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 193 | * User restrictions set via UserManager. This doesn't include restrictions set by |
| 194 | * device owner / profile owners. |
| 195 | * |
| 196 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 197 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 198 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 199 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 200 | * (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] | 201 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 202 | */ |
| 203 | @GuardedBy("mRestrictionsLock") |
| 204 | private final SparseArray<Bundle> mBaseUserRestrictions = new SparseArray<>(); |
| 205 | |
| 206 | /** |
| 207 | * Cached user restrictions that are in effect -- i.e. {@link #mBaseUserRestrictions} combined |
| 208 | * with device / profile owner restrictions. We'll initialize it lazily; use |
| 209 | * {@link #getEffectiveUserRestrictions} to access it. |
| 210 | * |
| 211 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 212 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 213 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 214 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 215 | * (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] | 216 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 217 | */ |
| 218 | @GuardedBy("mRestrictionsLock") |
| 219 | private final SparseArray<Bundle> mCachedEffectiveUserRestrictions = new SparseArray<>(); |
| 220 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 221 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 222 | * User restrictions that have already been applied in |
| 223 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)}. We use it to detect restrictions |
| 224 | * that have changed since the last |
| 225 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)} call. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 226 | */ |
| 227 | @GuardedBy("mRestrictionsLock") |
| 228 | private final SparseArray<Bundle> mAppliedUserRestrictions = new SparseArray<>(); |
| 229 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 230 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 231 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
| 232 | * that should be applied to all users, including guests. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 233 | */ |
| 234 | @GuardedBy("mRestrictionsLock") |
| 235 | private Bundle mDevicePolicyGlobalUserRestrictions; |
| 236 | |
| 237 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 238 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
| 239 | * for each user. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 240 | */ |
| 241 | @GuardedBy("mRestrictionsLock") |
| 242 | private final SparseArray<Bundle> mDevicePolicyLocalUserRestrictions = new SparseArray<>(); |
| 243 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 244 | @GuardedBy("mGuestRestrictions") |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 245 | private final Bundle mGuestRestrictions = new Bundle(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 249 | * for several seconds to work around a VFS caching issue. |
| 250 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 251 | @GuardedBy("mUsersLock") |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 252 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 253 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 254 | @GuardedBy("mUsersLock") |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 255 | private int[] mUserIds; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 256 | @GuardedBy("mPackagesLock") |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 257 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 258 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 259 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 260 | private IAppOpsService mAppOpsService; |
| 261 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 262 | private final LocalService mLocalService; |
| 263 | |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 264 | @GuardedBy("mUsersLock") |
| 265 | private boolean mIsDeviceManaged; |
| 266 | |
| 267 | @GuardedBy("mUsersLock") |
| 268 | private final SparseBooleanArray mIsUserManaged = new SparseBooleanArray(); |
| 269 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 270 | @GuardedBy("mUserRestrictionsListeners") |
| 271 | private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners = |
| 272 | new ArrayList<>(); |
| 273 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 274 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 275 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 276 | public static UserManagerService getInstance() { |
| 277 | synchronized (UserManagerService.class) { |
| 278 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 279 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 280 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 281 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 282 | @VisibleForTesting |
| 283 | UserManagerService(File dataDir) { |
| 284 | this(null, null, new Object(), dataDir); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Called by package manager to create the service. This is closely |
| 289 | * associated with the package manager, and the given lock is the |
| 290 | * package manager's own lock. |
| 291 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 292 | UserManagerService(Context context, PackageManagerService pm, Object packagesLock) { |
| 293 | this(context, pm, packagesLock, Environment.getDataDirectory()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 296 | private UserManagerService(Context context, PackageManagerService pm, |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 297 | Object packagesLock, File dataDir) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 298 | mContext = context; |
| 299 | mPm = pm; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 300 | mPackagesLock = packagesLock; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 301 | mHandler = new MainHandler(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 302 | synchronized (mPackagesLock) { |
| 303 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 304 | mUsersDir.mkdirs(); |
| 305 | // Make zeroth user directory, for services to migrate their files to that location |
| 306 | File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM)); |
| 307 | userZeroDir.mkdirs(); |
| 308 | FileUtils.setPermissions(mUsersDir.toString(), |
| 309 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, |
| 310 | -1, -1); |
| 311 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 312 | initDefaultGuestRestrictions(); |
| 313 | readUserListLP(); |
| 314 | sInstance = this; |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 315 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 316 | mLocalService = new LocalService(); |
| 317 | LocalServices.addService(UserManagerInternal.class, mLocalService); |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void systemReady() { |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 321 | // Prune out any partially created, partially removed and ephemeral users. |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 322 | ArrayList<UserInfo> partials = new ArrayList<>(); |
| 323 | synchronized (mUsersLock) { |
| 324 | final int userSize = mUsers.size(); |
| 325 | for (int i = 0; i < userSize; i++) { |
| 326 | UserInfo ui = mUsers.valueAt(i); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 327 | if ((ui.partial || ui.guestToRemove || ui.isEphemeral()) && i != 0) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 328 | partials.add(ui); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 329 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 330 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 331 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 332 | final int partialsSize = partials.size(); |
| 333 | for (int i = 0; i < partialsSize; i++) { |
| 334 | UserInfo ui = partials.get(i); |
| 335 | Slog.w(LOG_TAG, "Removing partially created user " + ui.id |
| 336 | + " (name=" + ui.name + ")"); |
| 337 | removeUserState(ui.id); |
| 338 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 339 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 340 | onUserForeground(UserHandle.USER_SYSTEM); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 341 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 342 | mAppOpsService = IAppOpsService.Stub.asInterface( |
| 343 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 344 | |
| 345 | synchronized (mRestrictionsLock) { |
| 346 | applyUserRestrictionsLR(UserHandle.USER_SYSTEM); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 347 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 351 | public String getUserAccount(int userId) { |
| 352 | checkManageUserAndAcrossUsersFullPermission("get user account"); |
| 353 | synchronized (mUsersLock) { |
| 354 | return mUserAccounts.get(userId); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void setUserAccount(int userId, String accountName) { |
| 360 | checkManageUserAndAcrossUsersFullPermission("set user account"); |
| 361 | UserInfo userToUpdate = null; |
| 362 | synchronized (mPackagesLock) { |
| 363 | synchronized (mUsersLock) { |
| 364 | String currentAccount = mUserAccounts.get(userId); |
| 365 | if (!Objects.equal(currentAccount, accountName)) { |
| 366 | mUserAccounts.put(userId, accountName); |
| 367 | userToUpdate = mUsers.get(userId); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (userToUpdate != null) { |
| 372 | writeUserLP(userToUpdate); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | @Override |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 378 | public UserInfo getPrimaryUser() { |
| 379 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 380 | synchronized (mUsersLock) { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 381 | final int userSize = mUsers.size(); |
| 382 | for (int i = 0; i < userSize; i++) { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 383 | UserInfo ui = mUsers.valueAt(i); |
Xiaohui Chen | d3e9e18 | 2015-11-18 13:37:32 -0800 | [diff] [blame] | 384 | if (ui.isPrimary() && !mRemovingUserIds.get(ui.id)) { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 385 | return ui; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | return null; |
| 390 | } |
| 391 | |
| 392 | @Override |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 393 | public @NonNull List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 394 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 395 | synchronized (mUsersLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 396 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 397 | final int userSize = mUsers.size(); |
| 398 | for (int i = 0; i < userSize; i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 399 | UserInfo ui = mUsers.valueAt(i); |
| 400 | if (ui.partial) { |
| 401 | continue; |
| 402 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 403 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 404 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 405 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 406 | } |
| 407 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 408 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 411 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 412 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 413 | if (userId != UserHandle.getCallingUserId()) { |
| 414 | checkManageUsersPermission("getting profiles related to user " + userId); |
| 415 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 416 | final long ident = Binder.clearCallingIdentity(); |
| 417 | try { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 418 | synchronized (mUsersLock) { |
| 419 | return getProfilesLU(userId, enabledOnly); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 420 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 421 | } finally { |
| 422 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 426 | /** Assume permissions already checked and caller's identity cleared */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 427 | private List<UserInfo> getProfilesLU(int userId, boolean enabledOnly) { |
| 428 | UserInfo user = getUserInfoLU(userId); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 429 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 430 | if (user == null) { |
| 431 | // Probably a dying user |
| 432 | return users; |
| 433 | } |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 434 | final int userSize = mUsers.size(); |
| 435 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 436 | UserInfo profile = mUsers.valueAt(i); |
| 437 | if (!isProfileOf(user, profile)) { |
| 438 | continue; |
| 439 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 440 | if (enabledOnly && !profile.isEnabled()) { |
| 441 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 442 | } |
Amith Yamasani | 70fcf0c | 2014-07-11 08:40:19 -0700 | [diff] [blame] | 443 | if (mRemovingUserIds.get(profile.id)) { |
| 444 | continue; |
| 445 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 446 | users.add(profile); |
| 447 | } |
| 448 | return users; |
| 449 | } |
| 450 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 451 | @Override |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 452 | public int getCredentialOwnerProfile(int userHandle) { |
| 453 | checkManageUsersPermission("get the credential owner"); |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 454 | if (!LockPatternUtils.isSeparateWorkChallengeEnabled()) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 455 | synchronized (mUsersLock) { |
| 456 | UserInfo profileParent = getProfileParentLU(userHandle); |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 457 | if (profileParent != null) { |
| 458 | return profileParent.id; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return userHandle; |
| 464 | } |
| 465 | |
| 466 | @Override |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 467 | public boolean isSameProfileGroup(int userId, int otherUserId) { |
| 468 | if (userId == otherUserId) return true; |
| 469 | checkManageUsersPermission("check if in the same profile group"); |
| 470 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 471 | return isSameProfileGroupLP(userId, otherUserId); |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 475 | private boolean isSameProfileGroupLP(int userId, int otherUserId) { |
| 476 | synchronized (mUsersLock) { |
| 477 | UserInfo userInfo = getUserInfoLU(userId); |
| 478 | if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 479 | return false; |
| 480 | } |
| 481 | UserInfo otherUserInfo = getUserInfoLU(otherUserId); |
| 482 | if (otherUserInfo == null |
| 483 | || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 484 | return false; |
| 485 | } |
| 486 | return userInfo.profileGroupId == otherUserInfo.profileGroupId; |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 487 | } |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Override |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 491 | public UserInfo getProfileParent(int userHandle) { |
| 492 | checkManageUsersPermission("get the profile parent"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 493 | synchronized (mUsersLock) { |
| 494 | return getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 498 | private UserInfo getProfileParentLU(int userHandle) { |
| 499 | UserInfo profile = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 500 | if (profile == null) { |
| 501 | return null; |
| 502 | } |
| 503 | int parentUserId = profile.profileGroupId; |
| 504 | if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 505 | return null; |
| 506 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 507 | return getUserInfoLU(parentUserId); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 511 | private static boolean isProfileOf(UserInfo user, UserInfo profile) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 512 | return user.id == profile.id || |
| 513 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 514 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 517 | private void broadcastProfileAvailabilityChanges(UserHandle profileHandle, |
| 518 | UserHandle parentHandle, Bundle extras) { |
| 519 | // Send intent to profile |
| 520 | Intent intent = new Intent(Intent.ACTION_AVAILABILITY_CHANGED); |
| 521 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 522 | intent.putExtras(extras); |
| 523 | mContext.sendBroadcastAsUser(intent, profileHandle); |
| 524 | |
| 525 | // Send intent to parent |
| 526 | if (parentHandle != null) { |
| 527 | intent = new Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED); |
| 528 | intent.putExtra(Intent.EXTRA_USER, profileHandle); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame^] | 529 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileHandle.getIdentifier()); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 530 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 531 | intent.putExtras(extras); |
| 532 | mContext.sendBroadcastAsUser(intent, parentHandle); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public void setQuietModeEnabled(int userHandle, boolean enableQuietMode) { |
| 538 | checkManageUsersPermission("silence profile"); |
| 539 | boolean changed = false; |
| 540 | UserInfo profile, parent; |
| 541 | synchronized (mPackagesLock) { |
| 542 | synchronized (mUsersLock) { |
| 543 | profile = getUserInfoLU(userHandle); |
| 544 | parent = getProfileParentLU(userHandle); |
| 545 | |
| 546 | } |
| 547 | if (profile == null || !profile.isManagedProfile()) { |
| 548 | throw new IllegalArgumentException("User " + userHandle + " is not a profile"); |
| 549 | } |
| 550 | if (profile.isQuietModeEnabled() != enableQuietMode) { |
| 551 | profile.flags ^= UserInfo.FLAG_QUIET_MODE; |
| 552 | writeUserLP(profile); |
| 553 | changed = true; |
| 554 | } |
| 555 | } |
| 556 | if (changed) { |
| 557 | Bundle extras = new Bundle(); |
| 558 | extras.putBoolean(Intent.EXTRA_QUIET_MODE, enableQuietMode); |
| 559 | broadcastProfileAvailabilityChanges(profile.getUserHandle(), |
| 560 | parent != null ? parent.getUserHandle() : null, extras); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public boolean isQuietModeEnabled(int userHandle) { |
| 566 | synchronized (mPackagesLock) { |
| 567 | UserInfo info; |
| 568 | synchronized (mUsersLock) { |
| 569 | info = getUserInfoLU(userHandle); |
| 570 | } |
| 571 | if (info == null || !info.isManagedProfile()) { |
| 572 | throw new IllegalArgumentException("User " + userHandle + " is not a profile"); |
| 573 | } |
| 574 | return info.isQuietModeEnabled(); |
| 575 | } |
| 576 | } |
| 577 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 578 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 579 | public void setUserEnabled(int userId) { |
| 580 | checkManageUsersPermission("enable user"); |
| 581 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 582 | UserInfo info; |
| 583 | synchronized (mUsersLock) { |
| 584 | info = getUserInfoLU(userId); |
| 585 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 586 | if (info != null && !info.isEnabled()) { |
| 587 | info.flags ^= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 588 | writeUserLP(info); |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 594 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 595 | checkManageUsersPermission("query user"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 596 | synchronized (mUsersLock) { |
| 597 | return getUserInfoLU(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 601 | @Override |
| 602 | public boolean isRestricted() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 603 | synchronized (mUsersLock) { |
| 604 | return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 608 | @Override |
| 609 | public boolean canHaveRestrictedProfile(int userId) { |
| 610 | checkManageUsersPermission("canHaveRestrictedProfile"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 611 | synchronized (mUsersLock) { |
| 612 | final UserInfo userInfo = getUserInfoLU(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 613 | if (userInfo == null || !userInfo.canHaveProfile()) { |
| 614 | return false; |
| 615 | } |
| 616 | if (!userInfo.isAdmin()) { |
| 617 | return false; |
| 618 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 619 | // restricted profile can be created if there is no DO set and the admin user has no PO; |
| 620 | return !mIsDeviceManaged && !mIsUserManaged.get(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 621 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 624 | /* |
| 625 | * Should be locked on mUsers before calling this. |
| 626 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 627 | private UserInfo getUserInfoLU(int userId) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 628 | UserInfo ui = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 629 | // If it is partial and not in the process of being removed, return as unknown user. |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 630 | if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 631 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 632 | return null; |
| 633 | } |
| 634 | return ui; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 637 | /** |
| 638 | * Obtains {@link #mUsersLock} and return UserInfo from mUsers. |
| 639 | * <p>No permissions checking or any addition checks are made</p> |
| 640 | */ |
| 641 | private UserInfo getUserInfoNoChecks(int userId) { |
| 642 | synchronized (mUsersLock) { |
| 643 | return mUsers.get(userId); |
| 644 | } |
| 645 | } |
| 646 | |
Amith Yamasani | 236b2b5 | 2015-08-18 14:32:14 -0700 | [diff] [blame] | 647 | /** Called by PackageManagerService */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 648 | public boolean exists(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 649 | return getUserInfoNoChecks(userId) != null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 652 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 653 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 654 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 655 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 656 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 657 | UserInfo info = getUserInfoNoChecks(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 658 | if (info == null || info.partial) { |
| 659 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 660 | return; |
| 661 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 662 | if (name != null && !name.equals(info.name)) { |
| 663 | info.name = name; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 664 | writeUserLP(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 665 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 668 | if (changed) { |
| 669 | sendUserInfoChangedBroadcast(userId); |
| 670 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 673 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 674 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 675 | checkManageUsersPermission("update users"); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 676 | long ident = Binder.clearCallingIdentity(); |
| 677 | try { |
| 678 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 679 | UserInfo info = getUserInfoNoChecks(userId); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 680 | if (info == null || info.partial) { |
| 681 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 682 | return; |
| 683 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 684 | writeBitmapLP(info, bitmap); |
| 685 | writeUserLP(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 686 | } |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 687 | sendUserInfoChangedBroadcast(userId); |
| 688 | } finally { |
| 689 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 690 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | private void sendUserInfoChangedBroadcast(int userId) { |
| 694 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 695 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 696 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 697 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 700 | @Override |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 701 | public ParcelFileDescriptor getUserIcon(int userId) { |
| 702 | String iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 703 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 704 | UserInfo info = getUserInfoNoChecks(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 705 | if (info == null || info.partial) { |
| 706 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 707 | return null; |
| 708 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 709 | int callingGroupId = getUserInfoNoChecks(UserHandle.getCallingUserId()).profileGroupId; |
Nicolas Prevot | 88cc346 | 2014-05-14 14:51:48 +0100 | [diff] [blame] | 710 | if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 711 | || callingGroupId != info.profileGroupId) { |
| 712 | checkManageUsersPermission("get the icon of a user who is not related"); |
| 713 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 714 | if (info.iconPath == null) { |
| 715 | return null; |
| 716 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 717 | iconPath = info.iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 718 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 719 | |
| 720 | try { |
| 721 | return ParcelFileDescriptor.open( |
| 722 | new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY); |
| 723 | } catch (FileNotFoundException e) { |
| 724 | Log.e(LOG_TAG, "Couldn't find icon file", e); |
| 725 | } |
| 726 | return null; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 729 | public void makeInitialized(int userId) { |
| 730 | checkManageUsersPermission("makeInitialized"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 731 | boolean scheduleWriteUser = false; |
| 732 | UserInfo info; |
| 733 | synchronized (mUsersLock) { |
| 734 | info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 735 | if (info == null || info.partial) { |
| 736 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 737 | return; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 738 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 739 | if ((info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 740 | info.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 741 | scheduleWriteUser = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 744 | if (scheduleWriteUser) { |
| 745 | scheduleWriteUser(info); |
| 746 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 749 | /** |
| 750 | * If default guest restrictions haven't been initialized yet, add the basic |
| 751 | * restrictions. |
| 752 | */ |
| 753 | private void initDefaultGuestRestrictions() { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 754 | synchronized (mGuestRestrictions) { |
| 755 | if (mGuestRestrictions.isEmpty()) { |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 756 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 757 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
| 758 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
| 759 | } |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 763 | @Override |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 764 | public Bundle getDefaultGuestRestrictions() { |
| 765 | checkManageUsersPermission("getDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 766 | synchronized (mGuestRestrictions) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 767 | return new Bundle(mGuestRestrictions); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 773 | checkManageUsersPermission("setDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 774 | synchronized (mGuestRestrictions) { |
| 775 | mGuestRestrictions.clear(); |
| 776 | mGuestRestrictions.putAll(restrictions); |
| 777 | } |
| 778 | synchronized (mPackagesLock) { |
| 779 | writeUserListLP(); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 783 | /** |
| 784 | * See {@link UserManagerInternal#setDevicePolicyUserRestrictions(int, Bundle, Bundle)} |
| 785 | */ |
| 786 | void setDevicePolicyUserRestrictions(int userId, @NonNull Bundle local, |
| 787 | @Nullable Bundle global) { |
| 788 | Preconditions.checkNotNull(local); |
| 789 | boolean globalChanged = false; |
| 790 | boolean localChanged; |
| 791 | synchronized (mRestrictionsLock) { |
| 792 | if (global != null) { |
| 793 | // Update global. |
| 794 | globalChanged = !UserRestrictionsUtils.areEqual( |
| 795 | mDevicePolicyGlobalUserRestrictions, global); |
| 796 | if (globalChanged) { |
| 797 | mDevicePolicyGlobalUserRestrictions = global; |
| 798 | } |
| 799 | } |
| 800 | { |
| 801 | // Update local. |
| 802 | final Bundle prev = mDevicePolicyLocalUserRestrictions.get(userId); |
| 803 | localChanged = !UserRestrictionsUtils.areEqual(prev, local); |
| 804 | if (localChanged) { |
| 805 | mDevicePolicyLocalUserRestrictions.put(userId, local); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | if (DBG) { |
| 810 | Log.d(LOG_TAG, "setDevicePolicyUserRestrictions: userId=" + userId |
| 811 | + " global=" + global + (globalChanged ? " (changed)" : "") |
| 812 | + " local=" + local + (localChanged ? " (changed)" : "") |
| 813 | ); |
| 814 | } |
| 815 | // Don't call them within the mRestrictionsLock. |
| 816 | synchronized (mPackagesLock) { |
| 817 | if (globalChanged) { |
| 818 | writeUserListLP(); |
| 819 | } |
| 820 | if (localChanged) { |
| 821 | writeUserLP(getUserInfoNoChecks(userId)); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | synchronized (mRestrictionsLock) { |
| 826 | if (globalChanged) { |
| 827 | applyUserRestrictionsForAllUsersLR(); |
| 828 | } else if (localChanged) { |
| 829 | applyUserRestrictionsLR(userId); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 834 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 835 | private Bundle computeEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 836 | final Bundle baseRestrictions = |
| 837 | UserRestrictionsUtils.nonNull(mBaseUserRestrictions.get(userId)); |
| 838 | final Bundle global = mDevicePolicyGlobalUserRestrictions; |
| 839 | final Bundle local = mDevicePolicyLocalUserRestrictions.get(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 840 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 841 | if (UserRestrictionsUtils.isEmpty(global) && UserRestrictionsUtils.isEmpty(local)) { |
| 842 | // Common case first. |
| 843 | return baseRestrictions; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 844 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 845 | final Bundle effective = UserRestrictionsUtils.clone(baseRestrictions); |
| 846 | UserRestrictionsUtils.merge(effective, global); |
| 847 | UserRestrictionsUtils.merge(effective, local); |
| 848 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 849 | return effective; |
| 850 | } |
| 851 | |
| 852 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 853 | private void invalidateEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 854 | if (DBG) { |
| 855 | Log.d(LOG_TAG, "invalidateEffectiveUserRestrictions userId=" + userId); |
| 856 | } |
| 857 | mCachedEffectiveUserRestrictions.remove(userId); |
| 858 | } |
| 859 | |
| 860 | private Bundle getEffectiveUserRestrictions(int userId) { |
| 861 | synchronized (mRestrictionsLock) { |
| 862 | Bundle restrictions = mCachedEffectiveUserRestrictions.get(userId); |
| 863 | if (restrictions == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 864 | restrictions = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 865 | mCachedEffectiveUserRestrictions.put(userId, restrictions); |
| 866 | } |
| 867 | return restrictions; |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 871 | /** @return a specific user restriction that's in effect currently. */ |
| 872 | @Override |
| 873 | public boolean hasUserRestriction(String restrictionKey, int userId) { |
| 874 | Bundle restrictions = getEffectiveUserRestrictions(userId); |
| 875 | return restrictions != null && restrictions.getBoolean(restrictionKey); |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * @return UserRestrictions that are in effect currently. This always returns a new |
| 880 | * {@link Bundle}. |
| 881 | */ |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 882 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 883 | public Bundle getUserRestrictions(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 884 | return UserRestrictionsUtils.clone(getEffectiveUserRestrictions(userId)); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | @Override |
Zoltan Szatmary-Ban | 3bbcedd | 2015-11-26 13:45:51 +0000 | [diff] [blame] | 888 | public boolean hasBaseUserRestriction(String restrictionKey, int userId) { |
| 889 | checkManageUsersPermission("hasBaseUserRestriction"); |
| 890 | synchronized (mRestrictionsLock) { |
| 891 | Bundle bundle = mBaseUserRestrictions.get(userId); |
| 892 | return (bundle != null && bundle.getBoolean(restrictionKey, false)); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | @Override |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 897 | public void setUserRestriction(String key, boolean value, int userId) { |
Fyodor Kupolov | d4b2604 | 2015-07-27 14:30:59 -0700 | [diff] [blame] | 898 | checkManageUsersPermission("setUserRestriction"); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 899 | synchronized (mRestrictionsLock) { |
| 900 | // Note we can't modify Bundles stored in mBaseUserRestrictions directly, so create |
| 901 | // a copy. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 902 | final Bundle newRestrictions = UserRestrictionsUtils.clone( |
| 903 | mBaseUserRestrictions.get(userId)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 904 | newRestrictions.putBoolean(key, value); |
| 905 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 906 | updateUserRestrictionsInternalLR(newRestrictions, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 910 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 911 | * Optionally updating user restrictions, calculate the effective user restrictions and also |
| 912 | * propagate to other services and system settings. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 913 | * |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 914 | * @param newRestrictions User restrictions to set. |
| 915 | * If null, will not update user restrictions and only does the propagation. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 916 | * @param userId target user ID. |
| 917 | */ |
| 918 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 919 | private void updateUserRestrictionsInternalLR( |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 920 | @Nullable Bundle newRestrictions, int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 921 | |
| 922 | final Bundle prevAppliedRestrictions = UserRestrictionsUtils.nonNull( |
| 923 | mAppliedUserRestrictions.get(userId)); |
| 924 | |
| 925 | // Update base restrictions. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 926 | if (newRestrictions != null) { |
| 927 | // If newRestrictions == the current one, it's probably a bug. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 928 | final Bundle prevBaseRestrictions = mBaseUserRestrictions.get(userId); |
| 929 | |
| 930 | Preconditions.checkState(prevBaseRestrictions != newRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 931 | Preconditions.checkState(mCachedEffectiveUserRestrictions.get(userId) |
| 932 | != newRestrictions); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 933 | |
| 934 | if (!UserRestrictionsUtils.areEqual(prevBaseRestrictions, newRestrictions)) { |
| 935 | mBaseUserRestrictions.put(userId, newRestrictions); |
| 936 | scheduleWriteUser(getUserInfoNoChecks(userId)); |
| 937 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 940 | final Bundle effective = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 941 | |
Makoto Onuki | 759a763 | 2015-10-28 16:43:10 -0700 | [diff] [blame] | 942 | mCachedEffectiveUserRestrictions.put(userId, effective); |
| 943 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 944 | // Apply the new restrictions. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 945 | if (DBG) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 946 | debug("Applying user restrictions: userId=" + userId |
| 947 | + " new=" + effective + " prev=" + prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 950 | if (mAppOpsService != null) { // We skip it until system-ready. |
| 951 | final long token = Binder.clearCallingIdentity(); |
| 952 | try { |
| 953 | mAppOpsService.setUserRestrictions(effective, userId); |
| 954 | } catch (RemoteException e) { |
| 955 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 956 | } finally { |
| 957 | Binder.restoreCallingIdentity(token); |
| 958 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 959 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 960 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 961 | propagateUserRestrictionsLR(userId, effective, prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 962 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 963 | mAppliedUserRestrictions.put(userId, new Bundle(effective)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 966 | private void propagateUserRestrictionsLR(final int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 967 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 968 | // Note this method doesn't touch any state, meaning it doesn't require mRestrictionsLock |
| 969 | // actually, but we still need some kind of synchronization otherwise we might end up |
| 970 | // calling listeners out-of-order, thus "LR". |
| 971 | |
| 972 | if (UserRestrictionsUtils.areEqual(newRestrictions, prevRestrictions)) { |
| 973 | return; |
| 974 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 975 | |
| 976 | final Bundle newRestrictionsFinal = new Bundle(newRestrictions); |
| 977 | final Bundle prevRestrictionsFinal = new Bundle(prevRestrictions); |
| 978 | |
| 979 | mHandler.post(new Runnable() { |
| 980 | @Override |
| 981 | public void run() { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 982 | UserRestrictionsUtils.applyUserRestrictions( |
| 983 | mContext, userId, newRestrictionsFinal, prevRestrictionsFinal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 984 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 985 | final UserRestrictionsListener[] listeners; |
| 986 | synchronized (mUserRestrictionsListeners) { |
| 987 | listeners = new UserRestrictionsListener[mUserRestrictionsListeners.size()]; |
| 988 | mUserRestrictionsListeners.toArray(listeners); |
| 989 | } |
| 990 | for (int i = 0; i < listeners.length; i++) { |
| 991 | listeners[i].onUserRestrictionsChanged(userId, |
| 992 | newRestrictionsFinal, prevRestrictionsFinal); |
| 993 | } |
| 994 | } |
| 995 | }); |
| 996 | } |
| 997 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 998 | // Package private for the inner class. |
| 999 | void applyUserRestrictionsLR(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1000 | updateUserRestrictionsInternalLR(null, userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @GuardedBy("mRestrictionsLock") |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1004 | // Package private for the inner class. |
| 1005 | void applyUserRestrictionsForAllUsersLR() { |
| 1006 | if (DBG) { |
| 1007 | debug("applyUserRestrictionsForAllUsersLR"); |
| 1008 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1009 | // First, invalidate all cached values. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1010 | mCachedEffectiveUserRestrictions.clear(); |
| 1011 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1012 | // We don't want to call into ActivityManagerNative while taking a lock, so we'll call |
| 1013 | // it on a handler. |
| 1014 | final Runnable r = new Runnable() { |
| 1015 | @Override |
| 1016 | public void run() { |
| 1017 | // Then get the list of running users. |
| 1018 | final int[] runningUsers; |
| 1019 | try { |
| 1020 | runningUsers = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 1021 | } catch (RemoteException e) { |
| 1022 | Log.w(LOG_TAG, "Unable to access ActivityManagerNative"); |
| 1023 | return; |
| 1024 | } |
| 1025 | // Then re-calculate the effective restrictions and apply, only for running users. |
| 1026 | // It's okay if a new user has started after the getRunningUserIds() call, |
| 1027 | // because we'll do the same thing (re-calculate the restrictions and apply) |
| 1028 | // when we start a user. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1029 | synchronized (mRestrictionsLock) { |
| 1030 | for (int i = 0; i < runningUsers.length; i++) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1031 | applyUserRestrictionsLR(runningUsers[i]); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | }; |
| 1036 | mHandler.post(r); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1039 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1040 | * Check if we've hit the limit of how many users can be created. |
| 1041 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1042 | private boolean isUserLimitReached() { |
| 1043 | int count; |
| 1044 | synchronized (mUsersLock) { |
| 1045 | count = getAliveUsersExcludingGuestsCountLU(); |
| 1046 | } |
| 1047 | return count >= UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | @Override |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1051 | public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1052 | checkManageUsersPermission("check if more managed profiles can be added."); |
| 1053 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1054 | return false; |
| 1055 | } |
Fyodor Kupolov | b615799 | 2015-06-05 15:32:28 -0700 | [diff] [blame] | 1056 | if (!mContext.getPackageManager().hasSystemFeature( |
| 1057 | PackageManager.FEATURE_MANAGED_USERS)) { |
| 1058 | return false; |
| 1059 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1060 | // Limit number of managed profiles that can be created |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1061 | final int managedProfilesCount = getProfiles(userId, true).size() - 1; |
| 1062 | final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0; |
| 1063 | if (managedProfilesCount - profilesRemovedCount >= MAX_MANAGED_PROFILES) { |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1066 | synchronized(mUsersLock) { |
| 1067 | UserInfo userInfo = getUserInfoLU(userId); |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1068 | if (!userInfo.canHaveProfile()) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1071 | int usersCountAfterRemoving = getAliveUsersExcludingGuestsCountLU() |
| 1072 | - profilesRemovedCount; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1073 | // 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] | 1074 | return usersCountAfterRemoving == 1 |
| 1075 | || usersCountAfterRemoving < UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1079 | private int getAliveUsersExcludingGuestsCountLU() { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1080 | int aliveUserCount = 0; |
| 1081 | final int totalUserCount = mUsers.size(); |
| 1082 | // Skip over users being removed |
| 1083 | for (int i = 0; i < totalUserCount; i++) { |
| 1084 | UserInfo user = mUsers.valueAt(i); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1085 | if (!mRemovingUserIds.get(user.id) |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1086 | && !user.isGuest() && !user.partial) { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1087 | aliveUserCount++; |
| 1088 | } |
| 1089 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1090 | return aliveUserCount; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1094 | * 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] | 1095 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} and |
| 1096 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL INTERACT_ACROSS_USERS_FULL} |
| 1097 | * permissions can make certain calls to the UserManager. |
| 1098 | * |
| 1099 | * @param message used as message if SecurityException is thrown |
| 1100 | * @throws SecurityException if the caller does not have enough privilege. |
| 1101 | */ |
| 1102 | private static final void checkManageUserAndAcrossUsersFullPermission(String message) { |
| 1103 | final int uid = Binder.getCallingUid(); |
| 1104 | if (uid != Process.SYSTEM_UID && uid != 0 |
| 1105 | && ActivityManager.checkComponentPermission( |
| 1106 | Manifest.permission.MANAGE_USERS, |
| 1107 | uid, -1, true) != PackageManager.PERMISSION_GRANTED |
| 1108 | && ActivityManager.checkComponentPermission( |
| 1109 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1110 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1111 | throw new SecurityException( |
| 1112 | "You need MANAGE_USERS and INTERACT_ACROSS_USERS_FULL permission to: " |
| 1113 | + message); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * 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] | 1119 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1120 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1121 | * |
| 1122 | * @param message used as message if SecurityException is thrown |
| 1123 | * @throws SecurityException if the caller is not system or root |
| 1124 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1125 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1126 | final int uid = Binder.getCallingUid(); |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1127 | if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 1128 | && ActivityManager.checkComponentPermission( |
| 1129 | android.Manifest.permission.MANAGE_USERS, |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1130 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1131 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 1132 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1135 | /** |
| 1136 | * Enforces that only the system UID or root's UID (on any user) can make certain calls to the |
| 1137 | * UserManager. |
| 1138 | * |
| 1139 | * @param message used as message if SecurityException is thrown |
| 1140 | * @throws SecurityException if the caller is not system or root |
| 1141 | */ |
| 1142 | private static void checkSystemOrRoot(String message) { |
| 1143 | final int uid = Binder.getCallingUid(); |
| 1144 | if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID) { |
| 1145 | throw new SecurityException("Only system may: " + message); |
| 1146 | } |
| 1147 | } |
| 1148 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1149 | private void writeBitmapLP(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1150 | try { |
| 1151 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 1152 | File file = new File(dir, USER_PHOTO_FILENAME); |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1153 | File tmp = new File(dir, USER_PHOTO_FILENAME_TMP); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1154 | if (!dir.exists()) { |
| 1155 | dir.mkdir(); |
| 1156 | FileUtils.setPermissions( |
| 1157 | dir.getPath(), |
| 1158 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1159 | -1, -1); |
| 1160 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1161 | FileOutputStream os; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1162 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp)) |
| 1163 | && tmp.renameTo(file)) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1164 | info.iconPath = file.getAbsolutePath(); |
| 1165 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1166 | try { |
| 1167 | os.close(); |
| 1168 | } catch (IOException ioe) { |
| 1169 | // What the ... ! |
| 1170 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1171 | tmp.delete(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1172 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1173 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1174 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1177 | /** |
| 1178 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 1179 | * cache it elsewhere. |
| 1180 | * @return the array of user ids. |
| 1181 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 1182 | public int[] getUserIds() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1183 | synchronized (mUsersLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1184 | return mUserIds; |
| 1185 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1188 | private void readUserListLP() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1189 | if (!mUserListFile.exists()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1190 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1191 | return; |
| 1192 | } |
| 1193 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1194 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1195 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1196 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1197 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1198 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1199 | int type; |
| 1200 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1201 | && type != XmlPullParser.END_DOCUMENT) { |
| 1202 | ; |
| 1203 | } |
| 1204 | |
| 1205 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1206 | Slog.e(LOG_TAG, "Unable to read user list"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1207 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1208 | return; |
| 1209 | } |
| 1210 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1211 | mNextSerialNumber = -1; |
| 1212 | if (parser.getName().equals(TAG_USERS)) { |
| 1213 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 1214 | if (lastSerialNumber != null) { |
| 1215 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 1216 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1217 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 1218 | if (versionNumber != null) { |
| 1219 | mUserVersion = Integer.parseInt(versionNumber); |
| 1220 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1223 | final Bundle newDevicePolicyGlobalUserRestrictions = new Bundle(); |
| 1224 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1225 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1226 | if (type == XmlPullParser.START_TAG) { |
| 1227 | final String name = parser.getName(); |
| 1228 | if (name.equals(TAG_USER)) { |
| 1229 | String id = parser.getAttributeValue(null, ATTR_ID); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1230 | Pair<UserInfo, String> userPair = readUserLP(Integer.parseInt(id)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1231 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1232 | if (userPair != null) { |
| 1233 | UserInfo user = userPair.first; |
| 1234 | String account = userPair.second; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1235 | synchronized (mUsersLock) { |
| 1236 | mUsers.put(user.id, user); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1237 | mUserAccounts.put(user.id, account); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1238 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 1239 | mNextSerialNumber = user.id + 1; |
| 1240 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1241 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1242 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1243 | } else if (name.equals(TAG_GUEST_RESTRICTIONS)) { |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1244 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1245 | && type != XmlPullParser.END_TAG) { |
| 1246 | if (type == XmlPullParser.START_TAG) { |
| 1247 | if (parser.getName().equals(TAG_RESTRICTIONS)) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1248 | synchronized (mGuestRestrictions) { |
| 1249 | UserRestrictionsUtils |
| 1250 | .readRestrictions(parser, mGuestRestrictions); |
| 1251 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1252 | } else if (parser.getName().equals(TAG_DEVICE_POLICY_RESTRICTIONS) |
| 1253 | ) { |
| 1254 | UserRestrictionsUtils.readRestrictions(parser, |
| 1255 | newDevicePolicyGlobalUserRestrictions); |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1256 | } |
| 1257 | break; |
| 1258 | } |
| 1259 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1260 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1263 | synchronized (mRestrictionsLock) { |
| 1264 | mDevicePolicyGlobalUserRestrictions = newDevicePolicyGlobalUserRestrictions; |
| 1265 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1266 | updateUserIds(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1267 | upgradeIfNecessaryLP(); |
| 1268 | } catch (IOException | XmlPullParserException e) { |
| 1269 | fallbackToSingleUserLP(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1270 | } finally { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1271 | IoUtils.closeQuietly(fis); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1275 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1276 | * Upgrade steps between versions, either for fixing bugs or changing the data format. |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1277 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1278 | private void upgradeIfNecessaryLP() { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1279 | final int originalVersion = mUserVersion; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1280 | int userVersion = mUserVersion; |
| 1281 | if (userVersion < 1) { |
| 1282 | // Assign a proper name for the owner, if not initialized correctly before |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1283 | UserInfo user = getUserInfoNoChecks(UserHandle.USER_SYSTEM); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1284 | if ("Primary".equals(user.name)) { |
| 1285 | user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name); |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1286 | scheduleWriteUser(user); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1287 | } |
| 1288 | userVersion = 1; |
| 1289 | } |
| 1290 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1291 | if (userVersion < 2) { |
| 1292 | // Owner should be marked as initialized |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1293 | UserInfo user = getUserInfoNoChecks(UserHandle.USER_SYSTEM); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1294 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 1295 | user.flags |= UserInfo.FLAG_INITIALIZED; |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1296 | scheduleWriteUser(user); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1297 | } |
| 1298 | userVersion = 2; |
| 1299 | } |
| 1300 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1301 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1302 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1303 | userVersion = 4; |
| 1304 | } |
| 1305 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1306 | if (userVersion < 5) { |
| 1307 | initDefaultGuestRestrictions(); |
| 1308 | userVersion = 5; |
| 1309 | } |
| 1310 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1311 | if (userVersion < 6) { |
| 1312 | final boolean splitSystemUser = UserManager.isSplitSystemUser(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1313 | synchronized (mUsersLock) { |
| 1314 | for (int i = 0; i < mUsers.size(); i++) { |
| 1315 | UserInfo user = mUsers.valueAt(i); |
| 1316 | // In non-split mode, only user 0 can have restricted profiles |
| 1317 | if (!splitSystemUser && user.isRestricted() |
| 1318 | && (user.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID)) { |
| 1319 | user.restrictedProfileParentId = UserHandle.USER_SYSTEM; |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1320 | scheduleWriteUser(user); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1321 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | userVersion = 6; |
| 1325 | } |
| 1326 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1327 | if (userVersion < USER_VERSION) { |
| 1328 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 1329 | + USER_VERSION); |
| 1330 | } else { |
| 1331 | mUserVersion = userVersion; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1332 | |
| 1333 | if (originalVersion < mUserVersion) { |
| 1334 | writeUserListLP(); |
| 1335 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1339 | private void fallbackToSingleUserLP() { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1340 | int flags = UserInfo.FLAG_INITIALIZED; |
| 1341 | // In split system user mode, the admin and primary flags are assigned to the first human |
| 1342 | // user. |
| 1343 | if (!UserManager.isSplitSystemUser()) { |
| 1344 | flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY; |
| 1345 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1346 | // Create the system user |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1347 | UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1348 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1349 | flags); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1350 | synchronized (mUsersLock) { |
| 1351 | mUsers.put(system.id, system); |
| 1352 | } |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1353 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 1354 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1355 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1356 | Bundle restrictions = new Bundle(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1357 | synchronized (mRestrictionsLock) { |
| 1358 | mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions); |
| 1359 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1360 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1361 | updateUserIds(); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1362 | initDefaultGuestRestrictions(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1363 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1364 | writeUserListLP(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1365 | writeUserLP(system); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1368 | private void scheduleWriteUser(UserInfo userInfo) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1369 | if (DBG) { |
| 1370 | debug("scheduleWriteUser"); |
| 1371 | } |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1372 | // No need to wrap it within a lock -- worst case, we'll just post the same message |
| 1373 | // twice. |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1374 | if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) { |
| 1375 | Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo); |
| 1376 | mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1380 | /* |
| 1381 | * Writes the user file in this format: |
| 1382 | * |
| 1383 | * <user flags="20039023" id="0"> |
| 1384 | * <name>Primary</name> |
| 1385 | * </user> |
| 1386 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1387 | private void writeUserLP(UserInfo userInfo) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1388 | if (DBG) { |
| 1389 | debug("writeUserLP " + userInfo); |
| 1390 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1391 | FileOutputStream fos = null; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1392 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1393 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1394 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1395 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1396 | |
| 1397 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1398 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1399 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1400 | serializer.startDocument(null, true); |
| 1401 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1402 | |
| 1403 | serializer.startTag(null, TAG_USER); |
| 1404 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1405 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1406 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1407 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 1408 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 1409 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1410 | if (userInfo.iconPath != null) { |
| 1411 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 1412 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1413 | if (userInfo.partial) { |
| 1414 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 1415 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1416 | if (userInfo.guestToRemove) { |
| 1417 | serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true"); |
| 1418 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1419 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1420 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 1421 | Integer.toString(userInfo.profileGroupId)); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1422 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1423 | if (userInfo.restrictedProfileParentId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1424 | serializer.attribute(null, ATTR_RESTRICTED_PROFILE_PARENT_ID, |
| 1425 | Integer.toString(userInfo.restrictedProfileParentId)); |
| 1426 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1427 | serializer.startTag(null, TAG_NAME); |
| 1428 | serializer.text(userInfo.name); |
| 1429 | serializer.endTag(null, TAG_NAME); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1430 | synchronized (mRestrictionsLock) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1431 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1432 | mBaseUserRestrictions.get(userInfo.id), TAG_RESTRICTIONS); |
| 1433 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1434 | mDevicePolicyLocalUserRestrictions.get(userInfo.id), |
| 1435 | TAG_DEVICE_POLICY_RESTRICTIONS); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1436 | } |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1437 | // Update the account field if it is set. |
| 1438 | String account; |
| 1439 | synchronized (mUsersLock) { |
| 1440 | account = mUserAccounts.get(userInfo.id); |
| 1441 | } |
| 1442 | if (account != null) { |
| 1443 | serializer.startTag(null, TAG_ACCOUNT); |
| 1444 | serializer.text(account); |
| 1445 | serializer.endTag(null, TAG_ACCOUNT); |
| 1446 | } |
| 1447 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1448 | serializer.endTag(null, TAG_USER); |
| 1449 | |
| 1450 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1451 | userFile.finishWrite(fos); |
| 1452 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1453 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1454 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Writes the user list file in this format: |
| 1460 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1461 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1462 | * <user id="0"></user> |
| 1463 | * <user id="2"></user> |
| 1464 | * </users> |
| 1465 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1466 | private void writeUserListLP() { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1467 | if (DBG) { |
| 1468 | debug("writeUserList"); |
| 1469 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1470 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1471 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1472 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1473 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1474 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1475 | |
| 1476 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1477 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1478 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1479 | serializer.startDocument(null, true); |
| 1480 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1481 | |
| 1482 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1483 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1484 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1485 | |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1486 | serializer.startTag(null, TAG_GUEST_RESTRICTIONS); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1487 | synchronized (mGuestRestrictions) { |
| 1488 | UserRestrictionsUtils |
| 1489 | .writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS); |
| 1490 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1491 | serializer.endTag(null, TAG_GUEST_RESTRICTIONS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1492 | synchronized (mRestrictionsLock) { |
| 1493 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1494 | mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS); |
| 1495 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1496 | int[] userIdsToWrite; |
| 1497 | synchronized (mUsersLock) { |
| 1498 | userIdsToWrite = new int[mUsers.size()]; |
| 1499 | for (int i = 0; i < userIdsToWrite.length; i++) { |
| 1500 | UserInfo user = mUsers.valueAt(i); |
| 1501 | userIdsToWrite[i] = user.id; |
| 1502 | } |
| 1503 | } |
| 1504 | for (int id : userIdsToWrite) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1505 | serializer.startTag(null, TAG_USER); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1506 | serializer.attribute(null, ATTR_ID, Integer.toString(id)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1507 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | serializer.endTag(null, TAG_USERS); |
| 1511 | |
| 1512 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1513 | userListFile.finishWrite(fos); |
| 1514 | } catch (Exception e) { |
| 1515 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1516 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1520 | private Pair<UserInfo, String> readUserLP(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1521 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1522 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1523 | String name = null; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1524 | String account = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1525 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1526 | long creationTime = 0L; |
| 1527 | long lastLoggedInTime = 0L; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1528 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1529 | int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1530 | boolean partial = false; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1531 | boolean guestToRemove = false; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1532 | Bundle baseRestrictions = new Bundle(); |
| 1533 | Bundle localRestrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1534 | |
| 1535 | FileInputStream fis = null; |
| 1536 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1537 | AtomicFile userFile = |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1538 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1539 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1540 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1541 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1542 | int type; |
| 1543 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1544 | && type != XmlPullParser.END_DOCUMENT) { |
| 1545 | ; |
| 1546 | } |
| 1547 | |
| 1548 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1549 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1550 | return null; |
| 1551 | } |
| 1552 | |
| 1553 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1554 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 1555 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1556 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1557 | return null; |
| 1558 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1559 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 1560 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1561 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1562 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 1563 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1564 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 1565 | UserInfo.NO_PROFILE_GROUP_ID); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1566 | restrictedProfileParentId = readIntAttribute(parser, |
| 1567 | ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1568 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 1569 | if ("true".equals(valueString)) { |
| 1570 | partial = true; |
| 1571 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1572 | valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE); |
| 1573 | if ("true".equals(valueString)) { |
| 1574 | guestToRemove = true; |
| 1575 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1576 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1577 | int outerDepth = parser.getDepth(); |
| 1578 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1579 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1580 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1581 | continue; |
| 1582 | } |
| 1583 | String tag = parser.getName(); |
| 1584 | if (TAG_NAME.equals(tag)) { |
| 1585 | type = parser.next(); |
| 1586 | if (type == XmlPullParser.TEXT) { |
| 1587 | name = parser.getText(); |
| 1588 | } |
| 1589 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1590 | UserRestrictionsUtils.readRestrictions(parser, baseRestrictions); |
| 1591 | } else if (TAG_DEVICE_POLICY_RESTRICTIONS.equals(tag)) { |
| 1592 | UserRestrictionsUtils.readRestrictions(parser, localRestrictions); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1593 | } else if (TAG_ACCOUNT.equals(tag)) { |
| 1594 | type = parser.next(); |
| 1595 | if (type == XmlPullParser.TEXT) { |
| 1596 | account = parser.getText(); |
| 1597 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1601 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1602 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1603 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1604 | userInfo.creationTime = creationTime; |
| 1605 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1606 | userInfo.partial = partial; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1607 | userInfo.guestToRemove = guestToRemove; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1608 | userInfo.profileGroupId = profileGroupId; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1609 | userInfo.restrictedProfileParentId = restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1610 | synchronized (mRestrictionsLock) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1611 | mBaseUserRestrictions.put(id, baseRestrictions); |
| 1612 | mDevicePolicyLocalUserRestrictions.put(id, localRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1613 | } |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1614 | return new Pair<>(userInfo, account); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1615 | |
| 1616 | } catch (IOException ioe) { |
| 1617 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1618 | } finally { |
| 1619 | if (fis != null) { |
| 1620 | try { |
| 1621 | fis.close(); |
| 1622 | } catch (IOException e) { |
| 1623 | } |
| 1624 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1625 | } |
| 1626 | return null; |
| 1627 | } |
| 1628 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1629 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 1630 | String valueString = parser.getAttributeValue(null, attr); |
| 1631 | if (valueString == null) return defaultValue; |
| 1632 | try { |
| 1633 | return Integer.parseInt(valueString); |
| 1634 | } catch (NumberFormatException nfe) { |
| 1635 | return defaultValue; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 1640 | String valueString = parser.getAttributeValue(null, attr); |
| 1641 | if (valueString == null) return defaultValue; |
| 1642 | try { |
| 1643 | return Long.parseLong(valueString); |
| 1644 | } catch (NumberFormatException nfe) { |
| 1645 | return defaultValue; |
| 1646 | } |
| 1647 | } |
| 1648 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1649 | private boolean isPackageInstalled(String pkg, int userId) { |
| 1650 | final ApplicationInfo info = mPm.getApplicationInfo(pkg, |
| 1651 | PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1652 | userId); |
| 1653 | if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 1654 | return false; |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1659 | /** |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1660 | * Removes all the restrictions files (res_<packagename>) for a given user. |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1661 | * Does not do any permissions checking. |
| 1662 | */ |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1663 | private void cleanAppRestrictions(int userId) { |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1664 | synchronized (mPackagesLock) { |
| 1665 | File dir = Environment.getUserSystemDirectory(userId); |
| 1666 | String[] files = dir.list(); |
| 1667 | if (files == null) return; |
| 1668 | for (String fileName : files) { |
| 1669 | if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) { |
| 1670 | File resFile = new File(dir, fileName); |
| 1671 | if (resFile.exists()) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1672 | resFile.delete(); |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1679 | /** |
| 1680 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 1681 | */ |
| 1682 | private void cleanAppRestrictionsForPackage(String pkg, int userId) { |
| 1683 | synchronized (mPackagesLock) { |
| 1684 | File dir = Environment.getUserSystemDirectory(userId); |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1685 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1686 | if (resFile.exists()) { |
| 1687 | resFile.delete(); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1692 | @Override |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1693 | public UserInfo createProfileForUser(String name, int flags, int userId) { |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1694 | checkManageUsersPermission("Only the system can create users"); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1695 | return createUserInternal(name, flags, userId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1698 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1699 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1700 | checkManageUsersPermission("Only the system can create users"); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1701 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1702 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1703 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1704 | private UserInfo createUserInternal(String name, int flags, int parentId) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1705 | if (hasUserRestriction(UserManager.DISALLOW_ADD_USER, UserHandle.getCallingUserId())) { |
Julia Reynolds | 7517502 | 2014-06-26 16:35:00 -0400 | [diff] [blame] | 1706 | Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled."); |
| 1707 | return null; |
| 1708 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1709 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1710 | return null; |
| 1711 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1712 | final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1713 | final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1714 | final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1715 | final long ident = Binder.clearCallingIdentity(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1716 | UserInfo userInfo; |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1717 | final int userId; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1718 | try { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1719 | synchronized (mPackagesLock) { |
| 1720 | UserInfo parent = null; |
| 1721 | if (parentId != UserHandle.USER_NULL) { |
| 1722 | synchronized (mUsersLock) { |
| 1723 | parent = getUserInfoLU(parentId); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1724 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1725 | if (parent == null) return null; |
| 1726 | } |
| 1727 | if (isManagedProfile && !canAddMoreManagedProfiles(parentId, false)) { |
| 1728 | Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId); |
| 1729 | return null; |
| 1730 | } |
| 1731 | if (!isGuest && !isManagedProfile && isUserLimitReached()) { |
| 1732 | // If we're not adding a guest user or a managed profile and the limit has |
| 1733 | // been reached, cannot add a user. |
| 1734 | return null; |
| 1735 | } |
| 1736 | // If we're adding a guest and there already exists one, bail. |
| 1737 | if (isGuest && findCurrentGuestUser() != null) { |
| 1738 | return null; |
| 1739 | } |
| 1740 | // In legacy mode, restricted profile's parent can only be the owner user |
| 1741 | if (isRestricted && !UserManager.isSplitSystemUser() |
| 1742 | && (parentId != UserHandle.USER_SYSTEM)) { |
| 1743 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be owner"); |
| 1744 | return null; |
| 1745 | } |
| 1746 | if (isRestricted && UserManager.isSplitSystemUser()) { |
| 1747 | if (parent == null) { |
| 1748 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be " |
| 1749 | + "specified"); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1750 | return null; |
| 1751 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1752 | if (!parent.canHaveProfile()) { |
| 1753 | Log.w(LOG_TAG, "Cannot add restricted profile - profiles cannot be " |
| 1754 | + "created for the specified parent user id " + parentId); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1755 | return null; |
| 1756 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1757 | } |
| 1758 | // In split system user mode, we assign the first human user the primary flag. |
| 1759 | // And if there is no device owner, we also assign the admin flag to primary user. |
| 1760 | if (UserManager.isSplitSystemUser() |
| 1761 | && !isGuest && !isManagedProfile && getPrimaryUser() == null) { |
| 1762 | flags |= UserInfo.FLAG_PRIMARY; |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 1763 | synchronized (mUsersLock) { |
| 1764 | if (!mIsDeviceManaged) { |
| 1765 | flags |= UserInfo.FLAG_ADMIN; |
| 1766 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1767 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1768 | } |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1769 | |
| 1770 | if (parent != null && parent.isEphemeral()) { |
| 1771 | flags |= UserInfo.FLAG_EPHEMERAL; |
| 1772 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1773 | userId = getNextAvailableId(); |
| 1774 | userInfo = new UserInfo(userId, name, null, flags); |
| 1775 | userInfo.serialNumber = mNextSerialNumber++; |
| 1776 | long now = System.currentTimeMillis(); |
| 1777 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
| 1778 | userInfo.partial = true; |
| 1779 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
| 1780 | synchronized (mUsersLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1781 | mUsers.put(userId, userInfo); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1782 | } |
| 1783 | writeUserListLP(); |
| 1784 | if (parent != null) { |
| 1785 | if (isManagedProfile) { |
| 1786 | if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1787 | parent.profileGroupId = parent.id; |
| 1788 | writeUserLP(parent); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1789 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1790 | userInfo.profileGroupId = parent.profileGroupId; |
| 1791 | } else if (isRestricted) { |
| 1792 | if (parent.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1793 | parent.restrictedProfileParentId = parent.id; |
| 1794 | writeUserLP(parent); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1795 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1796 | userInfo.restrictedProfileParentId = parent.restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1797 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1798 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1799 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1800 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1801 | storage.createUserKey(userId, userInfo.serialNumber, userInfo.isEphemeral()); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1802 | for (VolumeInfo vol : storage.getWritablePrivateVolumes()) { |
| 1803 | final String volumeUuid = vol.getFsUuid(); |
| 1804 | try { |
| 1805 | final File userDir = Environment.getDataUserDirectory(volumeUuid, userId); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1806 | storage.prepareUserStorage( |
| 1807 | volumeUuid, userId, userInfo.serialNumber, userInfo.isEphemeral()); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1808 | enforceSerialNumber(userDir, userInfo.serialNumber); |
| 1809 | } catch (IOException e) { |
| 1810 | Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e); |
| 1811 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1812 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1813 | mPm.createNewUser(userId); |
| 1814 | userInfo.partial = false; |
| 1815 | synchronized (mPackagesLock) { |
| 1816 | writeUserLP(userInfo); |
| 1817 | } |
| 1818 | updateUserIds(); |
| 1819 | Bundle restrictions = new Bundle(); |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 1820 | if (isGuest) { |
| 1821 | synchronized (mGuestRestrictions) { |
| 1822 | restrictions.putAll(mGuestRestrictions); |
| 1823 | } |
| 1824 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1825 | synchronized (mRestrictionsLock) { |
| 1826 | mBaseUserRestrictions.append(userId, restrictions); |
| 1827 | } |
| 1828 | mPm.newUserCreated(userId); |
| 1829 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 1830 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1831 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1832 | android.Manifest.permission.MANAGE_USERS); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1833 | } finally { |
| 1834 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1835 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1836 | return userInfo; |
| 1837 | } |
| 1838 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1839 | /** |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1840 | * @hide |
| 1841 | */ |
| 1842 | public UserInfo createRestrictedProfile(String name, int parentUserId) { |
| 1843 | checkManageUsersPermission("setupRestrictedProfile"); |
| 1844 | final UserInfo user = createProfileForUser(name, UserInfo.FLAG_RESTRICTED, parentUserId); |
| 1845 | if (user == null) { |
| 1846 | return null; |
| 1847 | } |
| 1848 | setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user.id); |
| 1849 | // Change the setting before applying the DISALLOW_SHARE_LOCATION restriction, otherwise |
| 1850 | // the putIntForUser() will fail. |
| 1851 | android.provider.Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1852 | android.provider.Settings.Secure.LOCATION_MODE, |
| 1853 | android.provider.Settings.Secure.LOCATION_MODE_OFF, user.id); |
| 1854 | setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user.id); |
| 1855 | return user; |
| 1856 | } |
| 1857 | |
| 1858 | /** |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1859 | * Find the current guest user. If the Guest user is partial, |
| 1860 | * 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] | 1861 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1862 | private UserInfo findCurrentGuestUser() { |
| 1863 | synchronized (mUsersLock) { |
| 1864 | final int size = mUsers.size(); |
| 1865 | for (int i = 0; i < size; i++) { |
| 1866 | final UserInfo user = mUsers.valueAt(i); |
| 1867 | if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) { |
| 1868 | return user; |
| 1869 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | return null; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1876 | * Mark this guest user for deletion to allow us to create another guest |
| 1877 | * and switch to that user before actually removing this guest. |
| 1878 | * @param userHandle the userid of the current guest |
| 1879 | * @return whether the user could be marked for deletion |
| 1880 | */ |
| 1881 | public boolean markGuestForDeletion(int userHandle) { |
| 1882 | checkManageUsersPermission("Only the system can remove users"); |
| 1883 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1884 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1885 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1886 | return false; |
| 1887 | } |
| 1888 | |
| 1889 | long ident = Binder.clearCallingIdentity(); |
| 1890 | try { |
| 1891 | final UserInfo user; |
| 1892 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1893 | synchronized (mUsersLock) { |
| 1894 | user = mUsers.get(userHandle); |
| 1895 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
| 1896 | return false; |
| 1897 | } |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1898 | } |
| 1899 | if (!user.isGuest()) { |
| 1900 | return false; |
| 1901 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1902 | // We set this to a guest user that is to be removed. This is a temporary state |
| 1903 | // where we are allowed to add new Guest users, even if this one is still not |
| 1904 | // removed. This user will still show up in getUserInfo() calls. |
| 1905 | // If we don't get around to removing this Guest user, it will be purged on next |
| 1906 | // startup. |
| 1907 | user.guestToRemove = true; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1908 | // Mark it as disabled, so that it isn't returned any more when |
| 1909 | // profiles are queried. |
| 1910 | user.flags |= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1911 | writeUserLP(user); |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1912 | } |
| 1913 | } finally { |
| 1914 | Binder.restoreCallingIdentity(ident); |
| 1915 | } |
| 1916 | return true; |
| 1917 | } |
| 1918 | |
| 1919 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1920 | * Removes a user and all data directories created for that user. This method should be called |
| 1921 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1922 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1923 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1924 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1925 | checkManageUsersPermission("Only the system can remove users"); |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 1926 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1927 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1928 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1932 | long ident = Binder.clearCallingIdentity(); |
| 1933 | try { |
| 1934 | final UserInfo user; |
Fyodor Kupolov | 0df68cd | 2015-10-01 13:54:22 -0700 | [diff] [blame] | 1935 | int currentUser = ActivityManager.getCurrentUser(); |
| 1936 | if (currentUser == userHandle) { |
| 1937 | Log.w(LOG_TAG, "Current user cannot be removed"); |
| 1938 | return false; |
| 1939 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1940 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1941 | synchronized (mUsersLock) { |
| 1942 | user = mUsers.get(userHandle); |
| 1943 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
| 1944 | return false; |
| 1945 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1946 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1947 | // We remember deleted user IDs to prevent them from being |
| 1948 | // reused during the current boot; they can still be reused |
| 1949 | // after a reboot. |
| 1950 | mRemovingUserIds.put(userHandle, true); |
| 1951 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1952 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1953 | try { |
| 1954 | mAppOpsService.removeUser(userHandle); |
| 1955 | } catch (RemoteException e) { |
| 1956 | Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e); |
| 1957 | } |
| 1958 | // Set this to a partially created user, so that the user will be purged |
| 1959 | // on next startup, in case the runtime stops now before stopping and |
| 1960 | // removing the user completely. |
| 1961 | user.partial = true; |
| 1962 | // Mark it as disabled, so that it isn't returned any more when |
| 1963 | // profiles are queried. |
| 1964 | user.flags |= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1965 | writeUserLP(user); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 1969 | && user.isManagedProfile()) { |
| 1970 | // Send broadcast to notify system that the user removed was a |
| 1971 | // managed user. |
| 1972 | sendProfileRemovedBroadcast(user.profileGroupId, user.id); |
| 1973 | } |
| 1974 | |
| 1975 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
| 1976 | int res; |
| 1977 | try { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1978 | res = ActivityManagerNative.getDefault().stopUser(userHandle, /* force= */ true, |
| 1979 | new IStopUserCallback.Stub() { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1980 | @Override |
| 1981 | public void userStopped(int userId) { |
| 1982 | finishRemoveUser(userId); |
| 1983 | } |
| 1984 | @Override |
| 1985 | public void userStopAborted(int userId) { |
| 1986 | } |
| 1987 | }); |
| 1988 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1991 | return res == ActivityManager.USER_OP_SUCCESS; |
| 1992 | } finally { |
| 1993 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1994 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1997 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1998 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1999 | // Let other services shutdown any activity and clean up their state before completely |
| 2000 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2001 | long ident = Binder.clearCallingIdentity(); |
| 2002 | try { |
| 2003 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 2004 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2005 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 2006 | android.Manifest.permission.MANAGE_USERS, |
| 2007 | |
| 2008 | new BroadcastReceiver() { |
| 2009 | @Override |
| 2010 | public void onReceive(Context context, Intent intent) { |
| 2011 | if (DBG) { |
| 2012 | Slog.i(LOG_TAG, |
| 2013 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 2014 | + userHandle); |
| 2015 | } |
| 2016 | new Thread() { |
| 2017 | public void run() { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 2018 | // Clean up any ActivityManager state |
| 2019 | LocalServices.getService(ActivityManagerInternal.class) |
| 2020 | .onUserRemoved(userHandle); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2021 | removeUserState(userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2022 | } |
| 2023 | }.start(); |
| 2024 | } |
| 2025 | }, |
| 2026 | |
| 2027 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2028 | } finally { |
| 2029 | Binder.restoreCallingIdentity(ident); |
| 2030 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2033 | private void removeUserState(final int userHandle) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2034 | mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2035 | // Cleanup package manager settings |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2036 | mPm.cleanUpUser(this, userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2037 | |
| 2038 | // Remove this user from the list |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2039 | synchronized (mUsersLock) { |
| 2040 | mUsers.remove(userHandle); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 2041 | mUserAccounts.delete(userHandle); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2042 | mIsUserManaged.delete(userHandle); |
| 2043 | } |
| 2044 | synchronized (mRestrictionsLock) { |
| 2045 | mBaseUserRestrictions.remove(userHandle); |
| 2046 | mAppliedUserRestrictions.remove(userHandle); |
| 2047 | mCachedEffectiveUserRestrictions.remove(userHandle); |
| 2048 | mDevicePolicyLocalUserRestrictions.remove(userHandle); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2049 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2050 | // Remove user file |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2051 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2052 | userFile.delete(); |
| 2053 | // Update the user list |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2054 | synchronized (mPackagesLock) { |
| 2055 | writeUserListLP(); |
| 2056 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2057 | updateUserIds(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2058 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
| 2059 | } |
| 2060 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2061 | private void removeDirectoryRecursive(File parent) { |
| 2062 | if (parent.isDirectory()) { |
| 2063 | String[] files = parent.list(); |
| 2064 | for (String filename : files) { |
| 2065 | File child = new File(parent, filename); |
| 2066 | removeDirectoryRecursive(child); |
| 2067 | } |
| 2068 | } |
| 2069 | parent.delete(); |
| 2070 | } |
| 2071 | |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2072 | private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2073 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Adam Connors | d4b584e | 2014-06-09 13:55:47 +0100 | [diff] [blame] | 2074 | managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 2075 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2076 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame^] | 2077 | managedProfileIntent.putExtra(Intent.EXTRA_USER_HANDLE, removedUserId); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2078 | mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null); |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2081 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2082 | public Bundle getApplicationRestrictions(String packageName) { |
| 2083 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 2084 | } |
| 2085 | |
| 2086 | @Override |
| 2087 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2088 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2089 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 2090 | checkSystemOrRoot("get application restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2091 | } |
| 2092 | synchronized (mPackagesLock) { |
| 2093 | // Read the restrictions from XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2094 | return readApplicationRestrictionsLP(packageName, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2099 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2100 | int userId) { |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 2101 | checkSystemOrRoot("set application restrictions"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2102 | synchronized (mPackagesLock) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2103 | if (restrictions == null || restrictions.isEmpty()) { |
| 2104 | cleanAppRestrictionsForPackage(packageName, userId); |
| 2105 | } else { |
| 2106 | // Write the restrictions to XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2107 | writeApplicationRestrictionsLP(packageName, restrictions, userId); |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2108 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2109 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 2110 | |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2111 | if (isPackageInstalled(packageName, userId)) { |
| 2112 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 2113 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 2114 | changeIntent.setPackage(packageName); |
| 2115 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 2116 | mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId)); |
| 2117 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 2120 | private void unhideAllInstalledAppsForUser(final int userHandle) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2121 | mHandler.post(new Runnable() { |
| 2122 | @Override |
| 2123 | public void run() { |
| 2124 | List<ApplicationInfo> apps = |
| 2125 | mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES, |
| 2126 | userHandle).getList(); |
| 2127 | final long ident = Binder.clearCallingIdentity(); |
| 2128 | try { |
| 2129 | for (ApplicationInfo appInfo : apps) { |
| 2130 | if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0 |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 2131 | && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN) |
| 2132 | != 0) { |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 2133 | mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false, |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2134 | userHandle); |
| 2135 | } |
| 2136 | } |
| 2137 | } finally { |
| 2138 | Binder.restoreCallingIdentity(ident); |
| 2139 | } |
| 2140 | } |
| 2141 | }); |
| 2142 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2143 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2144 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2145 | try { |
| 2146 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 2147 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 2148 | } catch (NameNotFoundException nnfe) { |
| 2149 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2150 | } finally { |
| 2151 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2155 | private Bundle readApplicationRestrictionsLP(String packageName, int userId) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2156 | AtomicFile restrictionsFile = |
| 2157 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 2158 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2159 | return readApplicationRestrictionsLP(restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2163 | static Bundle readApplicationRestrictionsLP(AtomicFile restrictionsFile) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2164 | final Bundle restrictions = new Bundle(); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2165 | final ArrayList<String> values = new ArrayList<>(); |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2166 | if (!restrictionsFile.getBaseFile().exists()) { |
Fyodor Kupolov | f6ee224 | 2015-04-06 10:15:07 -0700 | [diff] [blame] | 2167 | return restrictions; |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2168 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2169 | |
| 2170 | FileInputStream fis = null; |
| 2171 | try { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2172 | fis = restrictionsFile.openRead(); |
| 2173 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2174 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2175 | XmlUtils.nextElement(parser); |
| 2176 | if (parser.getEventType() != XmlPullParser.START_TAG) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2177 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 2178 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2179 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2180 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2181 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 2182 | readEntry(restrictions, values, parser); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2183 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2184 | } catch (IOException|XmlPullParserException e) { |
| 2185 | Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2186 | } finally { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2187 | IoUtils.closeQuietly(fis); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2188 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2189 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2190 | } |
| 2191 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2192 | private static void readEntry(Bundle restrictions, ArrayList<String> values, |
| 2193 | XmlPullParser parser) throws XmlPullParserException, IOException { |
| 2194 | int type = parser.getEventType(); |
| 2195 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 2196 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 2197 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
| 2198 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 2199 | if (multiple != null) { |
| 2200 | values.clear(); |
| 2201 | int count = Integer.parseInt(multiple); |
| 2202 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 2203 | if (type == XmlPullParser.START_TAG |
| 2204 | && parser.getName().equals(TAG_VALUE)) { |
| 2205 | values.add(parser.nextText().trim()); |
| 2206 | count--; |
| 2207 | } |
| 2208 | } |
| 2209 | String [] valueStrings = new String[values.size()]; |
| 2210 | values.toArray(valueStrings); |
| 2211 | restrictions.putStringArray(key, valueStrings); |
| 2212 | } else if (ATTR_TYPE_BUNDLE.equals(valType)) { |
| 2213 | restrictions.putBundle(key, readBundleEntry(parser, values)); |
| 2214 | } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) { |
| 2215 | final int outerDepth = parser.getDepth(); |
| 2216 | ArrayList<Bundle> bundleList = new ArrayList<>(); |
| 2217 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2218 | Bundle childBundle = readBundleEntry(parser, values); |
| 2219 | bundleList.add(childBundle); |
| 2220 | } |
| 2221 | restrictions.putParcelableArray(key, |
| 2222 | bundleList.toArray(new Bundle[bundleList.size()])); |
| 2223 | } else { |
| 2224 | String value = parser.nextText().trim(); |
| 2225 | if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 2226 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 2227 | } else if (ATTR_TYPE_INTEGER.equals(valType)) { |
| 2228 | restrictions.putInt(key, Integer.parseInt(value)); |
| 2229 | } else { |
| 2230 | restrictions.putString(key, value); |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values) |
| 2237 | throws IOException, XmlPullParserException { |
| 2238 | Bundle childBundle = new Bundle(); |
| 2239 | final int outerDepth = parser.getDepth(); |
| 2240 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2241 | readEntry(childBundle, values, parser); |
| 2242 | } |
| 2243 | return childBundle; |
| 2244 | } |
| 2245 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2246 | private void writeApplicationRestrictionsLP(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2247 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2248 | AtomicFile restrictionsFile = new AtomicFile( |
| 2249 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2250 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2251 | writeApplicationRestrictionsLP(restrictions, restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2255 | static void writeApplicationRestrictionsLP(Bundle restrictions, AtomicFile restrictionsFile) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2256 | FileOutputStream fos = null; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2257 | try { |
| 2258 | fos = restrictionsFile.startWrite(); |
| 2259 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 2260 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2261 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2262 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2263 | serializer.startDocument(null, true); |
| 2264 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 2265 | |
| 2266 | serializer.startTag(null, TAG_RESTRICTIONS); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2267 | writeBundle(restrictions, serializer); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2268 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 2269 | |
| 2270 | serializer.endDocument(); |
| 2271 | restrictionsFile.finishWrite(fos); |
| 2272 | } catch (Exception e) { |
| 2273 | restrictionsFile.failWrite(fos); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2274 | Slog.e(LOG_TAG, "Error writing application restrictions list", e); |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | private static void writeBundle(Bundle restrictions, XmlSerializer serializer) |
| 2279 | throws IOException { |
| 2280 | for (String key : restrictions.keySet()) { |
| 2281 | Object value = restrictions.get(key); |
| 2282 | serializer.startTag(null, TAG_ENTRY); |
| 2283 | serializer.attribute(null, ATTR_KEY, key); |
| 2284 | |
| 2285 | if (value instanceof Boolean) { |
| 2286 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 2287 | serializer.text(value.toString()); |
| 2288 | } else if (value instanceof Integer) { |
| 2289 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER); |
| 2290 | serializer.text(value.toString()); |
| 2291 | } else if (value == null || value instanceof String) { |
| 2292 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 2293 | serializer.text(value != null ? (String) value : ""); |
| 2294 | } else if (value instanceof Bundle) { |
| 2295 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2296 | writeBundle((Bundle) value, serializer); |
| 2297 | } else if (value instanceof Parcelable[]) { |
| 2298 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY); |
| 2299 | Parcelable[] array = (Parcelable[]) value; |
| 2300 | for (Parcelable parcelable : array) { |
| 2301 | if (!(parcelable instanceof Bundle)) { |
| 2302 | throw new IllegalArgumentException("bundle-array can only hold Bundles"); |
| 2303 | } |
| 2304 | serializer.startTag(null, TAG_ENTRY); |
| 2305 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2306 | writeBundle((Bundle) parcelable, serializer); |
| 2307 | serializer.endTag(null, TAG_ENTRY); |
| 2308 | } |
| 2309 | } else { |
| 2310 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 2311 | String[] values = (String[]) value; |
| 2312 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
| 2313 | for (String choice : values) { |
| 2314 | serializer.startTag(null, TAG_VALUE); |
| 2315 | serializer.text(choice != null ? choice : ""); |
| 2316 | serializer.endTag(null, TAG_VALUE); |
| 2317 | } |
| 2318 | } |
| 2319 | serializer.endTag(null, TAG_ENTRY); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2324 | public int getUserSerialNumber(int userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2325 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2326 | if (!exists(userHandle)) return -1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2327 | return getUserInfoLU(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | @Override |
| 2332 | public int getUserHandle(int userSerialNumber) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2333 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2334 | for (int userId : mUserIds) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2335 | UserInfo info = getUserInfoLU(userId); |
Kenny Guy | 945f883 | 2015-02-10 15:17:26 +0000 | [diff] [blame] | 2336 | if (info != null && info.serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2337 | } |
| 2338 | // Not found |
| 2339 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2340 | } |
| 2341 | } |
| 2342 | |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2343 | @Override |
| 2344 | public long getUserCreationTime(int userHandle) { |
| 2345 | int callingUserId = UserHandle.getCallingUserId(); |
| 2346 | UserInfo userInfo = null; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2347 | synchronized (mUsersLock) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2348 | if (callingUserId == userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2349 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2350 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2351 | UserInfo parent = getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2352 | if (parent != null && parent.id == callingUserId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2353 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | if (userInfo == null) { |
| 2358 | throw new SecurityException("userHandle can only be the calling user or a managed " |
| 2359 | + "profile associated with this user"); |
| 2360 | } |
| 2361 | return userInfo.creationTime; |
| 2362 | } |
| 2363 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2364 | /** |
| 2365 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 2366 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2367 | private void updateUserIds() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2368 | int num = 0; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2369 | synchronized (mUsersLock) { |
| 2370 | final int userSize = mUsers.size(); |
| 2371 | for (int i = 0; i < userSize; i++) { |
| 2372 | if (!mUsers.valueAt(i).partial) { |
| 2373 | num++; |
| 2374 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2375 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2376 | final int[] newUsers = new int[num]; |
| 2377 | int n = 0; |
| 2378 | for (int i = 0; i < userSize; i++) { |
| 2379 | if (!mUsers.valueAt(i).partial) { |
| 2380 | newUsers[n++] = mUsers.keyAt(i); |
| 2381 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2382 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2383 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2388 | * Called right before a user starts. This will not be called for the system user. |
| 2389 | */ |
| 2390 | public void onBeforeStartUser(int userId) { |
| 2391 | synchronized (mRestrictionsLock) { |
| 2392 | applyUserRestrictionsLR(userId); |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2397 | * Make a note of the last started time of a user and do some cleanup. |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2398 | * @param userId the user that was just foregrounded |
| 2399 | */ |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 2400 | public void onUserForeground(int userId) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2401 | UserInfo user = getUserInfoNoChecks(userId); |
| 2402 | if (user == null || user.partial) { |
| 2403 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 2404 | return; |
| 2405 | } |
| 2406 | long now = System.currentTimeMillis(); |
| 2407 | if (now > EPOCH_PLUS_30_YEARS) { |
| 2408 | user.lastLoggedInTime = now; |
| 2409 | scheduleWriteUser(user); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2414 | * Returns the next available user id, filling in any holes in the ids. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2415 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 2416 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2417 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2418 | private int getNextAvailableId() { |
| 2419 | synchronized (mUsersLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2420 | int i = MIN_USER_ID; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2421 | while (i < MAX_USER_ID) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2422 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2423 | return i; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 2424 | } |
| 2425 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2426 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2427 | } |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2428 | throw new IllegalStateException("No user id available!"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2429 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2430 | |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2431 | private String packageToRestrictionsFileName(String packageName) { |
| 2432 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 2433 | } |
| 2434 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2435 | /** |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2436 | * Enforce that serial number stored in user directory inode matches the |
| 2437 | * given expected value. Gracefully sets the serial number if currently |
| 2438 | * undefined. |
| 2439 | * |
| 2440 | * @throws IOException when problem extracting serial number, or serial |
| 2441 | * number is mismatched. |
| 2442 | */ |
| 2443 | public static void enforceSerialNumber(File file, int serialNumber) throws IOException { |
| 2444 | final int foundSerial = getSerialNumber(file); |
| 2445 | Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial); |
| 2446 | |
| 2447 | if (foundSerial == -1) { |
| 2448 | Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid"); |
| 2449 | try { |
| 2450 | setSerialNumber(file, serialNumber); |
| 2451 | } catch (IOException e) { |
| 2452 | Slog.w(LOG_TAG, "Failed to set serial number on " + file, e); |
| 2453 | } |
| 2454 | |
| 2455 | } else if (foundSerial != serialNumber) { |
| 2456 | throw new IOException("Found serial number " + foundSerial |
| 2457 | + " doesn't match expected " + serialNumber); |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | /** |
| 2462 | * Set serial number stored in user directory inode. |
| 2463 | * |
| 2464 | * @throws IOException if serial number was already set |
| 2465 | */ |
| 2466 | private static void setSerialNumber(File file, int serialNumber) |
| 2467 | throws IOException { |
| 2468 | try { |
| 2469 | final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8); |
| 2470 | Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE); |
| 2471 | } catch (ErrnoException e) { |
| 2472 | throw e.rethrowAsIOException(); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * Return serial number stored in user directory inode. |
| 2478 | * |
| 2479 | * @return parsed serial number, or -1 if not set |
| 2480 | */ |
| 2481 | private static int getSerialNumber(File file) throws IOException { |
| 2482 | try { |
| 2483 | final byte[] buf = new byte[256]; |
| 2484 | final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf); |
| 2485 | final String serial = new String(buf, 0, len); |
| 2486 | try { |
| 2487 | return Integer.parseInt(serial); |
| 2488 | } catch (NumberFormatException e) { |
| 2489 | throw new IOException("Bad serial number: " + serial); |
| 2490 | } |
| 2491 | } catch (ErrnoException e) { |
| 2492 | if (e.errno == OsConstants.ENODATA) { |
| 2493 | return -1; |
| 2494 | } else { |
| 2495 | throw e.rethrowAsIOException(); |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2500 | @Override |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2501 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 2502 | FileDescriptor err, String[] args, ResultReceiver resultReceiver) { |
| 2503 | (new Shell()).exec(this, in, out, err, args, resultReceiver); |
| 2504 | } |
| 2505 | |
| 2506 | int onShellCommand(Shell shell, String cmd) { |
| 2507 | if (cmd == null) { |
| 2508 | return shell.handleDefaultCommands(cmd); |
| 2509 | } |
| 2510 | |
| 2511 | final PrintWriter pw = shell.getOutPrintWriter(); |
| 2512 | try { |
| 2513 | switch(cmd) { |
| 2514 | case "list": |
| 2515 | return runList(pw); |
| 2516 | } |
| 2517 | } catch (RemoteException e) { |
| 2518 | pw.println("Remote exception: " + e); |
| 2519 | } |
| 2520 | return -1; |
| 2521 | } |
| 2522 | |
| 2523 | private int runList(PrintWriter pw) throws RemoteException { |
| 2524 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 2525 | final List<UserInfo> users = getUsers(false); |
| 2526 | if (users == null) { |
| 2527 | pw.println("Error: couldn't get users"); |
| 2528 | return 1; |
| 2529 | } else { |
| 2530 | pw.println("Users:"); |
| 2531 | for (int i = 0; i < users.size(); i++) { |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 2532 | String running = am.isUserRunning(users.get(i).id, 0) ? " running" : ""; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2533 | pw.println("\t" + users.get(i).toString() + running); |
| 2534 | } |
| 2535 | return 0; |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2540 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2541 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2542 | != PackageManager.PERMISSION_GRANTED) { |
| 2543 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 2544 | + Binder.getCallingPid() |
| 2545 | + ", uid=" + Binder.getCallingUid() |
| 2546 | + " without permission " |
| 2547 | + android.Manifest.permission.DUMP); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | long now = System.currentTimeMillis(); |
| 2552 | StringBuilder sb = new StringBuilder(); |
| 2553 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2554 | synchronized (mUsersLock) { |
| 2555 | pw.println("Users:"); |
| 2556 | for (int i = 0; i < mUsers.size(); i++) { |
| 2557 | UserInfo user = mUsers.valueAt(i); |
| 2558 | if (user == null) { |
| 2559 | continue; |
| 2560 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2561 | final int userId = user.id; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2562 | pw.print(" "); pw.print(user); |
| 2563 | pw.print(" serialNo="); pw.print(user.serialNumber); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2564 | if (mRemovingUserIds.get(userId)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2565 | pw.print(" <removing> "); |
| 2566 | } |
| 2567 | if (user.partial) { |
| 2568 | pw.print(" <partial>"); |
| 2569 | } |
| 2570 | pw.println(); |
| 2571 | pw.print(" Created: "); |
| 2572 | if (user.creationTime == 0) { |
| 2573 | pw.println("<unknown>"); |
| 2574 | } else { |
| 2575 | sb.setLength(0); |
| 2576 | TimeUtils.formatDuration(now - user.creationTime, sb); |
| 2577 | sb.append(" ago"); |
| 2578 | pw.println(sb); |
| 2579 | } |
| 2580 | pw.print(" Last logged in: "); |
| 2581 | if (user.lastLoggedInTime == 0) { |
| 2582 | pw.println("<unknown>"); |
| 2583 | } else { |
| 2584 | sb.setLength(0); |
| 2585 | TimeUtils.formatDuration(now - user.lastLoggedInTime, sb); |
| 2586 | sb.append(" ago"); |
| 2587 | pw.println(sb); |
| 2588 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2589 | pw.print(" Has profile owner: "); |
| 2590 | pw.println(mIsUserManaged.get(userId)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2591 | pw.println(" Restrictions:"); |
| 2592 | synchronized (mRestrictionsLock) { |
| 2593 | UserRestrictionsUtils.dumpRestrictions( |
| 2594 | pw, " ", mBaseUserRestrictions.get(user.id)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2595 | pw.println(" Device policy local restrictions:"); |
| 2596 | UserRestrictionsUtils.dumpRestrictions( |
| 2597 | pw, " ", mDevicePolicyLocalUserRestrictions.get(user.id)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2598 | pw.println(" Effective restrictions:"); |
| 2599 | UserRestrictionsUtils.dumpRestrictions( |
| 2600 | pw, " ", mCachedEffectiveUserRestrictions.get(user.id)); |
| 2601 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2602 | pw.println(); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 2603 | String accountName = mUserAccounts.get(userId); |
| 2604 | if (accountName != null) { |
| 2605 | pw.print(" Account name: " + accountName); |
| 2606 | pw.println(); |
| 2607 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2608 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2609 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2610 | pw.println(" Device policy global restrictions:"); |
| 2611 | synchronized (mRestrictionsLock) { |
| 2612 | UserRestrictionsUtils |
| 2613 | .dumpRestrictions(pw, " ", mDevicePolicyGlobalUserRestrictions); |
| 2614 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 2615 | pw.println(); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2616 | pw.println(" Guest restrictions:"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2617 | synchronized (mGuestRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2618 | UserRestrictionsUtils.dumpRestrictions(pw, " ", mGuestRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2619 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2620 | synchronized (mUsersLock) { |
| 2621 | pw.println(); |
| 2622 | pw.println(" Device managed: " + mIsDeviceManaged); |
| 2623 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2624 | } |
| 2625 | } |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2626 | |
| 2627 | final class MainHandler extends Handler { |
| 2628 | |
| 2629 | @Override |
| 2630 | public void handleMessage(Message msg) { |
| 2631 | switch (msg.what) { |
| 2632 | case WRITE_USER_MSG: |
| 2633 | removeMessages(WRITE_USER_MSG, msg.obj); |
| 2634 | synchronized (mPackagesLock) { |
| 2635 | int userId = ((UserInfo) msg.obj).id; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2636 | UserInfo userInfo = getUserInfoNoChecks(userId); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2637 | if (userInfo != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2638 | writeUserLP(userInfo); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | } |
| 2642 | } |
| 2643 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 2644 | |
| 2645 | /** |
| 2646 | * @param userId |
| 2647 | * @return whether the user has been initialized yet |
| 2648 | */ |
| 2649 | boolean isInitialized(int userId) { |
| 2650 | return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 2651 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2652 | |
| 2653 | private class LocalService extends UserManagerInternal { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2654 | @Override |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2655 | public void setDevicePolicyUserRestrictions(int userId, @NonNull Bundle localRestrictions, |
| 2656 | @Nullable Bundle globalRestrictions) { |
| 2657 | UserManagerService.this.setDevicePolicyUserRestrictions(userId, localRestrictions, |
| 2658 | globalRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | @Override |
| 2662 | public Bundle getBaseUserRestrictions(int userId) { |
| 2663 | synchronized (mRestrictionsLock) { |
| 2664 | return mBaseUserRestrictions.get(userId); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | @Override |
| 2669 | public void setBaseUserRestrictionsByDpmsForMigration( |
| 2670 | int userId, Bundle baseRestrictions) { |
| 2671 | synchronized (mRestrictionsLock) { |
| 2672 | mBaseUserRestrictions.put(userId, new Bundle(baseRestrictions)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2673 | invalidateEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2674 | } |
| 2675 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2676 | final UserInfo userInfo = getUserInfoNoChecks(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2677 | synchronized (mPackagesLock) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2678 | if (userInfo != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2679 | writeUserLP(userInfo); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2680 | } else { |
| 2681 | Slog.w(LOG_TAG, "UserInfo not found for " + userId); |
| 2682 | } |
| 2683 | } |
| 2684 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2685 | |
| 2686 | @Override |
| 2687 | public boolean getUserRestriction(int userId, String key) { |
| 2688 | return getUserRestrictions(userId).getBoolean(key); |
| 2689 | } |
| 2690 | |
| 2691 | @Override |
| 2692 | public void addUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2693 | synchronized (mUserRestrictionsListeners) { |
| 2694 | mUserRestrictionsListeners.add(listener); |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | @Override |
| 2699 | public void removeUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2700 | synchronized (mUserRestrictionsListeners) { |
| 2701 | mUserRestrictionsListeners.remove(listener); |
| 2702 | } |
| 2703 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2704 | |
| 2705 | @Override |
| 2706 | public void setDeviceManaged(boolean isManaged) { |
| 2707 | synchronized (mUsersLock) { |
| 2708 | mIsDeviceManaged = isManaged; |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | @Override |
| 2713 | public void setUserManaged(int userId, boolean isManaged) { |
| 2714 | synchronized (mUsersLock) { |
| 2715 | mIsUserManaged.put(userId, isManaged); |
| 2716 | } |
| 2717 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2718 | } |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2719 | |
| 2720 | private class Shell extends ShellCommand { |
| 2721 | @Override |
| 2722 | public int onCommand(String cmd) { |
| 2723 | return onShellCommand(this, cmd); |
| 2724 | } |
| 2725 | |
| 2726 | @Override |
| 2727 | public void onHelp() { |
| 2728 | final PrintWriter pw = getOutPrintWriter(); |
| 2729 | pw.println("User manager (user) commands:"); |
| 2730 | pw.println(" help"); |
| 2731 | pw.println(" Print this help text."); |
| 2732 | pw.println(""); |
| 2733 | pw.println(" list"); |
| 2734 | pw.println(" Prints all users on the system."); |
| 2735 | } |
| 2736 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2737 | |
| 2738 | private static void debug(String message) { |
| 2739 | Log.d(LOG_TAG, message + |
| 2740 | (DBG_WITH_STACKTRACE ? " called at\n" + Debug.getCallers(10, " ") : "")); |
| 2741 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2742 | } |