From 8249c10bb9b281c0da498ae8dcd18ea312fd84f9 Mon Sep 17 00:00:00 2001 From: langxiankui <33216671+langxiankui@users.noreply.github.com> Date: Tue, 20 Jul 2021 14:44:31 +0800 Subject: [PATCH] Add files via upload --- package.json | 17 ++++ plugin.xml | 17 ++++ .../plugin/hardwearKey/hardwearKey.java | 89 +++++++++++++++++++ www/cordova-plugin-hardwearKey.js | 13 +++ 4 files changed, 136 insertions(+) create mode 100644 package.json create mode 100644 plugin.xml create mode 100644 src/android/cordova/plugin/hardwearKey/hardwearKey.java create mode 100644 www/cordova-plugin-hardwearKey.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..11f94d6 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "cordova-plugin-hardwearKey", + "version": "0.0.1", + "description": "", + "cordova": { + "id": "cordova-plugin-hardwearKey", + "platforms": [ + "android" + ] + }, + "keywords": [ + "ecosystem:cordova", + "cordova-android" + ], + "author": "", + "license": "ISC" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..bac8f6e --- /dev/null +++ b/plugin.xml @@ -0,0 +1,17 @@ + + + Cordova hardwearKey Plugin + + + + + + + + + + + + diff --git a/src/android/cordova/plugin/hardwearKey/hardwearKey.java b/src/android/cordova/plugin/hardwearKey/hardwearKey.java new file mode 100644 index 0000000..f55833d --- /dev/null +++ b/src/android/cordova/plugin/hardwearKey/hardwearKey.java @@ -0,0 +1,89 @@ +package cordova.plugin.hardwearKey; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class hardwearKey extends CordovaPlugin { + + + private KeyReceiver keyReceiver; + @Override + protected void pluginInitialize() { + super.pluginInitialize(); + } + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + switch (action) { + case "startListen": + this.startListen(callbackContext); + return true; + case "stopListen": + this.stopListen(callbackContext); + return true; + } + return false; + } + + private void stopListen(CallbackContext callbackContext) { + cordova.getContext().unregisterReceiver(keyReceiver); + callbackContext.success("OK"); + } + + private void startListen(CallbackContext callbackContext) { + keyReceiver = new KeyReceiver(callbackContext); + IntentFilter filter = new IntentFilter(); + filter.addAction("android.rfid.FUN_KEY"); + filter.addAction("android.intent.action.FUN_KEY"); + cordova.getContext().registerReceiver(keyReceiver , filter); + } + + @Override + public void onDestroy() { + cordova.getContext().unregisterReceiver(keyReceiver); + super.onDestroy(); + } + + + private class KeyReceiver extends BroadcastReceiver { + private CallbackContext cb; + + public KeyReceiver(CallbackContext cb) { + super(); + this.cb = cb; + } + public KeyReceiver() { + super(); + } + + @Override + public void onReceive(Context context, Intent intent) { + int keyCode = intent.getIntExtra("keyCode", 0); + if (keyCode == 0) { + keyCode = intent.getIntExtra("keycode", 0); + } + boolean keyDown = intent.getBooleanExtra("keydown", false); + JSONObject jo = new JSONObject(); + try { + jo.put("keyCode", keyCode); + jo.put("keyDown", keyDown); + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jo); + pluginResult.setKeepCallback(true); + cb.sendPluginResult(pluginResult); + } catch (JSONException e) { + e.printStackTrace(); + cb.error(e.getMessage()); + } + } + + } +} diff --git a/www/cordova-plugin-hardwearKey.js b/www/cordova-plugin-hardwearKey.js new file mode 100644 index 0000000..e2eb4be --- /dev/null +++ b/www/cordova-plugin-hardwearKey.js @@ -0,0 +1,13 @@ +var exec = require('cordova/exec'); + +var coolMethod = function () {}; + +coolMethod.startListen = function (success, error) { + exec(success, error, 'hardwearKey', 'startListen', []); +} + +coolMethod.stopListen = function (success, error) { + exec(success, error, 'hardwearKey', 'stopListen', []); +} + +module.exports = coolMethod;