Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent transparency + getStatusBarHeight method #96

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Although in the global scope, it is not available until after the `deviceready`
- StatusBar.backgroundColorByHexString
- StatusBar.hide
- StatusBar.show
- StatusBar.getStatusBarHeight (Android only)

Properties
--------
Expand Down Expand Up @@ -286,6 +287,18 @@ Shows the statusbar.
StatusBar.show();


StatusBar.getStatusBarHeight
=================

Gets the current height (in CSS pixels) of system statusbar.

**Note that this is implemented currently only on Android**

StatusBar.getStatusBarHeight(function(height) {
// height in CSS pixels, i.e. 25
});


Supported Platforms
-------------------

Expand Down
42 changes: 38 additions & 4 deletions src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +41,8 @@
public class StatusBar extends CordovaPlugin {
private static final String TAG = "StatusBar";

private boolean transparent = false;

/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
Expand All @@ -62,6 +66,9 @@ public void run() {
// Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));

// Read 'StatusBarOverlaysWebView' from config.xml, default is false.
setStatusBarTransparent(preferences.getBoolean("StatusBarOverlaysWebView", false));

// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
setStatusBarStyle(preferences.getString("StatusBarStyle", "lightcontent"));
}
Expand Down Expand Up @@ -96,7 +103,11 @@ public void run() {
// use KitKat here to be aligned with "Fullscreen" preference
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int uiOptions = window.getDecorView().getSystemUiVisibility();
uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
if (transparent) {
uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
} else {
uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
}
uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;

window.getDecorView().setSystemUiVisibility(uiOptions);
Expand Down Expand Up @@ -203,6 +214,17 @@ 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;
}

Expand All @@ -227,20 +249,22 @@ private void setStatusBarBackgroundColor(final String colorPref) {
}

private void setStatusBarTransparent(final boolean transparent) {
if (Build.VERSION.SDK_INT >= 21) {
this.transparent = transparent;
final Window window = cordova.getActivity().getWindow();
if (transparent) {
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
window.setStatusBarColor(Color.TRANSPARENT);

if (Build.VERSION.SDK_INT >= 21) {
window.setStatusBarColor(Color.TRANSPARENT);
}
}
else {
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}

private void setStatusBarStyle(final String style) {
Expand Down Expand Up @@ -273,4 +297,14 @@ private void setStatusBarStyle(final String style) {
}
}
}

private int getStatusBarHeight() {

Rect rectangle = new Rect();
Window window = cordova.getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = Math.round(rectangle.top / Resources.getSystem().getDisplayMetrics().density);
return statusBarHeight;

}
}
7 changes: 7 additions & 0 deletions www/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ var StatusBar = {
show: function () {
exec(null, null, "StatusBar", "show", []);
StatusBar.isVisible = true;
},

getStatusBarHeight: function (successCallback, errorCallback) {
exec(function (result) {
successCallback(result);
}, errorCallback, "StatusBar", "getStatusBarHeight", []);
StatusBar.isVisible = true;
}

};
Expand Down