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 22 import com.android.launcher3.compat.UserHandleCompat; 23 import com.android.launcher3.compat.UserManagerCompat; 24 25 import java.util.Arrays; 26 27 public class ComponentKey { 28 29 public final ComponentName componentName; 30 public final UserHandleCompat user; 31 32 private final int mHashCode; 33 ComponentKey(ComponentName componentName, UserHandleCompat user)34 public ComponentKey(ComponentName componentName, UserHandleCompat user) { 35 Preconditions.assertNotNull(componentName); 36 Preconditions.assertNotNull(user); 37 this.componentName = componentName; 38 this.user = user; 39 mHashCode = Arrays.hashCode(new Object[] {componentName, user}); 40 41 } 42 43 /** 44 * Creates a new component key from an encoded component key string in the form of 45 * [flattenedComponentString#userId]. If the userId is not present, then it defaults 46 * to the current user. 47 */ ComponentKey(Context context, String componentKeyStr)48 public ComponentKey(Context context, String componentKeyStr) { 49 int userDelimiterIndex = componentKeyStr.indexOf("#"); 50 if (userDelimiterIndex != -1) { 51 String componentStr = componentKeyStr.substring(0, userDelimiterIndex); 52 Long componentUser = Long.valueOf(componentKeyStr.substring(userDelimiterIndex + 1)); 53 componentName = ComponentName.unflattenFromString(componentStr); 54 user = UserManagerCompat.getInstance(context) 55 .getUserForSerialNumber(componentUser.longValue()); 56 } else { 57 // No user provided, default to the current user 58 componentName = ComponentName.unflattenFromString(componentKeyStr); 59 user = UserHandleCompat.myUserHandle(); 60 } 61 Preconditions.assertNotNull(componentName); 62 Preconditions.assertNotNull(user); 63 mHashCode = Arrays.hashCode(new Object[] {componentName, user}); 64 } 65 66 @Override hashCode()67 public int hashCode() { 68 return mHashCode; 69 } 70 71 @Override equals(Object o)72 public boolean equals(Object o) { 73 ComponentKey other = (ComponentKey) o; 74 return other.componentName.equals(componentName) && other.user.equals(user); 75 } 76 77 /** 78 * Encodes a component key as a string of the form [flattenedComponentString#userId]. 79 */ 80 @Override toString()81 public String toString() { 82 return componentName.flattenToString() + "#" + user; 83 } 84 }