blob: 2c9006deb1899849e8e792d6184810ca95ad2105 [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;
33 private final int mModifiers;
34
35 /**
36 * @param label The label that identifies the action performed by this shortcut.
37 * @param icon An icon that identifies the action performed by this shortcut.
38 * @param baseCharacter The character that triggers the shortcut.
39 * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut.
40 * These should be a combination of {@link KeyEvent#META_CTRL_ON},
41 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON} and
42 * {@link KeyEvent#META_ALT_ON}.
43 *
44 * @hide
45 */
46 public KeyboardShortcutInfo(
47 @Nullable CharSequence label, @Nullable Icon icon, char baseCharacter, int modifiers) {
48 mLabel = label;
49 mIcon = icon;
50 checkArgument(baseCharacter != MIN_VALUE);
51 mBaseCharacter = baseCharacter;
52 mModifiers = modifiers;
53 }
54
55 /**
56 * Convenience constructor for shortcuts with a label and no icon.
57 *
58 * @param label The label that identifies the action performed by this shortcut.
59 * @param baseCharacter The character that triggers the shortcut.
60 * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut.
61 * These should be a combination of {@link KeyEvent#META_CTRL_ON},
62 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON} and
63 * {@link KeyEvent#META_ALT_ON}.
64 */
65 public KeyboardShortcutInfo(CharSequence label, char baseCharacter, int modifiers) {
66 mLabel = label;
67 checkArgument(baseCharacter != MIN_VALUE);
68 mBaseCharacter = baseCharacter;
69 mModifiers = modifiers;
70 mIcon = null;
71 }
72
73 private KeyboardShortcutInfo(Parcel source) {
74 mLabel = source.readCharSequence();
75 mIcon = (Icon) source.readParcelable(null);
76 mBaseCharacter = (char) source.readInt();
77 mModifiers = source.readInt();
78 }
79
80 /**
81 * Returns the label to be used to describe this shortcut.
82 */
83 @Nullable
84 public CharSequence getLabel() {
85 return mLabel;
86 }
87
88 /**
89 * Returns the icon to be used to describe this shortcut.
90 *
91 * @hide
92 */
93 @Nullable
94 public Icon getIcon() {
95 return mIcon;
96 }
97
98 /**
99 * Returns the base character that, combined with the modifiers, triggers this shortcut.
100 */
101 public char getBaseCharacter() {
102 return mBaseCharacter;
103 }
104
105 /**
106 * Returns the set of modifiers that, combined with the key, trigger this shortcut.
107 */
108 public int getModifiers() {
109 return mModifiers;
110 }
111
112 @Override
113 public int describeContents() {
114 return 0;
115 }
116
117 @Override
118 public void writeToParcel(Parcel dest, int flags) {
119 dest.writeCharSequence(mLabel);
120 dest.writeParcelable(mIcon, 0);
121 dest.writeInt(mBaseCharacter);
122 dest.writeInt(mModifiers);
123 }
124
125 public static final Creator<KeyboardShortcutInfo> CREATOR =
126 new Creator<KeyboardShortcutInfo>() {
127 public KeyboardShortcutInfo createFromParcel(Parcel source) {
128 return new KeyboardShortcutInfo(source);
129 }
130 public KeyboardShortcutInfo[] newArray(int size) {
131 return new KeyboardShortcutInfo[size];
132 }
133 };
134}