Fix hidden StatusBar reappearing after exiting app on Android

Summary:I changed the technique used to hide the status bar and now it works properly. Also changed the way flags are set on the decorView to make sure it doesn't cause issues with other flags already set and fix deprecated Promise.reject

**Test plan**
Test using the UIExplorer StatusBar example, change the `hidden` prop value and go in and out of the app making sure the status bar has the right visibility.

Fixes #5991
Closes https://github.com/facebook/react-native/pull/6051

Differential Revision: D2960060

Pulled By: nicklockwood

fb-gh-sync-id: ee1c541896f5771d27cfd3ff18537edb6c017284
shipit-source-id: ee1c541896f5771d27cfd3ff18537edb6c017284
This commit is contained in:
Janic Duplessis
2016-02-22 01:49:46 -08:00
committed by facebook-github-bot-8
parent b9ed72a043
commit cf3bd9f9a1

View File

@@ -17,6 +17,7 @@ import android.app.Activity;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.WindowManager;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
@@ -26,11 +27,10 @@ import com.facebook.react.bridge.UiThreadUtil;
public class StatusBarModule extends ReactContextBaseJavaModule {
private static final String ERROR_NO_ACTIVITY =
private static final String ERROR_NO_ACTIVITY = "E_NO_ACTIVITY";
private static final String ERROR_NO_ACTIVITY_MESSAGE =
"Tried to change the status bar while not attached to an Activity";
private int mWindowFlags = 0;
public StatusBarModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@@ -44,7 +44,7 @@ public class StatusBarModule extends ReactContextBaseJavaModule {
public void setColor(final int color, final boolean animated, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}
@@ -85,21 +85,20 @@ public class StatusBarModule extends ReactContextBaseJavaModule {
public void setTranslucent(final boolean translucent, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility();
if (translucent) {
mWindowFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
} else {
mWindowFlags &=
~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
flags &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
activity.getWindow().getDecorView().setSystemUiVisibility(mWindowFlags);
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
ViewCompat.requestApplyInsets(activity.getWindow().getDecorView());
res.resolve(null);
}
@@ -111,7 +110,7 @@ public class StatusBarModule extends ReactContextBaseJavaModule {
public void setHidden(final boolean hidden, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}
UiThreadUtil.runOnUiThread(
@@ -119,11 +118,13 @@ public class StatusBarModule extends ReactContextBaseJavaModule {
@Override
public void run() {
if (hidden) {
mWindowFlags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
mWindowFlags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
activity.getWindow().getDecorView().setSystemUiVisibility(mWindowFlags);
res.resolve(null);
}
}