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