1 2 /* 3 * Copyright (C) 2014 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.launcher3.compat; 19 20 import android.annotation.TargetApi; 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.content.pm.PackageManager; 24 import android.os.Build; 25 import android.os.UserHandle; 26 27 import com.android.launcher3.Utilities; 28 import com.android.launcher3.util.LongArrayMap; 29 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.HashMap; 33 import java.util.List; 34 35 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 36 public class UserManagerCompatVL extends UserManagerCompatV17 { 37 private static final String USER_CREATION_TIME_KEY = "user_creation_time_"; 38 39 private final PackageManager mPm; 40 private final Context mContext; 41 UserManagerCompatVL(Context context)42 UserManagerCompatVL(Context context) { 43 super(context); 44 mPm = context.getPackageManager(); 45 mContext = context; 46 } 47 48 @Override enableAndResetCache()49 public void enableAndResetCache() { 50 synchronized (this) { 51 mUsers = new LongArrayMap<>(); 52 mUserToSerialMap = new HashMap<>(); 53 List<UserHandle> users = mUserManager.getUserProfiles(); 54 if (users != null) { 55 for (UserHandle user : users) { 56 long serial = mUserManager.getSerialNumberForUser(user); 57 UserHandleCompat userCompat = UserHandleCompat.fromUser(user); 58 mUsers.put(serial, userCompat); 59 mUserToSerialMap.put(userCompat, serial); 60 } 61 } 62 } 63 } 64 65 @Override getUserProfiles()66 public List<UserHandleCompat> getUserProfiles() { 67 synchronized (this) { 68 if (mUsers != null) { 69 List<UserHandleCompat> users = new ArrayList<>(); 70 users.addAll(mUserToSerialMap.keySet()); 71 return users; 72 } 73 } 74 75 List<UserHandle> users = mUserManager.getUserProfiles(); 76 if (users == null) { 77 return Collections.emptyList(); 78 } 79 ArrayList<UserHandleCompat> compatUsers = new ArrayList<UserHandleCompat>( 80 users.size()); 81 for (UserHandle user : users) { 82 compatUsers.add(UserHandleCompat.fromUser(user)); 83 } 84 return compatUsers; 85 } 86 87 @Override getBadgedLabelForUser(CharSequence label, UserHandleCompat user)88 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandleCompat user) { 89 if (user == null) { 90 return label; 91 } 92 return mPm.getUserBadgedLabel(label, user.getUser()); 93 } 94 95 @Override getUserCreationTime(UserHandleCompat user)96 public long getUserCreationTime(UserHandleCompat user) { 97 if (Utilities.ATLEAST_MARSHMALLOW) { 98 return mUserManager.getUserCreationTime(user.getUser()); 99 } 100 SharedPreferences prefs = Utilities.getPrefs(mContext); 101 String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user); 102 if (!prefs.contains(key)) { 103 prefs.edit().putLong(key, System.currentTimeMillis()).apply(); 104 } 105 return prefs.getLong(key, 0); 106 } 107 } 108 109