1 package com.android.launcher3.util;
2 
3 import android.os.UserHandle;
4 import android.service.notification.StatusBarNotification;
5 
6 import androidx.annotation.Nullable;
7 
8 import com.android.launcher3.ItemInfo;
9 
10 import java.util.Arrays;
11 
12 /** Creates a hash key based on package name and user. */
13 public class PackageUserKey {
14 
15     public String mPackageName;
16     public UserHandle mUser;
17     private int mHashCode;
18 
19     @Nullable
fromItemInfo(ItemInfo info)20     public static PackageUserKey fromItemInfo(ItemInfo info) {
21         if (info.getTargetComponent() == null) return null;
22         return new PackageUserKey(info.getTargetComponent().getPackageName(), info.user);
23     }
24 
fromNotification(StatusBarNotification notification)25     public static PackageUserKey fromNotification(StatusBarNotification notification) {
26         return new PackageUserKey(notification.getPackageName(), notification.getUser());
27     }
28 
PackageUserKey(String packageName, UserHandle user)29     public PackageUserKey(String packageName, UserHandle user) {
30         update(packageName, user);
31     }
32 
update(String packageName, UserHandle user)33     public void update(String packageName, UserHandle user) {
34         mPackageName = packageName;
35         mUser = user;
36         mHashCode = Arrays.hashCode(new Object[] {packageName, user});
37     }
38 
39     /**
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      */
updateFromItemInfo(ItemInfo info)43     public boolean updateFromItemInfo(ItemInfo info) {
44         if (info.getTargetComponent() == null) return false;
45         if (ShortcutUtil.supportsShortcuts(info)) {
46             update(info.getTargetComponent().getPackageName(), info.user);
47             return true;
48         }
49         return false;
50     }
51 
52     @Override
hashCode()53     public int hashCode() {
54         return mHashCode;
55     }
56 
57     @Override
equals(Object obj)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 }
64