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; |
Tony Wickham | 18a1b5d | 2017-02-02 13:58:33 -0800 | [diff] [blame] | 7 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 8 | |
| 9 | import java.util.Arrays; |
| 10 | |
| 11 | /** Creates a hash key based on package name and user. */ |
| 12 | public class PackageUserKey { |
| 13 | |
Tony Wickham | 26b1746 | 2017-03-20 17:12:24 -0700 | [diff] [blame^] | 14 | public String mPackageName; |
| 15 | public UserHandle mUser; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 16 | private int mHashCode; |
| 17 | |
| 18 | public static PackageUserKey fromItemInfo(ItemInfo info) { |
| 19 | return new PackageUserKey(info.getTargetComponent().getPackageName(), info.user); |
| 20 | } |
| 21 | |
| 22 | public static PackageUserKey fromNotification(StatusBarNotification notification) { |
| 23 | return new PackageUserKey(notification.getPackageName(), notification.getUser()); |
| 24 | } |
| 25 | |
| 26 | public PackageUserKey(String packageName, UserHandle user) { |
| 27 | update(packageName, user); |
| 28 | } |
| 29 | |
| 30 | private void update(String packageName, UserHandle user) { |
| 31 | mPackageName = packageName; |
| 32 | mUser = user; |
| 33 | mHashCode = Arrays.hashCode(new Object[] {packageName, user}); |
| 34 | } |
| 35 | |
Tony Wickham | 18a1b5d | 2017-02-02 13:58:33 -0800 | [diff] [blame] | 36 | /** |
| 37 | * This should only be called to avoid new object creations in a loop. |
| 38 | * @return Whether this PackageUserKey was successfully updated - it shouldn't be used if not. |
| 39 | */ |
| 40 | public boolean updateFromItemInfo(ItemInfo info) { |
| 41 | if (DeepShortcutManager.supportsShortcuts(info)) { |
| 42 | update(info.getTargetComponent().getPackageName(), info.user); |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int hashCode() { |
| 50 | return mHashCode; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object obj) { |
| 55 | if (!(obj instanceof PackageUserKey)) return false; |
| 56 | PackageUserKey otherKey = (PackageUserKey) obj; |
| 57 | return mPackageName.equals(otherKey.mPackageName) && mUser.equals(otherKey.mUser); |
| 58 | } |
| 59 | } |