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 17 package com.android.inputmethod.event; 18 19 import android.text.TextUtils; 20 import android.view.KeyCharacterMap; 21 22 import com.android.inputmethod.latin.Constants; 23 24 import java.util.ArrayList; 25 26 import javax.annotation.Nonnull; 27 28 /** 29 * A combiner that handles dead keys. 30 */ 31 public class DeadKeyCombiner implements Combiner { 32 // TODO: make this a list of events instead 33 final StringBuilder mDeadSequence = new StringBuilder(); 34 35 @Override 36 @Nonnull 37 public Event processEvent(final ArrayList<Event> previousEvents, final Event event) { 38 if (TextUtils.isEmpty(mDeadSequence)) { 39 // No dead char is currently being tracked: this is the most common case. 40 if (event.isDead()) { 41 // The event was a dead key. Start tracking it. 42 mDeadSequence.appendCodePoint(event.mCodePoint); 43 return Event.createConsumedEvent(event); 44 } 45 // Regular keystroke when not keeping track of a dead key. Simply said, there are 46 // no dead keys at all in the current input, so this combiner has nothing to do and 47 // simply returns the event as is. The majority of events will go through this path. 48 return event; 49 } else { 50 // TODO: Allow combining for several dead chars rather than only the first one. 51 // The framework doesn't know how to do this now. 52 final int deadCodePoint = mDeadSequence.codePointAt(0); 53 mDeadSequence.setLength(0); 54 final int resultingCodePoint = 55 KeyCharacterMap.getDeadChar(deadCodePoint, event.mCodePoint); 56 if (0 == resultingCodePoint) { 57 // We can't combine both characters. We need to commit the dead key as a separate 58 // character, and the next char too unless it's a space (because as a special case, 59 // dead key + space should result in only the dead key being committed - that's 60 // how dead keys work). 61 // If the event is a space, we should commit the dead char alone, but if it's 62 // not, we need to commit both. 63 // TODO: this is not necessarily triggered by hardware key events, so it's not 64 // a good idea to masquerade as one. This should be typed as a software 65 // composite event or something. 66 return Event.createHardwareKeypressEvent(deadCodePoint, event.mKeyCode, 67 Constants.CODE_SPACE == event.mCodePoint ? null : event /* next */, 68 false /* isKeyRepeat */); 69 } else { 70 // We could combine the characters. 71 return Event.createHardwareKeypressEvent(resultingCodePoint, event.mKeyCode, 72 null /* next */, false /* isKeyRepeat */); 73 } 74 } 75 } 76 77 @Override 78 public void reset() { 79 mDeadSequence.setLength(0); 80 } 81 82 @Override 83 public CharSequence getCombiningStateFeedback() { 84 return mDeadSequence; 85 } 86 } 87