1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 import android.content.ComponentName; 20 import android.text.TextUtils; 21 22 import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType; 23 24 import com.google.common.base.Objects; 25 26 /** 27 * A data class for containing {@link ComponentName#flattenToString()} and 28 * {@link UserShortcutType}. Represents the preferred shortcuts of the service or activity. 29 */ 30 public class PreferredShortcut { 31 32 private static final char COMPONENT_NAME_SEPARATOR = ':'; 33 private static final TextUtils.SimpleStringSplitter sStringColonSplitter = 34 new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); 35 36 /** 37 * Creates a {@link PreferredShortcut} from a encoded string described in {@link #toString()}. 38 * 39 * @param preferredShortcutString A string conform to the format described in {@link 40 * #toString()} 41 * @return A {@link PreferredShortcut} with the specified value 42 * @throws IllegalArgumentException If preferredShortcutString does not conform to the format 43 * described in {@link #toString()} 44 */ fromString(String preferredShortcutString)45 public static PreferredShortcut fromString(String preferredShortcutString) { 46 sStringColonSplitter.setString(preferredShortcutString); 47 if (sStringColonSplitter.hasNext()) { 48 final String componentName = sStringColonSplitter.next(); 49 final int type = Integer.parseInt(sStringColonSplitter.next()); 50 return new PreferredShortcut(componentName, type); 51 } 52 53 throw new IllegalArgumentException( 54 "Invalid PreferredShortcut string: " + preferredShortcutString); 55 } 56 57 /** The format of {@link ComponentName#flattenToString()} */ 58 private String mComponentName; 59 /** The format of {@link UserShortcutType} */ 60 private int mType; 61 PreferredShortcut(String componentName, int type)62 public PreferredShortcut(String componentName, int type) { 63 mComponentName = componentName; 64 mType = type; 65 } 66 getComponentName()67 public String getComponentName() { 68 return mComponentName; 69 } 70 getType()71 public int getType() { 72 return mType; 73 } 74 75 @Override toString()76 public String toString() { 77 return mComponentName + COMPONENT_NAME_SEPARATOR + mType; 78 } 79 80 @Override equals(Object o)81 public boolean equals(Object o) { 82 if (this == o) { 83 return true; 84 } 85 if (o == null || getClass() != o.getClass()) { 86 return false; 87 } 88 PreferredShortcut that = (PreferredShortcut) o; 89 return mType == that.mType && Objects.equal(mComponentName, that.mComponentName); 90 } 91 92 @Override hashCode()93 public int hashCode() { 94 return Objects.hashCode(mComponentName, mType); 95 } 96 } 97