update android AppearanceModule to support dark mode in all OS versions

Summary:
We no longer need to gate by OS version since we want to allow in-app theming. This diff ensures that we are passing in the updated system context to retrieve the correct app theme.

Changelog:
[Android] Enable AppearanceModule for all OS versions

Reviewed By: mdvacca

Differential Revision: D18224915

fbshipit-source-id: 42d5db8497d8bead32c49e3e2a25d4ba779e2b33
This commit is contained in:
Elisa Lou
2019-10-31 10:05:38 -07:00
committed by Facebook Github Bot
parent b5080f7d77
commit 27d7d3fed5
2 changed files with 16 additions and 18 deletions
@@ -727,12 +727,14 @@ public class ReactInstanceManager {
/** Call this from {@link Activity#onConfigurationChanged()}. */
@ThreadConfined(UI)
public void onConfigurationChanged(@Nullable Configuration newConfig) {
public void onConfigurationChanged(Context updatedContext, @Nullable Configuration newConfig) {
UiThreadUtil.assertOnUiThread();
ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
currentContext.getNativeModule(AppearanceModule.class).onConfigurationChanged();
ReactContext currentReactContext = getCurrentReactContext();
if (currentReactContext != null) {
currentReactContext
.getNativeModule(AppearanceModule.class)
.onConfigurationChanged(updatedContext);
}
}
@@ -9,7 +9,6 @@ package com.facebook.react.modules.appearance;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -18,7 +17,7 @@ import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
/** Module that exposes the user's preferred color scheme. For API >= 29. */
/** Module that exposes the user's preferred color scheme. */
@ReactModule(name = AppearanceModule.NAME)
public class AppearanceModule extends ReactContextBaseJavaModule {
@@ -35,16 +34,13 @@ public class AppearanceModule extends ReactContextBaseJavaModule {
}
private static String colorSchemeForCurrentConfiguration(Context context) {
// Night Mode is only available in Android P and up.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
int currentNightMode =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return "light";
case Configuration.UI_MODE_NIGHT_YES:
return "dark";
}
int currentNightMode =
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return "light";
case Configuration.UI_MODE_NIGHT_YES:
return "dark";
}
return "light";
@@ -73,8 +69,8 @@ public class AppearanceModule extends ReactContextBaseJavaModule {
* Call this from your root activity whenever configuration changes. If the
* color scheme has changed, an event will emitted.
*/
public void onConfigurationChanged() {
String newColorScheme = colorSchemeForCurrentConfiguration(getReactApplicationContext());
public void onConfigurationChanged(Context currentContext) {
String newColorScheme = colorSchemeForCurrentConfiguration(currentContext);
if (!mColorScheme.equals(newColorScheme)) {
mColorScheme = newColorScheme;
emitAppearanceChanged(mColorScheme);