Skip to content

Commit b117bf5

Browse files
brentvatneKudo
authored andcommitted
[ReactAndroid] Respect local night mode configuration for activity to play nicely with Expo client color scheme locking mechanism. expo/expo#8793
This is a squashed commits with the following changes: commit 945096e Author: Kudo Chien <[email protected]> Date: Tue Jul 27 16:49:09 2021 +0800 [android] Upgrade androidx.appcompat to 1.2.0 Backport from: expo/expo@58fa52e
1 parent 398934b commit b117bf5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import com.facebook.react.module.annotations.ReactModule;
1919
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
2020

21+
import androidx.appcompat.app.AppCompatActivity;
22+
import androidx.appcompat.app.AppCompatDelegate;
23+
2124
/** Module that exposes the user's preferred color scheme. */
2225
@ReactModule(name = AppearanceModule.NAME)
2326
public class AppearanceModule extends NativeAppearanceSpec {
@@ -52,10 +55,23 @@ public AppearanceModule(
5255
mColorScheme = colorSchemeForCurrentConfiguration(reactContext);
5356
}
5457

58+
// NOTE(brentvatne): this was static previously, but it wasn't necessary and we need it to not be
59+
// in order to use getCurrentActivity
5560
private String colorSchemeForCurrentConfiguration(Context context) {
5661
if (mOverrideColorScheme != null) {
5762
return mOverrideColorScheme.getScheme();
5863
}
64+
// NOTE(brentvatne): Same code (roughly) that we use in ExpoAppearanceModule to get the config
65+
// as set by ExperienceActivityUtils to force the dark/light mode config on the activity
66+
if (getCurrentActivity() instanceof AppCompatActivity) {
67+
int mode = ((AppCompatActivity) getCurrentActivity()).getDelegate().getLocalNightMode();
68+
switch (mode) {
69+
case AppCompatDelegate.MODE_NIGHT_YES:
70+
return "dark";
71+
case AppCompatDelegate.MODE_NIGHT_NO:
72+
return "light";
73+
}
74+
}
5975
int currentNightMode =
6076
context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
6177
switch (currentNightMode) {

0 commit comments

Comments
 (0)