InputMethodSubtypeSwitchingController.java revision d7443c83ceae0bdd20d68bf84648cf3b40115d85
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
17package com.android.internal.inputmethod;
18
19import android.util.Slog;
20import android.view.inputmethod.InputMethodInfo;
21import android.view.inputmethod.InputMethodSubtype;
22
23import java.util.ArrayDeque;
24
25/**
26 * InputMethodSubtypeSwitchingController controls the switching behavior of the subtypes.
27 */
28public class InputMethodSubtypeSwitchingController {
29    private static final String TAG = InputMethodSubtypeSwitchingController.class.getSimpleName();
30    private static final boolean DEBUG = false;
31    private static final int MAX_HISTORY_SIZE = 4;
32    private static class SubtypeParams {
33        public final InputMethodInfo mImi;
34        public final InputMethodSubtype mSubtype;
35        public final long mTime;
36        public SubtypeParams(InputMethodInfo imi, InputMethodSubtype subtype) {
37            mImi = imi;
38            mSubtype = subtype;
39            mTime = System.currentTimeMillis();
40        }
41    }
42
43    private final ArrayDeque<SubtypeParams> mTypedSubtypeHistory = new ArrayDeque<SubtypeParams>();
44
45    // TODO: write unit tests for this method and the logic that determines the next subtype
46    public void onCommitText(InputMethodInfo imi, InputMethodSubtype subtype) {
47        synchronized(mTypedSubtypeHistory) {
48            if (subtype == null) {
49                Slog.w(TAG, "Invalid InputMethodSubtype: " + imi.getId() + ", " + subtype);
50                return;
51            }
52            if (DEBUG) {
53                Slog.d(TAG, "onCommitText: " + imi.getId() + ", " + subtype);
54            }
55            if (!imi.supportsSwitchingToNextInputMethod()) {
56                Slog.w(TAG, imi.getId() + " doesn't support switching to next input method.");
57                return;
58            }
59            if (mTypedSubtypeHistory.size() >= MAX_HISTORY_SIZE) {
60                mTypedSubtypeHistory.poll();
61            }
62            mTypedSubtypeHistory.addFirst(new SubtypeParams(imi, subtype));
63        }
64    }
65}
66