blob: 3a3b5a261e65425824caee1cdb877a82dc18b5d8 [file] [log] [blame]
Tony Wickham010d2552017-01-20 08:15:28 -08001package com.android.launcher3.util;
2
3import android.os.UserHandle;
4import android.service.notification.StatusBarNotification;
5
Tony Wickham646e4482020-04-06 17:25:53 -07006import androidx.annotation.NonNull;
Pinyao Ting2f9019e2019-08-16 13:28:24 -07007import androidx.annotation.Nullable;
8
Sunny Goyale396abf2020-04-06 15:11:17 -07009import com.android.launcher3.model.data.ItemInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080010
11import java.util.Arrays;
12
13/** Creates a hash key based on package name and user. */
14public class PackageUserKey {
15
Tony Wickham26b17462017-03-20 17:12:24 -070016 public String mPackageName;
17 public UserHandle mUser;
Tony Wickham010d2552017-01-20 08:15:28 -080018 private int mHashCode;
19
Pinyao Ting2f9019e2019-08-16 13:28:24 -070020 @Nullable
Tony Wickham010d2552017-01-20 08:15:28 -080021 public static PackageUserKey fromItemInfo(ItemInfo info) {
Pinyao Ting2f9019e2019-08-16 13:28:24 -070022 if (info.getTargetComponent() == null) return null;
Tony Wickham010d2552017-01-20 08:15:28 -080023 return new PackageUserKey(info.getTargetComponent().getPackageName(), info.user);
24 }
25
26 public static PackageUserKey fromNotification(StatusBarNotification notification) {
27 return new PackageUserKey(notification.getPackageName(), notification.getUser());
28 }
29
30 public PackageUserKey(String packageName, UserHandle user) {
31 update(packageName, user);
32 }
33
Jon Mirandac1322b62019-09-16 21:38:28 -070034 public void update(String packageName, UserHandle user) {
Tony Wickham010d2552017-01-20 08:15:28 -080035 mPackageName = packageName;
36 mUser = user;
37 mHashCode = Arrays.hashCode(new Object[] {packageName, user});
38 }
39
Tony Wickham18a1b5d2017-02-02 13:58:33 -080040 /**
41 * This should only be called to avoid new object creations in a loop.
42 * @return Whether this PackageUserKey was successfully updated - it shouldn't be used if not.
43 */
44 public boolean updateFromItemInfo(ItemInfo info) {
Pinyao Ting2f9019e2019-08-16 13:28:24 -070045 if (info.getTargetComponent() == null) return false;
Pinyao Ting49a3e692019-07-26 12:28:38 -070046 if (ShortcutUtil.supportsShortcuts(info)) {
Tony Wickham18a1b5d2017-02-02 13:58:33 -080047 update(info.getTargetComponent().getPackageName(), info.user);
48 return true;
49 }
50 return false;
Tony Wickham010d2552017-01-20 08:15:28 -080051 }
52
53 @Override
54 public int hashCode() {
55 return mHashCode;
56 }
57
58 @Override
59 public boolean equals(Object obj) {
60 if (!(obj instanceof PackageUserKey)) return false;
61 PackageUserKey otherKey = (PackageUserKey) obj;
62 return mPackageName.equals(otherKey.mPackageName) && mUser.equals(otherKey.mUser);
63 }
Tony Wickham646e4482020-04-06 17:25:53 -070064
65 @NonNull
66 @Override
67 public String toString() {
68 return mPackageName + "#" + mUser;
69 }
Tony Wickham010d2552017-01-20 08:15:28 -080070}