KeyboardShortcutInfo.java revision 1d648a14a410f21eb679982c4c0977201d97aac1
1/* 2 * Copyright (C) 2015 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 */ 16package android.view; 17 18import android.annotation.Nullable; 19import android.graphics.drawable.Icon; 20import android.os.Parcel; 21import android.os.Parcelable; 22 23import static com.android.internal.util.Preconditions.checkArgument; 24import static java.lang.Character.MIN_VALUE; 25 26/** 27 * Information about a Keyboard Shortcut. 28 */ 29public final class KeyboardShortcutInfo implements Parcelable { 30 private final CharSequence mLabel; 31 private final Icon mIcon; 32 private final char mBaseCharacter; 33 private final int mKeycode; 34 private final int mModifiers; 35 36 /** 37 * @param label The label that identifies the action performed by this shortcut. 38 * @param icon An icon that identifies the action performed by this shortcut. 39 * @param keycode The keycode that triggers the shortcut. This should be a valid constant 40 * defined in {@link KeyEvent}. 41 * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut. 42 * These should be a combination of {@link KeyEvent#META_CTRL_ON}, 43 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON}, 44 * {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_FUNCTION_ON} and 45 * {@link KeyEvent#META_SYM_ON}. 46 * 47 * @hide 48 */ 49 public KeyboardShortcutInfo( 50 @Nullable CharSequence label, @Nullable Icon icon, int keycode, int modifiers) { 51 mLabel = label; 52 mIcon = icon; 53 mBaseCharacter = MIN_VALUE; 54 checkArgument(keycode >= KeyEvent.KEYCODE_UNKNOWN && keycode <= KeyEvent.getMaxKeyCode()); 55 mKeycode = keycode; 56 mModifiers = modifiers; 57 } 58 59 /** 60 * @param label The label that identifies the action performed by this shortcut. 61 * @param keycode The keycode that triggers the shortcut. This should be a valid constant 62 * defined in {@link KeyEvent}. 63 * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut. 64 * These should be a combination of {@link KeyEvent#META_CTRL_ON}, 65 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON} and 66 * {@link KeyEvent#META_ALT_ON}. 67 */ 68 public KeyboardShortcutInfo(CharSequence label, int keycode, int modifiers) { 69 this(label, null, keycode, modifiers); 70 } 71 72 /** 73 * @param label The label that identifies the action performed by this shortcut. 74 * @param baseCharacter The character that triggers the shortcut. 75 * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut. 76 * These should be a combination of {@link KeyEvent#META_CTRL_ON}, 77 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON} and 78 * {@link KeyEvent#META_ALT_ON}. 79 */ 80 public KeyboardShortcutInfo(CharSequence label, char baseCharacter, int modifiers) { 81 mLabel = label; 82 checkArgument(baseCharacter != MIN_VALUE); 83 mBaseCharacter = baseCharacter; 84 mKeycode = KeyEvent.KEYCODE_UNKNOWN; 85 mModifiers = modifiers; 86 mIcon = null; 87 } 88 89 private KeyboardShortcutInfo(Parcel source) { 90 mLabel = source.readCharSequence(); 91 mIcon = source.readParcelable(null); 92 mBaseCharacter = (char) source.readInt(); 93 mKeycode = source.readInt(); 94 mModifiers = source.readInt(); 95 } 96 97 /** 98 * Returns the label to be used to describe this shortcut. 99 */ 100 @Nullable 101 public CharSequence getLabel() { 102 return mLabel; 103 } 104 105 /** 106 * Returns the icon to be used to describe this shortcut. 107 * 108 * @hide 109 */ 110 @Nullable 111 public Icon getIcon() { 112 return mIcon; 113 } 114 115 /** 116 * Returns the base keycode that, combined with the modifiers, triggers this shortcut. If the 117 * base character was set instead, returns {@link KeyEvent#KEYCODE_UNKNOWN}. 118 */ 119 public int getKeycode() { 120 return mKeycode; 121 } 122 123 /** 124 * Returns the base character that, combined with the modifiers, triggers this shortcut. If the 125 * keycode was set instead, returns {@link Character#MIN_VALUE}. 126 */ 127 public char getBaseCharacter() { 128 return mBaseCharacter; 129 } 130 131 /** 132 * Returns the set of modifiers that, combined with the key, trigger this shortcut. 133 */ 134 public int getModifiers() { 135 return mModifiers; 136 } 137 138 @Override 139 public int describeContents() { 140 return 0; 141 } 142 143 @Override 144 public void writeToParcel(Parcel dest, int flags) { 145 dest.writeCharSequence(mLabel); 146 dest.writeParcelable(mIcon, 0); 147 dest.writeInt(mBaseCharacter); 148 dest.writeInt(mKeycode); 149 dest.writeInt(mModifiers); 150 } 151 152 public static final Creator<KeyboardShortcutInfo> CREATOR = 153 new Creator<KeyboardShortcutInfo>() { 154 public KeyboardShortcutInfo createFromParcel(Parcel source) { 155 return new KeyboardShortcutInfo(source); 156 } 157 public KeyboardShortcutInfo[] newArray(int size) { 158 return new KeyboardShortcutInfo[size]; 159 } 160 }; 161}