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.graphics.drawable.Drawable; 25 import android.os.Build; 26 import android.os.UserHandle; 27 import com.android.launcher3.LauncherAppState; 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.List; 31 32 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 33 public class UserManagerCompatVL extends UserManagerCompatV17 { 34 private static final String USER_CREATION_TIME_KEY = "user_creation_time_"; 35 36 private final PackageManager mPm; 37 private final Context mContext; 38 39 UserManagerCompatVL(Context context) { 40 super(context); 41 mPm = context.getPackageManager(); 42 mContext = context; 43 } 44 45 @Override 46 public List<UserHandleCompat> getUserProfiles() { 47 List<UserHandle> users = mUserManager.getUserProfiles(); 48 if (users == null) { 49 return Collections.emptyList(); 50 } 51 ArrayList<UserHandleCompat> compatUsers = new ArrayList<UserHandleCompat>( 52 users.size()); 53 for (UserHandle user : users) { 54 compatUsers.add(UserHandleCompat.fromUser(user)); 55 } 56 return compatUsers; 57 } 58 59 @Override 60 public Drawable getBadgedDrawableForUser(Drawable unbadged, UserHandleCompat user) { 61 return mPm.getUserBadgedIcon(unbadged, user.getUser()); 62 } 63 64 @Override 65 public CharSequence getBadgedLabelForUser(CharSequence label, UserHandleCompat user) { 66 if (user == null) { 67 return label; 68 } 69 return mPm.getUserBadgedLabel(label, user.getUser()); 70 } 71 72 @Override 73 public long getUserCreationTime(UserHandleCompat user) { 74 // TODO: Use system API once available. 75 SharedPreferences prefs = mContext.getSharedPreferences( 76 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE); 77 String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user); 78 if (!prefs.contains(key)) { 79 prefs.edit().putLong(key, System.currentTimeMillis()).apply(); 80 } 81 return prefs.getLong(key, 0); 82 } 83 } 84 85