diff --git a/README.md b/README.md index db99f92d..ee606770 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ Although in the global scope, it is not available until after the `deviceready` - StatusBar.backgroundColorByHexString - StatusBar.hide - StatusBar.show +- StatusBar.getStatusBarHeight Properties -------- @@ -301,6 +302,23 @@ Supported Platforms - Windows Phone 8 - Windows Phone 8.1 +StatusBar.getStatusBarHeight +================= + +Gets the current height (in CSS pixels) of system statusbar. + +Note that default Android StatusBar height is setup as `24dp` which in practice means `24px` in WebView. + +This method works well with Android Split Window mode so when app is at the bottom of split view, it returns `0` as the StatusBar height as there is no bar that overlaps app from the top. + + StatusBar.getStatusBarHeight(function(height) { + // height in CSS pixels, i.e. 25 + }); + +Supported Platforms +------------------- + +- Android StatusBar.isVisible ================= diff --git a/src/android/StatusBar.java b/src/android/StatusBar.java index 714c30e8..0d2c5d02 100644 --- a/src/android/StatusBar.java +++ b/src/android/StatusBar.java @@ -25,6 +25,8 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; +import android.graphics.Rect; +import android.content.res.Resources; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaArgs; @@ -38,6 +40,7 @@ public class StatusBar extends CordovaPlugin { private static final String TAG = "StatusBar"; + private static final int STATUS_BAR_HEIGHT_PX = 24; /** * Sets the context of the Command. This can then be used to do things like @@ -203,6 +206,16 @@ public void run() { return true; } + if ("getStatusBarHeight".equals(action)) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getStatusBarHeight())); + } + }); + return true; + } + return false; } @@ -273,4 +286,20 @@ private void setStatusBarStyle(final String style) { } } } + + private int getStatusBarHeight() { + + // Get WebView top offset + Rect rectangle = new Rect(); + Window window = cordova.getActivity().getWindow(); + window.getDecorView().getWindowVisibleDisplayFrame(rectangle); + int webViewTopOffsetPx = Math.round(rectangle.top / Resources.getSystem().getDisplayMetrics().density); + + if (webViewTopOffsetPx > STATUS_BAR_HEIGHT_PX) { + // we're in vertical split mode at the bottom so no statusbar overlaying our app + return 0; + } + return STATUS_BAR_HEIGHT_PX; + + } } diff --git a/www/statusbar.js b/www/statusbar.js index d9d0ea59..ac172921 100644 --- a/www/statusbar.js +++ b/www/statusbar.js @@ -93,6 +93,12 @@ var StatusBar = { show: function () { exec(null, null, "StatusBar", "show", []); StatusBar.isVisible = true; + }, + + getStatusBarHeight: function (successCallback, errorCallback) { + exec(function (result) { + successCallback(result); + }, errorCallback, "StatusBar", "getStatusBarHeight", []); } };