1ed13187a745866483139e2878037e1f8427ce567Kenny Guy
2ed13187a745866483139e2878037e1f8427ce567Kenny Guy/*
3ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Copyright (C) 2014 The Android Open Source Project
4ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
5ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Licensed under the Apache License, Version 2.0 (the "License");
6ed13187a745866483139e2878037e1f8427ce567Kenny Guy * you may not use this file except in compliance with the License.
7ed13187a745866483139e2878037e1f8427ce567Kenny Guy * You may obtain a copy of the License at
8ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
9ed13187a745866483139e2878037e1f8427ce567Kenny Guy *      http://www.apache.org/licenses/LICENSE-2.0
10ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
11ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Unless required by applicable law or agreed to in writing, software
12ed13187a745866483139e2878037e1f8427ce567Kenny Guy * distributed under the License is distributed on an "AS IS" BASIS,
13ed13187a745866483139e2878037e1f8427ce567Kenny Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ed13187a745866483139e2878037e1f8427ce567Kenny Guy * See the License for the specific language governing permissions and
15ed13187a745866483139e2878037e1f8427ce567Kenny Guy * limitations under the License.
16ed13187a745866483139e2878037e1f8427ce567Kenny Guy */
17ed13187a745866483139e2878037e1f8427ce567Kenny Guy
18ed13187a745866483139e2878037e1f8427ce567Kenny Guypackage com.android.launcher3.compat;
19ed13187a745866483139e2878037e1f8427ce567Kenny Guy
207066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.annotation.TargetApi;
21ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.Context;
2218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyalimport android.content.SharedPreferences;
23f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokaniimport android.content.pm.PackageManager;
24ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.graphics.drawable.Drawable;
257066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.os.Build;
26ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.os.UserHandle;
27823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
2818bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyalimport com.android.launcher3.LauncherAppState;
29ad0c2f28ceb750646e0132a01d393f5115cbfca9Sunny Goyalimport com.android.launcher3.Utilities;
30823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyalimport com.android.launcher3.util.LongArrayMap;
31823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
32ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport java.util.ArrayList;
33c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guyimport java.util.Collections;
34a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyalimport java.util.HashMap;
35ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport java.util.List;
36ed13187a745866483139e2878037e1f8427ce567Kenny Guy
377066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal@TargetApi(Build.VERSION_CODES.LOLLIPOP)
38ed13187a745866483139e2878037e1f8427ce567Kenny Guypublic class UserManagerCompatVL extends UserManagerCompatV17 {
3918bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    private static final String USER_CREATION_TIME_KEY = "user_creation_time_";
4018bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal
41f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokani    private final PackageManager mPm;
4218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    private final Context mContext;
43ed13187a745866483139e2878037e1f8427ce567Kenny Guy
44ed13187a745866483139e2878037e1f8427ce567Kenny Guy    UserManagerCompatVL(Context context) {
45ed13187a745866483139e2878037e1f8427ce567Kenny Guy        super(context);
46f9847b9bd3e5286fe10667ec12dab5b4c43355faDharmesh Mokani        mPm = context.getPackageManager();
4718bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        mContext = context;
48ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
49ed13187a745866483139e2878037e1f8427ce567Kenny Guy
506075170b838bfe7a040bbff25c2c22859b7d6ee5Dharmesh Mokani    @Override
51823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    public void enableAndResetCache() {
52823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
53a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUsers = new LongArrayMap<>();
54a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUserToSerialMap = new HashMap<>();
55823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            List<UserHandle> users = mUserManager.getUserProfiles();
56823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            if (users != null) {
57823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                for (UserHandle user : users) {
58a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    long serial = mUserManager.getSerialNumberForUser(user);
59a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    UserHandleCompat userCompat = UserHandleCompat.fromUser(user);
60a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    mUsers.put(serial, userCompat);
61a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                    mUserToSerialMap.put(userCompat, serial);
62823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                }
63823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
64823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
65823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    }
66823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
67823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    @Override
68ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public List<UserHandleCompat> getUserProfiles() {
69823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
70823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            if (mUsers != null) {
71823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                List<UserHandleCompat> users = new ArrayList<>();
72a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                users.addAll(mUserToSerialMap.keySet());
73823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                return users;
74823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
75823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
76823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
77c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        List<UserHandle> users = mUserManager.getUserProfiles();
78c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        if (users == null) {
79b1622cc30f2fd9b579cb918083e063685950df92Sunny Goyal            return Collections.emptyList();
80ed13187a745866483139e2878037e1f8427ce567Kenny Guy        }
81c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        ArrayList<UserHandleCompat> compatUsers = new ArrayList<UserHandleCompat>(
82c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy                users.size());
83c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        for (UserHandle user : users) {
84c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            compatUsers.add(UserHandleCompat.fromUser(user));
85c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        }
86c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        return compatUsers;
87ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
88ed13187a745866483139e2878037e1f8427ce567Kenny Guy
896075170b838bfe7a040bbff25c2c22859b7d6ee5Dharmesh Mokani    @Override
90ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public Drawable getBadgedDrawableForUser(Drawable unbadged, UserHandleCompat user) {
913d0854e4b8c0a6daaa80ac14c6bc7572a4e90916Dharmesh Mokani        return mPm.getUserBadgedIcon(unbadged, user.getUser());
92c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    }
93c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy
946075170b838bfe7a040bbff25c2c22859b7d6ee5Dharmesh Mokani    @Override
95d6fe52636dcaa96ec1e10ce2daebe98b820c9739Kenny Guy    public CharSequence getBadgedLabelForUser(CharSequence label, UserHandleCompat user) {
96c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        if (user == null) {
97c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            return label;
98ed13187a745866483139e2878037e1f8427ce567Kenny Guy        }
993d0854e4b8c0a6daaa80ac14c6bc7572a4e90916Dharmesh Mokani        return mPm.getUserBadgedLabel(label, user.getUser());
100ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
10118bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal
10218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    @Override
10318bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    public long getUserCreationTime(UserHandleCompat user) {
104ad0c2f28ceb750646e0132a01d393f5115cbfca9Sunny Goyal        if (Utilities.ATLEAST_MARSHMALLOW) {
105ad0c2f28ceb750646e0132a01d393f5115cbfca9Sunny Goyal            return mUserManager.getUserCreationTime(user.getUser());
106ad0c2f28ceb750646e0132a01d393f5115cbfca9Sunny Goyal        }
10718bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        SharedPreferences prefs = mContext.getSharedPreferences(
10818bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal                LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
10918bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user);
11018bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        if (!prefs.contains(key)) {
11118bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal            prefs.edit().putLong(key, System.currentTimeMillis()).apply();
11218bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        }
11318bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal        return prefs.getLong(key, 0);
11418bf8e2ffde3444d53aaa9654da02cdedd0b7cd1Sunny Goyal    }
115ed13187a745866483139e2878037e1f8427ce567Kenny Guy}
116ed13187a745866483139e2878037e1f8427ce567Kenny Guy
117