1 /* 2 * Copyright (C) 2021 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 android.view.inputmethod; 18 19 import android.annotation.AnyThread; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.graphics.Rect; 23 import android.os.Bundle; 24 import android.os.RemoteException; 25 import android.util.Log; 26 27 import com.android.internal.view.IInputMethodSession; 28 29 /** 30 * This class wrap the {@link IInputMethodSession} object from {@link InputMethodManager}. 31 * Using current {@link IInputMethodSession} object to communicate with 32 * {@link android.inputmethodservice.InputMethodService}. 33 */ 34 final class InputMethodSessionWrapper { 35 36 private static final String TAG = "InputMethodSessionWrapper"; 37 38 /** 39 * The actual instance of the method to make calls on it. 40 */ 41 @NonNull 42 private final IInputMethodSession mSession; 43 InputMethodSessionWrapper(@onNull IInputMethodSession inputMethodSession)44 private InputMethodSessionWrapper(@NonNull IInputMethodSession inputMethodSession) { 45 mSession = inputMethodSession; 46 } 47 48 /** 49 * Create a {@link InputMethodSessionWrapper} instance if applicability. 50 * 51 * @param inputMethodSession {@link IInputMethodSession} object to be wrapped. 52 * @return an instance of {@link InputMethodSessionWrapper} if {@code inputMethodSession} is not 53 * {@code null}. {@code null} otherwise. 54 */ 55 @Nullable createOrNull( @onNull IInputMethodSession inputMethodSession)56 public static InputMethodSessionWrapper createOrNull( 57 @NonNull IInputMethodSession inputMethodSession) { 58 return inputMethodSession != null ? new InputMethodSessionWrapper(inputMethodSession) 59 : null; 60 } 61 62 @AnyThread finishInput()63 void finishInput() { 64 try { 65 mSession.finishInput(); 66 } catch (RemoteException e) { 67 Log.w(TAG, "IME died", e); 68 } 69 } 70 71 @AnyThread updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo)72 void updateCursorAnchorInfo(CursorAnchorInfo cursorAnchorInfo) { 73 try { 74 mSession.updateCursorAnchorInfo(cursorAnchorInfo); 75 } catch (RemoteException e) { 76 Log.w(TAG, "IME died", e); 77 } 78 } 79 80 @AnyThread displayCompletions(CompletionInfo[] completions)81 void displayCompletions(CompletionInfo[] completions) { 82 try { 83 mSession.displayCompletions(completions); 84 } catch (RemoteException e) { 85 Log.w(TAG, "IME died", e); 86 } 87 } 88 89 @AnyThread updateExtractedText(int token, ExtractedText text)90 void updateExtractedText(int token, ExtractedText text) { 91 try { 92 mSession.updateExtractedText(token, text); 93 } catch (RemoteException e) { 94 Log.w(TAG, "IME died", e); 95 } 96 } 97 98 @AnyThread appPrivateCommand(String action, Bundle data)99 void appPrivateCommand(String action, Bundle data) { 100 try { 101 mSession.appPrivateCommand(action, data); 102 } catch (RemoteException e) { 103 Log.w(TAG, "IME died", e); 104 } 105 } 106 107 @AnyThread notifyImeHidden()108 void notifyImeHidden() { 109 try { 110 mSession.notifyImeHidden(); 111 } catch (RemoteException e) { 112 Log.w(TAG, "IME died", e); 113 } 114 } 115 116 @AnyThread viewClicked(boolean focusChanged)117 void viewClicked(boolean focusChanged) { 118 try { 119 mSession.viewClicked(focusChanged); 120 } catch (RemoteException e) { 121 Log.w(TAG, "IME died", e); 122 } 123 } 124 125 @AnyThread updateCursor(Rect newCursor)126 void updateCursor(Rect newCursor) { 127 try { 128 mSession.updateCursor(newCursor); 129 } catch (RemoteException e) { 130 Log.w(TAG, "IME died", e); 131 } 132 } 133 134 @AnyThread updateSelection(int oldSelStart, int oldSelEnd, int selStart, int selEnd, int candidatesStart, int candidatesEnd)135 void updateSelection(int oldSelStart, int oldSelEnd, int selStart, int selEnd, 136 int candidatesStart, int candidatesEnd) { 137 try { 138 mSession.updateSelection( 139 oldSelStart, oldSelEnd, selStart, selEnd, candidatesStart, candidatesEnd); 140 } catch (RemoteException e) { 141 Log.w(TAG, "IME died", e); 142 } 143 } 144 145 /** 146 * @return {@link IInputMethodSession#toString()} as a debug string. 147 */ 148 @AnyThread 149 @NonNull 150 @Override toString()151 public String toString() { 152 return mSession.toString(); 153 } 154 } 155