UserManagerService.java revision e1df482c173d975923dba2d3cf644f5a0fadee17
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
17package com.android.server.pm;
18
19import android.app.Activity;
20import android.app.ActivityManager;
21import android.app.ActivityManagerNative;
22import android.app.IStopUserCallback;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.content.pm.UserInfo;
30import android.graphics.Bitmap;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Environment;
34import android.os.FileUtils;
35import android.os.Handler;
36import android.os.IUserManager;
37import android.os.Message;
38import android.os.ParcelFileDescriptor;
39import android.os.Parcelable;
40import android.os.Process;
41import android.os.RemoteException;
42import android.os.ServiceManager;
43import android.os.UserHandle;
44import android.os.UserManager;
45import android.os.storage.StorageManager;
46import android.os.storage.VolumeInfo;
47import android.system.ErrnoException;
48import android.system.Os;
49import android.system.OsConstants;
50import android.util.AtomicFile;
51import android.util.Log;
52import android.util.Slog;
53import android.util.SparseArray;
54import android.util.SparseBooleanArray;
55import android.util.TimeUtils;
56import android.util.Xml;
57
58import com.google.android.collect.Sets;
59
60import com.android.internal.annotations.VisibleForTesting;
61import com.android.internal.app.IAppOpsService;
62import com.android.internal.util.ArrayUtils;
63import com.android.internal.util.FastXmlSerializer;
64import com.android.internal.util.XmlUtils;
65
66import org.xmlpull.v1.XmlPullParser;
67import org.xmlpull.v1.XmlPullParserException;
68import org.xmlpull.v1.XmlSerializer;
69
70import java.io.BufferedOutputStream;
71import java.io.File;
72import java.io.FileDescriptor;
73import java.io.FileInputStream;
74import java.io.FileNotFoundException;
75import java.io.FileOutputStream;
76import java.io.IOException;
77import java.io.PrintWriter;
78import java.nio.charset.StandardCharsets;
79import java.util.ArrayList;
80import java.util.List;
81import java.util.Set;
82
83import libcore.io.IoUtils;
84
85public class UserManagerService extends IUserManager.Stub {
86
87    private static final String LOG_TAG = "UserManagerService";
88
89    private static final boolean DBG = false;
90
91    private static final String TAG_NAME = "name";
92    private static final String ATTR_FLAGS = "flags";
93    private static final String ATTR_ICON_PATH = "icon";
94    private static final String ATTR_ID = "id";
95    private static final String ATTR_CREATION_TIME = "created";
96    private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
97    private static final String ATTR_SERIAL_NO = "serialNumber";
98    private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
99    private static final String ATTR_PARTIAL = "partial";
100    private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove";
101    private static final String ATTR_USER_VERSION = "version";
102    private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId";
103    private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions";
104    private static final String TAG_USERS = "users";
105    private static final String TAG_USER = "user";
106    private static final String TAG_RESTRICTIONS = "restrictions";
107    private static final String TAG_ENTRY = "entry";
108    private static final String TAG_VALUE = "value";
109    private static final String ATTR_KEY = "key";
110    private static final String ATTR_VALUE_TYPE = "type";
111    private static final String ATTR_MULTIPLE = "m";
112
113    private static final String ATTR_TYPE_STRING_ARRAY = "sa";
114    private static final String ATTR_TYPE_STRING = "s";
115    private static final String ATTR_TYPE_BOOLEAN = "b";
116    private static final String ATTR_TYPE_INTEGER = "i";
117    private static final String ATTR_TYPE_BUNDLE = "B";
118    private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA";
119
120    private static final String USER_INFO_DIR = "system" + File.separator + "users";
121    private static final String USER_LIST_FILENAME = "userlist.xml";
122    private static final String USER_PHOTO_FILENAME = "photo.png";
123    private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp";
124
125    private static final String RESTRICTIONS_FILE_PREFIX = "res_";
126    private static final String XML_SUFFIX = ".xml";
127
128    private static final int MIN_USER_ID = 10;
129
130    private static final int USER_VERSION = 5;
131
132    private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
133
134    // Maximum number of managed profiles permitted is 1. This cannot be increased
135    // without first making sure that the rest of the framework is prepared for it.
136    private static final int MAX_MANAGED_PROFILES = 1;
137
138    /**
139     * Flag indicating whether device credentials are shared among same-user profiles.
140     */
141    private static final boolean CONFIG_PROFILES_SHARE_CREDENTIAL = true;
142
143    // Set of user restrictions, which can only be enforced by the system
144    private static final Set<String> SYSTEM_CONTROLLED_RESTRICTIONS = Sets.newArraySet(
145            UserManager.DISALLOW_RECORD_AUDIO);
146
147    static final int WRITE_USER_MSG = 1;
148    static final int WRITE_USER_DELAY = 2*1000;  // 2 seconds
149
150    private static final String XATTR_SERIAL = "user.serial";
151
152    private final Context mContext;
153    private final PackageManagerService mPm;
154    private final Object mInstallLock;
155    private final Object mPackagesLock;
156
157    private final Handler mHandler;
158
159    private final File mUsersDir;
160    private final File mUserListFile;
161
162    private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>();
163    private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>();
164    private final Bundle mGuestRestrictions = new Bundle();
165
166    /**
167     * Set of user IDs being actively removed. Removed IDs linger in this set
168     * for several seconds to work around a VFS caching issue.
169     */
170    // @GuardedBy("mPackagesLock")
171    private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
172
173    private int[] mUserIds;
174    private int mNextSerialNumber;
175    private int mUserVersion = 0;
176
177    private IAppOpsService mAppOpsService;
178
179    private static UserManagerService sInstance;
180
181    public static UserManagerService getInstance() {
182        synchronized (UserManagerService.class) {
183            return sInstance;
184        }
185    }
186
187    /**
188     * Available for testing purposes.
189     */
190    UserManagerService(File dataDir, File baseUserPath) {
191        this(null, null, new Object(), new Object(), dataDir, baseUserPath);
192    }
193
194    /**
195     * Called by package manager to create the service.  This is closely
196     * associated with the package manager, and the given lock is the
197     * package manager's own lock.
198     */
199    UserManagerService(Context context, PackageManagerService pm,
200            Object installLock, Object packagesLock) {
201        this(context, pm, installLock, packagesLock,
202                Environment.getDataDirectory(),
203                new File(Environment.getDataDirectory(), "user"));
204    }
205
206    /**
207     * Available for testing purposes.
208     */
209    private UserManagerService(Context context, PackageManagerService pm,
210            Object installLock, Object packagesLock,
211            File dataDir, File baseUserPath) {
212        mContext = context;
213        mPm = pm;
214        mInstallLock = installLock;
215        mPackagesLock = packagesLock;
216        mHandler = new MainHandler();
217        synchronized (mInstallLock) {
218            synchronized (mPackagesLock) {
219                mUsersDir = new File(dataDir, USER_INFO_DIR);
220                mUsersDir.mkdirs();
221                // Make zeroth user directory, for services to migrate their files to that location
222                File userZeroDir = new File(mUsersDir, "0");
223                userZeroDir.mkdirs();
224                FileUtils.setPermissions(mUsersDir.toString(),
225                        FileUtils.S_IRWXU|FileUtils.S_IRWXG
226                        |FileUtils.S_IROTH|FileUtils.S_IXOTH,
227                        -1, -1);
228                mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
229                initDefaultGuestRestrictions();
230                readUserListLocked();
231                sInstance = this;
232            }
233        }
234    }
235
236    void systemReady() {
237        synchronized (mInstallLock) {
238            synchronized (mPackagesLock) {
239                // Prune out any partially created/partially removed users.
240                ArrayList<UserInfo> partials = new ArrayList<UserInfo>();
241                for (int i = 0; i < mUsers.size(); i++) {
242                    UserInfo ui = mUsers.valueAt(i);
243                    if ((ui.partial || ui.guestToRemove) && i != 0) {
244                        partials.add(ui);
245                    }
246                }
247                for (int i = 0; i < partials.size(); i++) {
248                    UserInfo ui = partials.get(i);
249                    Slog.w(LOG_TAG, "Removing partially created user " + ui.id
250                            + " (name=" + ui.name + ")");
251                    removeUserStateLocked(ui.id);
252                }
253            }
254        }
255        onUserForeground(UserHandle.USER_OWNER);
256        mAppOpsService = IAppOpsService.Stub.asInterface(
257                ServiceManager.getService(Context.APP_OPS_SERVICE));
258        for (int i = 0; i < mUserIds.length; ++i) {
259            try {
260                mAppOpsService.setUserRestrictions(mUserRestrictions.get(mUserIds[i]), mUserIds[i]);
261            } catch (RemoteException e) {
262                Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
263            }
264        }
265    }
266
267    @Override
268    public List<UserInfo> getUsers(boolean excludeDying) {
269        checkManageUsersPermission("query users");
270        synchronized (mPackagesLock) {
271            ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
272            for (int i = 0; i < mUsers.size(); i++) {
273                UserInfo ui = mUsers.valueAt(i);
274                if (ui.partial) {
275                    continue;
276                }
277                if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
278                    users.add(ui);
279                }
280            }
281            return users;
282        }
283    }
284
285    @Override
286    public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
287        if (userId != UserHandle.getCallingUserId()) {
288            checkManageUsersPermission("getting profiles related to user " + userId);
289        }
290        final long ident = Binder.clearCallingIdentity();
291        try {
292            synchronized (mPackagesLock) {
293                return getProfilesLocked(userId, enabledOnly);
294            }
295        } finally {
296            Binder.restoreCallingIdentity(ident);
297        }
298    }
299
300    /** Assume permissions already checked and caller's identity cleared */
301    private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) {
302        UserInfo user = getUserInfoLocked(userId);
303        ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
304        if (user == null) {
305            // Probably a dying user
306            return users;
307        }
308        for (int i = 0; i < mUsers.size(); i++) {
309            UserInfo profile = mUsers.valueAt(i);
310            if (!isProfileOf(user, profile)) {
311                continue;
312            }
313            if (enabledOnly && !profile.isEnabled()) {
314                continue;
315            }
316            if (mRemovingUserIds.get(profile.id)) {
317                continue;
318            }
319            users.add(profile);
320        }
321        return users;
322    }
323
324    @Override
325    public int getCredentialOwnerProfile(int userHandle) {
326        checkManageUsersPermission("get the credential owner");
327        if (CONFIG_PROFILES_SHARE_CREDENTIAL) {
328            synchronized (mPackagesLock) {
329                UserInfo profileParent = getProfileParentLocked(userHandle);
330                if (profileParent != null) {
331                    return profileParent.id;
332                }
333            }
334        }
335
336        return userHandle;
337    }
338
339    @Override
340    public UserInfo getProfileParent(int userHandle) {
341        checkManageUsersPermission("get the profile parent");
342        synchronized (mPackagesLock) {
343            return getProfileParentLocked(userHandle);
344        }
345    }
346
347    private UserInfo getProfileParentLocked(int userHandle) {
348        UserInfo profile = getUserInfoLocked(userHandle);
349        if (profile == null) {
350            return null;
351        }
352        int parentUserId = profile.profileGroupId;
353        if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
354            return null;
355        } else {
356            return getUserInfoLocked(parentUserId);
357        }
358    }
359
360    private boolean isProfileOf(UserInfo user, UserInfo profile) {
361        return user.id == profile.id ||
362                (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
363                && user.profileGroupId == profile.profileGroupId);
364    }
365
366    @Override
367    public void setUserEnabled(int userId) {
368        checkManageUsersPermission("enable user");
369        synchronized (mPackagesLock) {
370            UserInfo info = getUserInfoLocked(userId);
371            if (info != null && !info.isEnabled()) {
372                info.flags ^= UserInfo.FLAG_DISABLED;
373                writeUserLocked(info);
374            }
375        }
376    }
377
378    @Override
379    public UserInfo getUserInfo(int userId) {
380        checkManageUsersPermission("query user");
381        synchronized (mPackagesLock) {
382            return getUserInfoLocked(userId);
383        }
384    }
385
386    @Override
387    public boolean isRestricted() {
388        synchronized (mPackagesLock) {
389            return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted();
390        }
391    }
392
393    /*
394     * Should be locked on mUsers before calling this.
395     */
396    private UserInfo getUserInfoLocked(int userId) {
397        UserInfo ui = mUsers.get(userId);
398        // If it is partial and not in the process of being removed, return as unknown user.
399        if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) {
400            Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
401            return null;
402        }
403        return ui;
404    }
405
406    /** Called by PackageManagerService */
407    public boolean exists(int userId) {
408        synchronized (mPackagesLock) {
409            return mUsers.get(userId) != null;
410        }
411    }
412
413    @Override
414    public void setUserName(int userId, String name) {
415        checkManageUsersPermission("rename users");
416        boolean changed = false;
417        synchronized (mPackagesLock) {
418            UserInfo info = mUsers.get(userId);
419            if (info == null || info.partial) {
420                Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
421                return;
422            }
423            if (name != null && !name.equals(info.name)) {
424                info.name = name;
425                writeUserLocked(info);
426                changed = true;
427            }
428        }
429        if (changed) {
430            sendUserInfoChangedBroadcast(userId);
431        }
432    }
433
434    @Override
435    public void setUserIcon(int userId, Bitmap bitmap) {
436        checkManageUsersPermission("update users");
437        long ident = Binder.clearCallingIdentity();
438        try {
439            synchronized (mPackagesLock) {
440                UserInfo info = mUsers.get(userId);
441                if (info == null || info.partial) {
442                    Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
443                    return;
444                }
445                writeBitmapLocked(info, bitmap);
446                writeUserLocked(info);
447            }
448            sendUserInfoChangedBroadcast(userId);
449        } finally {
450            Binder.restoreCallingIdentity(ident);
451        }
452    }
453
454    private void sendUserInfoChangedBroadcast(int userId) {
455        Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
456        changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
457        changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
458        mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
459    }
460
461    @Override
462    public ParcelFileDescriptor getUserIcon(int userId) {
463        String iconPath;
464        synchronized (mPackagesLock) {
465            UserInfo info = mUsers.get(userId);
466            if (info == null || info.partial) {
467                Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId);
468                return null;
469            }
470            int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId;
471            if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID
472                    || callingGroupId != info.profileGroupId) {
473                checkManageUsersPermission("get the icon of a user who is not related");
474            }
475            if (info.iconPath == null) {
476                return null;
477            }
478            iconPath = info.iconPath;
479        }
480
481        try {
482            return ParcelFileDescriptor.open(
483                    new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
484        } catch (FileNotFoundException e) {
485            Log.e(LOG_TAG, "Couldn't find icon file", e);
486        }
487        return null;
488    }
489
490    public void makeInitialized(int userId) {
491        checkManageUsersPermission("makeInitialized");
492        synchronized (mPackagesLock) {
493            UserInfo info = mUsers.get(userId);
494            if (info == null || info.partial) {
495                Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
496            }
497            if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) {
498                info.flags |= UserInfo.FLAG_INITIALIZED;
499                scheduleWriteUserLocked(info);
500            }
501        }
502    }
503
504    /**
505     * If default guest restrictions haven't been initialized yet, add the basic
506     * restrictions.
507     */
508    private void initDefaultGuestRestrictions() {
509        if (mGuestRestrictions.isEmpty()) {
510            mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
511            mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
512        }
513    }
514
515    @Override
516    public Bundle getDefaultGuestRestrictions() {
517        checkManageUsersPermission("getDefaultGuestRestrictions");
518        synchronized (mPackagesLock) {
519            return new Bundle(mGuestRestrictions);
520        }
521    }
522
523    @Override
524    public void setDefaultGuestRestrictions(Bundle restrictions) {
525        checkManageUsersPermission("setDefaultGuestRestrictions");
526        synchronized (mPackagesLock) {
527            mGuestRestrictions.clear();
528            mGuestRestrictions.putAll(restrictions);
529            writeUserListLocked();
530        }
531    }
532
533    @Override
534    public boolean hasUserRestriction(String restrictionKey, int userId) {
535        synchronized (mPackagesLock) {
536            Bundle restrictions = mUserRestrictions.get(userId);
537            return restrictions != null && restrictions.getBoolean(restrictionKey);
538        }
539    }
540
541    @Override
542    public Bundle getUserRestrictions(int userId) {
543        // checkManageUsersPermission("getUserRestrictions");
544
545        synchronized (mPackagesLock) {
546            Bundle restrictions = mUserRestrictions.get(userId);
547            return restrictions != null ? new Bundle(restrictions) : new Bundle();
548        }
549    }
550
551    @Override
552    public void setUserRestriction(String key, boolean value, int userId) {
553        checkManageUsersPermission("setUserRestriction");
554        synchronized (mPackagesLock) {
555            if (!SYSTEM_CONTROLLED_RESTRICTIONS.contains(key)) {
556                Bundle restrictions = getUserRestrictions(userId);
557                restrictions.putBoolean(key, value);
558                setUserRestrictionsInternalLocked(restrictions, userId);
559            }
560        }
561    }
562
563    @Override
564    public void setSystemControlledUserRestriction(String key, boolean value, int userId) {
565        checkSystemOrRoot("setSystemControlledUserRestriction");
566        synchronized (mPackagesLock) {
567            Bundle restrictions = getUserRestrictions(userId);
568            restrictions.putBoolean(key, value);
569            setUserRestrictionsInternalLocked(restrictions, userId);
570        }
571    }
572
573    @Override
574    public void setUserRestrictions(Bundle restrictions, int userId) {
575        checkManageUsersPermission("setUserRestrictions");
576        if (restrictions == null) return;
577
578        synchronized (mPackagesLock) {
579            final Bundle oldUserRestrictions = mUserRestrictions.get(userId);
580            // Restore the original state of system controlled restrictions from oldUserRestrictions
581            for (String key : SYSTEM_CONTROLLED_RESTRICTIONS) {
582                restrictions.remove(key);
583                if (oldUserRestrictions.containsKey(key)) {
584                    restrictions.putBoolean(key, oldUserRestrictions.getBoolean(key));
585                }
586            }
587            setUserRestrictionsInternalLocked(restrictions, userId);
588        }
589    }
590
591    private void setUserRestrictionsInternalLocked(Bundle restrictions, int userId) {
592        final Bundle userRestrictions = mUserRestrictions.get(userId);
593        userRestrictions.clear();
594        userRestrictions.putAll(restrictions);
595        long token = Binder.clearCallingIdentity();
596        try {
597        mAppOpsService.setUserRestrictions(userRestrictions, userId);
598        } catch (RemoteException e) {
599            Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
600        } finally {
601            Binder.restoreCallingIdentity(token);
602        }
603        scheduleWriteUserLocked(mUsers.get(userId));
604    }
605
606    /**
607     * Check if we've hit the limit of how many users can be created.
608     */
609    private boolean isUserLimitReachedLocked() {
610        return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers();
611    }
612
613    @Override
614    public boolean canAddMoreManagedProfiles() {
615        checkManageUsersPermission("check if more managed profiles can be added.");
616        if (ActivityManager.isLowRamDeviceStatic()) {
617            return false;
618        }
619        if (!mContext.getPackageManager().hasSystemFeature(
620                PackageManager.FEATURE_MANAGED_USERS)) {
621            return false;
622        }
623        synchronized(mPackagesLock) {
624            // Limit number of managed profiles that can be created
625            if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
626                    >= MAX_MANAGED_PROFILES) {
627                return false;
628            }
629            int usersCount = getAliveUsersExcludingGuestsCountLocked();
630            // We allow creating a managed profile in the special case where there is only one user.
631            return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
632        }
633    }
634
635    private int getAliveUsersExcludingGuestsCountLocked() {
636        int aliveUserCount = 0;
637        final int totalUserCount = mUsers.size();
638        // Skip over users being removed
639        for (int i = 0; i < totalUserCount; i++) {
640            UserInfo user = mUsers.valueAt(i);
641            if (!mRemovingUserIds.get(user.id)
642                    && !user.isGuest() && !user.partial) {
643                aliveUserCount++;
644            }
645        }
646        return aliveUserCount;
647    }
648
649    /**
650     * Enforces that only the system UID or root's UID or apps that have the
651     * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
652     * permission can make certain calls to the UserManager.
653     *
654     * @param message used as message if SecurityException is thrown
655     * @throws SecurityException if the caller is not system or root
656     */
657    private static final void checkManageUsersPermission(String message) {
658        final int uid = Binder.getCallingUid();
659        if (uid != Process.SYSTEM_UID && uid != 0
660                && ActivityManager.checkComponentPermission(
661                        android.Manifest.permission.MANAGE_USERS,
662                        uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
663            throw new SecurityException("You need MANAGE_USERS permission to: " + message);
664        }
665    }
666
667    private static void checkSystemOrRoot(String message) {
668        final int uid = Binder.getCallingUid();
669        if (uid != Process.SYSTEM_UID && uid != 0) {
670            throw new SecurityException("Only system may call: " + message);
671        }
672    }
673
674    private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
675        try {
676            File dir = new File(mUsersDir, Integer.toString(info.id));
677            File file = new File(dir, USER_PHOTO_FILENAME);
678            File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
679            if (!dir.exists()) {
680                dir.mkdir();
681                FileUtils.setPermissions(
682                        dir.getPath(),
683                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
684                        -1, -1);
685            }
686            FileOutputStream os;
687            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
688                    && tmp.renameTo(file)) {
689                info.iconPath = file.getAbsolutePath();
690            }
691            try {
692                os.close();
693            } catch (IOException ioe) {
694                // What the ... !
695            }
696            tmp.delete();
697        } catch (FileNotFoundException e) {
698            Slog.w(LOG_TAG, "Error setting photo for user ", e);
699        }
700    }
701
702    /**
703     * Returns an array of user ids. This array is cached here for quick access, so do not modify or
704     * cache it elsewhere.
705     * @return the array of user ids.
706     */
707    public int[] getUserIds() {
708        synchronized (mPackagesLock) {
709            return mUserIds;
710        }
711    }
712
713    int[] getUserIdsLPr() {
714        return mUserIds;
715    }
716
717    private void readUserListLocked() {
718        if (!mUserListFile.exists()) {
719            fallbackToSingleUserLocked();
720            return;
721        }
722        FileInputStream fis = null;
723        AtomicFile userListFile = new AtomicFile(mUserListFile);
724        try {
725            fis = userListFile.openRead();
726            XmlPullParser parser = Xml.newPullParser();
727            parser.setInput(fis, StandardCharsets.UTF_8.name());
728            int type;
729            while ((type = parser.next()) != XmlPullParser.START_TAG
730                    && type != XmlPullParser.END_DOCUMENT) {
731                ;
732            }
733
734            if (type != XmlPullParser.START_TAG) {
735                Slog.e(LOG_TAG, "Unable to read user list");
736                fallbackToSingleUserLocked();
737                return;
738            }
739
740            mNextSerialNumber = -1;
741            if (parser.getName().equals(TAG_USERS)) {
742                String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
743                if (lastSerialNumber != null) {
744                    mNextSerialNumber = Integer.parseInt(lastSerialNumber);
745                }
746                String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
747                if (versionNumber != null) {
748                    mUserVersion = Integer.parseInt(versionNumber);
749                }
750            }
751
752            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
753                if (type == XmlPullParser.START_TAG) {
754                    final String name = parser.getName();
755                    if (name.equals(TAG_USER)) {
756                        String id = parser.getAttributeValue(null, ATTR_ID);
757                        UserInfo user = readUserLocked(Integer.parseInt(id));
758
759                        if (user != null) {
760                            mUsers.put(user.id, user);
761                            if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
762                                mNextSerialNumber = user.id + 1;
763                            }
764                        }
765                    } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
766                        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
767                                && type != XmlPullParser.END_TAG) {
768                            if (type == XmlPullParser.START_TAG) {
769                                if (parser.getName().equals(TAG_RESTRICTIONS)) {
770                                    readRestrictionsLocked(parser, mGuestRestrictions);
771                                }
772                                break;
773                            }
774                        }
775                    }
776                }
777            }
778            updateUserIdsLocked();
779            upgradeIfNecessaryLocked();
780        } catch (IOException ioe) {
781            fallbackToSingleUserLocked();
782        } catch (XmlPullParserException pe) {
783            fallbackToSingleUserLocked();
784        } finally {
785            if (fis != null) {
786                try {
787                    fis.close();
788                } catch (IOException e) {
789                }
790            }
791        }
792    }
793
794    /**
795     * Upgrade steps between versions, either for fixing bugs or changing the data format.
796     */
797    private void upgradeIfNecessaryLocked() {
798        int userVersion = mUserVersion;
799        if (userVersion < 1) {
800            // Assign a proper name for the owner, if not initialized correctly before
801            UserInfo user = mUsers.get(UserHandle.USER_OWNER);
802            if ("Primary".equals(user.name)) {
803                user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
804                scheduleWriteUserLocked(user);
805            }
806            userVersion = 1;
807        }
808
809        if (userVersion < 2) {
810            // Owner should be marked as initialized
811            UserInfo user = mUsers.get(UserHandle.USER_OWNER);
812            if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) {
813                user.flags |= UserInfo.FLAG_INITIALIZED;
814                scheduleWriteUserLocked(user);
815            }
816            userVersion = 2;
817        }
818
819
820        if (userVersion < 4) {
821            userVersion = 4;
822        }
823
824        if (userVersion < 5) {
825            initDefaultGuestRestrictions();
826            userVersion = 5;
827        }
828
829        if (userVersion < USER_VERSION) {
830            Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
831                    + USER_VERSION);
832        } else {
833            mUserVersion = userVersion;
834            writeUserListLocked();
835        }
836    }
837
838    private void fallbackToSingleUserLocked() {
839        // Create the primary user
840        UserInfo primary = new UserInfo(UserHandle.USER_OWNER,
841                mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
842                UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED);
843        mUsers.put(0, primary);
844        mNextSerialNumber = MIN_USER_ID;
845        mUserVersion = USER_VERSION;
846
847        Bundle restrictions = new Bundle();
848        mUserRestrictions.append(UserHandle.USER_OWNER, restrictions);
849
850        updateUserIdsLocked();
851        initDefaultGuestRestrictions();
852
853        writeUserListLocked();
854        writeUserLocked(primary);
855    }
856
857    private void scheduleWriteUserLocked(UserInfo userInfo) {
858        if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) {
859            Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo);
860            mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
861        }
862    }
863
864    /*
865     * Writes the user file in this format:
866     *
867     * <user flags="20039023" id="0">
868     *   <name>Primary</name>
869     * </user>
870     */
871    private void writeUserLocked(UserInfo userInfo) {
872        FileOutputStream fos = null;
873        AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX));
874        try {
875            fos = userFile.startWrite();
876            final BufferedOutputStream bos = new BufferedOutputStream(fos);
877
878            // XmlSerializer serializer = XmlUtils.serializerInstance();
879            final XmlSerializer serializer = new FastXmlSerializer();
880            serializer.setOutput(bos, StandardCharsets.UTF_8.name());
881            serializer.startDocument(null, true);
882            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
883
884            serializer.startTag(null, TAG_USER);
885            serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
886            serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
887            serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
888            serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
889            serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
890                    Long.toString(userInfo.lastLoggedInTime));
891            if (userInfo.iconPath != null) {
892                serializer.attribute(null,  ATTR_ICON_PATH, userInfo.iconPath);
893            }
894            if (userInfo.partial) {
895                serializer.attribute(null, ATTR_PARTIAL, "true");
896            }
897            if (userInfo.guestToRemove) {
898                serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
899            }
900            if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
901                serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
902                        Integer.toString(userInfo.profileGroupId));
903            }
904
905            serializer.startTag(null, TAG_NAME);
906            serializer.text(userInfo.name);
907            serializer.endTag(null, TAG_NAME);
908            Bundle restrictions = mUserRestrictions.get(userInfo.id);
909            if (restrictions != null) {
910                writeRestrictionsLocked(serializer, restrictions);
911            }
912            serializer.endTag(null, TAG_USER);
913
914            serializer.endDocument();
915            userFile.finishWrite(fos);
916        } catch (Exception ioe) {
917            Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
918            userFile.failWrite(fos);
919        }
920    }
921
922    /*
923     * Writes the user list file in this format:
924     *
925     * <users nextSerialNumber="3">
926     *   <user id="0"></user>
927     *   <user id="2"></user>
928     * </users>
929     */
930    private void writeUserListLocked() {
931        FileOutputStream fos = null;
932        AtomicFile userListFile = new AtomicFile(mUserListFile);
933        try {
934            fos = userListFile.startWrite();
935            final BufferedOutputStream bos = new BufferedOutputStream(fos);
936
937            // XmlSerializer serializer = XmlUtils.serializerInstance();
938            final XmlSerializer serializer = new FastXmlSerializer();
939            serializer.setOutput(bos, StandardCharsets.UTF_8.name());
940            serializer.startDocument(null, true);
941            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
942
943            serializer.startTag(null, TAG_USERS);
944            serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
945            serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
946
947            serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
948            writeRestrictionsLocked(serializer, mGuestRestrictions);
949            serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
950            for (int i = 0; i < mUsers.size(); i++) {
951                UserInfo user = mUsers.valueAt(i);
952                serializer.startTag(null, TAG_USER);
953                serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
954                serializer.endTag(null, TAG_USER);
955            }
956
957            serializer.endTag(null, TAG_USERS);
958
959            serializer.endDocument();
960            userListFile.finishWrite(fos);
961        } catch (Exception e) {
962            userListFile.failWrite(fos);
963            Slog.e(LOG_TAG, "Error writing user list");
964        }
965    }
966
967    private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions)
968            throws IOException {
969        serializer.startTag(null, TAG_RESTRICTIONS);
970        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
971        writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
972        writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
973        writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
974        writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
975        writeBoolean(serializer, restrictions,
976                UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
977        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
978        writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
979        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
980        writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
981        writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
982        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN);
983        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
984        writeBoolean(serializer, restrictions, UserManager.DISALLOW_NETWORK_RESET);
985        writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET);
986        writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER);
987        writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS);
988        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
989        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
990        writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL);
991        writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
992        writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
993        writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
994        writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
995        writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS);
996        writeBoolean(serializer, restrictions, UserManager.DISALLOW_FUN);
997        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
998        writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
999        writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
1000        writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER);
1001        writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT);
1002        writeBoolean(serializer, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING);
1003        serializer.endTag(null, TAG_RESTRICTIONS);
1004    }
1005
1006    private UserInfo readUserLocked(int id) {
1007        int flags = 0;
1008        int serialNumber = id;
1009        String name = null;
1010        String iconPath = null;
1011        long creationTime = 0L;
1012        long lastLoggedInTime = 0L;
1013        int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
1014        boolean partial = false;
1015        boolean guestToRemove = false;
1016        Bundle restrictions = new Bundle();
1017
1018        FileInputStream fis = null;
1019        try {
1020            AtomicFile userFile =
1021                    new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
1022            fis = userFile.openRead();
1023            XmlPullParser parser = Xml.newPullParser();
1024            parser.setInput(fis, StandardCharsets.UTF_8.name());
1025            int type;
1026            while ((type = parser.next()) != XmlPullParser.START_TAG
1027                    && type != XmlPullParser.END_DOCUMENT) {
1028                ;
1029            }
1030
1031            if (type != XmlPullParser.START_TAG) {
1032                Slog.e(LOG_TAG, "Unable to read user " + id);
1033                return null;
1034            }
1035
1036            if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
1037                int storedId = readIntAttribute(parser, ATTR_ID, -1);
1038                if (storedId != id) {
1039                    Slog.e(LOG_TAG, "User id does not match the file name");
1040                    return null;
1041                }
1042                serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1043                flags = readIntAttribute(parser, ATTR_FLAGS, 0);
1044                iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
1045                creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1046                lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
1047                profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1048                        UserInfo.NO_PROFILE_GROUP_ID);
1049                String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1050                if ("true".equals(valueString)) {
1051                    partial = true;
1052                }
1053                valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1054                if ("true".equals(valueString)) {
1055                    guestToRemove = true;
1056                }
1057
1058                int outerDepth = parser.getDepth();
1059                while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1060                       && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1061                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1062                        continue;
1063                    }
1064                    String tag = parser.getName();
1065                    if (TAG_NAME.equals(tag)) {
1066                        type = parser.next();
1067                        if (type == XmlPullParser.TEXT) {
1068                            name = parser.getText();
1069                        }
1070                    } else if (TAG_RESTRICTIONS.equals(tag)) {
1071                        readRestrictionsLocked(parser, restrictions);
1072                    }
1073                }
1074            }
1075
1076            UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
1077            userInfo.serialNumber = serialNumber;
1078            userInfo.creationTime = creationTime;
1079            userInfo.lastLoggedInTime = lastLoggedInTime;
1080            userInfo.partial = partial;
1081            userInfo.guestToRemove = guestToRemove;
1082            userInfo.profileGroupId = profileGroupId;
1083            mUserRestrictions.append(id, restrictions);
1084            return userInfo;
1085
1086        } catch (IOException ioe) {
1087        } catch (XmlPullParserException pe) {
1088        } finally {
1089            if (fis != null) {
1090                try {
1091                    fis.close();
1092                } catch (IOException e) {
1093                }
1094            }
1095        }
1096        return null;
1097    }
1098
1099    private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions)
1100            throws IOException {
1101        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
1102        readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
1103        readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS);
1104        readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
1105        readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
1106        readBoolean(parser, restrictions,
1107                UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
1108        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
1109        readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
1110        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
1111        readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER);
1112        readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES);
1113        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN);
1114        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING);
1115        readBoolean(parser, restrictions, UserManager.DISALLOW_NETWORK_RESET);
1116        readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET);
1117        readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER);
1118        readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS);
1119        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
1120        readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
1121        readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL);
1122        readBoolean(parser, restrictions,
1123                UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA);
1124        readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1125        readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME);
1126        readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS);
1127        readBoolean(parser, restrictions, UserManager.DISALLOW_SMS);
1128        readBoolean(parser, restrictions, UserManager.DISALLOW_FUN);
1129        readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS);
1130        readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE);
1131        readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM);
1132        readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER);
1133        readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT);
1134        readBoolean(parser, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING);
1135    }
1136
1137    private void readBoolean(XmlPullParser parser, Bundle restrictions,
1138            String restrictionKey) {
1139        String value = parser.getAttributeValue(null, restrictionKey);
1140        if (value != null) {
1141            restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value));
1142        }
1143    }
1144
1145    private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey)
1146            throws IOException {
1147        if (restrictions.containsKey(restrictionKey)) {
1148            xml.attribute(null, restrictionKey,
1149                    Boolean.toString(restrictions.getBoolean(restrictionKey)));
1150        }
1151    }
1152
1153    private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1154        String valueString = parser.getAttributeValue(null, attr);
1155        if (valueString == null) return defaultValue;
1156        try {
1157            return Integer.parseInt(valueString);
1158        } catch (NumberFormatException nfe) {
1159            return defaultValue;
1160        }
1161    }
1162
1163    private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1164        String valueString = parser.getAttributeValue(null, attr);
1165        if (valueString == null) return defaultValue;
1166        try {
1167            return Long.parseLong(valueString);
1168        } catch (NumberFormatException nfe) {
1169            return defaultValue;
1170        }
1171    }
1172
1173    private boolean isPackageInstalled(String pkg, int userId) {
1174        final ApplicationInfo info = mPm.getApplicationInfo(pkg,
1175                PackageManager.GET_UNINSTALLED_PACKAGES,
1176                userId);
1177        if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
1178            return false;
1179        }
1180        return true;
1181    }
1182
1183    /**
1184     * Removes all the restrictions files (res_<packagename>) for a given user.
1185     * Does not do any permissions checking.
1186     */
1187    private void cleanAppRestrictions(int userId) {
1188        synchronized (mPackagesLock) {
1189            File dir = Environment.getUserSystemDirectory(userId);
1190            String[] files = dir.list();
1191            if (files == null) return;
1192            for (String fileName : files) {
1193                if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) {
1194                    File resFile = new File(dir, fileName);
1195                    if (resFile.exists()) {
1196                        resFile.delete();
1197                    }
1198                }
1199            }
1200        }
1201    }
1202
1203    /**
1204     * Removes the app restrictions file for a specific package and user id, if it exists.
1205     */
1206    private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1207        synchronized (mPackagesLock) {
1208            File dir = Environment.getUserSystemDirectory(userId);
1209            File resFile = new File(dir, packageToRestrictionsFileName(pkg));
1210            if (resFile.exists()) {
1211                resFile.delete();
1212            }
1213        }
1214    }
1215
1216    @Override
1217    public UserInfo createProfileForUser(String name, int flags, int userId) {
1218        checkManageUsersPermission("Only the system can create users");
1219        if (userId != UserHandle.USER_OWNER) {
1220            Slog.w(LOG_TAG, "Only user owner can have profiles");
1221            return null;
1222        }
1223        return createUserInternal(name, flags, userId);
1224    }
1225
1226    @Override
1227    public UserInfo createUser(String name, int flags) {
1228        checkManageUsersPermission("Only the system can create users");
1229        return createUserInternal(name, flags, UserHandle.USER_NULL);
1230    }
1231
1232    private UserInfo createUserInternal(String name, int flags, int parentId) {
1233        if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1234                UserManager.DISALLOW_ADD_USER, false)) {
1235            Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1236            return null;
1237        }
1238        if (ActivityManager.isLowRamDeviceStatic()) {
1239            return null;
1240        }
1241        final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
1242        final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
1243        final long ident = Binder.clearCallingIdentity();
1244        UserInfo userInfo = null;
1245        final int userId;
1246        try {
1247            synchronized (mInstallLock) {
1248                synchronized (mPackagesLock) {
1249                    UserInfo parent = null;
1250                    if (parentId != UserHandle.USER_NULL) {
1251                        parent = getUserInfoLocked(parentId);
1252                        if (parent == null) return null;
1253                    }
1254                    if (isManagedProfile && !canAddMoreManagedProfiles()) {
1255                        return null;
1256                    }
1257                    if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
1258                        // If we're not adding a guest user or a managed profile and the limit has
1259                        // been reached, cannot add a user.
1260                        return null;
1261                    }
1262                    // If we're adding a guest and there already exists one, bail.
1263                    if (isGuest && findCurrentGuestUserLocked() != null) {
1264                        return null;
1265                    }
1266                    userId = getNextAvailableIdLocked();
1267                    userInfo = new UserInfo(userId, name, null, flags);
1268                    userInfo.serialNumber = mNextSerialNumber++;
1269                    long now = System.currentTimeMillis();
1270                    userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
1271                    userInfo.partial = true;
1272                    Environment.getUserSystemDirectory(userInfo.id).mkdirs();
1273                    mUsers.put(userId, userInfo);
1274                    writeUserListLocked();
1275                    if (parent != null) {
1276                        if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1277                            parent.profileGroupId = parent.id;
1278                            scheduleWriteUserLocked(parent);
1279                        }
1280                        userInfo.profileGroupId = parent.profileGroupId;
1281                    }
1282                    final StorageManager storage = mContext.getSystemService(StorageManager.class);
1283                    for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
1284                        final String volumeUuid = vol.getFsUuid();
1285                        try {
1286                            final File userDir = Environment.getDataUserDirectory(volumeUuid,
1287                                    userId);
1288                            prepareUserDirectory(mContext, volumeUuid, userId);
1289                            enforceSerialNumber(userDir, userInfo.serialNumber);
1290                        } catch (IOException e) {
1291                            Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e);
1292                        }
1293                    }
1294                    mPm.createNewUserLILPw(userId);
1295                    userInfo.partial = false;
1296                    scheduleWriteUserLocked(userInfo);
1297                    updateUserIdsLocked();
1298                    Bundle restrictions = new Bundle();
1299                    mUserRestrictions.append(userId, restrictions);
1300                }
1301            }
1302            mPm.newUserCreated(userId);
1303            if (userInfo != null) {
1304                Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1305                addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id);
1306                mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1307                        android.Manifest.permission.MANAGE_USERS);
1308            }
1309        } finally {
1310            Binder.restoreCallingIdentity(ident);
1311        }
1312        return userInfo;
1313    }
1314
1315    private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) {
1316        int count = 0;
1317        for (int i = mUsers.size() - 1; i >= 0; i--) {
1318            UserInfo user = mUsers.valueAt(i);
1319            if (!excludeDying || !mRemovingUserIds.get(user.id)) {
1320                if ((user.flags & flags) != 0) {
1321                    count++;
1322                }
1323            }
1324        }
1325        return count;
1326    }
1327
1328    /**
1329     * Find the current guest user. If the Guest user is partial,
1330     * then do not include it in the results as it is about to die.
1331     * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to
1332     * the special handling of Guests being removed.
1333     */
1334    private UserInfo findCurrentGuestUserLocked() {
1335        final int size = mUsers.size();
1336        for (int i = 0; i < size; i++) {
1337            final UserInfo user = mUsers.valueAt(i);
1338            if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1339                return user;
1340            }
1341        }
1342        return null;
1343    }
1344
1345    /**
1346     * Mark this guest user for deletion to allow us to create another guest
1347     * and switch to that user before actually removing this guest.
1348     * @param userHandle the userid of the current guest
1349     * @return whether the user could be marked for deletion
1350     */
1351    public boolean markGuestForDeletion(int userHandle) {
1352        checkManageUsersPermission("Only the system can remove users");
1353        if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1354                UserManager.DISALLOW_REMOVE_USER, false)) {
1355            Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1356            return false;
1357        }
1358
1359        long ident = Binder.clearCallingIdentity();
1360        try {
1361            final UserInfo user;
1362            synchronized (mPackagesLock) {
1363                user = mUsers.get(userHandle);
1364                if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1365                    return false;
1366                }
1367                if (!user.isGuest()) {
1368                    return false;
1369                }
1370                // We set this to a guest user that is to be removed. This is a temporary state
1371                // where we are allowed to add new Guest users, even if this one is still not
1372                // removed. This user will still show up in getUserInfo() calls.
1373                // If we don't get around to removing this Guest user, it will be purged on next
1374                // startup.
1375                user.guestToRemove = true;
1376                // Mark it as disabled, so that it isn't returned any more when
1377                // profiles are queried.
1378                user.flags |= UserInfo.FLAG_DISABLED;
1379                writeUserLocked(user);
1380            }
1381        } finally {
1382            Binder.restoreCallingIdentity(ident);
1383        }
1384        return true;
1385    }
1386
1387    /**
1388     * Removes a user and all data directories created for that user. This method should be called
1389     * after the user's processes have been terminated.
1390     * @param userHandle the user's id
1391     */
1392    public boolean removeUser(int userHandle) {
1393        checkManageUsersPermission("Only the system can remove users");
1394        if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
1395                UserManager.DISALLOW_REMOVE_USER, false)) {
1396            Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
1397            return false;
1398        }
1399
1400        long ident = Binder.clearCallingIdentity();
1401        try {
1402            final UserInfo user;
1403            synchronized (mPackagesLock) {
1404                user = mUsers.get(userHandle);
1405                if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) {
1406                    return false;
1407                }
1408
1409                // We remember deleted user IDs to prevent them from being
1410                // reused during the current boot; they can still be reused
1411                // after a reboot.
1412                mRemovingUserIds.put(userHandle, true);
1413
1414                try {
1415                    mAppOpsService.removeUser(userHandle);
1416                } catch (RemoteException e) {
1417                    Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
1418                }
1419                // Set this to a partially created user, so that the user will be purged
1420                // on next startup, in case the runtime stops now before stopping and
1421                // removing the user completely.
1422                user.partial = true;
1423                // Mark it as disabled, so that it isn't returned any more when
1424                // profiles are queried.
1425                user.flags |= UserInfo.FLAG_DISABLED;
1426                writeUserLocked(user);
1427            }
1428
1429            if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
1430                    && user.isManagedProfile()) {
1431                // Send broadcast to notify system that the user removed was a
1432                // managed user.
1433                sendProfileRemovedBroadcast(user.profileGroupId, user.id);
1434            }
1435
1436            if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
1437            int res;
1438            try {
1439                res = ActivityManagerNative.getDefault().stopUser(userHandle,
1440                        new IStopUserCallback.Stub() {
1441                            @Override
1442                            public void userStopped(int userId) {
1443                                finishRemoveUser(userId);
1444                            }
1445                            @Override
1446                            public void userStopAborted(int userId) {
1447                            }
1448                        });
1449            } catch (RemoteException e) {
1450                return false;
1451            }
1452            return res == ActivityManager.USER_OP_SUCCESS;
1453        } finally {
1454            Binder.restoreCallingIdentity(ident);
1455        }
1456    }
1457
1458    void finishRemoveUser(final int userHandle) {
1459        if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
1460        // Let other services shutdown any activity and clean up their state before completely
1461        // wiping the user's system directory and removing from the user list
1462        long ident = Binder.clearCallingIdentity();
1463        try {
1464            Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
1465            addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
1466            mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
1467                    android.Manifest.permission.MANAGE_USERS,
1468
1469                    new BroadcastReceiver() {
1470                        @Override
1471                        public void onReceive(Context context, Intent intent) {
1472                            if (DBG) {
1473                                Slog.i(LOG_TAG,
1474                                        "USER_REMOVED broadcast sent, cleaning up user data "
1475                                        + userHandle);
1476                            }
1477                            new Thread() {
1478                                public void run() {
1479                                    synchronized (mInstallLock) {
1480                                        synchronized (mPackagesLock) {
1481                                            removeUserStateLocked(userHandle);
1482                                        }
1483                                    }
1484                                }
1485                            }.start();
1486                        }
1487                    },
1488
1489                    null, Activity.RESULT_OK, null, null);
1490        } finally {
1491            Binder.restoreCallingIdentity(ident);
1492        }
1493    }
1494
1495    private void removeUserStateLocked(final int userHandle) {
1496        mContext.getSystemService(StorageManager.class)
1497            .deleteUserKey(userHandle);
1498        // Cleanup package manager settings
1499        mPm.cleanUpUserLILPw(this, userHandle);
1500
1501        // Remove this user from the list
1502        mUsers.remove(userHandle);
1503        // Remove user file
1504        AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
1505        userFile.delete();
1506        // Update the user list
1507        writeUserListLocked();
1508        updateUserIdsLocked();
1509        removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
1510    }
1511
1512    private void removeDirectoryRecursive(File parent) {
1513        if (parent.isDirectory()) {
1514            String[] files = parent.list();
1515            for (String filename : files) {
1516                File child = new File(parent, filename);
1517                removeDirectoryRecursive(child);
1518            }
1519        }
1520        parent.delete();
1521    }
1522
1523    private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
1524        Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
1525        managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
1526                Intent.FLAG_RECEIVER_FOREGROUND);
1527        managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
1528        mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
1529    }
1530
1531    @Override
1532    public Bundle getApplicationRestrictions(String packageName) {
1533        return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
1534    }
1535
1536    @Override
1537    public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
1538        if (UserHandle.getCallingUserId() != userId
1539                || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
1540            checkManageUsersPermission("Only system can get restrictions for other users/apps");
1541        }
1542        synchronized (mPackagesLock) {
1543            // Read the restrictions from XML
1544            return readApplicationRestrictionsLocked(packageName, userId);
1545        }
1546    }
1547
1548    @Override
1549    public void setApplicationRestrictions(String packageName, Bundle restrictions,
1550            int userId) {
1551        if (UserHandle.getCallingUserId() != userId
1552                || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
1553            checkManageUsersPermission("Only system can set restrictions for other users/apps");
1554        }
1555        synchronized (mPackagesLock) {
1556            if (restrictions == null || restrictions.isEmpty()) {
1557                cleanAppRestrictionsForPackage(packageName, userId);
1558            } else {
1559                // Write the restrictions to XML
1560                writeApplicationRestrictionsLocked(packageName, restrictions, userId);
1561            }
1562        }
1563
1564        if (isPackageInstalled(packageName, userId)) {
1565            // Notify package of changes via an intent - only sent to explicitly registered receivers.
1566            Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
1567            changeIntent.setPackage(packageName);
1568            changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1569            mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId));
1570        }
1571    }
1572
1573    @Override
1574    public void removeRestrictions() {
1575        checkManageUsersPermission("Only system can remove restrictions");
1576        final int userHandle = UserHandle.getCallingUserId();
1577        removeRestrictionsForUser(userHandle, true);
1578    }
1579
1580    private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) {
1581        synchronized (mPackagesLock) {
1582            // Remove all user restrictions
1583            setUserRestrictions(new Bundle(), userHandle);
1584            // Remove any app restrictions
1585            cleanAppRestrictions(userHandle);
1586        }
1587        if (unhideApps) {
1588            unhideAllInstalledAppsForUser(userHandle);
1589        }
1590    }
1591
1592    private void unhideAllInstalledAppsForUser(final int userHandle) {
1593        mHandler.post(new Runnable() {
1594            @Override
1595            public void run() {
1596                List<ApplicationInfo> apps =
1597                        mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES,
1598                                userHandle).getList();
1599                final long ident = Binder.clearCallingIdentity();
1600                try {
1601                    for (ApplicationInfo appInfo : apps) {
1602                        if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0
1603                                && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN)
1604                                        != 0) {
1605                            mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false,
1606                                    userHandle);
1607                        }
1608                    }
1609                } finally {
1610                    Binder.restoreCallingIdentity(ident);
1611                }
1612            }
1613        });
1614    }
1615    private int getUidForPackage(String packageName) {
1616        long ident = Binder.clearCallingIdentity();
1617        try {
1618            return mContext.getPackageManager().getApplicationInfo(packageName,
1619                    PackageManager.GET_UNINSTALLED_PACKAGES).uid;
1620        } catch (NameNotFoundException nnfe) {
1621            return -1;
1622        } finally {
1623            Binder.restoreCallingIdentity(ident);
1624        }
1625    }
1626
1627    private Bundle readApplicationRestrictionsLocked(String packageName,
1628            int userId) {
1629        AtomicFile restrictionsFile =
1630                new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
1631                        packageToRestrictionsFileName(packageName)));
1632        return readApplicationRestrictionsLocked(restrictionsFile);
1633    }
1634
1635    @VisibleForTesting
1636    static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) {
1637        final Bundle restrictions = new Bundle();
1638        final ArrayList<String> values = new ArrayList<>();
1639        if (!restrictionsFile.getBaseFile().exists()) {
1640            return restrictions;
1641        }
1642
1643        FileInputStream fis = null;
1644        try {
1645            fis = restrictionsFile.openRead();
1646            XmlPullParser parser = Xml.newPullParser();
1647            parser.setInput(fis, StandardCharsets.UTF_8.name());
1648            XmlUtils.nextElement(parser);
1649            if (parser.getEventType() != XmlPullParser.START_TAG) {
1650                Slog.e(LOG_TAG, "Unable to read restrictions file "
1651                        + restrictionsFile.getBaseFile());
1652                return restrictions;
1653            }
1654            while (parser.next() != XmlPullParser.END_DOCUMENT) {
1655                readEntry(restrictions, values, parser);
1656            }
1657        } catch (IOException|XmlPullParserException e) {
1658            Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
1659        } finally {
1660            IoUtils.closeQuietly(fis);
1661        }
1662        return restrictions;
1663    }
1664
1665    private static void readEntry(Bundle restrictions, ArrayList<String> values,
1666            XmlPullParser parser) throws XmlPullParserException, IOException {
1667        int type = parser.getEventType();
1668        if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
1669            String key = parser.getAttributeValue(null, ATTR_KEY);
1670            String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
1671            String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
1672            if (multiple != null) {
1673                values.clear();
1674                int count = Integer.parseInt(multiple);
1675                while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1676                    if (type == XmlPullParser.START_TAG
1677                            && parser.getName().equals(TAG_VALUE)) {
1678                        values.add(parser.nextText().trim());
1679                        count--;
1680                    }
1681                }
1682                String [] valueStrings = new String[values.size()];
1683                values.toArray(valueStrings);
1684                restrictions.putStringArray(key, valueStrings);
1685            } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
1686                restrictions.putBundle(key, readBundleEntry(parser, values));
1687            } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
1688                final int outerDepth = parser.getDepth();
1689                ArrayList<Bundle> bundleList = new ArrayList<>();
1690                while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1691                    Bundle childBundle = readBundleEntry(parser, values);
1692                    bundleList.add(childBundle);
1693                }
1694                restrictions.putParcelableArray(key,
1695                        bundleList.toArray(new Bundle[bundleList.size()]));
1696            } else {
1697                String value = parser.nextText().trim();
1698                if (ATTR_TYPE_BOOLEAN.equals(valType)) {
1699                    restrictions.putBoolean(key, Boolean.parseBoolean(value));
1700                } else if (ATTR_TYPE_INTEGER.equals(valType)) {
1701                    restrictions.putInt(key, Integer.parseInt(value));
1702                } else {
1703                    restrictions.putString(key, value);
1704                }
1705            }
1706        }
1707    }
1708
1709    private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
1710            throws IOException, XmlPullParserException {
1711        Bundle childBundle = new Bundle();
1712        final int outerDepth = parser.getDepth();
1713        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1714            readEntry(childBundle, values, parser);
1715        }
1716        return childBundle;
1717    }
1718
1719    private void writeApplicationRestrictionsLocked(String packageName,
1720            Bundle restrictions, int userId) {
1721        AtomicFile restrictionsFile = new AtomicFile(
1722                new File(Environment.getUserSystemDirectory(userId),
1723                        packageToRestrictionsFileName(packageName)));
1724        writeApplicationRestrictionsLocked(restrictions, restrictionsFile);
1725    }
1726
1727    @VisibleForTesting
1728    static void writeApplicationRestrictionsLocked(Bundle restrictions,
1729            AtomicFile restrictionsFile) {
1730        FileOutputStream fos = null;
1731        try {
1732            fos = restrictionsFile.startWrite();
1733            final BufferedOutputStream bos = new BufferedOutputStream(fos);
1734
1735            final XmlSerializer serializer = new FastXmlSerializer();
1736            serializer.setOutput(bos, StandardCharsets.UTF_8.name());
1737            serializer.startDocument(null, true);
1738            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1739
1740            serializer.startTag(null, TAG_RESTRICTIONS);
1741            writeBundle(restrictions, serializer);
1742            serializer.endTag(null, TAG_RESTRICTIONS);
1743
1744            serializer.endDocument();
1745            restrictionsFile.finishWrite(fos);
1746        } catch (Exception e) {
1747            restrictionsFile.failWrite(fos);
1748            Slog.e(LOG_TAG, "Error writing application restrictions list", e);
1749        }
1750    }
1751
1752    private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
1753            throws IOException {
1754        for (String key : restrictions.keySet()) {
1755            Object value = restrictions.get(key);
1756            serializer.startTag(null, TAG_ENTRY);
1757            serializer.attribute(null, ATTR_KEY, key);
1758
1759            if (value instanceof Boolean) {
1760                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
1761                serializer.text(value.toString());
1762            } else if (value instanceof Integer) {
1763                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
1764                serializer.text(value.toString());
1765            } else if (value == null || value instanceof String) {
1766                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
1767                serializer.text(value != null ? (String) value : "");
1768            } else if (value instanceof Bundle) {
1769                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1770                writeBundle((Bundle) value, serializer);
1771            } else if (value instanceof Parcelable[]) {
1772                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
1773                Parcelable[] array = (Parcelable[]) value;
1774                for (Parcelable parcelable : array) {
1775                    if (!(parcelable instanceof Bundle)) {
1776                        throw new IllegalArgumentException("bundle-array can only hold Bundles");
1777                    }
1778                    serializer.startTag(null, TAG_ENTRY);
1779                    serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
1780                    writeBundle((Bundle) parcelable, serializer);
1781                    serializer.endTag(null, TAG_ENTRY);
1782                }
1783            } else {
1784                serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
1785                String[] values = (String[]) value;
1786                serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
1787                for (String choice : values) {
1788                    serializer.startTag(null, TAG_VALUE);
1789                    serializer.text(choice != null ? choice : "");
1790                    serializer.endTag(null, TAG_VALUE);
1791                }
1792            }
1793            serializer.endTag(null, TAG_ENTRY);
1794        }
1795    }
1796
1797    @Override
1798    public int getUserSerialNumber(int userHandle) {
1799        synchronized (mPackagesLock) {
1800            if (!exists(userHandle)) return -1;
1801            return getUserInfoLocked(userHandle).serialNumber;
1802        }
1803    }
1804
1805    @Override
1806    public int getUserHandle(int userSerialNumber) {
1807        synchronized (mPackagesLock) {
1808            for (int userId : mUserIds) {
1809                UserInfo info = getUserInfoLocked(userId);
1810                if (info != null && info.serialNumber == userSerialNumber) return userId;
1811            }
1812            // Not found
1813            return -1;
1814        }
1815    }
1816
1817    @Override
1818    public long getUserCreationTime(int userHandle) {
1819        int callingUserId = UserHandle.getCallingUserId();
1820        UserInfo userInfo = null;
1821        synchronized (mPackagesLock) {
1822            if (callingUserId == userHandle) {
1823                userInfo = getUserInfoLocked(userHandle);
1824            } else {
1825                UserInfo parent = getProfileParentLocked(userHandle);
1826                if (parent != null && parent.id == callingUserId) {
1827                    userInfo = getUserInfoLocked(userHandle);
1828                }
1829            }
1830        }
1831        if (userInfo == null) {
1832            throw new SecurityException("userHandle can only be the calling user or a managed "
1833                    + "profile associated with this user");
1834        }
1835        return userInfo.creationTime;
1836    }
1837
1838    /**
1839     * Caches the list of user ids in an array, adjusting the array size when necessary.
1840     */
1841    private void updateUserIdsLocked() {
1842        int num = 0;
1843        for (int i = 0; i < mUsers.size(); i++) {
1844            if (!mUsers.valueAt(i).partial) {
1845                num++;
1846            }
1847        }
1848        final int[] newUsers = new int[num];
1849        int n = 0;
1850        for (int i = 0; i < mUsers.size(); i++) {
1851            if (!mUsers.valueAt(i).partial) {
1852                newUsers[n++] = mUsers.keyAt(i);
1853            }
1854        }
1855        mUserIds = newUsers;
1856    }
1857
1858    /**
1859     * Make a note of the last started time of a user and do some cleanup.
1860     * @param userId the user that was just foregrounded
1861     */
1862    public void onUserForeground(int userId) {
1863        synchronized (mPackagesLock) {
1864            UserInfo user = mUsers.get(userId);
1865            long now = System.currentTimeMillis();
1866            if (user == null || user.partial) {
1867                Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
1868                return;
1869            }
1870            if (now > EPOCH_PLUS_30_YEARS) {
1871                user.lastLoggedInTime = now;
1872                scheduleWriteUserLocked(user);
1873            }
1874        }
1875    }
1876
1877    /**
1878     * Returns the next available user id, filling in any holes in the ids.
1879     * TODO: May not be a good idea to recycle ids, in case it results in confusion
1880     * for data and battery stats collection, or unexpected cross-talk.
1881     * @return
1882     */
1883    private int getNextAvailableIdLocked() {
1884        synchronized (mPackagesLock) {
1885            int i = MIN_USER_ID;
1886            while (i < Integer.MAX_VALUE) {
1887                if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
1888                    break;
1889                }
1890                i++;
1891            }
1892            return i;
1893        }
1894    }
1895
1896    private String packageToRestrictionsFileName(String packageName) {
1897        return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
1898    }
1899
1900    /**
1901     * Create new {@code /data/user/[id]} directory and sets default
1902     * permissions.
1903     */
1904    public static void prepareUserDirectory(Context context, String volumeUuid, int userId) {
1905        final StorageManager storage = context.getSystemService(StorageManager.class);
1906        final File userDir = Environment.getDataUserDirectory(volumeUuid, userId);
1907        storage.createNewUserDir(userId, userDir);
1908    }
1909
1910    /**
1911     * Enforce that serial number stored in user directory inode matches the
1912     * given expected value. Gracefully sets the serial number if currently
1913     * undefined.
1914     *
1915     * @throws IOException when problem extracting serial number, or serial
1916     *             number is mismatched.
1917     */
1918    public static void enforceSerialNumber(File file, int serialNumber) throws IOException {
1919        final int foundSerial = getSerialNumber(file);
1920        Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial);
1921
1922        if (foundSerial == -1) {
1923            Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid");
1924            try {
1925                setSerialNumber(file, serialNumber);
1926            } catch (IOException e) {
1927                Slog.w(LOG_TAG, "Failed to set serial number on " + file, e);
1928            }
1929
1930        } else if (foundSerial != serialNumber) {
1931            throw new IOException("Found serial number " + foundSerial
1932                    + " doesn't match expected " + serialNumber);
1933        }
1934    }
1935
1936    /**
1937     * Set serial number stored in user directory inode.
1938     *
1939     * @throws IOException if serial number was already set
1940     */
1941    private static void setSerialNumber(File file, int serialNumber)
1942            throws IOException {
1943        try {
1944            final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8);
1945            Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE);
1946        } catch (ErrnoException e) {
1947            throw e.rethrowAsIOException();
1948        }
1949    }
1950
1951    /**
1952     * Return serial number stored in user directory inode.
1953     *
1954     * @return parsed serial number, or -1 if not set
1955     */
1956    private static int getSerialNumber(File file) throws IOException {
1957        try {
1958            final byte[] buf = new byte[256];
1959            final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf);
1960            final String serial = new String(buf, 0, len);
1961            try {
1962                return Integer.parseInt(serial);
1963            } catch (NumberFormatException e) {
1964                throw new IOException("Bad serial number: " + serial);
1965            }
1966        } catch (ErrnoException e) {
1967            if (e.errno == OsConstants.ENODATA) {
1968                return -1;
1969            } else {
1970                throw e.rethrowAsIOException();
1971            }
1972        }
1973    }
1974
1975    @Override
1976    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1977        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1978                != PackageManager.PERMISSION_GRANTED) {
1979            pw.println("Permission Denial: can't dump UserManager from from pid="
1980                    + Binder.getCallingPid()
1981                    + ", uid=" + Binder.getCallingUid()
1982                    + " without permission "
1983                    + android.Manifest.permission.DUMP);
1984            return;
1985        }
1986
1987        long now = System.currentTimeMillis();
1988        StringBuilder sb = new StringBuilder();
1989        synchronized (mPackagesLock) {
1990            pw.println("Users:");
1991            for (int i = 0; i < mUsers.size(); i++) {
1992                UserInfo user = mUsers.valueAt(i);
1993                if (user == null) continue;
1994                pw.print("  "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber);
1995                if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> ");
1996                if (user.partial) pw.print(" <partial>");
1997                pw.println();
1998                pw.print("    Created: ");
1999                if (user.creationTime == 0) {
2000                    pw.println("<unknown>");
2001                } else {
2002                    sb.setLength(0);
2003                    TimeUtils.formatDuration(now - user.creationTime, sb);
2004                    sb.append(" ago");
2005                    pw.println(sb);
2006                }
2007                pw.print("    Last logged in: ");
2008                if (user.lastLoggedInTime == 0) {
2009                    pw.println("<unknown>");
2010                } else {
2011                    sb.setLength(0);
2012                    TimeUtils.formatDuration(now - user.lastLoggedInTime, sb);
2013                    sb.append(" ago");
2014                    pw.println(sb);
2015                }
2016            }
2017        }
2018    }
2019
2020    final class MainHandler extends Handler {
2021
2022        @Override
2023        public void handleMessage(Message msg) {
2024            switch (msg.what) {
2025                case WRITE_USER_MSG:
2026                    removeMessages(WRITE_USER_MSG, msg.obj);
2027                    synchronized (mPackagesLock) {
2028                        int userId = ((UserInfo) msg.obj).id;
2029                        UserInfo userInfo = mUsers.get(userId);
2030                        if (userInfo != null) {
2031                            writeUserLocked(userInfo);
2032                        }
2033                    }
2034            }
2035        }
2036    }
2037
2038    /**
2039     * @param userId
2040     * @return whether the user has been initialized yet
2041     */
2042    boolean isInitialized(int userId) {
2043        return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0;
2044    }
2045}
2046