소스 검색

app: fix external bluetooth keyboards for android

darkfi 11 달 전
부모
커밋
1052333fd4
2개의 변경된 파일59개의 추가작업 그리고 1개의 파일을 삭제
  1. 51 0
      bin/app/java/autosuggest/InvisibleInputView.java
  2. 8 1
      bin/app/src/app/schema/test.rs

+ 51 - 0
bin/app/java/autosuggest/InvisibleInputView.java

@@ -23,6 +23,7 @@ import android.graphics.Rect;
 import android.text.Editable;
 import android.text.Selection;
 import android.util.Log;
+import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.inputmethod.BaseInputConnection;
@@ -106,6 +107,56 @@ public class InvisibleInputView extends View {
         Log.d("darkfi", "onFocusChanged: " + gainFocus);
     }
 
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        Log.d("darkfi", "onKeyDown(" + keyCode + ", " + event + ")");
+        // Copied from CustomInputConnection
+        // Seems only the down event is sent.
+        int selectionStart = Selection.getSelectionStart(editable);
+        if (event.getAction() == KeyEvent.ACTION_DOWN) {
+            if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
+                if (selectionStart > 0) {
+                    editable.delete(selectionStart - 1, selectionStart);
+                    CustomInputConnection.onDeleteSurroundingText(id, 1, 0);
+                }
+            } else if (event.getKeyCode() == KeyEvent.KEYCODE_FORWARD_DEL) {
+                if (selectionStart < editable.length()) {
+                    editable.delete(selectionStart, selectionStart + 1);
+                    CustomInputConnection.onDeleteSurroundingText(id, 0, 1);
+                }
+            } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
+                if (selectionStart > 0) {
+                    Selection.setSelection(editable, selectionStart - 1);
+                    CustomInputConnection.onSetComposeRegion(
+                        id, selectionStart - 1, selectionStart);
+                }
+            } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
+                if (selectionStart < editable.length()) {
+                    Selection.setSelection(editable, selectionStart + 1);
+                    CustomInputConnection.onSetComposeRegion(
+                        id, selectionStart + 1, selectionStart + 2);
+                }
+            } else {
+                int unicodeChar = event.getUnicodeChar();
+                if (unicodeChar != 0) {
+                    int selectionEnd = Selection.getSelectionEnd(editable);
+                    if (selectionStart > selectionEnd) {
+                        int temp = selectionStart;
+                        selectionStart = selectionEnd;
+                        selectionEnd = temp;
+                    }
+
+                    String inputChar = Character.toString((char)unicodeChar);
+                    Log.d("darkfi", "-> " + inputChar + " [" + selectionStart + ", " + selectionEnd + "]");
+                    editable.replace(selectionStart, selectionEnd, inputChar);
+                    CustomInputConnection.onCompose(
+                        id, inputChar, selectionStart, true);
+                }
+            }
+        }
+        return super.onKeyDown(keyCode, event);
+    }
+
     public String debugEditableStr() {
         return CustomInputConnection.editableToXml(editable);
     }

+ 8 - 1
bin/app/src/app/schema/test.rs

@@ -524,5 +524,12 @@ pub async fn make(app: &App, window: SceneNodePtr, i18n_fish: &I18nBabelFish) {
     node.set_property_u32(atom, Role::App, "z_index", 3).unwrap();
     let node =
         node.setup(|me| ChatEdit::new(me, window_scale.clone(), app.render_api.clone())).await;
-    layer_node.link(node);
+    layer_node.link(node.clone());
+
+    // android: show on screen keyboard
+    let focus_task = app.ex.spawn(async move {
+        darkfi::system::sleep(1).await;
+        node.call_method("focus", vec![]).await.unwrap();
+    });
+    app.tasks.lock().unwrap().push(focus_task);
 }