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