blob: 46e7070ed599a601c9b592e55a0609432957808f [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 Ting2f9019e2019-08-16 13:28:24 -07006import androidx.annotation.Nullable;
7
Sunny Goyale396abf2020-04-06 15:11:17 -07008import com.android.launcher3.model.data.ItemInfo;
Tony Wickham010d2552017-01-20 08:15:28 -08009
10import java.util.Arrays;
11
12/** Creates a hash key based on package name and user. */
13public class PackageUserKey {
14
Tony Wickham26b17462017-03-20 17:12:24 -070015 public String mPackageName;
16 public UserHandle mUser;
Tony Wickham010d2552017-01-20 08:15:28 -080017 private int mHashCode;
18
Pinyao Ting2f9019e2019-08-16 13:28:24 -070019 @Nullable
Tony Wickham010d2552017-01-20 08:15:28 -080020 public static PackageUserKey fromItemInfo(ItemInfo info) {
Pinyao Ting2f9019e2019-08-16 13:28:24 -070021 if (info.getTargetComponent() == null) return null;
Tony Wickham010d2552017-01-20 08:15:28 -080022 return new PackageUserKey(info.getTargetComponent().getPackageName(), info.user);
23 }
24
25 public static PackageUserKey fromNotification(StatusBarNotification notification) {
26 return new PackageUserKey(notification.getPackageName(), notification.getUser());
27 }
28
29 public PackageUserKey(String packageName, UserHandle user) {
30 update(packageName, user);
31 }
32
Jon Mirandac1322b62019-09-16 21:38:28 -070033 public void update(String packageName, UserHandle user) {
Tony Wickham010d2552017-01-20 08:15:28 -080034 mPackageName = packageName;
35 mUser = user;
36 mHashCode = Arrays.hashCode(new Object[] {packageName, user});
37 }
38
Tony Wickham18a1b5d2017-02-02 13:58:33 -080039 /**
40 * This should only be called to avoid new object creations in a loop.
41 * @return Whether this PackageUserKey was successfully updated - it shouldn't be used if not.
42 */
43 public boolean updateFromItemInfo(ItemInfo info) {
Pinyao Ting2f9019e2019-08-16 13:28:24 -070044 if (info.getTargetComponent() == null) return false;
Pinyao Ting49a3e692019-07-26 12:28:38 -070045 if (ShortcutUtil.supportsShortcuts(info)) {
Tony Wickham18a1b5d2017-02-02 13:58:33 -080046 update(info.getTargetComponent().getPackageName(), info.user);
47 return true;
48 }
49 return false;
Tony Wickham010d2552017-01-20 08:15:28 -080050 }
51
52 @Override
53 public int hashCode() {
54 return mHashCode;
55 }
56
57 @Override
58 public boolean equals(Object obj) {
59 if (!(obj instanceof PackageUserKey)) return false;
60 PackageUserKey otherKey = (PackageUserKey) obj;
61 return mPackageName.equals(otherKey.mPackageName) && mUser.equals(otherKey.mUser);
62 }
63}