UserManagerCompatVL.java revision b84046dacf48b38069c89ac908ff5ae1e8fc1d13
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 18package com.android.launcher3.compat; 19 20import android.annotation.TargetApi; 21import android.content.Context; 22import android.content.SharedPreferences; 23import android.content.pm.PackageManager; 24import android.os.Build; 25import android.os.UserHandle; 26 27import com.android.launcher3.LauncherAppState; 28import com.android.launcher3.Utilities; 29import com.android.launcher3.util.LongArrayMap; 30 31import java.util.ArrayList; 32import java.util.Collections; 33import java.util.HashMap; 34import java.util.List; 35 36@TargetApi(Build.VERSION_CODES.LOLLIPOP) 37public class UserManagerCompatVL extends UserManagerCompatV17 { 38 private static final String USER_CREATION_TIME_KEY = "user_creation_time_"; 39 40 private final PackageManager mPm; 41 private final Context mContext; 42 43 UserManagerCompatVL(Context context) { 44 super(context); 45 mPm = context.getPackageManager(); 46 mContext = context; 47 } 48 49 @Override 50 public void enableAndResetCache() { 51 synchronized (this) { 52 mUsers = new LongArrayMap<>(); 53 mUserToSerialMap = new HashMap<>(); 54 List<UserHandle> users = mUserManager.getUserProfiles(); 55 if (users != null) { 56 for (UserHandle user : users) { 57 long serial = mUserManager.getSerialNumberForUser(user); 58 UserHandleCompat userCompat = UserHandleCompat.fromUser(user); 59 mUsers.put(serial, userCompat); 60 mUserToSerialMap.put(userCompat, serial); 61 } 62 } 63 } 64 } 65 66 @Override 67 public List<UserHandleCompat> getUserProfiles() { 68 synchronized (this) { 69 if (mUsers != null) { 70 List<UserHandleCompat> users = new ArrayList<>(); 71 users.addAll(mUserToSerialMap.keySet()); 72 return users; 73 } 74 } 75 76 List<UserHandle> users = mUserManager.getUserProfiles(); 77 if (users == null) { 78 return Collections.emptyList(); 79 } 80 ArrayList<UserHandleCompat> compatUsers = new ArrayList<UserHandleCompat>( 81 users.size()); 82 for (UserHandle user : users) { 83 compatUsers.add(UserHandleCompat.fromUser(user)); 84 } 85 return compatUsers; 86 } 87 88 @Override 89 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandleCompat user) { 90 if (user == null) { 91 return label; 92 } 93 return mPm.getUserBadgedLabel(label, user.getUser()); 94 } 95 96 @Override 97 public long getUserCreationTime(UserHandleCompat user) { 98 if (Utilities.ATLEAST_MARSHMALLOW) { 99 return mUserManager.getUserCreationTime(user.getUser()); 100 } 101 SharedPreferences prefs = mContext.getSharedPreferences( 102 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE); 103 String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user); 104 if (!prefs.contains(key)) { 105 prefs.edit().putLong(key, System.currentTimeMillis()).apply(); 106 } 107 return prefs.getLong(key, 0); 108 } 109} 110 111