Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame^] | 1 | package com.android.launcher3.util; |
| 2 | |
| 3 | import android.os.UserHandle; |
| 4 | import android.service.notification.StatusBarNotification; |
| 5 | |
| 6 | import com.android.launcher3.ItemInfo; |
| 7 | |
| 8 | import java.util.Arrays; |
| 9 | |
| 10 | /** Creates a hash key based on package name and user. */ |
| 11 | public 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 | } |