-
Notifications
You must be signed in to change notification settings - Fork 481
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
WIP: (android) Remember statusbar color during overlay state changes #172
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,8 @@ | |
|
||
public class StatusBar extends CordovaPlugin { | ||
private static final String TAG = "StatusBar"; | ||
private String _bgColor; | ||
private String _overlayBgColor; | ||
|
||
/** | ||
* Sets the context of the Command. This can then be used to do things like | ||
|
@@ -54,13 +56,30 @@ public void initialize(final CordovaInterface cordova, CordovaWebView webView) { | |
this.cordova.getActivity().runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
|
||
// 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 'StatusBarBackgroundColor' from config.xml, default is #000000. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not your code, but this comment is useless and could be removed. |
||
setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000")); | ||
_bgColor = preferences.getString("StatusBarBackgroundColor", "#000000"); | ||
setStatusBarBackgroundColor(_bgColor); | ||
|
||
int color; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be moved to the try block? |
||
try { | ||
color = Color.parseColor(_bgColor); | ||
// If `StatusBarBackgroundColor` contains alpha other than 1, then it | ||
// is reasonable to assume that the color is intended for overlayed statusbar | ||
if (Color.alpha(color) != 1) { | ||
_overlayBgColor = _bgColor; | ||
} else { | ||
_overlayBgColor = "#00000000"; //Transparent | ||
} | ||
} catch (IllegalArgumentException e) { | ||
LOG.w(TAG, "Invalid color " + _bgColor + ". Defaulting to #00000000."); | ||
_overlayBgColor = "#00000000"; | ||
} | ||
|
||
// Read 'StatusBarStyle' from config.xml, default is 'lightcontent'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above with the comment ;) |
||
setStatusBarStyle(preferences.getString("StatusBarStyle", "lightcontent")); | ||
|
@@ -210,6 +229,17 @@ private void setStatusBarBackgroundColor(final String colorPref) { | |
if (Build.VERSION.SDK_INT >= 21) { | ||
if (colorPref != null && !colorPref.isEmpty()) { | ||
final Window window = cordova.getActivity().getWindow(); | ||
|
||
// This is so that we can return to the last set statusbar colours if overlay state changes. | ||
int uiOptions = window.getDecorView().getSystemUiVisibility(); | ||
boolean isOverlayed = ((uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) == uiOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I think that this method should have a different name, It's name is a bit misleading; You can see that it's used for the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try the SYSTEM_UI_FLAG_LAYOUT_STABLE, however it is now deprecated... along with the other system ui flag constants. But it looks like the replacement is only going to be available in the API 30 (unreleased, under API R), soooo we will have no choice but to continue using them for the next foreseeable future.
Makes me wonder if this will correct #158 ... |
||
if (isOverlayed) { | ||
_overlayBgColor = colorPref; | ||
} else { | ||
_bgColor = colorPref; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls remove one newline |
||
|
||
// 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); | ||
|
@@ -233,12 +263,13 @@ private void setStatusBarTransparent(final boolean transparent) { | |
window.getDecorView().setSystemUiVisibility( | ||
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | ||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | ||
window.setStatusBarColor(Color.TRANSPARENT); | ||
setStatusBarBackgroundColor(_overlayBgColor); | ||
} | ||
else { | ||
window.getDecorView().setSystemUiVisibility( | ||
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | ||
| View.SYSTEM_UI_FLAG_VISIBLE); | ||
setStatusBarBackgroundColor(_bgColor); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO these variables should not begin with an underscore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My habits leaked into the PR, even though it's not consistent with Apache's codebase 😁
I'll remove the underscores when I come back to this task.