1/*
2 * Copyright (C) 2014 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.launcher3.compat;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.pm.PackageManager;
22import android.os.UserHandle;
23import android.os.UserManager;
24import android.util.ArrayMap;
25import com.android.launcher3.util.LongArrayMap;
26import com.android.launcher3.util.ManagedProfileHeuristic;
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30
31public class UserManagerCompatVL extends UserManagerCompat {
32    private static final String USER_CREATION_TIME_KEY = "user_creation_time_";
33
34    protected final UserManager mUserManager;
35    private final PackageManager mPm;
36    private final Context mContext;
37
38    protected LongArrayMap<UserHandle> mUsers;
39    // Create a separate reverse map as LongArrayMap.indexOfValue checks if objects are same
40    // and not {@link Object#equals}
41    protected ArrayMap<UserHandle, Long> mUserToSerialMap;
42
43    UserManagerCompatVL(Context context) {
44        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
45        mPm = context.getPackageManager();
46        mContext = context;
47    }
48
49    @Override
50    public long getSerialNumberForUser(UserHandle user) {
51        synchronized (this) {
52            if (mUserToSerialMap != null) {
53                Long serial = mUserToSerialMap.get(user);
54                return serial == null ? 0 : serial;
55            }
56        }
57        return mUserManager.getSerialNumberForUser(user);
58    }
59
60    @Override
61    public UserHandle getUserForSerialNumber(long serialNumber) {
62        synchronized (this) {
63            if (mUsers != null) {
64                return mUsers.get(serialNumber);
65            }
66        }
67        return mUserManager.getUserForSerialNumber(serialNumber);
68    }
69
70    @Override
71    public boolean isQuietModeEnabled(UserHandle user) {
72        return false;
73    }
74
75    @Override
76    public boolean isUserUnlocked(UserHandle user) {
77        return true;
78    }
79
80    @Override
81    public boolean isDemoUser() {
82        return false;
83    }
84
85    @Override
86    public void enableAndResetCache() {
87        synchronized (this) {
88            mUsers = new LongArrayMap<>();
89            mUserToSerialMap = new ArrayMap<>();
90            List<UserHandle> users = mUserManager.getUserProfiles();
91            if (users != null) {
92                for (UserHandle user : users) {
93                    long serial = mUserManager.getSerialNumberForUser(user);
94                    mUsers.put(serial, user);
95                    mUserToSerialMap.put(user, serial);
96                }
97            }
98        }
99    }
100
101    @Override
102    public List<UserHandle> getUserProfiles() {
103        synchronized (this) {
104            if (mUsers != null) {
105                return new ArrayList<>(mUserToSerialMap.keySet());
106            }
107        }
108
109        List<UserHandle> users = mUserManager.getUserProfiles();
110        return users == null ? Collections.<UserHandle>emptyList() : users;
111    }
112
113    @Override
114    public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
115        if (user == null) {
116            return label;
117        }
118        return mPm.getUserBadgedLabel(label, user);
119    }
120
121    @Override
122    public long getUserCreationTime(UserHandle user) {
123        SharedPreferences prefs = ManagedProfileHeuristic.prefs(mContext);
124        String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user);
125        if (!prefs.contains(key)) {
126            prefs.edit().putLong(key, System.currentTimeMillis()).apply();
127        }
128        return prefs.getLong(key, 0);
129    }
130}
131
132