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
19import android.util.SparseArray;
20import android.view.KeyEvent;
21
22import com.android.inputmethod.latin.Constants;
23import com.android.inputmethod.latin.LatinIME;
24import com.android.inputmethod.latin.utils.CollectionUtils;
25
26import java.util.ArrayList;
27
28/**
29 * This class implements the logic between receiving events and generating code points.
30 *
31 * Event sources are multiple. It may be a hardware keyboard, a D-PAD, a software keyboard,
32 * or any exotic input source.
33 * This class will orchestrate the decoding chain that starts with an event and ends up with
34 * a stream of code points + decoding state.
35 */
36public class EventInterpreter {
37    // TODO: Implement an object pool for events, as we'll create a lot of them
38    // TODO: Create a combiner
39    // TODO: Create an object type to represent input material + visual feedback + decoding state
40    // TODO: Create an interface to call back to Latin IME through the above object
41
42    final EventDecoderSpec mDecoderSpec;
43    final SparseArray<HardwareEventDecoder> mHardwareEventDecoders;
44    final SoftwareEventDecoder mSoftwareEventDecoder;
45    final LatinIME mLatinIme;
46    final ArrayList<Combiner> mCombiners;
47
48    /**
49     * Create a default interpreter.
50     *
51     * This creates a default interpreter that does nothing. A default interpreter should normally
52     * only be used for fallback purposes, when we really don't know what we want to do with input.
53     *
54     * @param latinIme a reference to the ime.
55     */
56    public EventInterpreter(final LatinIME latinIme) {
57        this(null, latinIme);
58    }
59
60    /**
61     * Create an event interpreter according to a specification.
62     *
63     * The specification contains information about what to do with events. Typically, it will
64     * contain information about the type of keyboards - for example, if hardware keyboard(s) is/are
65     * attached, their type will be included here so that the decoder knows what to do with each
66     * keypress (a 10-key keyboard is not handled like a qwerty-ish keyboard).
67     * It also contains information for combining characters. For example, if the input language
68     * is Japanese, the specification will typically request kana conversion.
69     * Also note that the specification can be null. This means that we need to create a default
70     * interpreter that does no specific combining, and assumes the most common cases.
71     *
72     * @param specification the specification for event interpretation. null for default.
73     * @param latinIme a reference to the ime.
74     */
75    public EventInterpreter(final EventDecoderSpec specification, final LatinIME latinIme) {
76        mDecoderSpec = null != specification ? specification : new EventDecoderSpec();
77        // For both, we expect to have only one decoder in almost all cases, hence the default
78        // capacity of 1.
79        mHardwareEventDecoders = new SparseArray<HardwareEventDecoder>(1);
80        mSoftwareEventDecoder = new SoftwareKeyboardEventDecoder();
81        mCombiners = CollectionUtils.newArrayList();
82        mCombiners.add(new DeadKeyCombiner());
83        mLatinIme = latinIme;
84    }
85
86    // Helper method to decode a hardware key event into a generic event, and execute any
87    // necessary action.
88    public boolean onHardwareKeyEvent(final KeyEvent hardwareKeyEvent) {
89        final Event decodedEvent = getHardwareKeyEventDecoder(hardwareKeyEvent.getDeviceId())
90                .decodeHardwareKey(hardwareKeyEvent);
91        return onEvent(decodedEvent);
92    }
93
94    public boolean onSoftwareEvent() {
95        final Event decodedEvent = getSoftwareEventDecoder().decodeSoftwareEvent();
96        return onEvent(decodedEvent);
97    }
98
99    private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) {
100        final HardwareEventDecoder decoder = mHardwareEventDecoders.get(deviceId);
101        if (null != decoder) return decoder;
102        // TODO: create the decoder according to the specification
103        final HardwareEventDecoder newDecoder = new HardwareKeyboardEventDecoder(deviceId);
104        mHardwareEventDecoders.put(deviceId, newDecoder);
105        return newDecoder;
106    }
107
108    private SoftwareEventDecoder getSoftwareEventDecoder() {
109        // Within the context of Latin IME, since we never present several software interfaces
110        // at the time, we should never need multiple software event decoders at a time.
111        return mSoftwareEventDecoder;
112    }
113
114    private boolean onEvent(final Event event) {
115        Event currentlyProcessingEvent = event;
116        boolean processed = false;
117        for (int i = 0; i < mCombiners.size(); ++i) {
118            currentlyProcessingEvent = mCombiners.get(i).combine(event);
119        }
120        while (null != currentlyProcessingEvent) {
121            if (currentlyProcessingEvent.isCommittable()) {
122                mLatinIme.onCodeInput(currentlyProcessingEvent.mCodePoint,
123                        Constants.EXTERNAL_KEYBOARD_COORDINATE,
124                        Constants.EXTERNAL_KEYBOARD_COORDINATE);
125                processed = true;
126            } else if (event.isDead()) {
127                processed = true;
128            }
129            currentlyProcessingEvent = currentlyProcessingEvent.mNextEvent;
130        }
131        return processed;
132    }
133}
134