|
| 1 | +package com.callstack.agentdevice.imehelper; |
| 2 | + |
| 3 | +import android.content.BroadcastReceiver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.IntentFilter; |
| 7 | +import android.inputmethodservice.InputMethodService; |
| 8 | +import android.os.Build; |
| 9 | +import android.util.Base64; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.View; |
| 12 | +import android.view.inputmethod.EditorInfo; |
| 13 | +import android.view.inputmethod.InputConnection; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +/** |
| 17 | + * Minimal headless test IME for agent-device: renders no input view, injects text over a |
| 18 | + * base64-encoded broadcast channel. Treats every broadcast extra as untrusted input. |
| 19 | + */ |
| 20 | +public class TestInputMethodService extends InputMethodService { |
| 21 | + private static final String TAG = "AgentDeviceTestIME"; |
| 22 | + private static final String PROTOCOL = "android-ime-helper-v1"; |
| 23 | + |
| 24 | + public static final String ACTION_INPUT_TEXT = |
| 25 | + "com.callstack.agentdevice.imehelper.ACTION_INPUT_TEXT"; |
| 26 | + public static final String ACTION_INPUT_TEXT_B64 = |
| 27 | + "com.callstack.agentdevice.imehelper.ACTION_INPUT_TEXT_B64"; |
| 28 | + public static final String ACTION_CLEAR_TEXT = |
| 29 | + "com.callstack.agentdevice.imehelper.ACTION_CLEAR_TEXT"; |
| 30 | + public static final String ACTION_ENTER = |
| 31 | + "com.callstack.agentdevice.imehelper.ACTION_ENTER"; |
| 32 | + public static final String EXTRA_TEXT = "text"; |
| 33 | + public static final String EXTRA_PROTOCOL = "protocol"; |
| 34 | + |
| 35 | + // Upper bound on a single broadcast payload. |
| 36 | + private static final int MAX_TEXT_LENGTH = 32_000; |
| 37 | + |
| 38 | + private BroadcastReceiver receiver; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onCreate() { |
| 42 | + super.onCreate(); |
| 43 | + receiver = |
| 44 | + new BroadcastReceiver() { |
| 45 | + @Override |
| 46 | + public void onReceive(Context context, Intent intent) { |
| 47 | + try { |
| 48 | + handleAction(intent); |
| 49 | + } catch (Throwable error) { |
| 50 | + // Never let untrusted extras crash the IME process. |
| 51 | + Log.w(TAG, "handleAction failed for " + intent.getAction(), error); |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + IntentFilter filter = new IntentFilter(); |
| 56 | + filter.addAction(ACTION_INPUT_TEXT); |
| 57 | + filter.addAction(ACTION_INPUT_TEXT_B64); |
| 58 | + filter.addAction(ACTION_CLEAR_TEXT); |
| 59 | + filter.addAction(ACTION_ENTER); |
| 60 | + if (Build.VERSION.SDK_INT >= 33) { |
| 61 | + registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED); |
| 62 | + } else { |
| 63 | + registerReceiver(receiver, filter); |
| 64 | + } |
| 65 | + Log.i(TAG, "onCreate: receiver registered"); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onDestroy() { |
| 70 | + if (receiver != null) { |
| 71 | + unregisterReceiver(receiver); |
| 72 | + receiver = null; |
| 73 | + } |
| 74 | + super.onDestroy(); |
| 75 | + } |
| 76 | + |
| 77 | + private void handleAction(Intent intent) { |
| 78 | + String action = intent.getAction(); |
| 79 | + if (action == null) return; |
| 80 | + if (!isKnownAction(action)) return; |
| 81 | + if (!isProtocolAcceptable(intent)) { |
| 82 | + Log.w(TAG, "handleAction: rejected " + action + ", protocol mismatch"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + InputConnection ic = getCurrentInputConnection(); |
| 87 | + if (ic == null) { |
| 88 | + Log.w(TAG, "handleAction: no current input connection for " + action); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (ACTION_INPUT_TEXT.equals(action)) { |
| 93 | + commitValidatedText(ic, intent.getStringExtra(EXTRA_TEXT)); |
| 94 | + } else if (ACTION_INPUT_TEXT_B64.equals(action)) { |
| 95 | + commitValidatedText(ic, decodeBase64Text(intent.getStringExtra(EXTRA_TEXT))); |
| 96 | + } else if (ACTION_CLEAR_TEXT.equals(action)) { |
| 97 | + clearText(ic); |
| 98 | + } else if (ACTION_ENTER.equals(action)) { |
| 99 | + ic.performEditorAction(EditorInfo.IME_ACTION_DONE); |
| 100 | + Log.i(TAG, "enter"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static boolean isKnownAction(String action) { |
| 105 | + return ACTION_INPUT_TEXT.equals(action) |
| 106 | + || ACTION_INPUT_TEXT_B64.equals(action) |
| 107 | + || ACTION_CLEAR_TEXT.equals(action) |
| 108 | + || ACTION_ENTER.equals(action); |
| 109 | + } |
| 110 | + |
| 111 | + // Optional sanity check, not a security boundary; missing extra is accepted. |
| 112 | + private static boolean isProtocolAcceptable(Intent intent) { |
| 113 | + if (!intent.hasExtra(EXTRA_PROTOCOL)) return true; |
| 114 | + return PROTOCOL.equals(intent.getStringExtra(EXTRA_PROTOCOL)); |
| 115 | + } |
| 116 | + |
| 117 | + private static String decodeBase64Text(String encoded) { |
| 118 | + if (encoded == null || encoded.isEmpty()) return null; |
| 119 | + if (encoded.length() > MAX_TEXT_LENGTH * 2) { |
| 120 | + Log.w(TAG, "decodeBase64Text: payload too large, dropping"); |
| 121 | + return null; |
| 122 | + } |
| 123 | + try { |
| 124 | + byte[] decoded = Base64.decode(encoded, Base64.DEFAULT); |
| 125 | + return new String(decoded, StandardCharsets.UTF_8); |
| 126 | + } catch (IllegalArgumentException error) { |
| 127 | + Log.w(TAG, "decodeBase64Text: malformed base64 payload", error); |
| 128 | + return null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void commitValidatedText(InputConnection ic, String text) { |
| 133 | + if (text == null) return; |
| 134 | + String bounded = text.length() > MAX_TEXT_LENGTH ? text.substring(0, MAX_TEXT_LENGTH) : text; |
| 135 | + ic.commitText(bounded, 1); |
| 136 | + Log.i(TAG, "commitText length=" + bounded.length()); |
| 137 | + } |
| 138 | + |
| 139 | + private void clearText(InputConnection ic) { |
| 140 | + ic.beginBatchEdit(); |
| 141 | + ic.performContextMenuAction(android.R.id.selectAll); |
| 142 | + ic.commitText("", 1); |
| 143 | + ic.endBatchEdit(); |
| 144 | + Log.i(TAG, "clearText"); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean onEvaluateInputViewShown() { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean onEvaluateFullscreenMode() { |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public View onCreateInputView() { |
| 159 | + return null; |
| 160 | + } |
| 161 | +} |
0 commit comments