DeadKeyCombiner.java revision bfa6f9533ca0d7ebb2650639765c77a77f37e0d9
1/*
2 * Copyright (C) 2013 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.text.TextUtils;
20import android.view.KeyCharacterMap;
21
22import com.android.inputmethod.latin.Constants;
23
24import java.util.ArrayList;
25
26/**
27 * A combiner that handles dead keys.
28 */
29public class DeadKeyCombiner implements Combiner {
30    // TODO: make this a list of events instead
31    final StringBuilder mDeadSequence = new StringBuilder();
32
33    @Override
34    public Event processEvent(final ArrayList<Event> previousEvents, final Event event) {
35        if (null == event) return null; // Just in case some combiner is broken
36        if (TextUtils.isEmpty(mDeadSequence)) {
37            if (event.isDead()) {
38                mDeadSequence.appendCodePoint(event.mCodePoint);
39            }
40            return event;
41        } else {
42            // TODO: Allow combining for several dead chars rather than only the first one.
43            // The framework doesn't know how to do this now.
44            final int deadCodePoint = mDeadSequence.codePointAt(0);
45            mDeadSequence.setLength(0);
46            final int resultingCodePoint =
47                    KeyCharacterMap.getDeadChar(deadCodePoint, event.mCodePoint);
48            if (0 == resultingCodePoint) {
49                // We can't combine both characters. We need to commit the dead key as a separate
50                // character, and the next char too unless it's a space (because as a special case,
51                // dead key + space should result in only the dead key being committed - that's
52                // how dead keys work).
53                // If the event is a space, we should commit the dead char alone, but if it's
54                // not, we need to commit both.
55                // TODO: this is not necessarily triggered by hardware key events, so it's not
56                // a good idea to masquerade as one. This should be typed as a software
57                // composite event or something.
58                return Event.createHardwareKeypressEvent(deadCodePoint, event.mKeyCode,
59                        Constants.CODE_SPACE == event.mCodePoint ? null : event /* next */,
60                        false /* isKeyRepeat */);
61            } else {
62                // We could combine the characters.
63                return Event.createHardwareKeypressEvent(resultingCodePoint, event.mKeyCode,
64                        null /* next */, false /* isKeyRepeat */);
65            }
66        }
67    }
68
69    @Override
70    public void reset() {
71        mDeadSequence.setLength(0);
72    }
73
74    @Override
75    public CharSequence getCombiningStateFeedback() {
76        return mDeadSequence;
77    }
78}
79