Skip to content

Commit

Permalink
refactor(android): simplify window & activity (apache#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu authored Oct 5, 2022
1 parent 7e9db21 commit d4dcd71
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 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,8 +99,6 @@ 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();

switch (action) {
case ACTION_READY:
Expand Down Expand Up @@ -175,7 +178,6 @@ private void setStatusBarBackgroundColor(final String colorPref) {
return;
}

final Window window = cordova.getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // SDK 19-30
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // SDK 21
window.setStatusBarColor(color);
Expand All @@ -196,7 +198,6 @@ private void setStatusBarTransparent(final boolean isTransparent) {

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 d4dcd71

Please sign in to comment.