2016-08-31 14:15:40 -07:00
|
|
|
/*
|
2019-12-10 12:19:13 -08:00
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
2016-08-31 14:15:40 -07:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
package com.android.launcher3.pm;
|
2016-08-31 14:15:40 -07:00
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
import static com.android.launcher3.Utilities.ATLEAST_U;
|
|
|
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
2023-06-01 17:09:43 -07:00
|
|
|
|
2016-08-31 14:15:40 -07:00
|
|
|
import android.content.Context;
|
2019-12-10 12:19:13 -08:00
|
|
|
import android.content.Intent;
|
2023-06-07 12:52:30 -07:00
|
|
|
import android.os.Process;
|
2019-01-25 15:10:18 -08:00
|
|
|
import android.os.UserHandle;
|
|
|
|
|
import android.os.UserManager;
|
2024-04-21 19:05:42 +01:00
|
|
|
import android.util.ArrayMap;
|
2023-06-07 12:52:30 -07:00
|
|
|
|
|
|
|
|
import androidx.annotation.AnyThread;
|
|
|
|
|
import androidx.annotation.NonNull;
|
2024-01-16 09:27:53 -08:00
|
|
|
import androidx.annotation.Nullable;
|
2023-11-03 14:41:45 +00:00
|
|
|
import androidx.annotation.VisibleForTesting;
|
2023-06-07 12:52:30 -07:00
|
|
|
import androidx.annotation.WorkerThread;
|
2019-01-25 15:10:18 -08:00
|
|
|
|
2024-01-16 09:27:53 -08:00
|
|
|
import com.android.launcher3.icons.BitmapInfo;
|
|
|
|
|
import com.android.launcher3.icons.UserBadgeDrawable;
|
2024-03-25 16:12:10 -07:00
|
|
|
import com.android.launcher3.util.ApiWrapper;
|
2024-01-16 09:27:53 -08:00
|
|
|
import com.android.launcher3.util.FlagOp;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.util.MainThreadInitializedObject;
|
|
|
|
|
import com.android.launcher3.util.SafeCloseable;
|
|
|
|
|
import com.android.launcher3.util.SimpleBroadcastReceiver;
|
2023-10-12 13:33:54 -07:00
|
|
|
import com.android.launcher3.util.UserIconInfo;
|
2019-12-10 12:19:13 -08:00
|
|
|
|
2019-01-25 15:10:18 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
2023-06-07 12:52:30 -07:00
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.BiConsumer;
|
2016-08-31 14:15:40 -07:00
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
/**
|
|
|
|
|
* Class which manages a local cache of user handles to avoid system rpc
|
|
|
|
|
*/
|
2023-06-07 12:52:30 -07:00
|
|
|
public class UserCache implements SafeCloseable {
|
|
|
|
|
|
|
|
|
|
public static final String ACTION_PROFILE_ADDED = ATLEAST_U
|
|
|
|
|
? Intent.ACTION_PROFILE_ADDED : Intent.ACTION_MANAGED_PROFILE_ADDED;
|
|
|
|
|
public static final String ACTION_PROFILE_REMOVED = ATLEAST_U
|
|
|
|
|
? Intent.ACTION_PROFILE_REMOVED : Intent.ACTION_MANAGED_PROFILE_REMOVED;
|
|
|
|
|
|
|
|
|
|
public static final String ACTION_PROFILE_UNLOCKED = ATLEAST_U
|
|
|
|
|
? Intent.ACTION_PROFILE_ACCESSIBLE : Intent.ACTION_MANAGED_PROFILE_UNLOCKED;
|
|
|
|
|
public static final String ACTION_PROFILE_LOCKED = ATLEAST_U
|
|
|
|
|
? Intent.ACTION_PROFILE_INACCESSIBLE : Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE;
|
2023-10-12 16:53:00 +01:00
|
|
|
public static final String ACTION_PROFILE_AVAILABLE = "android.intent.action.PROFILE_AVAILABLE";
|
|
|
|
|
public static final String ACTION_PROFILE_UNAVAILABLE =
|
|
|
|
|
"android.intent.action.PROFILE_UNAVAILABLE";
|
2019-12-10 12:19:13 -08:00
|
|
|
|
|
|
|
|
public static final MainThreadInitializedObject<UserCache> INSTANCE =
|
|
|
|
|
new MainThreadInitializedObject<>(UserCache::new);
|
2019-01-25 15:10:18 -08:00
|
|
|
|
2023-10-12 16:53:00 +01:00
|
|
|
/** Returns an instance of UserCache bound to the context provided. */
|
|
|
|
|
public static UserCache getInstance(Context context) {
|
|
|
|
|
return INSTANCE.get(context);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
private final List<BiConsumer<UserHandle, String>> mUserEventListeners = new ArrayList<>();
|
2019-12-10 12:19:13 -08:00
|
|
|
private final SimpleBroadcastReceiver mUserChangeReceiver =
|
|
|
|
|
new SimpleBroadcastReceiver(this::onUsersChanged);
|
2019-01-25 15:10:18 -08:00
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
|
|
@NonNull
|
2023-10-12 13:33:54 -07:00
|
|
|
private Map<UserHandle, UserIconInfo> mUserToSerialMap;
|
2016-08-31 14:15:40 -07:00
|
|
|
|
2024-04-21 19:05:42 +01:00
|
|
|
@NonNull
|
|
|
|
|
private Map<UserHandle, List<String>> mUserToPreInstallAppMap;
|
|
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
private UserCache(Context context) {
|
|
|
|
|
mContext = context;
|
2023-06-07 12:52:30 -07:00
|
|
|
mUserToSerialMap = Collections.emptyMap();
|
|
|
|
|
MODEL_EXECUTOR.execute(this::initAsync);
|
2019-01-25 15:10:18 -08:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
@Override
|
|
|
|
|
public void close() {
|
|
|
|
|
MODEL_EXECUTOR.execute(() -> mUserChangeReceiver.unregisterReceiverSafely(mContext));
|
2019-01-25 15:10:18 -08:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
@WorkerThread
|
|
|
|
|
private void initAsync() {
|
|
|
|
|
mUserChangeReceiver.register(mContext,
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
|
2024-05-13 11:06:50 +00:00
|
|
|
Intent.ACTION_MANAGED_PROFILE_REMOVED,
|
2023-06-07 12:52:30 -07:00
|
|
|
ACTION_PROFILE_ADDED,
|
|
|
|
|
ACTION_PROFILE_REMOVED,
|
|
|
|
|
ACTION_PROFILE_UNLOCKED,
|
2023-10-12 16:53:00 +01:00
|
|
|
ACTION_PROFILE_LOCKED,
|
|
|
|
|
ACTION_PROFILE_AVAILABLE,
|
|
|
|
|
ACTION_PROFILE_UNAVAILABLE);
|
2023-06-07 12:52:30 -07:00
|
|
|
updateCache();
|
2019-01-25 15:10:18 -08:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
@AnyThread
|
|
|
|
|
private void onUsersChanged(Intent intent) {
|
|
|
|
|
MODEL_EXECUTOR.execute(this::updateCache);
|
|
|
|
|
UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
|
|
|
|
|
if (user == null) {
|
|
|
|
|
return;
|
2019-12-10 12:19:13 -08:00
|
|
|
}
|
2023-06-07 12:52:30 -07:00
|
|
|
String action = intent.getAction();
|
|
|
|
|
mUserEventListeners.forEach(l -> l.accept(user, action));
|
2019-12-10 12:19:13 -08:00
|
|
|
}
|
|
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
@WorkerThread
|
|
|
|
|
private void updateCache() {
|
2024-03-25 16:12:10 -07:00
|
|
|
mUserToSerialMap = ApiWrapper.INSTANCE.get(mContext).queryAllUsers();
|
2024-04-21 19:05:42 +01:00
|
|
|
mUserToPreInstallAppMap = fetchPreInstallApps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@WorkerThread
|
|
|
|
|
private Map<UserHandle, List<String>> fetchPreInstallApps() {
|
|
|
|
|
Map<UserHandle, List<String>> userToPreInstallApp = new ArrayMap<>();
|
|
|
|
|
mUserToSerialMap.forEach((userHandle, userIconInfo) -> {
|
|
|
|
|
// Fetch only for private profile, as other profiles have no usages yet.
|
|
|
|
|
List<String> preInstallApp = userIconInfo.isPrivate()
|
|
|
|
|
? ApiWrapper.INSTANCE.get(mContext).getPreInstalledSystemPackages(userHandle)
|
|
|
|
|
: new ArrayList<>();
|
|
|
|
|
userToPreInstallApp.put(userHandle, preInstallApp);
|
|
|
|
|
});
|
|
|
|
|
return userToPreInstallApp;
|
2023-06-07 12:52:30 -07:00
|
|
|
}
|
2019-12-10 12:19:13 -08:00
|
|
|
|
2023-06-07 12:52:30 -07:00
|
|
|
/**
|
|
|
|
|
* Adds a listener for user additions and removals
|
|
|
|
|
*/
|
|
|
|
|
public SafeCloseable addUserEventListener(BiConsumer<UserHandle, String> listener) {
|
|
|
|
|
mUserEventListeners.add(listener);
|
|
|
|
|
return () -> mUserEventListeners.remove(listener);
|
2019-12-10 12:19:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see UserManager#getSerialNumberForUser(UserHandle)
|
|
|
|
|
*/
|
2019-01-25 15:10:18 -08:00
|
|
|
public long getSerialNumberForUser(UserHandle user) {
|
2023-10-12 13:33:54 -07:00
|
|
|
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;
|
2019-01-25 15:10:18 -08:00
|
|
|
}
|
|
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
/**
|
|
|
|
|
* @see UserManager#getUserForSerialNumber(long)
|
|
|
|
|
*/
|
2019-01-25 15:10:18 -08:00
|
|
|
public UserHandle getUserForSerialNumber(long serialNumber) {
|
2023-06-07 12:52:30 -07:00
|
|
|
return mUserToSerialMap
|
|
|
|
|
.entrySet()
|
|
|
|
|
.stream()
|
2023-10-12 13:33:54 -07:00
|
|
|
.filter(entry -> serialNumber == entry.getValue().userSerial)
|
2023-06-07 12:52:30 -07:00
|
|
|
.findFirst()
|
|
|
|
|
.map(Map.Entry::getKey)
|
|
|
|
|
.orElse(Process.myUserHandle());
|
2016-08-31 14:15:40 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-03 14:41:45 +00:00
|
|
|
@VisibleForTesting
|
|
|
|
|
public void putToCache(UserHandle userHandle, UserIconInfo info) {
|
|
|
|
|
mUserToSerialMap.put(userHandle, info);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
/**
|
|
|
|
|
* @see UserManager#getUserProfiles()
|
|
|
|
|
*/
|
2019-01-25 15:10:18 -08:00
|
|
|
public List<UserHandle> getUserProfiles() {
|
2023-06-07 12:52:30 -07:00
|
|
|
return List.copyOf(mUserToSerialMap.keySet());
|
|
|
|
|
}
|
2024-01-16 09:27:53 -08:00
|
|
|
|
2024-04-21 19:05:42 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the pre-installed apps for a user.
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
public List<String> getPreInstallApps(UserHandle user) {
|
|
|
|
|
List<String> preInstallApp = mUserToPreInstallAppMap.get(user);
|
|
|
|
|
return preInstallApp == null ? new ArrayList<>() : preInstallApp;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 09:27:53 -08:00
|
|
|
/**
|
|
|
|
|
* Get a non-themed {@link UserBadgeDrawable} based on the provided {@link UserHandle}.
|
|
|
|
|
*/
|
|
|
|
|
@Nullable
|
|
|
|
|
public static UserBadgeDrawable getBadgeDrawable(Context context, UserHandle userHandle) {
|
|
|
|
|
return (UserBadgeDrawable) BitmapInfo.LOW_RES_INFO.withFlags(UserCache.getInstance(context)
|
|
|
|
|
.getUserInfo(userHandle).applyBitmapInfoFlags(FlagOp.NO_OP))
|
|
|
|
|
.getBadgeDrawable(context, false /* isThemed */);
|
|
|
|
|
}
|
2016-08-31 14:15:40 -07:00
|
|
|
}
|