| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.accessibilityservice; |
| 18 |
|
| 19 |
import android.annotation.AnyThread; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.os.Handler; |
| 23 |
import android.os.Looper; |
| 24 |
import android.view.inputmethod.EditorInfo; |
| 25 |
|
| 26 |
import com.android.internal.inputmethod.IAccessibilityInputMethodSession; |
| 27 |
import com.android.internal.inputmethod.IRemoteAccessibilityInputConnection; |
| 28 |
|
| 29 |
import java.util.concurrent.atomic.AtomicReference; |
| 30 |
|
| 31 |
final class AccessibilityInputMethodSessionWrapper extends IAccessibilityInputMethodSession.Stub { |
| 32 |
private final Handler mHandler; |
| 33 |
|
| 34 |
@NonNull |
| 35 |
private final AtomicReference<AccessibilityInputMethodSession> mSessionRef; |
| 36 |
|
| 37 |
AccessibilityInputMethodSessionWrapper( |
| 38 |
@NonNull Looper looper, @NonNull AccessibilityInputMethodSession session) { |
| 39 |
mSessionRef = new AtomicReference<>(session); |
| 40 |
mHandler = Handler.createAsync(looper); |
| 41 |
} |
| 42 |
|
| 43 |
@AnyThread |
| 44 |
@Nullable |
| 45 |
AccessibilityInputMethodSession getSession() { |
| 46 |
return mSessionRef.get(); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void updateSelection(int oldSelStart, int oldSelEnd, |
| 51 |
int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) { |
| 52 |
if (mHandler.getLooper().isCurrentThread()) { |
| 53 |
doUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, |
| 54 |
candidatesEnd); |
| 55 |
} else { |
| 56 |
mHandler.post(() -> doUpdateSelection(oldSelStart, oldSelEnd, newSelStart, |
| 57 |
newSelEnd, candidatesStart, candidatesEnd)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private void doUpdateSelection(int oldSelStart, int oldSelEnd, |
| 62 |
int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) { |
| 63 |
final AccessibilityInputMethodSession session = mSessionRef.get(); |
| 64 |
if (session != null) { |
| 65 |
session.updateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, |
| 66 |
candidatesEnd); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void finishInput() { |
| 72 |
if (mHandler.getLooper().isCurrentThread()) { |
| 73 |
doFinishInput(); |
| 74 |
} else { |
| 75 |
mHandler.post(this::doFinishInput); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private void doFinishInput() { |
| 80 |
final AccessibilityInputMethodSession session = mSessionRef.get(); |
| 81 |
if (session != null) { |
| 82 |
session.finishInput(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void finishSession() { |
| 88 |
if (mHandler.getLooper().isCurrentThread()) { |
| 89 |
doFinishSession(); |
| 90 |
} else { |
| 91 |
mHandler.post(this::doFinishSession); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private void doFinishSession() { |
| 96 |
mSessionRef.set(null); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void invalidateInput(EditorInfo editorInfo, |
| 101 |
IRemoteAccessibilityInputConnection connection, int sessionId) { |
| 102 |
if (mHandler.getLooper().isCurrentThread()) { |
| 103 |
doInvalidateInput(editorInfo, connection, sessionId); |
| 104 |
} else { |
| 105 |
mHandler.post(() -> doInvalidateInput(editorInfo, connection, sessionId)); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
private void doInvalidateInput(EditorInfo editorInfo, |
| 110 |
IRemoteAccessibilityInputConnection connection, int sessionId) { |
| 111 |
final AccessibilityInputMethodSession session = mSessionRef.get(); |
| 112 |
if (session != null) { |
| 113 |
session.invalidateInput(editorInfo, connection, sessionId); |
| 114 |
} |
| 115 |
} |
| 116 |
} |