Skip to content

Commit

Permalink
refactor(android): simplify window & activity
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Oct 3, 2022
1 parent ddc1cc6 commit 261baa4
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
*/
package org.apache.cordova.statusbar;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

Expand All @@ -52,6 +52,9 @@ public class StatusBar extends CordovaPlugin {
private static final String STYLE_DEFAULT = "default";
private static final String STYLE_LIGHT_CONTENT = "lightcontent";

private AppCompatActivity activity;
private Window window;

/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
Expand All @@ -64,10 +67,12 @@ public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
LOG.v(TAG, "StatusBar: initialization");
super.initialize(cordova, webView);

this.cordova.getActivity().runOnUiThread(() -> {
activity = this.cordova.getActivity();
window = activity.getWindow();

activity.runOnUiThread(() -> {
// Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
// by the Cordova.
Window window = cordova.getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

// Read 'StatusBarOverlaysWebView' from config.xml, default is true.
Expand All @@ -94,17 +99,14 @@ public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
@Override
public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) {
LOG.v(TAG, "Executing action: " + action);
final Activity activity = this.cordova.getActivity();
final Window window = activity.getWindow();

if (ACTION_READY.equals(action)) {
boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
return true;
}

if (ACTION_SHOW.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> {
activity.runOnUiThread(() -> {
int uiOptions = window.getDecorView().getSystemUiVisibility();
uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
Expand All @@ -119,7 +121,7 @@ public boolean execute(final String action, final CordovaArgs args, final Callba
}

if (ACTION_HIDE.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> {
activity.runOnUiThread(() -> {
int uiOptions = window.getDecorView().getSystemUiVisibility()
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
Expand All @@ -134,7 +136,7 @@ public boolean execute(final String action, final CordovaArgs args, final Callba
}

if (ACTION_BACKGROUND_COLOR_BY_HEX_STRING.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> {
activity.runOnUiThread(() -> {
try {
setStatusBarBackgroundColor(args.getString(0));
} catch (JSONException ignore) {
Expand All @@ -145,7 +147,7 @@ public boolean execute(final String action, final CordovaArgs args, final Callba
}

if (ACTION_OVERLAYS_WEB_VIEW.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> {
activity.runOnUiThread(() -> {
try {
setStatusBarTransparent(args.getBoolean(0));
} catch (JSONException ignore) {
Expand All @@ -156,12 +158,12 @@ public boolean execute(final String action, final CordovaArgs args, final Callba
}

if (ACTION_STYLE_DEFAULT.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> setStatusBarStyle(STYLE_DEFAULT));
activity.runOnUiThread(() -> setStatusBarStyle(STYLE_DEFAULT));
return true;
}

if (ACTION_STYLE_LIGHT_CONTENT.equals(action)) {
this.cordova.getActivity().runOnUiThread(() -> setStatusBarStyle(STYLE_LIGHT_CONTENT));
activity.runOnUiThread(() -> setStatusBarStyle(STYLE_LIGHT_CONTENT));
return true;
}

Expand All @@ -170,7 +172,6 @@ public boolean execute(final String action, final CordovaArgs args, final Callba

private void setStatusBarBackgroundColor(final String colorPref) {
if (colorPref != null && !colorPref.isEmpty()) {
final Window window = cordova.getActivity().getWindow();
// Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Expand All @@ -187,7 +188,6 @@ private void setStatusBarBackgroundColor(final String colorPref) {
}

private void setStatusBarTransparent(final boolean transparent) {
final Window window = cordova.getActivity().getWindow();
if (transparent) {
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
Expand All @@ -203,7 +203,6 @@ private void setStatusBarTransparent(final boolean transparent) {

private void setStatusBarStyle(final String style) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !style.isEmpty()) {
Window window = cordova.getActivity().getWindow();
View decorView = window.getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView);

Expand Down

0 comments on commit 261baa4

Please sign in to comment.