DeadKeyCombiner.java revision 99aff0af98e66b1d8515225a103f5beb84d098b9
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 (TextUtils.isEmpty(mDeadSequence)) {
36            if (event.isDead()) {
37                mDeadSequence.appendCodePoint(event.mCodePoint);
38            }
39            return Event.createConsumedEvent(event);
40        } else {
41            // TODO: Allow combining for several dead chars rather than only the first one.
42            // The framework doesn't know how to do this now.
43            final int deadCodePoint = mDeadSequence.codePointAt(0);
44            mDeadSequence.setLength(0);
45            final int resultingCodePoint =
46                    KeyCharacterMap.getDeadChar(deadCodePoint, event.mCodePoint);
47            if (0 == resultingCodePoint) {
48                // We can't combine both characters. We need to commit the dead key as a separate
49                // character, and the next char too unless it's a space (because as a special case,
50                // dead key + space should result in only the dead key being committed - that's
51                // how dead keys work).
52                // If the event is a space, we should commit the dead char alone, but if it's
53                // not, we need to commit both.
54                // TODO: this is not necessarily triggered by hardware key events, so it's not
55                // a good idea to masquerade as one. This should be typed as a software
56                // composite event or something.
57                return Event.createHardwareKeypressEvent(deadCodePoint, event.mKeyCode,
58                        Constants.CODE_SPACE == event.mCodePoint ? null : event /* next */,
59                        false /* isKeyRepeat */);
60            } else {
61                // We could combine the characters.
62                return Event.createHardwareKeypressEvent(resultingCodePoint, event.mKeyCode,
63                        null /* next */, false /* isKeyRepeat */);
64            }
65        }
66    }
67
68    @Override
69    public void reset() {
70        mDeadSequence.setLength(0);
71    }
72
73    @Override
74    public CharSequence getCombiningStateFeedback() {
75        return mDeadSequence;
76    }
77}
78