diff --git a/package.json b/package.json new file mode 100644 index 0000000..a8da12e --- /dev/null +++ b/package.json @@ -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" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..c49072c --- /dev/null +++ b/plugin.xml @@ -0,0 +1,2 @@ + +StatusBarHeight \ No newline at end of file diff --git a/src/android/StatusBarHeight.java b/src/android/StatusBarHeight.java new file mode 100644 index 0000000..ad7e5ff --- /dev/null +++ b/src/android/StatusBarHeight.java @@ -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("未获取到状态栏高度"); + } + } +} + + diff --git a/src/ios/StatusBarHeight.m b/src/ios/StatusBarHeight.m new file mode 100644 index 0000000..1345112 --- /dev/null +++ b/src/ios/StatusBarHeight.m @@ -0,0 +1,39 @@ +/********* StatusBarHeight.m Cordova Plugin Implementation *******/ + +#import + +@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 diff --git a/www/StatusBarHeight.js b/www/StatusBarHeight.js new file mode 100644 index 0000000..e0b730a --- /dev/null +++ b/www/StatusBarHeight.js @@ -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', []); +}