From a56f1164e9ad4a89b0094806882537391ffb5bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 18 Mar 2021 18:47:58 +0800 Subject: [PATCH] Android: implement keyboard plugin --- .../main/java/pub/doric/DoricRegistry.java | 2 + .../java/pub/doric/plugin/KeyboardPlugin.java | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 doric-android/doric/src/main/java/pub/doric/plugin/KeyboardPlugin.java diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index df3a607f..51bd3f48 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; import pub.doric.plugin.AnimatePlugin; import pub.doric.plugin.CoordinatorPlugin; import pub.doric.plugin.DoricJavaPlugin; +import pub.doric.plugin.KeyboardPlugin; import pub.doric.plugin.ModalPlugin; import pub.doric.plugin.NavBarPlugin; import pub.doric.plugin.NavigatorPlugin; @@ -102,6 +103,7 @@ public class DoricRegistry { this.registerNativePlugin(StatusBarPlugin.class); this.registerNativePlugin(CoordinatorPlugin.class); this.registerNativePlugin(NotchPlugin.class); + this.registerNativePlugin(KeyboardPlugin.class); this.registerViewNode(RootNode.class); this.registerViewNode(TextNode.class); diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/KeyboardPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/KeyboardPlugin.java new file mode 100644 index 00000000..1ce73378 --- /dev/null +++ b/doric-android/doric/src/main/java/pub/doric/plugin/KeyboardPlugin.java @@ -0,0 +1,95 @@ +package pub.doric.plugin; + +import android.app.Activity; +import android.os.Build; +import android.util.DisplayMetrics; + +import androidx.annotation.RequiresApi; + +import com.github.pengfeizhou.jscore.JSString; +import com.github.pengfeizhou.jscore.JavaValue; +import com.qmuiteam.qmui.util.QMUIKeyboardHelper; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import pub.doric.DoricContext; +import pub.doric.extension.bridge.DoricMethod; +import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; +import pub.doric.utils.DoricUtils; + +/** + * @Description: pub.doric.plugin + * @Author: jingpeng.wang + * @CreateDate: 2021-03-18 + */ + +@DoricPlugin(name = "keyboard") +public class KeyboardPlugin extends DoricJavaPlugin { + + private final Set callbackIds = new HashSet<>(); + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) + public KeyboardPlugin(DoricContext doricContext) { + super(doricContext); + + if (getDoricContext().getContext() instanceof Activity) { + Activity activity = (Activity) getDoricContext().getContext(); + + DisplayMetrics metrics = new DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); + int realHeight = metrics.heightPixels; + + activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); + int usableHeight = metrics.heightPixels; + + final int virtualNavigationHeight = realHeight - usableHeight; + + QMUIKeyboardHelper.setVisibilityEventListener(activity, new QMUIKeyboardHelper.KeyboardVisibilityEventListener() { + @Override + public boolean onVisibilityChanged(boolean isOpen, int heightDiff) { + Iterator it = callbackIds.iterator(); + if (it.hasNext()) { + do { + String callbackId = it.next(); + DoricPromise callback = new DoricPromise(getDoricContext(), callbackId); + + JSONObject data = new JSONObject(); + try { + data.put("height", DoricUtils.px2dp(heightDiff - virtualNavigationHeight)); + callback.resolve(new JavaValue(data)); + } catch (JSONException e) { + e.printStackTrace(); + callback.reject(); + } + } while (it.hasNext()); + } + return false; + } + }); + } + } + + @DoricMethod + public void subscribe(JSString args, DoricPromise promise) { + final String callbackId = args.asString().value(); + callbackIds.add(callbackId); + promise.resolve(new JavaValue(callbackId)); + } + + @DoricMethod + public void unsubscribe(String subscribeId, DoricPromise promise) { + if (callbackIds.contains(subscribeId)) { + callbackIds.remove(subscribeId); + + promise.resolve(new JavaValue(subscribeId)); + } else { + promise.reject(new JavaValue("subscribeId does not exist")); + } + } +}