• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.UserHandle;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 import java.util.Arrays;
26 
27 public class ComponentKey {
28 
29     public final ComponentName componentName;
30     public final UserHandle user;
31 
32     private final int mHashCode;
33 
ComponentKey(ComponentName componentName, UserHandle user)34     public ComponentKey(ComponentName componentName, UserHandle user) {
35         if (componentName == null || user == null) {
36             throw new NullPointerException();
37         }
38         this.componentName = componentName;
39         this.user = user;
40         mHashCode = Arrays.hashCode(new Object[] {componentName, user});
41 
42     }
43 
44     @Override
hashCode()45     public int hashCode() {
46         return mHashCode;
47     }
48 
49     @Override
equals(Object o)50     public boolean equals(Object o) {
51         ComponentKey other = (ComponentKey) o;
52         return other.componentName.equals(componentName) && other.user.equals(user);
53     }
54 
55     /**
56      * Encodes a component key as a string of the form [flattenedComponentString#userId].
57      */
58     @Override
toString()59     public String toString() {
60         return componentName.flattenToString() + "#" + user.hashCode();
61     }
62 
63     /**
64      * Parses and returns ComponentKey objected from string representation
65      * Returns null if string is not properly formatted
66      */
67     @Nullable
fromString(@onNull String str)68     public static ComponentKey fromString(@NonNull String str) {
69         int sep = str.indexOf('#');
70         if (sep < 0 || (sep + 1) >= str.length()) {
71             return null;
72         }
73         ComponentName componentName = ComponentName.unflattenFromString(str.substring(0, sep));
74         if (componentName == null) {
75             return null;
76         }
77         try {
78             return new ComponentKey(componentName,
79                     UserHandle.getUserHandleForUid(Integer.parseInt(str.substring(sep + 1))));
80         } catch (NumberFormatException ex) {
81             return null;
82         }
83     }
84 }