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;
245d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    private static final CompatUtils.ToBooleanMethodWrapper sRequestCursorUpdatesMethod;
25413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    static {
26cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa        sInputConnectionType = new CompatUtils.ClassWrapper(InputConnection.class);
275d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        sRequestCursorUpdatesMethod = sInputConnectionType.getPrimitiveMethod(
285d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa                "requestCursorUpdates", false, int.class);
29413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
30413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
315d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    public static boolean isRequestCursorUpdatesAvailable() {
325d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        return sRequestCursorUpdatesMethod != null;
33413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
34413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
35413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    /**
365d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa     * Local copies of some constants in InputConnection until the SDK becomes publicly available.
37413aded6007032278120de0f43c4d520c269b53cYohei Yukawa     */
385d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    private static int CURSOR_UPDATE_IMMEDIATE = 1 << 0;
395d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    private static int CURSOR_UPDATE_MONITOR = 1 << 1;
40413aded6007032278120de0f43c4d520c269b53cYohei Yukawa
415d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    private static boolean requestCursorUpdatesImpl(final InputConnection inputConnection,
42cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa            final int cursorUpdateMode) {
435d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        if (!isRequestCursorUpdatesAvailable()) {
44cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0eYohei Yukawa             return false;
45413aded6007032278120de0f43c4d520c269b53cYohei Yukawa        }
465d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        return sRequestCursorUpdatesMethod.invoke(inputConnection, cursorUpdateMode);
47413aded6007032278120de0f43c4d520c269b53cYohei Yukawa    }
485696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa
495696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa    /**
505696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * Requests the editor to call back {@link InputMethodManager#updateCursorAnchorInfo}.
515696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param inputConnection the input connection to which the request is to be sent.
525696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param enableMonitor {@code true} to request the editor to call back the method whenever the
535696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * cursor/anchor position is changed.
545696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @param requestImmediateCallback {@code true} to request the editor to call back the method
555696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * as soon as possible to notify the current cursor/anchor position to the input method.
565696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     * @return {@code false} if the request is not handled. Otherwise returns {@code true}.
575696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa     */
585d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa    public static boolean requestCursorUpdates(final InputConnection inputConnection,
595696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa            final boolean enableMonitor, final boolean requestImmediateCallback) {
605d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        final int cursorUpdateMode = (enableMonitor ? CURSOR_UPDATE_MONITOR : 0)
615d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa                | (requestImmediateCallback ? CURSOR_UPDATE_IMMEDIATE : 0);
625d6ac77732b6fe29489deecc297d771642150a2bYohei Yukawa        return requestCursorUpdatesImpl(inputConnection, cursorUpdateMode);
635696ac95acf5b70b25c8e164ab30047ba13a4827Yohei Yukawa    }
64413aded6007032278120de0f43c4d520c269b53cYohei Yukawa}
65