CombinerChain.java revision 1db3d860ca0d7380c5a6a3a0f95751565f23043b
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 * Process an event through the combining chain, and return a processed event to apply. 85 * @param previousEvents the list of previous events in this composition 86 * @param newEvent the new event to process 87 */ 88 public Event 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 return event; 101 } 102 103 /** 104 * Apply a processed event. 105 * @param event the event to be applied 106 */ 107 public void applyProcessedEvent(final Event event) { 108 if (null != event) { 109 // TODO: figure out the generic way of doing this 110 if (Constants.CODE_DELETE == event.mKeyCode) { 111 final int length = mCombinedText.length(); 112 if (length > 0) { 113 final int lastCodePoint = mCombinedText.codePointBefore(length); 114 mCombinedText.delete(length - Character.charCount(lastCodePoint), length); 115 } 116 } else { 117 final CharSequence textToCommit = event.getTextToCommit(); 118 if (!TextUtils.isEmpty(textToCommit)) { 119 mCombinedText.append(textToCommit); 120 } 121 } 122 } 123 mStateFeedback.clear(); 124 for (int i = mCombiners.size() - 1; i >= 0; --i) { 125 mStateFeedback.append(mCombiners.get(i).getCombiningStateFeedback()); 126 } 127 } 128 129 /** 130 * Get the char sequence that should be displayed as the composing word. It may include 131 * styling spans. 132 */ 133 public CharSequence getComposingWordWithCombiningFeedback() { 134 final SpannableStringBuilder s = new SpannableStringBuilder(mCombinedText); 135 return s.append(mStateFeedback); 136 } 137 138 public static Combiner[] createCombiners(final String spec) { 139 if (TextUtils.isEmpty(spec)) { 140 return new Combiner[0]; 141 } 142 final String[] combinerDescriptors = spec.split(COMBINER_SPEC_SEPARATOR); 143 final Combiner[] combiners = new Combiner[combinerDescriptors.length]; 144 int i = 0; 145 for (final String combinerDescriptor : combinerDescriptors) { 146 final Class<? extends Combiner> combinerClass = 147 IMPLEMENTED_COMBINERS.get(combinerDescriptor); 148 if (null == combinerClass) { 149 throw new RuntimeException("Unknown combiner descriptor: " + combinerDescriptor); 150 } 151 try { 152 combiners[i++] = combinerClass.newInstance(); 153 } catch (InstantiationException e) { 154 throw new RuntimeException("Unable to instantiate combiner: " + combinerDescriptor, 155 e); 156 } catch (IllegalAccessException e) { 157 throw new RuntimeException("Unable to instantiate combiner: " + combinerDescriptor, 158 e); 159 } 160 } 161 return combiners; 162 } 163} 164