GioView.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package org.gioui;
  3. import java.lang.Class;
  4. import java.lang.IllegalAccessException;
  5. import java.lang.InstantiationException;
  6. import java.lang.ExceptionInInitializerError;
  7. import java.lang.SecurityException;
  8. import android.app.Activity;
  9. import android.app.Fragment;
  10. import android.app.FragmentManager;
  11. import android.app.FragmentTransaction;
  12. import android.content.Context;
  13. import android.graphics.Canvas;
  14. import android.graphics.Color;
  15. import android.graphics.Matrix;
  16. import android.graphics.Rect;
  17. import android.os.Build;
  18. import android.os.Bundle;
  19. import android.os.Handler;
  20. import android.os.SystemClock;
  21. import android.text.TextUtils;
  22. import android.text.Selection;
  23. import android.text.SpannableStringBuilder;
  24. import android.util.AttributeSet;
  25. import android.util.TypedValue;
  26. import android.view.Choreographer;
  27. import android.view.Display;
  28. import android.view.KeyCharacterMap;
  29. import android.view.KeyEvent;
  30. import android.view.MotionEvent;
  31. import android.view.PointerIcon;
  32. import android.view.View;
  33. import android.view.ViewConfiguration;
  34. import android.view.WindowInsets;
  35. import android.view.Surface;
  36. import android.view.SurfaceView;
  37. import android.view.SurfaceHolder;
  38. import android.view.Window;
  39. import android.view.WindowInsetsController;
  40. import android.view.WindowManager;
  41. import android.view.inputmethod.CorrectionInfo;
  42. import android.view.inputmethod.CompletionInfo;
  43. import android.view.inputmethod.CursorAnchorInfo;
  44. import android.view.inputmethod.EditorInfo;
  45. import android.view.inputmethod.ExtractedText;
  46. import android.view.inputmethod.ExtractedTextRequest;
  47. import android.view.inputmethod.InputConnection;
  48. import android.view.inputmethod.InputMethodManager;
  49. import android.view.inputmethod.InputContentInfo;
  50. import android.view.inputmethod.SurroundingText;
  51. import android.view.accessibility.AccessibilityNodeProvider;
  52. import android.view.accessibility.AccessibilityNodeInfo;
  53. import android.view.accessibility.AccessibilityEvent;
  54. import android.view.accessibility.AccessibilityManager;
  55. import java.io.UnsupportedEncodingException;
  56. public final class GioView extends SurfaceView implements Choreographer.FrameCallback {
  57. private static boolean jniLoaded;
  58. private final SurfaceHolder.Callback surfCallbacks;
  59. private final View.OnFocusChangeListener focusCallback;
  60. private final InputMethodManager imm;
  61. private final float scrollXScale;
  62. private final float scrollYScale;
  63. private final AccessibilityManager accessManager;
  64. private int keyboardHint;
  65. private long nhandle;
  66. public GioView(Context context) {
  67. this(context, null);
  68. }
  69. public GioView(Context context, AttributeSet attrs) {
  70. super(context, attrs);
  71. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  72. setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  73. }
  74. setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
  75. // Late initialization of the Go runtime to wait for a valid context.
  76. Gio.init(context.getApplicationContext());
  77. // Set background color to transparent to avoid a flickering
  78. // issue on ChromeOS.
  79. setBackgroundColor(Color.argb(0, 0, 0, 0));
  80. ViewConfiguration conf = ViewConfiguration.get(context);
  81. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  82. scrollXScale = conf.getScaledHorizontalScrollFactor();
  83. scrollYScale = conf.getScaledVerticalScrollFactor();
  84. // The platform focus highlight is not aware of Gio's widgets.
  85. setDefaultFocusHighlightEnabled(false);
  86. } else {
  87. float listItemHeight = 48; // dp
  88. float px = TypedValue.applyDimension(
  89. TypedValue.COMPLEX_UNIT_DIP,
  90. listItemHeight,
  91. getResources().getDisplayMetrics()
  92. );
  93. scrollXScale = px;
  94. scrollYScale = px;
  95. }
  96. setHighRefreshRate();
  97. accessManager = (AccessibilityManager)context.getSystemService(Context.ACCESSIBILITY_SERVICE);
  98. imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
  99. nhandle = onCreateView(this);
  100. setFocusable(true);
  101. setFocusableInTouchMode(true);
  102. focusCallback = new View.OnFocusChangeListener() {
  103. @Override public void onFocusChange(View v, boolean focus) {
  104. GioView.this.onFocusChange(nhandle, focus);
  105. }
  106. };
  107. setOnFocusChangeListener(focusCallback);
  108. surfCallbacks = new SurfaceHolder.Callback() {
  109. @Override public void surfaceCreated(SurfaceHolder holder) {
  110. // Ignore; surfaceChanged is guaranteed to be called immediately after this.
  111. }
  112. @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  113. onSurfaceChanged(nhandle, getHolder().getSurface());
  114. }
  115. @Override public void surfaceDestroyed(SurfaceHolder holder) {
  116. onSurfaceDestroyed(nhandle);
  117. }
  118. };
  119. getHolder().addCallback(surfCallbacks);
  120. }
  121. @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
  122. if (nhandle != 0) {
  123. onKeyEvent(nhandle, keyCode, event.getUnicodeChar(), true, event.getEventTime());
  124. }
  125. return false;
  126. }
  127. @Override public boolean onKeyUp(int keyCode, KeyEvent event) {
  128. if (nhandle != 0) {
  129. onKeyEvent(nhandle, keyCode, event.getUnicodeChar(), false, event.getEventTime());
  130. }
  131. return false;
  132. }
  133. @Override public boolean onGenericMotionEvent(MotionEvent event) {
  134. dispatchMotionEvent(event);
  135. return true;
  136. }
  137. @Override public boolean onTouchEvent(MotionEvent event) {
  138. // Ask for unbuffered events. Flutter and Chrome do it
  139. // so assume it's good for us as well.
  140. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  141. requestUnbufferedDispatch(event);
  142. }
  143. dispatchMotionEvent(event);
  144. return true;
  145. }
  146. private void setCursor(int id) {
  147. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
  148. return;
  149. }
  150. PointerIcon pointerIcon = PointerIcon.getSystemIcon(getContext(), id);
  151. setPointerIcon(pointerIcon);
  152. }
  153. private void setOrientation(int id, int fallback) {
  154. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
  155. id = fallback;
  156. }
  157. ((Activity) this.getContext()).setRequestedOrientation(id);
  158. }
  159. private void setFullscreen(boolean enabled) {
  160. int flags = this.getSystemUiVisibility();
  161. if (enabled) {
  162. flags |= SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  163. flags |= SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  164. flags |= SYSTEM_UI_FLAG_FULLSCREEN;
  165. flags |= SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
  166. } else {
  167. flags &= ~SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  168. flags &= ~SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  169. flags &= ~SYSTEM_UI_FLAG_FULLSCREEN;
  170. flags &= ~SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
  171. }
  172. this.setSystemUiVisibility(flags);
  173. }
  174. private enum Bar {
  175. NAVIGATION,
  176. STATUS,
  177. }
  178. private void setBarColor(Bar t, int color, int luminance) {
  179. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  180. return;
  181. }
  182. Window window = ((Activity) this.getContext()).getWindow();
  183. int insetsMask;
  184. int viewMask;
  185. switch (t) {
  186. case STATUS:
  187. insetsMask = WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
  188. viewMask = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  189. window.setStatusBarColor(color);
  190. break;
  191. case NAVIGATION:
  192. insetsMask = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
  193. viewMask = View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
  194. window.setNavigationBarColor(color);
  195. break;
  196. default:
  197. throw new RuntimeException("invalid bar type");
  198. }
  199. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  200. return;
  201. }
  202. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
  203. int flags = this.getSystemUiVisibility();
  204. if (luminance > 128) {
  205. flags |= viewMask;
  206. } else {
  207. flags &= ~viewMask;
  208. }
  209. this.setSystemUiVisibility(flags);
  210. return;
  211. }
  212. WindowInsetsController insetsController = window.getInsetsController();
  213. if (insetsController == null) {
  214. return;
  215. }
  216. if (luminance > 128) {
  217. insetsController.setSystemBarsAppearance(insetsMask, insetsMask);
  218. } else {
  219. insetsController.setSystemBarsAppearance(0, insetsMask);
  220. }
  221. }
  222. private void setStatusColor(int color, int luminance) {
  223. this.setBarColor(Bar.STATUS, color, luminance);
  224. }
  225. private void setNavigationColor(int color, int luminance) {
  226. this.setBarColor(Bar.NAVIGATION, color, luminance);
  227. }
  228. private void setHighRefreshRate() {
  229. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
  230. return;
  231. }
  232. Context context = getContext();
  233. Display display = context.getDisplay();
  234. Display.Mode[] supportedModes = display.getSupportedModes();
  235. if (supportedModes.length <= 1) {
  236. // Nothing to set
  237. return;
  238. }
  239. Display.Mode currentMode = display.getMode();
  240. int currentWidth = currentMode.getPhysicalWidth();
  241. int currentHeight = currentMode.getPhysicalHeight();
  242. float minRefreshRate = -1;
  243. float maxRefreshRate = -1;
  244. float bestRefreshRate = -1;
  245. int bestModeId = -1;
  246. for (Display.Mode mode : supportedModes) {
  247. float refreshRate = mode.getRefreshRate();
  248. float width = mode.getPhysicalWidth();
  249. float height = mode.getPhysicalHeight();
  250. if (minRefreshRate == -1 || refreshRate < minRefreshRate) {
  251. minRefreshRate = refreshRate;
  252. }
  253. if (maxRefreshRate == -1 || refreshRate > maxRefreshRate) {
  254. maxRefreshRate = refreshRate;
  255. }
  256. boolean refreshRateIsBetter = bestRefreshRate == -1 || refreshRate > bestRefreshRate;
  257. if (width == currentWidth && height == currentHeight && refreshRateIsBetter) {
  258. int modeId = mode.getModeId();
  259. bestRefreshRate = refreshRate;
  260. bestModeId = modeId;
  261. }
  262. }
  263. if (bestModeId == -1) {
  264. // Not expecting this but just in case
  265. return;
  266. }
  267. if (minRefreshRate == maxRefreshRate) {
  268. // Can't improve the refresh rate
  269. return;
  270. }
  271. Window window = ((Activity) context).getWindow();
  272. WindowManager.LayoutParams layoutParams = window.getAttributes();
  273. layoutParams.preferredDisplayModeId = bestModeId;
  274. window.setAttributes(layoutParams);
  275. }
  276. @Override protected boolean dispatchHoverEvent(MotionEvent event) {
  277. if (!accessManager.isTouchExplorationEnabled()) {
  278. return super.dispatchHoverEvent(event);
  279. }
  280. switch (event.getAction()) {
  281. case MotionEvent.ACTION_HOVER_ENTER:
  282. // Fall through.
  283. case MotionEvent.ACTION_HOVER_MOVE:
  284. onTouchExploration(nhandle, event.getX(), event.getY());
  285. break;
  286. case MotionEvent.ACTION_HOVER_EXIT:
  287. onExitTouchExploration(nhandle);
  288. break;
  289. }
  290. return true;
  291. }
  292. void sendA11yEvent(int eventType, int viewId) {
  293. if (!accessManager.isEnabled()) {
  294. return;
  295. }
  296. AccessibilityEvent event = obtainA11yEvent(eventType, viewId);
  297. getParent().requestSendAccessibilityEvent(this, event);
  298. }
  299. AccessibilityEvent obtainA11yEvent(int eventType, int viewId) {
  300. AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
  301. event.setPackageName(getContext().getPackageName());
  302. event.setSource(this, viewId);
  303. return event;
  304. }
  305. boolean isA11yActive() {
  306. return accessManager.isEnabled();
  307. }
  308. void sendA11yChange(int viewId) {
  309. if (!accessManager.isEnabled()) {
  310. return;
  311. }
  312. AccessibilityEvent event = obtainA11yEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED, viewId);
  313. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  314. event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE);
  315. }
  316. getParent().requestSendAccessibilityEvent(this, event);
  317. }
  318. private void dispatchMotionEvent(MotionEvent event) {
  319. if (nhandle == 0) {
  320. return;
  321. }
  322. for (int j = 0; j < event.getHistorySize(); j++) {
  323. long time = event.getHistoricalEventTime(j);
  324. for (int i = 0; i < event.getPointerCount(); i++) {
  325. onTouchEvent(
  326. nhandle,
  327. event.ACTION_MOVE,
  328. event.getPointerId(i),
  329. event.getToolType(i),
  330. event.getHistoricalX(i, j),
  331. event.getHistoricalY(i, j),
  332. scrollXScale*event.getHistoricalAxisValue(MotionEvent.AXIS_HSCROLL, i, j),
  333. scrollYScale*event.getHistoricalAxisValue(MotionEvent.AXIS_VSCROLL, i, j),
  334. event.getButtonState(),
  335. time);
  336. }
  337. }
  338. int act = event.getActionMasked();
  339. int idx = event.getActionIndex();
  340. for (int i = 0; i < event.getPointerCount(); i++) {
  341. int pact = event.ACTION_MOVE;
  342. if (i == idx) {
  343. pact = act;
  344. }
  345. onTouchEvent(
  346. nhandle,
  347. pact,
  348. event.getPointerId(i),
  349. event.getToolType(i),
  350. event.getX(i), event.getY(i),
  351. scrollXScale*event.getAxisValue(MotionEvent.AXIS_HSCROLL, i),
  352. scrollYScale*event.getAxisValue(MotionEvent.AXIS_VSCROLL, i),
  353. event.getButtonState(),
  354. event.getEventTime());
  355. }
  356. }
  357. @Override public InputConnection onCreateInputConnection(EditorInfo editor) {
  358. Snippet snip = getSnippet();
  359. editor.inputType = this.keyboardHint;
  360. editor.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  361. editor.initialSelStart = imeToUTF16(nhandle, imeSelectionStart(nhandle));
  362. editor.initialSelEnd = imeToUTF16(nhandle, imeSelectionEnd(nhandle));
  363. int selStart = editor.initialSelStart - snip.offset;
  364. editor.initialCapsMode = TextUtils.getCapsMode(snip.snippet, selStart, this.keyboardHint);
  365. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
  366. editor.setInitialSurroundingSubText(snip.snippet, imeToUTF16(nhandle, snip.offset));
  367. }
  368. imeSetComposingRegion(nhandle, -1, -1);
  369. return new GioInputConnection();
  370. }
  371. void setInputHint(int hint) {
  372. if (hint == this.keyboardHint) {
  373. return;
  374. }
  375. this.keyboardHint = hint;
  376. restartInput();
  377. }
  378. void showTextInput() {
  379. GioView.this.requestFocus();
  380. imm.showSoftInput(GioView.this, 0);
  381. }
  382. void hideTextInput() {
  383. imm.hideSoftInputFromWindow(getWindowToken(), 0);
  384. }
  385. @Override protected boolean fitSystemWindows(Rect insets) {
  386. if (nhandle != 0) {
  387. onWindowInsets(nhandle, insets.top, insets.right, insets.bottom, insets.left);
  388. }
  389. return true;
  390. }
  391. void postFrameCallback() {
  392. Choreographer.getInstance().removeFrameCallback(this);
  393. Choreographer.getInstance().postFrameCallback(this);
  394. }
  395. @Override public void doFrame(long nanos) {
  396. if (nhandle != 0) {
  397. onFrameCallback(nhandle);
  398. }
  399. }
  400. int getDensity() {
  401. return getResources().getDisplayMetrics().densityDpi;
  402. }
  403. float getFontScale() {
  404. return getResources().getConfiguration().fontScale;
  405. }
  406. public void start() {
  407. if (nhandle != 0) {
  408. onStartView(nhandle);
  409. }
  410. }
  411. public void stop() {
  412. if (nhandle != 0) {
  413. onStopView(nhandle);
  414. }
  415. }
  416. public void destroy() {
  417. if (nhandle != 0) {
  418. onDestroyView(nhandle);
  419. }
  420. }
  421. protected void unregister() {
  422. setOnFocusChangeListener(null);
  423. getHolder().removeCallback(surfCallbacks);
  424. nhandle = 0;
  425. }
  426. public void configurationChanged() {
  427. if (nhandle != 0) {
  428. onConfigurationChanged(nhandle);
  429. }
  430. }
  431. public boolean backPressed() {
  432. if (nhandle == 0) {
  433. return false;
  434. }
  435. return onBack(nhandle);
  436. }
  437. void restartInput() {
  438. imm.restartInput(this);
  439. }
  440. void updateSelection() {
  441. int selStart = imeToUTF16(nhandle, imeSelectionStart(nhandle));
  442. int selEnd = imeToUTF16(nhandle, imeSelectionEnd(nhandle));
  443. int compStart = imeToUTF16(nhandle, imeComposingStart(nhandle));
  444. int compEnd = imeToUTF16(nhandle, imeComposingEnd(nhandle));
  445. imm.updateSelection(this, selStart, selEnd, compStart, compEnd);
  446. }
  447. void updateCaret(float m00, float m01, float m02, float m10, float m11, float m12, float caretX, float caretTop, float caretBase, float caretBottom) {
  448. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  449. return;
  450. }
  451. Matrix m = new Matrix();
  452. m.setValues(new float[]{m00, m01, m02, m10, m11, m12, 0.0f, 0.0f, 1.0f});
  453. m.setConcat(getMatrix(), m);
  454. int selStart = imeSelectionStart(nhandle);
  455. int selEnd = imeSelectionEnd(nhandle);
  456. int compStart = imeComposingStart(nhandle);
  457. int compEnd = imeComposingEnd(nhandle);
  458. Snippet snip = getSnippet();
  459. String composing = "";
  460. if (compStart != -1) {
  461. composing = snip.substringRunes(compStart, compEnd);
  462. }
  463. CursorAnchorInfo inf = new CursorAnchorInfo.Builder()
  464. .setMatrix(m)
  465. .setComposingText(imeToUTF16(nhandle, compStart), composing)
  466. .setSelectionRange(imeToUTF16(nhandle, selStart), imeToUTF16(nhandle, selEnd))
  467. .setInsertionMarkerLocation(caretX, caretTop, caretBase, caretBottom, 0)
  468. .build();
  469. imm.updateCursorAnchorInfo(this, inf);
  470. }
  471. static private native long onCreateView(GioView view);
  472. static private native void onDestroyView(long handle);
  473. static private native void onStartView(long handle);
  474. static private native void onStopView(long handle);
  475. static private native void onSurfaceDestroyed(long handle);
  476. static private native void onSurfaceChanged(long handle, Surface surface);
  477. static private native void onConfigurationChanged(long handle);
  478. static private native void onWindowInsets(long handle, int top, int right, int bottom, int left);
  479. static public native void onLowMemory();
  480. static private native void onTouchEvent(long handle, int action, int pointerID, int tool, float x, float y, float scrollX, float scrollY, int buttons, long time);
  481. static private native void onKeyEvent(long handle, int code, int character, boolean pressed, long time);
  482. static private native void onFrameCallback(long handle);
  483. static private native boolean onBack(long handle);
  484. static private native void onFocusChange(long handle, boolean focus);
  485. static private native AccessibilityNodeInfo initializeAccessibilityNodeInfo(long handle, int viewId, int screenX, int screenY, AccessibilityNodeInfo info);
  486. static private native void onTouchExploration(long handle, float x, float y);
  487. static private native void onExitTouchExploration(long handle);
  488. static private native void onA11yFocus(long handle, int viewId);
  489. static private native void onClearA11yFocus(long handle, int viewId);
  490. static private native void imeSetSnippet(long handle, int start, int end);
  491. static private native String imeSnippet(long handle);
  492. static private native int imeSnippetStart(long handle);
  493. static private native int imeSelectionStart(long handle);
  494. static private native int imeSelectionEnd(long handle);
  495. static private native int imeComposingStart(long handle);
  496. static private native int imeComposingEnd(long handle);
  497. static private native int imeReplace(long handle, int start, int end, String text);
  498. static private native int imeSetSelection(long handle, int start, int end);
  499. static private native int imeSetComposingRegion(long handle, int start, int end);
  500. // imeToRunes converts the Java character index into runes (Java code points).
  501. static private native int imeToRunes(long handle, int chars);
  502. // imeToUTF16 converts the rune index into Java characters.
  503. static private native int imeToUTF16(long handle, int runes);
  504. private class GioInputConnection implements InputConnection {
  505. private int batchDepth;
  506. @Override public boolean beginBatchEdit() {
  507. batchDepth++;
  508. return true;
  509. }
  510. @Override public boolean endBatchEdit() {
  511. batchDepth--;
  512. return batchDepth > 0;
  513. }
  514. @Override public boolean clearMetaKeyStates(int states) {
  515. return false;
  516. }
  517. @Override public boolean commitCompletion(CompletionInfo text) {
  518. return false;
  519. }
  520. @Override public boolean commitCorrection(CorrectionInfo info) {
  521. return false;
  522. }
  523. @Override public boolean commitText(CharSequence text, int cursor) {
  524. setComposingText(text, cursor);
  525. return finishComposingText();
  526. }
  527. @Override public boolean deleteSurroundingText(int beforeChars, int afterChars) {
  528. // translate before and after to runes.
  529. int selStart = imeSelectionStart(nhandle);
  530. int selEnd = imeSelectionEnd(nhandle);
  531. int before = selStart - imeToRunes(nhandle, imeToUTF16(nhandle, selStart) - beforeChars);
  532. int after = selEnd - imeToRunes(nhandle, imeToUTF16(nhandle, selEnd) - afterChars);
  533. return deleteSurroundingTextInCodePoints(before, after);
  534. }
  535. @Override public boolean finishComposingText() {
  536. imeSetComposingRegion(nhandle, -1, -1);
  537. return true;
  538. }
  539. @Override public int getCursorCapsMode(int reqModes) {
  540. Snippet snip = getSnippet();
  541. int selStart = imeSelectionStart(nhandle);
  542. int off = imeToUTF16(nhandle, selStart - snip.offset);
  543. if (off < 0 || off > snip.snippet.length()) {
  544. return 0;
  545. }
  546. return TextUtils.getCapsMode(snip.snippet, off, reqModes);
  547. }
  548. @Override public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
  549. return null;
  550. }
  551. @Override public CharSequence getSelectedText(int flags) {
  552. Snippet snip = getSnippet();
  553. int selStart = imeSelectionStart(nhandle);
  554. int selEnd = imeSelectionEnd(nhandle);
  555. String sub = snip.substringRunes(selStart, selEnd);
  556. return sub;
  557. }
  558. @Override public CharSequence getTextAfterCursor(int n, int flags) {
  559. Snippet snip = getSnippet();
  560. int selStart = imeSelectionStart(nhandle);
  561. int selEnd = imeSelectionEnd(nhandle);
  562. // n are in Java characters, but in worst case we'll just ask for more runes
  563. // than wanted.
  564. imeSetSnippet(nhandle, selStart - n, selEnd + n);
  565. int start = selEnd;
  566. int end = imeToRunes(nhandle, imeToUTF16(nhandle, selEnd) + n);
  567. String ret = snip.substringRunes(start, end);
  568. return ret;
  569. }
  570. @Override public CharSequence getTextBeforeCursor(int n, int flags) {
  571. Snippet snip = getSnippet();
  572. int selStart = imeSelectionStart(nhandle);
  573. int selEnd = imeSelectionEnd(nhandle);
  574. // n are in Java characters, but in worst case we'll just ask for more runes
  575. // than wanted.
  576. imeSetSnippet(nhandle, selStart - n, selEnd + n);
  577. int start = imeToRunes(nhandle, imeToUTF16(nhandle, selStart) - n);
  578. int end = selStart;
  579. String ret = snip.substringRunes(start, end);
  580. return ret;
  581. }
  582. @Override public boolean performContextMenuAction(int id) {
  583. return false;
  584. }
  585. @Override public boolean performEditorAction(int editorAction) {
  586. long eventTime = SystemClock.uptimeMillis();
  587. // Translate to enter key.
  588. onKeyEvent(nhandle, KeyEvent.KEYCODE_ENTER, '\n', true, eventTime);
  589. onKeyEvent(nhandle, KeyEvent.KEYCODE_ENTER, '\n', false, eventTime);
  590. return true;
  591. }
  592. @Override public boolean performPrivateCommand(String action, Bundle data) {
  593. return false;
  594. }
  595. @Override public boolean reportFullscreenMode(boolean enabled) {
  596. return false;
  597. }
  598. @Override public boolean sendKeyEvent(KeyEvent event) {
  599. boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
  600. onKeyEvent(nhandle, event.getKeyCode(), event.getUnicodeChar(), pressed, event.getEventTime());
  601. return true;
  602. }
  603. @Override public boolean setComposingRegion(int startChars, int endChars) {
  604. int compStart = imeToRunes(nhandle, startChars);
  605. int compEnd = imeToRunes(nhandle, endChars);
  606. imeSetComposingRegion(nhandle, compStart, compEnd);
  607. return true;
  608. }
  609. @Override public boolean setComposingText(CharSequence text, int relCursor) {
  610. int start = imeComposingStart(nhandle);
  611. int end = imeComposingEnd(nhandle);
  612. if (start == -1 || end == -1) {
  613. start = imeSelectionStart(nhandle);
  614. end = imeSelectionEnd(nhandle);
  615. }
  616. String str = text.toString();
  617. imeReplace(nhandle, start, end, str);
  618. int cursor = start;
  619. int runes = str.codePointCount(0, str.length());
  620. if (relCursor > 0) {
  621. cursor += runes;
  622. relCursor--;
  623. }
  624. imeSetComposingRegion(nhandle, start, start + runes);
  625. // Move cursor.
  626. Snippet snip = getSnippet();
  627. cursor = imeToRunes(nhandle, imeToUTF16(nhandle, cursor) + relCursor);
  628. imeSetSelection(nhandle, cursor, cursor);
  629. return true;
  630. }
  631. @Override public boolean setSelection(int startChars, int endChars) {
  632. int start = imeToRunes(nhandle, startChars);
  633. int end = imeToRunes(nhandle, endChars);
  634. imeSetSelection(nhandle, start, end);
  635. return true;
  636. }
  637. /*@Override*/ public boolean requestCursorUpdates(int cursorUpdateMode) {
  638. // We always provide cursor updates.
  639. return true;
  640. }
  641. /*@Override*/ public void closeConnection() {
  642. }
  643. /*@Override*/ public Handler getHandler() {
  644. return null;
  645. }
  646. /*@Override*/ public boolean commitContent(InputContentInfo info, int flags, Bundle opts) {
  647. return false;
  648. }
  649. /*@Override*/ public boolean deleteSurroundingTextInCodePoints(int before, int after) {
  650. if (after > 0) {
  651. int selEnd = imeSelectionEnd(nhandle);
  652. imeReplace(nhandle, selEnd, selEnd + after, "");
  653. }
  654. if (before > 0) {
  655. int selStart = imeSelectionStart(nhandle);
  656. imeReplace(nhandle, selStart - before, selStart, "");
  657. }
  658. return true;
  659. }
  660. /*@Override*/ public SurroundingText getSurroundingText(int beforeChars, int afterChars, int flags) {
  661. Snippet snip = getSnippet();
  662. int selStart = imeSelectionStart(nhandle);
  663. int selEnd = imeSelectionEnd(nhandle);
  664. // Expanding in Java characters is ok.
  665. imeSetSnippet(nhandle, selStart - beforeChars, selEnd + afterChars);
  666. return new SurroundingText(snip.snippet, imeToUTF16(nhandle, selStart), imeToUTF16(nhandle, selEnd), imeToUTF16(nhandle, snip.offset));
  667. }
  668. }
  669. private Snippet getSnippet() {
  670. Snippet snip = new Snippet();
  671. snip.snippet = imeSnippet(nhandle);
  672. snip.offset = imeSnippetStart(nhandle);
  673. return snip;
  674. }
  675. // Snippet is like android.view.inputmethod.SurroundingText but available for Android < 31.
  676. private static class Snippet {
  677. String snippet;
  678. // offset of snippet into the entire editor content. It is in runes because we won't require
  679. // Gio editors to keep track of UTF-16 offsets. The distinction won't matter in practice because IMEs only
  680. // ever see snippets.
  681. int offset;
  682. // substringRunes returns the substring from start to end in runes. The resuls is
  683. // truncated to the snippet.
  684. String substringRunes(int start, int end) {
  685. start -= this.offset;
  686. end -= this.offset;
  687. int runes = snippet.codePointCount(0, snippet.length());
  688. if (start < 0) {
  689. start = 0;
  690. }
  691. if (end < 0) {
  692. end = 0;
  693. }
  694. if (start > runes) {
  695. start = runes;
  696. }
  697. if (end > runes) {
  698. end = runes;
  699. }
  700. return snippet.substring(
  701. snippet.offsetByCodePoints(0, start),
  702. snippet.offsetByCodePoints(0, end)
  703. );
  704. }
  705. }
  706. @Override public AccessibilityNodeProvider getAccessibilityNodeProvider() {
  707. return new AccessibilityNodeProvider() {
  708. private final int[] screenOff = new int[2];
  709. @Override public AccessibilityNodeInfo createAccessibilityNodeInfo(int viewId) {
  710. AccessibilityNodeInfo info = null;
  711. if (viewId == View.NO_ID) {
  712. info = AccessibilityNodeInfo.obtain(GioView.this);
  713. GioView.this.onInitializeAccessibilityNodeInfo(info);
  714. } else {
  715. info = AccessibilityNodeInfo.obtain(GioView.this, viewId);
  716. info.setPackageName(getContext().getPackageName());
  717. info.setVisibleToUser(true);
  718. }
  719. GioView.this.getLocationOnScreen(screenOff);
  720. info = GioView.this.initializeAccessibilityNodeInfo(nhandle, viewId, screenOff[0], screenOff[1], info);
  721. return info;
  722. }
  723. @Override public boolean performAction(int viewId, int action, Bundle arguments) {
  724. if (viewId == View.NO_ID) {
  725. return GioView.this.performAccessibilityAction(action, arguments);
  726. }
  727. switch (action) {
  728. case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS:
  729. GioView.this.onA11yFocus(nhandle, viewId);
  730. GioView.this.sendA11yEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED, viewId);
  731. return true;
  732. case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS:
  733. GioView.this.onClearA11yFocus(nhandle, viewId);
  734. GioView.this.sendA11yEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED, viewId);
  735. return true;
  736. }
  737. return false;
  738. }
  739. };
  740. }
  741. }