Add files via upload

This commit is contained in:
langxiankui 2021-07-20 14:44:31 +08:00 committed by GitHub
commit 8249c10bb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 0 deletions

17
package.json Normal file
View File

@ -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"
}

17
plugin.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-hardwearKey" version="0.0.2"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://apache.org/cordova/ns/plugins/1.0">
<name>Cordova hardwearKey Plugin</name>
<js-module name="cordova-plugin-hardwearKey" src="www/cordova-plugin-hardwearKey.js">
<clobbers target="cordova.plugin.hardwearKey"/>
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="hardwearKey">
<param name="android-package" value="cordova.plugin.hardwearKey.hardwearKey"/>
</feature>
</config-file>
<source-file src="src/android/cordova/plugin/hardwearKey/hardwearKey.java"
target-dir="src/cordova/plugin/hardwearKey"/>
</platform>
</plugin>

View File

@ -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());
}
}
}
}

13
www/cordova-plugin-hardwearKey.js vendored Normal file
View File

@ -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;