mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-02-19 18:58:19 +00:00
Generalize userproperties to support for more user types in iconCache
Bug: 305062259 Flag: None Test: Presubmit Change-Id: I35cd20a03520ada233809930fcc56cdea1dabec6
This commit is contained in:
@@ -22,11 +22,17 @@ import android.content.Context;
|
||||
import android.content.pm.LauncherActivityInfo;
|
||||
import android.content.pm.LauncherApps;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.ArrayMap;
|
||||
import android.window.RemoteTransition;
|
||||
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.util.UserIconInfo;
|
||||
import com.android.quickstep.util.FadeOutRemoteTransition;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -53,4 +59,41 @@ public class ApiWrapper {
|
||||
options.setRemoteTransition(new RemoteTransition(new FadeOutRemoteTransition()));
|
||||
return options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map of all users on the device to their corresponding UI properties
|
||||
*/
|
||||
public static Map<UserHandle, UserIconInfo> queryAllUsers(Context context) {
|
||||
UserManager um = context.getSystemService(UserManager.class);
|
||||
Map<UserHandle, UserIconInfo> users = new ArrayMap<>();
|
||||
List<UserHandle> usersActual = um.getUserProfiles();
|
||||
if (usersActual != null) {
|
||||
for (UserHandle user : usersActual) {
|
||||
long serial = um.getSerialNumberForUser(user);
|
||||
|
||||
// Simple check to check if the provided user is work profile
|
||||
// TODO: Migrate to a better platform API
|
||||
NoopDrawable d = new NoopDrawable();
|
||||
boolean isWork = (d != context.getPackageManager().getUserBadgedIcon(d, user));
|
||||
UserIconInfo info = new UserIconInfo(
|
||||
user,
|
||||
isWork ? UserIconInfo.TYPE_WORK : UserIconInfo.TYPE_MAIN,
|
||||
serial);
|
||||
users.put(user, info);
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
private static class NoopDrawable extends ColorDrawable {
|
||||
@Override
|
||||
public int getIntrinsicHeight() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicWidth() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,16 @@ import static com.android.launcher3.config.FeatureFlags.ENABLE_FORCED_MONO_ICON;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.UserHandle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.android.launcher3.InvariantDeviceProfile;
|
||||
import com.android.launcher3.graphics.IconShape;
|
||||
import com.android.launcher3.graphics.LauncherPreviewRenderer;
|
||||
import com.android.launcher3.pm.UserCache;
|
||||
import com.android.launcher3.util.Themes;
|
||||
import com.android.launcher3.util.UserIconInfo;
|
||||
|
||||
/**
|
||||
* Wrapper class to provide access to {@link BaseIconFactory} and also to provide pool of this class
|
||||
@@ -107,6 +112,12 @@ public class LauncherIcons extends BaseIconFactory implements AutoCloseable {
|
||||
return mMonochromeIconFactory.wrap(base);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected UserIconInfo getUserInfo(@NonNull UserHandle user) {
|
||||
return UserCache.INSTANCE.get(mContext).getUserInfo(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
recycle();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.android.launcher3.pm;
|
||||
|
||||
import static com.android.launcher3.Utilities.ATLEAST_U;
|
||||
import static com.android.launcher3.uioverrides.ApiWrapper.queryAllUsers;
|
||||
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -24,7 +25,6 @@ import android.content.Intent;
|
||||
import android.os.Process;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import androidx.annotation.AnyThread;
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -33,6 +33,7 @@ import androidx.annotation.WorkerThread;
|
||||
import com.android.launcher3.util.MainThreadInitializedObject;
|
||||
import com.android.launcher3.util.SafeCloseable;
|
||||
import com.android.launcher3.util.SimpleBroadcastReceiver;
|
||||
import com.android.launcher3.util.UserIconInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -65,7 +66,7 @@ public class UserCache implements SafeCloseable {
|
||||
private final Context mContext;
|
||||
|
||||
@NonNull
|
||||
private Map<UserHandle, Long> mUserToSerialMap;
|
||||
private Map<UserHandle, UserIconInfo> mUserToSerialMap;
|
||||
|
||||
private UserCache(Context context) {
|
||||
mContext = context;
|
||||
@@ -103,7 +104,7 @@ public class UserCache implements SafeCloseable {
|
||||
|
||||
@WorkerThread
|
||||
private void updateCache() {
|
||||
mUserToSerialMap = queryAllUsers(mContext.getSystemService(UserManager.class));
|
||||
mUserToSerialMap = queryAllUsers(mContext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,19 +119,26 @@ public class UserCache implements SafeCloseable {
|
||||
* @see UserManager#getSerialNumberForUser(UserHandle)
|
||||
*/
|
||||
public long getSerialNumberForUser(UserHandle user) {
|
||||
Long serial = mUserToSerialMap.get(user);
|
||||
return serial == null ? 0 : serial;
|
||||
return getUserInfo(user).userSerial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user properties for the provided user or default values
|
||||
*/
|
||||
@NonNull
|
||||
public UserIconInfo getUserInfo(UserHandle user) {
|
||||
UserIconInfo info = mUserToSerialMap.get(user);
|
||||
return info == null ? new UserIconInfo(user, UserIconInfo.TYPE_MAIN) : info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see UserManager#getUserForSerialNumber(long)
|
||||
*/
|
||||
public UserHandle getUserForSerialNumber(long serialNumber) {
|
||||
Long value = serialNumber;
|
||||
return mUserToSerialMap
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> value.equals(entry.getValue()))
|
||||
.filter(entry -> serialNumber == entry.getValue().userSerial)
|
||||
.findFirst()
|
||||
.map(Map.Entry::getKey)
|
||||
.orElse(Process.myUserHandle());
|
||||
@@ -142,16 +150,4 @@ public class UserCache implements SafeCloseable {
|
||||
public List<UserHandle> getUserProfiles() {
|
||||
return List.copyOf(mUserToSerialMap.keySet());
|
||||
}
|
||||
|
||||
private static Map<UserHandle, Long> queryAllUsers(UserManager userManager) {
|
||||
Map<UserHandle, Long> users = new ArrayMap<>();
|
||||
List<UserHandle> usersActual = userManager.getUserProfiles();
|
||||
if (usersActual != null) {
|
||||
for (UserHandle user : usersActual) {
|
||||
long serial = userManager.getSerialNumberForUser(user);
|
||||
users.put(user, serial);
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,10 +21,16 @@ import android.app.Person;
|
||||
import android.content.Context;
|
||||
import android.content.pm.LauncherActivityInfo;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.util.UserIconInfo;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -48,4 +54,41 @@ public class ApiWrapper {
|
||||
public static ActivityOptions createFadeOutAnimOptions(Context context) {
|
||||
return ActivityOptions.makeCustomAnimation(context, 0, android.R.anim.fade_out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map of all users on the device to their corresponding UI properties
|
||||
*/
|
||||
public static Map<UserHandle, UserIconInfo> queryAllUsers(Context context) {
|
||||
UserManager um = context.getSystemService(UserManager.class);
|
||||
Map<UserHandle, UserIconInfo> users = new ArrayMap<>();
|
||||
List<UserHandle> usersActual = um.getUserProfiles();
|
||||
if (usersActual != null) {
|
||||
for (UserHandle user : usersActual) {
|
||||
long serial = um.getSerialNumberForUser(user);
|
||||
|
||||
// Simple check to check if the provided user is work profile
|
||||
// TODO: Migrate to a better platform API
|
||||
NoopDrawable d = new NoopDrawable();
|
||||
boolean isWork = (d != context.getPackageManager().getUserBadgedIcon(d, user));
|
||||
UserIconInfo info = new UserIconInfo(
|
||||
user,
|
||||
isWork ? UserIconInfo.TYPE_WORK : UserIconInfo.TYPE_MAIN,
|
||||
serial);
|
||||
users.put(user, info);
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
private static class NoopDrawable extends ColorDrawable {
|
||||
@Override
|
||||
public int getIntrinsicHeight() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntrinsicWidth() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user