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