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},
66     *     {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_FUNCTION_ON} and
67     *     {@link KeyEvent#META_SYM_ON}.
68     */
69    public KeyboardShortcutInfo(CharSequence label, int keycode, int modifiers) {
70        this(label, null, keycode, modifiers);
71    }
72
73    /**
74     * @param label The label that identifies the action performed by this shortcut.
75     * @param baseCharacter The character that triggers the shortcut.
76     * @param modifiers The set of modifiers that, combined with the key, trigger the shortcut.
77     *     These should be a combination of {@link KeyEvent#META_CTRL_ON},
78     *     {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_META_ON},
79     *     {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_FUNCTION_ON} and
80     *     {@link KeyEvent#META_SYM_ON}.
81     */
82    public KeyboardShortcutInfo(CharSequence label, char baseCharacter, int modifiers) {
83        mLabel = label;
84        checkArgument(baseCharacter != MIN_VALUE);
85        mBaseCharacter = baseCharacter;
86        mKeycode = KeyEvent.KEYCODE_UNKNOWN;
87        mModifiers = modifiers;
88        mIcon = null;
89    }
90
91    private KeyboardShortcutInfo(Parcel source) {
92        mLabel = source.readCharSequence();
93        mIcon = source.readParcelable(null);
94        mBaseCharacter = (char) source.readInt();
95        mKeycode = source.readInt();
96        mModifiers = source.readInt();
97    }
98
99    /**
100     * Returns the label to be used to describe this shortcut.
101     */
102    @Nullable
103    public CharSequence getLabel() {
104        return mLabel;
105    }
106
107    /**
108     * Returns the icon to be used to describe this shortcut.
109     *
110     * @hide
111     */
112    @Nullable
113    public Icon getIcon() {
114        return mIcon;
115    }
116
117    /**
118     * Returns the base keycode that, combined with the modifiers, triggers this shortcut. If the
119     * base character was set instead, returns {@link KeyEvent#KEYCODE_UNKNOWN}. Valid keycodes are
120     * defined as constants in {@link KeyEvent}.
121     */
122    public int getKeycode() {
123        return mKeycode;
124    }
125
126    /**
127     * Returns the base character that, combined with the modifiers, triggers this shortcut. If the
128     * keycode was set instead, returns {@link Character#MIN_VALUE}.
129     */
130    public char getBaseCharacter() {
131        return mBaseCharacter;
132    }
133
134    /**
135     * Returns the set of modifiers that, combined with the key, trigger this shortcut. These can
136     * be a combination of {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_SHIFT_ON},
137     * {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_ALT_ON},
138     * {@link KeyEvent#META_FUNCTION_ON} and {@link KeyEvent#META_SYM_ON}.
139     */
140    public int getModifiers() {
141        return mModifiers;
142    }
143
144    @Override
145    public int describeContents() {
146        return 0;
147    }
148
149    @Override
150    public void writeToParcel(Parcel dest, int flags) {
151        dest.writeCharSequence(mLabel);
152        dest.writeParcelable(mIcon, 0);
153        dest.writeInt(mBaseCharacter);
154        dest.writeInt(mKeycode);
155        dest.writeInt(mModifiers);
156    }
157
158    public static final Creator<KeyboardShortcutInfo> CREATOR =
159            new Creator<KeyboardShortcutInfo>() {
160        public KeyboardShortcutInfo createFromParcel(Parcel source) {
161            return new KeyboardShortcutInfo(source);
162        }
163        public KeyboardShortcutInfo[] newArray(int size) {
164            return new KeyboardShortcutInfo[size];
165        }
166    };
167}