Skip to content

Commit

Permalink
Enables system chrome customization on Android M/O and greater (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored May 3, 2018
1 parent 8a04028 commit f2db5df
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.Window;
import android.util.Log;

import io.flutter.plugin.common.ActivityLifecycleListener;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand All @@ -30,6 +32,7 @@
*/
public class PlatformPlugin implements MethodCallHandler, ActivityLifecycleListener {
private final Activity mActivity;
private JSONObject mCurrentTheme;
public static final int DEFAULT_SYSTEM_UI = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
private static final String kTextPlainFormat = "text/plain";
Expand Down Expand Up @@ -60,7 +63,7 @@ public void onMethodCall(MethodCall call, Result result) {
setSystemChromeEnabledSystemUIOverlays((JSONArray) arguments);
result.success(null);
} else if (method.equals("SystemChrome.setSystemUIOverlayStyle")) {
setSystemChromeSystemUIOverlayStyle((String) arguments);
setSystemChromeSystemUIOverlayStyle((JSONObject) arguments);
result.success(null);
} else if (method.equals("SystemNavigator.pop")) {
popSystemNavigator();
Expand Down Expand Up @@ -218,14 +221,70 @@ private void setSystemChromeEnabledSystemUIOverlays(JSONArray overlays) throws J
updateSystemUiOverlays();
}

private void updateSystemUiOverlays() {
private void updateSystemUiOverlays(){
mActivity.getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays);
if (mCurrentTheme != null) {
setSystemChromeSystemUIOverlayStyle(mCurrentTheme);
}
}

private void setSystemChromeSystemUIOverlayStyle(String style) {
// You can change the navigation bar color (including translucent colors)
// in Android, but you can't change the color of the navigation buttons,
// so LIGHT vs DARK effectively isn't supported in Android.
private void setSystemChromeSystemUIOverlayStyle(JSONObject message) {
Window window = mActivity.getWindow();
View view = window.getDecorView();
int flags = view.getSystemUiVisibility();
try {
// You can change the navigation bar color (including translucent colors)
// in Android, but you can't change the color of the navigation buttons until Android O.
// LIGHT vs DARK effectively isn't supported until then.
// Build.VERSION_CODES.O
if (Build.VERSION.SDK_INT >= 26) {
if (!message.isNull("systemNavigationBarIconBrightness")) {
String systemNavigationBarIconBrightness = message.getString("systemNavigationBarIconBrightness");
switch (systemNavigationBarIconBrightness) {
case "Brightness.dark":
//View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
flags |= 0x10;
break;
case "Brightness.light":
if ((flags & 0x10) == 0x10) {
flags ^= 0x10;
}
break;
}
}
if (!message.isNull("systemNavigationBarColor")) {
window.setNavigationBarColor(message.getInt("systemNavigationBarColor"));
}
}
// Build.VERSION_CODES.M
if (Build.VERSION.SDK_INT >= 23) {
if (!message.isNull("statusBarIconBrightness")) {
String statusBarIconBrightness = message.getString("statusBarIconBrightness");
switch (statusBarIconBrightness) {
case "Brightness.dark":
// View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
flags |= 0x2000;
break;
case "Brightness.light":
if ((flags & 0x2000) == 0x2000) {
flags ^= 0x2000;
}
break;
}
}
if (!message.isNull("statusBarColor")) {
window.setStatusBarColor(message.getInt("statusBarColor"));
}
}
if (!message.isNull("systemNavigationBarDividerColor")) {
// Not availible until Android P.
// window.setNavigationBarDividerColor(systemNavigationBarDividerColor);
}
view.setSystemUiVisibility(flags);
mCurrentTheme = message;
} catch (JSONException err) {
Log.i("PlatformPlugin", err.toString());
}
}

private void popSystemNavigator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ - (void)setSystemChromeEnabledSystemUIOverlays:(NSArray*)overlays {
![overlays containsObject:@"SystemUiOverlay.top"];
}

- (void)setSystemChromeSystemUIOverlayStyle:(NSString*)style {
UIStatusBarStyle statusBarStyle;
if ([style isEqualToString:@"SystemUiOverlayStyle.light"])
- (void)setSystemChromeSystemUIOverlayStyle:(NSDictionary*)message {
NSString* style = message[@"statusBarBrightness"]
UIStatusBarStyle statusBarStyle
if ([style isEqualToString:@"Brightness.light"])
statusBarStyle = UIStatusBarStyleLightContent;
else if ([style isEqualToString:@"SystemUiOverlayStyle.dark"])
else if ([style isEqualToString:@"Brightness.dark"])
statusBarStyle = UIStatusBarStyleDefault;
else
return;
Expand Down

0 comments on commit f2db5df

Please sign in to comment.