blob: c2bd347687a6c3e36f0047d5e2f02cba53b48ed4 [file] [log] [blame]
Clara Bayarri75e09792015-07-29 16:20:40 +01001/*
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;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080033 private final int mKeycode;
Clara Bayarri75e09792015-07-29 16:20:40 +010034 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 Bayarri4e850ff2016-03-02 11:12:32 -080039 * @param keycode The keycode that triggers the shortcut. This should be a valid constant
40 * defined in {@link KeyEvent}.
Clara Bayarri75e09792015-07-29 16:20:40 +010041 * @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 Bayarri4e850ff2016-03-02 11:12:32 -080043 * {@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 Bayarri75e09792015-07-29 16:20:40 +010046 *
47 * @hide
48 */
49 public KeyboardShortcutInfo(
Clara Bayarri4e850ff2016-03-02 11:12:32 -080050 @Nullable CharSequence label, @Nullable Icon icon, int keycode, int modifiers) {
Clara Bayarri75e09792015-07-29 16:20:40 +010051 mLabel = label;
52 mIcon = icon;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080053 mBaseCharacter = MIN_VALUE;
54 checkArgument(keycode > KeyEvent.KEYCODE_UNKNOWN && keycode <= KeyEvent.getMaxKeyCode());
55 mKeycode = keycode;
Clara Bayarri75e09792015-07-29 16:20:40 +010056 mModifiers = modifiers;
57 }
58
59 /**
Clara Bayarri4e850ff2016-03-02 11:12:32 -080060 * @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 Bayarri75e09792015-07-29 16:20:40 +010073 * @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 Bayarri4e850ff2016-03-02 11:12:32 -080084 mKeycode = KeyEvent.KEYCODE_UNKNOWN;
Clara Bayarri75e09792015-07-29 16:20:40 +010085 mModifiers = modifiers;
86 mIcon = null;
87 }
88
89 private KeyboardShortcutInfo(Parcel source) {
90 mLabel = source.readCharSequence();
Clara Bayarri4e850ff2016-03-02 11:12:32 -080091 mIcon = source.readParcelable(null);
Clara Bayarri75e09792015-07-29 16:20:40 +010092 mBaseCharacter = (char) source.readInt();
Clara Bayarri4e850ff2016-03-02 11:12:32 -080093 mKeycode = source.readInt();
Clara Bayarri75e09792015-07-29 16:20:40 +010094 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 Bayarri4e850ff2016-03-02 11:12:32 -0800116 * 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 Bayarri75e09792015-07-29 16:20:40 +0100126 */
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 Bayarri4e850ff2016-03-02 11:12:32 -0800148 dest.writeInt(mKeycode);
Clara Bayarri75e09792015-07-29 16:20:40 +0100149 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}