CombinerChain.java revision f2bb15b0ab212e1ef45be2d2ea6610cfa9c9f15c
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 com.android.inputmethod.latin.utils.CollectionUtils;
20
21import java.util.ArrayList;
22
23/**
24 * This class implements the logic chain between receiving events and generating code points.
25 *
26 * Event sources are multiple. It may be a hardware keyboard, a D-PAD, a software keyboard,
27 * or any exotic input source.
28 * This class will orchestrate the composing chain that starts with an event as its input. Each
29 * composer will be given turns one after the other.
30 * The output is composed of two sequences of code points: the first, representing the already
31 * finished combining part, will be shown normally as the composing string, while the second is
32 * feedback on the composing state and will typically be shown with different styling such as
33 * a colored background.
34 */
35public class CombinerChain {
36    // TODO: Create an object type to represent input material + visual feedback + decoding state
37
38    private final ArrayList<Combiner> mCombiners;
39
40    /**
41     * Create an combiner chain.
42     *
43     * The combiner chain takes events as inputs and outputs code points and combining state.
44     * For example, if the input language is Japanese, the combining chain will typically perform
45     * kana conversion.
46     *
47     * @param combinerList A list of combiners to be applied in order.
48     */
49    public CombinerChain(final Combiner... combinerList) {
50        mCombiners = CollectionUtils.newArrayList();
51        // The dead key combiner is always active, and always first
52        mCombiners.add(new DeadKeyCombiner());
53    }
54
55    // Pass a new event through the whole chain.
56    public void processEvent(final ArrayList<Event> previousEvents, final Event newEvent) {
57        final ArrayList<Event> modifiablePreviousEvents = new ArrayList<Event>(previousEvents);
58        Event event = newEvent;
59        for (final Combiner combiner : mCombiners) {
60            // A combiner can never return more than one event; it can return several
61            // code points, but they should be encapsulated within one event.
62            event = combiner.processEvent(modifiablePreviousEvents, event);
63            if (null == event) {
64                // Combiners return null if they eat the event.
65                return;
66            }
67        }
68    }
69}
70