Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.launcher3.compat;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.content.pm.PackageManager;
     22 import android.os.UserHandle;
     23 import android.os.UserManager;
     24 
     25 import com.android.launcher3.Utilities;
     26 import com.android.launcher3.util.LongArrayMap;
     27 
     28 import java.util.ArrayList;
     29 import java.util.Collections;
     30 import java.util.HashMap;
     31 import java.util.List;
     32 
     33 public class UserManagerCompatVL extends UserManagerCompat {
     34     private static final String USER_CREATION_TIME_KEY = "user_creation_time_";
     35 
     36     protected final UserManager mUserManager;
     37     private final PackageManager mPm;
     38     private final Context mContext;
     39 
     40     protected LongArrayMap<UserHandle> mUsers;
     41     // Create a separate reverse map as LongArrayMap.indexOfValue checks if objects are same
     42     // and not {@link Object#equals}
     43     protected HashMap<UserHandle, Long> mUserToSerialMap;
     44 
     45     UserManagerCompatVL(Context context) {
     46         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     47         mPm = context.getPackageManager();
     48         mContext = context;
     49     }
     50 
     51     @Override
     52     public long getSerialNumberForUser(UserHandle user) {
     53         synchronized (this) {
     54             if (mUserToSerialMap != null) {
     55                 Long serial = mUserToSerialMap.get(user);
     56                 return serial == null ? 0 : serial;
     57             }
     58         }
     59         return mUserManager.getSerialNumberForUser(user);
     60     }
     61 
     62     @Override
     63     public UserHandle getUserForSerialNumber(long serialNumber) {
     64         synchronized (this) {
     65             if (mUsers != null) {
     66                 return mUsers.get(serialNumber);
     67             }
     68         }
     69         return mUserManager.getUserForSerialNumber(serialNumber);
     70     }
     71 
     72     @Override
     73     public boolean isQuietModeEnabled(UserHandle user) {
     74         return false;
     75     }
     76 
     77     @Override
     78     public boolean isUserUnlocked(UserHandle user) {
     79         return true;
     80     }
     81 
     82     @Override
     83     public boolean isDemoUser() {
     84         return false;
     85     }
     86 
     87     @Override
     88     public void enableAndResetCache() {
     89         synchronized (this) {
     90             mUsers = new LongArrayMap<>();
     91             mUserToSerialMap = new HashMap<>();
     92             List<UserHandle> users = mUserManager.getUserProfiles();
     93             if (users != null) {
     94                 for (UserHandle user : users) {
     95                     long serial = mUserManager.getSerialNumberForUser(user);
     96                     mUsers.put(serial, user);
     97                     mUserToSerialMap.put(user, serial);
     98                 }
     99             }
    100         }
    101     }
    102 
    103     @Override
    104     public List<UserHandle> getUserProfiles() {
    105         synchronized (this) {
    106             if (mUsers != null) {
    107                 return new ArrayList<>(mUserToSerialMap.keySet());
    108             }
    109         }
    110 
    111         List<UserHandle> users = mUserManager.getUserProfiles();
    112         return users == null ? Collections.<UserHandle>emptyList() : users;
    113     }
    114 
    115     @Override
    116     public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
    117         if (user == null) {
    118             return label;
    119         }
    120         return mPm.getUserBadgedLabel(label, user);
    121     }
    122 
    123     @Override
    124     public long getUserCreationTime(UserHandle user) {
    125         SharedPreferences prefs = Utilities.getPrefs(mContext);
    126         String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user);
    127         if (!prefs.contains(key)) {
    128             prefs.edit().putLong(key, System.currentTimeMillis()).apply();
    129         }
    130         return prefs.getLong(key, 0);
    131     }
    132 }
    133 
    134