1ed13187a745866483139e2878037e1f8427ce567Kenny Guy/*
2ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Copyright (C) 2014 The Android Open Source Project
3ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
4ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Licensed under the Apache License, Version 2.0 (the "License");
5ed13187a745866483139e2878037e1f8427ce567Kenny Guy * you may not use this file except in compliance with the License.
6ed13187a745866483139e2878037e1f8427ce567Kenny Guy * You may obtain a copy of the License at
7ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
8ed13187a745866483139e2878037e1f8427ce567Kenny Guy *      http://www.apache.org/licenses/LICENSE-2.0
9ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
10ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Unless required by applicable law or agreed to in writing, software
11ed13187a745866483139e2878037e1f8427ce567Kenny Guy * distributed under the License is distributed on an "AS IS" BASIS,
12ed13187a745866483139e2878037e1f8427ce567Kenny Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ed13187a745866483139e2878037e1f8427ce567Kenny Guy * See the License for the specific language governing permissions and
14ed13187a745866483139e2878037e1f8427ce567Kenny Guy * limitations under the License.
15ed13187a745866483139e2878037e1f8427ce567Kenny Guy */
16ed13187a745866483139e2878037e1f8427ce567Kenny Guy
17ed13187a745866483139e2878037e1f8427ce567Kenny Guypackage com.android.launcher3.compat;
18ed13187a745866483139e2878037e1f8427ce567Kenny Guy
197066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.annotation.TargetApi;
20ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.Context;
2118bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyalimport android.content.SharedPreferences;
22f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokaniimport android.content.pm.PackageManager;
237066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.os.Build;
24ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.os.UserHandle;
25823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
26ad0c2f28ceb750646e0132a01d393f5115cbfca9Sunny Goyalimport com.android.launcher3.Utilities;
27823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyalimport com.android.launcher3.util.LongArrayMap;
28823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
29ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport java.util.ArrayList;
30c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guyimport java.util.Collections;
31a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyalimport java.util.HashMap;
32ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport java.util.List;
33ed13187a745866483139e2878037e1f8427ce567Kenny Guy
347066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal@TargetApi(Build.VERSION_CODES.LOLLIPOP)
35ed13187a745866483139e2878037e1f8427ce567Kenny Guypublic class UserManagerCompatVL extends UserManagerCompatV17 {
3618bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    private static final String USER_CREATION_TIME_KEY = "user_creation_time_";
3718bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal
38f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokani    private final PackageManager mPm;
3918bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    private final Context mContext;
40ed13187a745866483139e2878037e1f8427ce567Kenny Guy
41ed13187a745866483139e2878037e1f8427ce567Kenny Guy    UserManagerCompatVL(Context context) {
42ed13187a745866483139e2878037e1f8427ce567Kenny Guy        super(context);
43f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokani        mPm = context.getPackageManager();
4418bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        mContext = context;
45ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
46ed13187a745866483139e2878037e1f8427ce567Kenny Guy
476075170b838bfe7a040bbff25c2c22859b7d6ee5Dharmesh Mokani    @Override
48823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    public void enableAndResetCache() {
49823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
50a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUsers = new LongArrayMap<>();
51a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUserToSerialMap = new HashMap<>();
52823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            List<UserHandle> users = mUserManager.getUserProfiles();
53823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            if (users != null) {
54823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                for (UserHandle user : users) {
55a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    long serial = mUserManager.getSerialNumberForUser(user);
56a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    UserHandleCompat userCompat = UserHandleCompat.fromUser(user);
57a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    mUsers.put(serial, userCompat);
58a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    mUserToSerialMap.put(userCompat, serial);
59823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                }
60823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
61823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
62823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    }
63823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
64823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    @Override
65ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public List<UserHandleCompat> getUserProfiles() {
66823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
67823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            if (mUsers != null) {
68823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                List<UserHandleCompat> users = new ArrayList<>();
69a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                users.addAll(mUserToSerialMap.keySet());
70823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                return users;
71823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
72823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
73823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
74c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        List<UserHandle> users = mUserManager.getUserProfiles();
75c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        if (users == null) {
76b1622cc30f2fd9b579cb918083e063685950df92Sunny Goyal            return Collections.emptyList();
77ed13187a745866483139e2878037e1f8427ce567Kenny Guy        }
78c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        ArrayList<UserHandleCompat> compatUsers = new ArrayList<UserHandleCompat>(
79c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy                users.size());
80c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        for (UserHandle user : users) {
81c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            compatUsers.add(UserHandleCompat.fromUser(user));
82c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        }
83c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        return compatUsers;
84ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
85ed13187a745866483139e2878037e1f8427ce567Kenny Guy
866075170b838bfe7a040bbff25c2c22859b7d6ee5Dharmesh Mokani    @Override
87d6fe52636dcaa96ec1e10ce2daebe98b820c9739Kenny Guy    public CharSequence getBadgedLabelForUser(CharSequence label, UserHandleCompat user) {
88c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        if (user == null) {
89c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            return label;
90ed13187a745866483139e2878037e1f8427ce567Kenny Guy        }
913d0854e4b8c0a6daaa80ac14c6bc7572a4e90916Dharmesh Mokani        return mPm.getUserBadgedLabel(label, user.getUser());
92ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
9318bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal
9418bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    @Override
9518bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    public long getUserCreationTime(UserHandleCompat user) {
96f725824fa2091cab44cf1bcbfe5b5b8d06475560Sunny Goyal        SharedPreferences prefs = Utilities.getPrefs(mContext);
9718bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user);
9818bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        if (!prefs.contains(key)) {
9918bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal            prefs.edit().putLong(key, System.currentTimeMillis()).apply();
10018bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        }
10118bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        return prefs.getLong(key, 0);
10218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    }
103ed13187a745866483139e2878037e1f8427ce567Kenny Guy}
104ed13187a745866483139e2878037e1f8427ce567Kenny Guy
105