create project

This commit is contained in:
wangjunget 2019-11-08 00:25:24 +08:00
parent 53aec558a2
commit 4f612bc135
5 changed files with 139 additions and 0 deletions

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "cordova-plugin-statusbar-height",
"version": "1.0.0",
"description": "",
"cordova": {
"id": "cordova-plugin-statusbar-height",
"platforms": [
"android",
"ios"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android",
"cordova-ios"
],
"author": "gene.wang",
"license": "ISC"
}

2
plugin.xml Normal file
View File

@ -0,0 +1,2 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-statusbar-height" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"><name>StatusBarHeight</name><js-module name="StatusBarHeight" src="www/StatusBarHeight.js"><clobbers target="cordova.plugins.StatusBarHeight" /></js-module><platform name="android"><config-file parent="/*" target="res/xml/config.xml"><feature name="StatusBarHeight"><param name="android-package" value="org.apache.cordova.statusbarheight.StatusBarHeight" /></feature></config-file><config-file parent="/*" target="AndroidManifest.xml" /><source-file src="src/android/StatusBarHeight.java" target-dir="src/org/apache/cordova/statusbarheight" /></platform><platform name="ios"><config-file parent="/*" target="config.xml"><feature name="StatusBarHeight"><param name="ios-package" value="StatusBarHeight" /></feature></config-file><source-file src="src/ios/StatusBarHeight.m" /></platform></plugin>

View File

@ -0,0 +1,61 @@
package org.apache.cordova.statusbarheight;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.res.Resources;
import android.content.Context;
/**
* This class echoes a string called from JavaScript.
*/
public class StatusBarHeight extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// 测试命令
if (action.equals("echo")) {
String message = args.getString(0);
this.echo(message, callbackContext);
return true;
}
// 获取状态栏高度命令
if (action.equals("getValue")) {
this.getValue(callbackContext);
return true;
}
return false;
}
// 测试方法
private void echo(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
// 获取状态栏高度
private void getValue(CallbackContext callbackContext) {
Context contextApplication = cordova.getActivity().getApplicationContext();
Resources resources = contextApplication.getResources();
int statusBarHeight = -1;
//获取status_bar_height资源的ID
int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
if (resourceId > 0) {
//根据资源ID获取响应的尺寸值
statusBarHeight = resources.getDimensionPixelSize(resourceId);
callbackContext.success(statusBarHeight); // 成功回调返回状态栏高度值
} else {
callbackContext.error("未获取到状态栏高度");
}
}
}

39
src/ios/StatusBarHeight.m Normal file
View File

@ -0,0 +1,39 @@
/********* StatusBarHeight.m Cordova Plugin Implementation *******/
#import <Cordova/CDV.h>
@interface StatusBarHeight : CDVPlugin {
// Member variables go here.
}
- (void)echo:(CDVInvokedUrlCommand*)command;
- (void)getValue:(CDVInvokedUrlCommand*)command;
@end
@implementation StatusBarHeight
- (void)echo:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)getValue:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
CGFloat statusBarHeight = statusRect.size.height;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:statusBarHeight];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end

18
www/StatusBarHeight.js Normal file
View File

@ -0,0 +1,18 @@
var exec = require('cordova/exec');
// 对外开放接, 回调函数接收状态栏高度值, 设备独立像素值
exports.getValue = function(success, error) {
// android px to pd
// android 返回px值
// ios 返回pt值
function pxToPd(value) {
var pdr = window.devicePixelRatio;
var pd = value / pdr;
if (cordova.platformId == 'android') {
success(pd)
} else {
success(value)
}
}
exec(pxToPd, error, 'StatusBarHeight', 'getValue', []);
}