Files
Lawnchair/src/com/android/launcher3/compat/UserManagerCompatVL.java
T
Sunny Goyal a474a9bcf5 Simplifying logic for managed for icon addition
> Checking for duplicate icons before adding new icons

For O and above, icon addition is controlled using
SessionCommitReceiver.
As long as the Launcher is the default app, it will keep adding
icons on the homescreen. Apps installed while launcher was
not the default homescreen, no icons will be added.

For below O, icons are added based on package event. As long as
the Launcher process is running, it will keep adding icons on the
homescreen. Apps installed while the launcher app was dead, no
icons will be added.

Bug: 37528649
Bug: 37082950
Bug: 34112546
Change-Id: Ic99501fa476c00474a479f2a36c24614bfa3f4bf
2017-05-22 13:31:04 -07:00

134 lines
4.1 KiB
Java

/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.compat;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.launcher3.util.LongArrayMap;
import com.android.launcher3.util.ManagedProfileHeuristic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class UserManagerCompatVL extends UserManagerCompat {
private static final String USER_CREATION_TIME_KEY = "user_creation_time_";
protected final UserManager mUserManager;
private final PackageManager mPm;
private final Context mContext;
protected LongArrayMap<UserHandle> mUsers;
// Create a separate reverse map as LongArrayMap.indexOfValue checks if objects are same
// and not {@link Object#equals}
protected HashMap<UserHandle, Long> mUserToSerialMap;
UserManagerCompatVL(Context context) {
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mPm = context.getPackageManager();
mContext = context;
}
@Override
public long getSerialNumberForUser(UserHandle user) {
synchronized (this) {
if (mUserToSerialMap != null) {
Long serial = mUserToSerialMap.get(user);
return serial == null ? 0 : serial;
}
}
return mUserManager.getSerialNumberForUser(user);
}
@Override
public UserHandle getUserForSerialNumber(long serialNumber) {
synchronized (this) {
if (mUsers != null) {
return mUsers.get(serialNumber);
}
}
return mUserManager.getUserForSerialNumber(serialNumber);
}
@Override
public boolean isQuietModeEnabled(UserHandle user) {
return false;
}
@Override
public boolean isUserUnlocked(UserHandle user) {
return true;
}
@Override
public boolean isDemoUser() {
return false;
}
@Override
public void enableAndResetCache() {
synchronized (this) {
mUsers = new LongArrayMap<>();
mUserToSerialMap = new HashMap<>();
List<UserHandle> users = mUserManager.getUserProfiles();
if (users != null) {
for (UserHandle user : users) {
long serial = mUserManager.getSerialNumberForUser(user);
mUsers.put(serial, user);
mUserToSerialMap.put(user, serial);
}
}
}
}
@Override
public List<UserHandle> getUserProfiles() {
synchronized (this) {
if (mUsers != null) {
return new ArrayList<>(mUserToSerialMap.keySet());
}
}
List<UserHandle> users = mUserManager.getUserProfiles();
return users == null ? Collections.<UserHandle>emptyList() : users;
}
@Override
public CharSequence getBadgedLabelForUser(CharSequence label, UserHandle user) {
if (user == null) {
return label;
}
return mPm.getUserBadgedLabel(label, user);
}
@Override
public long getUserCreationTime(UserHandle user) {
SharedPreferences prefs = ManagedProfileHeuristic.prefs(mContext);
String key = USER_CREATION_TIME_KEY + getSerialNumberForUser(user);
if (!prefs.contains(key)) {
prefs.edit().putLong(key, System.currentTimeMillis()).apply();
}
return prefs.getLong(key, 0);
}
}