mirror of
https://github.com/apache/cordova-plugin-statusbar.git
synced 2025-01-31 10:35:41 +08:00
refactor(android): Remove unused code (#242)
This commit is contained in:
parent
1672883a7f
commit
53ead01614
@ -54,24 +54,21 @@ public class StatusBar extends CordovaPlugin {
|
|||||||
LOG.v(TAG, "StatusBar: initialization");
|
LOG.v(TAG, "StatusBar: initialization");
|
||||||
super.initialize(cordova, webView);
|
super.initialize(cordova, webView);
|
||||||
|
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> {
|
||||||
@Override
|
// Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
|
||||||
public void run() {
|
// by the Cordova.
|
||||||
// Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
|
Window window = cordova.getActivity().getWindow();
|
||||||
// by the Cordova.
|
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
||||||
Window window = cordova.getActivity().getWindow();
|
|
||||||
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
|
||||||
|
|
||||||
// Read 'StatusBarOverlaysWebView' from config.xml, default is true.
|
// Read 'StatusBarOverlaysWebView' from config.xml, default is true.
|
||||||
setStatusBarTransparent(preferences.getBoolean("StatusBarOverlaysWebView", true));
|
setStatusBarTransparent(preferences.getBoolean("StatusBarOverlaysWebView", true));
|
||||||
|
|
||||||
// Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
|
// Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
|
||||||
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));
|
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));
|
||||||
|
|
||||||
// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
|
// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
|
||||||
String styleSetting = preferences.getString("StatusBarStyle", "lightcontent");
|
String styleSetting = preferences.getString("StatusBarStyle", "lightcontent");
|
||||||
setStatusBarStyle(styleSetting);
|
setStatusBarStyle(styleSetting);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +81,7 @@ public class StatusBar extends CordovaPlugin {
|
|||||||
* @return True if the action was valid, false otherwise.
|
* @return True if the action was valid, false otherwise.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
|
public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) {
|
||||||
LOG.v(TAG, "Executing action: " + action);
|
LOG.v(TAG, "Executing action: " + action);
|
||||||
final Activity activity = this.cordova.getActivity();
|
final Activity activity = this.cordova.getActivity();
|
||||||
final Window window = activity.getWindow();
|
final Window window = activity.getWindow();
|
||||||
@ -96,97 +93,64 @@ public class StatusBar extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ("show".equals(action)) {
|
if ("show".equals(action)) {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> {
|
||||||
@Override
|
int uiOptions = window.getDecorView().getSystemUiVisibility();
|
||||||
public void run() {
|
uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
||||||
// SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
|
uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
|
||||||
// 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;
|
|
||||||
uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
|
|
||||||
|
|
||||||
window.getDecorView().setSystemUiVisibility(uiOptions);
|
window.getDecorView().setSystemUiVisibility(uiOptions);
|
||||||
}
|
|
||||||
|
|
||||||
// CB-11197 We still need to update LayoutParams to force status bar
|
// CB-11197 We still need to update LayoutParams to force status bar
|
||||||
// to be hidden when entering e.g. text fields
|
// to be hidden when entering e.g. text fields
|
||||||
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("hide".equals(action)) {
|
if ("hide".equals(action)) {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> {
|
||||||
@Override
|
int uiOptions = window.getDecorView().getSystemUiVisibility()
|
||||||
public void run() {
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||||
// SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
|
| View.SYSTEM_UI_FLAG_FULLSCREEN;
|
||||||
// use KitKat here to be aligned with "Fullscreen" preference
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
||||||
int uiOptions = window.getDecorView().getSystemUiVisibility()
|
|
||||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
||||||
| View.SYSTEM_UI_FLAG_FULLSCREEN;
|
|
||||||
|
|
||||||
window.getDecorView().setSystemUiVisibility(uiOptions);
|
window.getDecorView().setSystemUiVisibility(uiOptions);
|
||||||
}
|
|
||||||
|
|
||||||
// CB-11197 We still need to update LayoutParams to force status bar
|
// CB-11197 We still need to update LayoutParams to force status bar
|
||||||
// to be hidden when entering e.g. text fields
|
// to be hidden when entering e.g. text fields
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("backgroundColorByHexString".equals(action)) {
|
if ("backgroundColorByHexString".equals(action)) {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
setStatusBarBackgroundColor(args.getString(0));
|
||||||
try {
|
} catch (JSONException ignore) {
|
||||||
setStatusBarBackgroundColor(args.getString(0));
|
LOG.e(TAG, "Invalid hexString argument, use f.i. '#777777'");
|
||||||
} catch (JSONException ignore) {
|
|
||||||
LOG.e(TAG, "Invalid hexString argument, use f.i. '#777777'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("overlaysWebView".equals(action)) {
|
if ("overlaysWebView".equals(action)) {
|
||||||
if (Build.VERSION.SDK_INT >= 21) {
|
this.cordova.getActivity().runOnUiThread(() -> {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
try {
|
||||||
@Override
|
setStatusBarTransparent(args.getBoolean(0));
|
||||||
public void run() {
|
} catch (JSONException ignore) {
|
||||||
try {
|
LOG.e(TAG, "Invalid boolean argument");
|
||||||
setStatusBarTransparent(args.getBoolean(0));
|
|
||||||
} catch (JSONException ignore) {
|
|
||||||
LOG.e(TAG, "Invalid boolean argument");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
else return args.getBoolean(0) == false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("styleDefault".equals(action)) {
|
if ("styleDefault".equals(action)) {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> setStatusBarStyle("default"));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
setStatusBarStyle("default");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("styleLightContent".equals(action)) {
|
if ("styleLightContent".equals(action)) {
|
||||||
this.cordova.getActivity().runOnUiThread(new Runnable() {
|
this.cordova.getActivity().runOnUiThread(() -> setStatusBarStyle("lightcontent"));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
setStatusBarStyle("lightcontent");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,39 +158,35 @@ public class StatusBar extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setStatusBarBackgroundColor(final String colorPref) {
|
private void setStatusBarBackgroundColor(final String colorPref) {
|
||||||
if (Build.VERSION.SDK_INT >= 21) {
|
if (colorPref != null && !colorPref.isEmpty()) {
|
||||||
if (colorPref != null && !colorPref.isEmpty()) {
|
final Window window = cordova.getActivity().getWindow();
|
||||||
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
|
||||||
// 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.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
||||||
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
|
try {
|
||||||
try {
|
// Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
|
||||||
// Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
|
window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
|
||||||
window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
|
} catch (IllegalArgumentException ignore) {
|
||||||
} catch (IllegalArgumentException ignore) {
|
LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
|
||||||
LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
|
} catch (Exception ignore) {
|
||||||
} catch (Exception ignore) {
|
// this should not happen, only in case Android removes this method in a version > 21
|
||||||
// this should not happen, only in case Android removes this method in a version > 21
|
LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
|
||||||
LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setStatusBarTransparent(final boolean transparent) {
|
private void setStatusBarTransparent(final boolean transparent) {
|
||||||
if (Build.VERSION.SDK_INT >= 21) {
|
final Window window = cordova.getActivity().getWindow();
|
||||||
final Window window = cordova.getActivity().getWindow();
|
if (transparent) {
|
||||||
if (transparent) {
|
window.getDecorView().setSystemUiVisibility(
|
||||||
window.getDecorView().setSystemUiVisibility(
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||||
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
|
||||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
|
window.setStatusBarColor(Color.TRANSPARENT);
|
||||||
window.setStatusBarColor(Color.TRANSPARENT);
|
}
|
||||||
}
|
else {
|
||||||
else {
|
window.getDecorView().setSystemUiVisibility(
|
||||||
window.getDecorView().setSystemUiVisibility(
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||||
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
| View.SYSTEM_UI_FLAG_VISIBLE);
|
||||||
| View.SYSTEM_UI_FLAG_VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user