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;
217066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.os.Build;
22ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.os.UserManager;
23ed13187a745866483139e2878037e1f8427ce567Kenny Guy
24823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyalimport com.android.launcher3.util.LongArrayMap;
25823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
26a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyalimport java.util.HashMap;
27a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal
287066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
29ed13187a745866483139e2878037e1f8427ce567Kenny Guypublic class UserManagerCompatV17 extends UserManagerCompatV16 {
30823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
31823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    protected LongArrayMap<UserHandleCompat> mUsers;
32a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal    // Create a separate reverse map as LongArrayMap.indexOfValue checks if objects are same
33a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal    // and not {@link Object#equals}
34a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal    protected HashMap<UserHandleCompat, Long> mUserToSerialMap;
35a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal
36ed13187a745866483139e2878037e1f8427ce567Kenny Guy    protected UserManager mUserManager;
37ed13187a745866483139e2878037e1f8427ce567Kenny Guy
38ed13187a745866483139e2878037e1f8427ce567Kenny Guy    UserManagerCompatV17(Context context) {
39ed13187a745866483139e2878037e1f8427ce567Kenny Guy        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
40ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
41ed13187a745866483139e2878037e1f8427ce567Kenny Guy
42ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public long getSerialNumberForUser(UserHandleCompat user) {
43823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
44a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            if (mUserToSerialMap != null) {
45a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                Long serial = mUserToSerialMap.get(user);
46a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal                return serial == null ? 0 : serial;
47823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
48823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
49ed13187a745866483139e2878037e1f8427ce567Kenny Guy        return mUserManager.getSerialNumberForUser(user.getUser());
50ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
51ed13187a745866483139e2878037e1f8427ce567Kenny Guy
52ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public UserHandleCompat getUserForSerialNumber(long serialNumber) {
53823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
54823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            if (mUsers != null) {
55823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal                return mUsers.get(serialNumber);
56823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            }
57823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
58ed13187a745866483139e2878037e1f8427ce567Kenny Guy        return UserHandleCompat.fromUser(mUserManager.getUserForSerialNumber(serialNumber));
59ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
60823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal
61823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    @Override
62823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    public void enableAndResetCache() {
63823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        synchronized (this) {
64a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUsers = new LongArrayMap<>();
65a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUserToSerialMap = new HashMap<>();
66823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal            UserHandleCompat myUser = UserHandleCompat.myUserHandle();
67a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            long serial = mUserManager.getSerialNumberForUser(myUser.getUser());
68a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUsers.put(serial, myUser);
69a2a039b66f05a1164f954387b1c12a8fb4063e92Sunny Goyal            mUserToSerialMap.put(myUser, serial);
70823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal        }
71823fd5090209017a029460e7dbd8ab9d51d013ddSunny Goyal    }
72ed13187a745866483139e2878037e1f8427ce567Kenny Guy}
73ed13187a745866483139e2878037e1f8427ce567Kenny Guy
74