CombinerChain.java revision 4399849dea9f3cc1c8b1828739a0dd7bedc1f730
1/* 2 * Copyright (C) 2014 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.SpannableStringBuilder; 20import android.text.TextUtils; 21 22import com.android.inputmethod.latin.Constants; 23 24import java.util.ArrayList; 25import java.util.HashMap; 26 27/** 28 * This class implements the logic chain between receiving events and generating code points. 29 * 30 * Event sources are multiple. It may be a hardware keyboard, a D-PAD, a software keyboard, 31 * or any exotic input source. 32 * This class will orchestrate the composing chain that starts with an event as its input. Each 33 * composer will be given turns one after the other. 34 * The output is composed of two sequences of code points: the first, representing the already 35 * finished combining part, will be shown normally as the composing string, while the second is 36 * feedback on the composing state and will typically be shown with different styling such as 37 * a colored background. 38 */ 39public class CombinerChain { 40 // The already combined text, as described above 41 private StringBuilder mCombinedText; 42 // The feedback on the composing state, as described above 43 private SpannableStringBuilder mStateFeedback; 44 private final ArrayList<Combiner> mCombiners; 45 46 private static final HashMap<String, Class<? extends Combiner>> IMPLEMENTED_COMBINERS = 47 new HashMap<>(); 48 static { 49 IMPLEMENTED_COMBINERS.put("MyanmarReordering", MyanmarReordering.class); 50 } 51 private static final String COMBINER_SPEC_SEPARATOR = ";"; 52 53 /** 54 * Create an combiner chain. 55 * 56 * The combiner chain takes events as inputs and outputs code points and combining state. 57 * For example, if the input language is Japanese, the combining chain will typically perform 58 * kana conversion. This takes a string for initial text, taken to be present before the 59 * cursor: we'll start after this. 60 * 61 * @param initialText The text that has already been combined so far. 62 * @param combinerList A list of combiners to be applied in order. 63 */ 64 public CombinerChain(final String initialText, final Combiner... combinerList) { 65 mCombiners = new ArrayList<>(); 66 // The dead key combiner is always active, and always first 67 mCombiners.add(new DeadKeyCombiner()); 68 for (final Combiner combiner : combinerList) { 69 mCombiners.add(combiner); 70 } 71 mCombinedText = new StringBuilder(initialText); 72 mStateFeedback = new SpannableStringBuilder(); 73 } 74 75 public void reset() { 76 mCombinedText.setLength(0); 77 mStateFeedback.clear(); 78 for (final Combiner c : mCombiners) { 79 c.reset(); 80 } 81 } 82 83 /** 84 * Pass a new event through the whole chain. 85 * @param previousEvents the list of previous events in this composition 86 * @param newEvent the new event to process 87 */ 88 public void processEvent(final ArrayList<Event> previousEvents, final Event newEvent) { 89 final ArrayList<Event> modifiablePreviousEvents = new ArrayList<>(previousEvents); 90 Event event = newEvent; 91 for (final Combiner combiner : mCombiners) { 92 // A combiner can never return more than one event; it can return several 93 // code points, but they should be encapsulated within one event. 94 event = combiner.processEvent(modifiablePreviousEvents, event); 95 if (null == event) { 96 // Combiners return null if they eat the event. 97 break; 98 } 99 } 100 if (null != event) { 101 // TODO: figure out the generic way of doing this 102 if (Constants.CODE_DELETE == event.mKeyCode) { 103 final int length = mCombinedText.length(); 104 if (length > 0) { 105 final int lastCodePoint = mCombinedText.codePointBefore(length); 106 mCombinedText.delete(length - Character.charCount(lastCodePoint), length); 107 } 108 } else { 109 final CharSequence textToCommit = event.getTextToCommit(); 110 if (!TextUtils.isEmpty(textToCommit)) { 111 mCombinedText.append(textToCommit); 112 } 113 } 114 } 115 mStateFeedback.clear(); 116 for (int i = mCombiners.size() - 1; i >= 0; --i) { 117 mStateFeedback.append(mCombiners.get(i).getCombiningStateFeedback()); 118 } 119 } 120 121 /** 122 * Get the char sequence that should be displayed as the composing word. It may include 123 * styling spans. 124 */ 125 public CharSequence getComposingWordWithCombiningFeedback() { 126 final SpannableStringBuilder s = new SpannableStringBuilder(mCombinedText); 127 return s.append(mStateFeedback); 128 } 129 130 public static Combiner[] createCombiners(final String spec) { 131 if (TextUtils.isEmpty(spec)) { 132 return new Combiner[0]; 133 } 134 final String[] combinerDescriptors = spec.split(COMBINER_SPEC_SEPARATOR); 135 final Combiner[] combiners = new Combiner[combinerDescriptors.length]; 136 int i = 0; 137 for (final String combinerDescriptor : combinerDescriptors) { 138 final Class<? extends Combiner> combinerClass = 139 IMPLEMENTED_COMBINERS.get(combinerDescriptor); 140 if (null == combinerClass) { 141 throw new RuntimeException("Unknown combiner descriptor: " + combinerDescriptor); 142 } 143 try { 144 combiners[i++] = combinerClass.newInstance(); 145 } catch (InstantiationException e) { 146 throw new RuntimeException("Unable to instantiate combiner: " + combinerDescriptor, 147 e); 148 } catch (IllegalAccessException e) { 149 throw new RuntimeException("Unable to instantiate combiner: " + combinerDescriptor, 150 e); 151 } 152 } 153 return combiners; 154 } 155} 156