InputConnectionCompatUtils.java revision cabb66e9bc2d5c13d83ccae6ce2d2e673b6ebf0e
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.compat;
18
19import android.view.inputmethod.InputConnection;
20import android.view.inputmethod.InputMethodManager;
21
22public final class InputConnectionCompatUtils {
23    private static final CompatUtils.ClassWrapper sInputConnectionType;
24    private static final CompatUtils.ToBooleanMethodWrapper sRequestUpdateCursorAnchorInfoMethod;
25    static {
26        sInputConnectionType = new CompatUtils.ClassWrapper(InputConnection.class);
27        sRequestUpdateCursorAnchorInfoMethod = sInputConnectionType.getPrimitiveMethod(
28                "requestUpdateCursorAnchorInfo", false, int.class);
29    }
30
31    public static boolean isRequestUpdateCursorAnchorInfoAvailable() {
32        return sRequestUpdateCursorAnchorInfoMethod != null;
33    }
34
35    /**
36     * Local copies of some constants in CursorAnchorInfoRequest until the SDK becomes publicly
37     * available.
38     */
39    private static int REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE = 1 << 0;
40    private static int REQUEST_UPDATE_CURSOR_UPDATE_MONITOR = 1 << 1;
41
42    private static boolean requestUpdateCursorAnchorInfoImpl(final InputConnection inputConnection,
43            final int cursorUpdateMode) {
44        if (!isRequestUpdateCursorAnchorInfoAvailable()) {
45             return false;
46        }
47        return sRequestUpdateCursorAnchorInfoMethod.invoke(inputConnection, cursorUpdateMode);
48    }
49
50    /**
51     * Requests the editor to call back {@link InputMethodManager#updateCursorAnchorInfo}.
52     * @param inputConnection the input connection to which the request is to be sent.
53     * @param enableMonitor {@code true} to request the editor to call back the method whenever the
54     * cursor/anchor position is changed.
55     * @param requestImmediateCallback {@code true} to request the editor to call back the method
56     * as soon as possible to notify the current cursor/anchor position to the input method.
57     * @return {@code false} if the request is not handled. Otherwise returns {@code true}.
58     */
59    public static boolean requestUpdateCursorAnchorInfo(final InputConnection inputConnection,
60            final boolean enableMonitor, final boolean requestImmediateCallback) {
61        final int cursorUpdateMode = (enableMonitor ? REQUEST_UPDATE_CURSOR_UPDATE_MONITOR : 0)
62                | (requestImmediateCallback ? REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE : 0);
63        return requestUpdateCursorAnchorInfoImpl(inputConnection, cursorUpdateMode);
64    }
65
66}
67