InputConnectionCompatUtils.java revision cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0e
1413aded6007032278120de0f43c4d520c269b53cYohei Yukawa/*
2413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * Copyright (C) 2014 The Android Open Source Project
3413aded6007032278120de0f43c4d520c269b53cYohei Yukawa *
4413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * Licensed under the Apache License, Version 2.0 (the "License");
5413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * you may not use this file except in compliance with the License.
6413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * You may obtain a copy of the License at
7413aded6007032278120de0f43c4d520c269b53cYohei Yukawa *
8413aded6007032278120de0f43c4d520c269b53cYohei Yukawa *      http://www.apache.org/licenses/LICENSE-2.0
9413aded6007032278120de0f43c4d520c269b53cYohei Yukawa *
10413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * Unless required by applicable law or agreed to in writing, software
11413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * distributed under the License is distributed on an "AS IS" BASIS,
12413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * See the License for the specific language governing permissions and
14413aded6007032278120de0f43c4d520c269b53cYohei Yukawa * limitations under the License.
15413aded6007032278120de0f43c4d520c269b53cYohei Yukawa */
16413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
17413aded6007032278120de0f43c4d520c269b53cYohei Yukawapackage com.android.inputmethod.compat;
18413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
19413aded6007032278120de0f43c4d520c269b53cYohei Yukawaimport android.view.inputmethod.InputConnection;
20cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawaimport android.view.inputmethod.InputMethodManager;
21413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
22413aded6007032278120de0f43c4d520c269b53cYohei Yukawapublic final class InputConnectionCompatUtils {
23cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    private static final CompatUtils.ClassWrapper sInputConnectionType;
24cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    private static final CompatUtils.ToBooleanMethodWrapper sRequestUpdateCursorAnchorInfoMethod;
25413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    static {
26cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        sInputConnectionType = new CompatUtils.ClassWrapper(InputConnection.class);
27cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        sRequestUpdateCursorAnchorInfoMethod = sInputConnectionType.getPrimitiveMethod(
28cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa                "requestUpdateCursorAnchorInfo", false, int.class);
29413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
30413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
31cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    public static boolean isRequestUpdateCursorAnchorInfoAvailable() {
32cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        return sRequestUpdateCursorAnchorInfoMethod != null;
33413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
34413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
35413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    /**
365696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * Local copies of some constants in CursorAnchorInfoRequest until the SDK becomes publicly
37413aded6007032278120de0f43c4d520c269b53cYohei Yukawa     * available.
38413aded6007032278120de0f43c4d520c269b53cYohei Yukawa     */
39cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    private static int REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE = 1 << 0;
40cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    private static int REQUEST_UPDATE_CURSOR_UPDATE_MONITOR = 1 << 1;
41413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
42cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    private static boolean requestUpdateCursorAnchorInfoImpl(final InputConnection inputConnection,
43cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa            final int cursorUpdateMode) {
44cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        if (!isRequestUpdateCursorAnchorInfoAvailable()) {
45cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa             return false;
46413aded6007032278120de0f43c4d520c269b53cYohei Yukawa        }
47cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        return sRequestUpdateCursorAnchorInfoMethod.invoke(inputConnection, cursorUpdateMode);
48413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
495696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa
505696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa    /**
515696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * Requests the editor to call back {@link InputMethodManager#updateCursorAnchorInfo}.
525696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param inputConnection the input connection to which the request is to be sent.
535696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param enableMonitor {@code true} to request the editor to call back the method whenever the
545696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * cursor/anchor position is changed.
555696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param requestImmediateCallback {@code true} to request the editor to call back the method
565696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * as soon as possible to notify the current cursor/anchor position to the input method.
575696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @return {@code false} if the request is not handled. Otherwise returns {@code true}.
585696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     */
59cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa    public static boolean requestUpdateCursorAnchorInfo(final InputConnection inputConnection,
605696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa            final boolean enableMonitor, final boolean requestImmediateCallback) {
61cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        final int cursorUpdateMode = (enableMonitor ? REQUEST_UPDATE_CURSOR_UPDATE_MONITOR : 0)
62cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa                | (requestImmediateCallback ? REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE : 0);
63cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        return requestUpdateCursorAnchorInfoImpl(inputConnection, cursorUpdateMode);
645696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa    }
655696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa
66413aded6007032278120de0f43c4d520c269b53cYohei Yukawa}
67