1/*
2 * Copyright (C) 2012 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
17package com.android.inputmethod.event;
18
19/**
20 * Class representing a generic input event as handled by Latin IME.
21 *
22 * This contains information about the origin of the event, but it is generalized and should
23 * represent a software keypress, hardware keypress, or d-pad move alike.
24 * Very importantly, this does not necessarily result in inputting one character, or even anything
25 * at all - it may be a dead key, it may be a partial input, it may be a special key on the
26 * keyboard, it may be a cancellation of a keypress (e.g. in a soft keyboard the finger of the
27 * user has slid out of the key), etc. It may also be a batch input from a gesture or handwriting
28 * for example.
29 * The combiner should figure out what to do with this.
30 */
31public class Event {
32    // Should the types below be represented by separate classes instead? It would be cleaner
33    // but probably a bit too much
34    // An event we don't handle in Latin IME, for example pressing Ctrl on a hardware keyboard.
35    final public static int EVENT_NOT_HANDLED = 0;
36    // A character that is already final, for example pressing an alphabetic character on a
37    // hardware qwerty keyboard.
38    final public static int EVENT_COMMITTABLE = 1;
39    // A dead key, which means a character that should combine with what is coming next. Examples
40    // include the "^" character on an azerty keyboard which combines with "e" to make "ê", or
41    // AltGr+' on a dvorak international keyboard which combines with "e" to make "é". This is
42    // true regardless of the language or combining mode, and should be seen as a property of the
43    // key - a dead key followed by another key with which it can combine should be regarded as if
44    // the keyboard actually had such a key.
45    final public static int EVENT_DEAD = 2;
46    // A toggle event is triggered by a key that affects the previous character. An example would
47    // be a numeric key on a 10-key keyboard, which would toggle between 1 - a - b - c with
48    // repeated presses.
49    final public static int EVENT_TOGGLE = 3;
50    // A mode event instructs the combiner to change modes. The canonical example would be the
51    // hankaku/zenkaku key on a Japanese keyboard, or even the caps lock key on a qwerty keyboard
52    // if handled at the combiner level.
53    final public static int EVENT_MODE_KEY = 4;
54
55    final private static int NOT_A_CODE_POINT = 0;
56
57    final private int mType; // The type of event - one of the constants above
58    // The code point associated with the event, if relevant. This is a unicode code point, and
59    // has nothing to do with other representations of the key. It is only relevant if this event
60    // is the right type: COMMITTABLE or DEAD or TOGGLE, but for a mode key like hankaku/zenkaku or
61    // ctrl, there is no code point associated so this should be NOT_A_CODE_POINT to avoid
62    // unintentional use of its value when it's not relevant.
63    final public int mCodePoint;
64    // The next event, if any. Null if there is no next event yet.
65    final public Event mNextEvent;
66
67    // This method is private - to create a new event, use one of the create* utility methods.
68    private Event(final int type, final int codePoint, final Event next) {
69        mType = type;
70        mCodePoint = codePoint;
71        mNextEvent = next;
72    }
73
74    public static Event createDeadEvent(final int codePoint, final Event next) {
75        return new Event(EVENT_DEAD, codePoint, next);
76    }
77
78    public static Event createCommittableEvent(final int codePoint, final Event next) {
79        return new Event(EVENT_COMMITTABLE, codePoint, next);
80    }
81
82    public static Event createNotHandledEvent() {
83        return new Event(EVENT_NOT_HANDLED, NOT_A_CODE_POINT, null);
84    }
85
86    public boolean isCommittable() {
87        return EVENT_COMMITTABLE == mType;
88    }
89
90    public boolean isDead() {
91        return EVENT_DEAD == mType;
92    }
93}
94