1 package com.android.launcher3.util;
2 
3 /**
4  * Copyright (C) 2015 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import com.android.launcher3.compat.UserHandleCompat;
22 import com.android.launcher3.compat.UserManagerCompat;
23 
24 import java.util.Arrays;
25 
26 public class ComponentKey {
27 
28     public final ComponentName componentName;
29     public final UserHandleCompat user;
30 
31     private final int mHashCode;
32 
ComponentKey(ComponentName componentName, UserHandleCompat user)33     public ComponentKey(ComponentName componentName, UserHandleCompat user) {
34         assert (componentName != null);
35         assert (user != null);
36         this.componentName = componentName;
37         this.user = user;
38         mHashCode = Arrays.hashCode(new Object[] {componentName, user});
39 
40     }
41 
42     /**
43      * Creates a new component key from an encoded component key string in the form of
44      * [flattenedComponentString#userId].  If the userId is not present, then it defaults
45      * to the current user.
46      */
ComponentKey(Context context, String componentKeyStr)47     public ComponentKey(Context context, String componentKeyStr) {
48         int userDelimiterIndex = componentKeyStr.indexOf("#");
49         if (userDelimiterIndex != -1) {
50             String componentStr = componentKeyStr.substring(0, userDelimiterIndex);
51             Long componentUser = Long.valueOf(componentKeyStr.substring(userDelimiterIndex + 1));
52             componentName = ComponentName.unflattenFromString(componentStr);
53             user = UserManagerCompat.getInstance(context)
54                     .getUserForSerialNumber(componentUser.longValue());
55         } else {
56             // No user provided, default to the current user
57             componentName = ComponentName.unflattenFromString(componentKeyStr);
58             user = UserHandleCompat.myUserHandle();
59         }
60         mHashCode = Arrays.hashCode(new Object[] {componentName, user});
61     }
62 
63     /**
64      * Encodes a component key as a string of the form [flattenedComponentString#userId].
65      */
flattenToString(Context context)66     public String flattenToString(Context context) {
67         String flattened = componentName.flattenToString();
68         if (user != null) {
69             flattened += "#" + UserManagerCompat.getInstance(context).getSerialNumberForUser(user);
70         }
71         return flattened;
72     }
73 
74     @Override
hashCode()75     public int hashCode() {
76         return mHashCode;
77     }
78 
79     @Override
equals(Object o)80     public boolean equals(Object o) {
81         ComponentKey other = (ComponentKey) o;
82         return other.componentName.equals(componentName) && other.user.equals(user);
83     }
84 }