Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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 | */ |
| 16 | package android.view; |
| 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.graphics.drawable.Icon; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
| 23 | import static com.android.internal.util.Preconditions.checkArgument; |
| 24 | import static java.lang.Character.MIN_VALUE; |
| 25 | |
| 26 | /** |
| 27 | * Information about a Keyboard Shortcut. |
| 28 | */ |
| 29 | public final class KeyboardShortcutInfo implements Parcelable { |
| 30 | private final CharSequence mLabel; |
| 31 | private final Icon mIcon; |
| 32 | private final char mBaseCharacter; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 33 | private final int mKeycode; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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. |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 39 | * @param keycode The keycode that triggers the shortcut. This should be a valid constant |
| 40 | * defined in {@link KeyEvent}. |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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}, |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 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}. |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 46 | * |
| 47 | * @hide |
| 48 | */ |
| 49 | public KeyboardShortcutInfo( |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 50 | @Nullable CharSequence label, @Nullable Icon icon, int keycode, int modifiers) { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 51 | mLabel = label; |
| 52 | mIcon = icon; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 53 | mBaseCharacter = MIN_VALUE; |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 54 | checkArgument(keycode >= KeyEvent.KEYCODE_UNKNOWN && keycode <= KeyEvent.getMaxKeyCode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 55 | mKeycode = keycode; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 56 | mModifiers = modifiers; |
| 57 | } |
| 58 | |
| 59 | /** |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 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 | /** |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 84 | mKeycode = KeyEvent.KEYCODE_UNKNOWN; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 85 | mModifiers = modifiers; |
| 86 | mIcon = null; |
| 87 | } |
| 88 | |
| 89 | private KeyboardShortcutInfo(Parcel source) { |
| 90 | mLabel = source.readCharSequence(); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 91 | mIcon = source.readParcelable(null); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 92 | mBaseCharacter = (char) source.readInt(); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 93 | mKeycode = source.readInt(); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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 | /** |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 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}. |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 148 | dest.writeInt(mKeycode); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 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 | } |