blob: d08b0e9314c7eb774227d8d534c27750bbc3c655 [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
6import com.android.launcher3.ItemInfo;
7
8import java.util.Arrays;
9
10/** Creates a hash key based on package name and user. */
11public class PackageUserKey {
12
13 private String mPackageName;
14 private UserHandle mUser;
15 private int mHashCode;
16
17 public static PackageUserKey fromItemInfo(ItemInfo info) {
18 return new PackageUserKey(info.getTargetComponent().getPackageName(), info.user);
19 }
20
21 public static PackageUserKey fromNotification(StatusBarNotification notification) {
22 return new PackageUserKey(notification.getPackageName(), notification.getUser());
23 }
24
25 public PackageUserKey(String packageName, UserHandle user) {
26 update(packageName, user);
27 }
28
29 private void update(String packageName, UserHandle user) {
30 mPackageName = packageName;
31 mUser = user;
32 mHashCode = Arrays.hashCode(new Object[] {packageName, user});
33 }
34
35 /** This should only be called to avoid new object creations in a loop. */
36 public void updateFromItemInfo(ItemInfo info) {
37 update(info.getTargetComponent().getPackageName(), info.user);
38 }
39
40 @Override
41 public int hashCode() {
42 return mHashCode;
43 }
44
45 @Override
46 public boolean equals(Object obj) {
47 if (!(obj instanceof PackageUserKey)) return false;
48 PackageUserKey otherKey = (PackageUserKey) obj;
49 return mPackageName.equals(otherKey.mPackageName) && mUser.equals(otherKey.mUser);
50 }
51}