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