blob: aa11968e4149f9e14d676669b944a32531659539 [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
Pinyao Tingd88f63b2019-08-16 13:28:24 -07006import androidx.annotation.Nullable;
7
Tony Wickham010d2552017-01-20 08:15:28 -08008import com.android.launcher3.ItemInfo;
Tony Wickham18a1b5d2017-02-02 13:58:33 -08009import com.android.launcher3.shortcuts.DeepShortcutManager;
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 Tingd88f63b2019-08-16 13:28:24 -070020 @Nullable
Tony Wickham010d2552017-01-20 08:15:28 -080021 public static PackageUserKey fromItemInfo(ItemInfo info) {
Pinyao Tingd88f63b2019-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 Tingd88f63b2019-08-16 13:28:24 -070045 if (info.getTargetComponent() == null) return false;
Pinyao Tinge4061fc2019-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 }
64}