@@ -9,12 +9,14 @@ import {
99 AndroidApplication as AndroidApplicationDefinition ,
1010 ApplicationEventData ,
1111 CssChangedEventData ,
12- OrientationChangedEventData
12+ OrientationChangedEventData ,
13+ SystemAppearanceChangedEventData
1314} from "." ;
1415
1516import {
1617 displayedEvent , hasListeners , livesync , lowMemoryEvent , notify , Observable , on ,
17- orientationChanged , orientationChangedEvent , setApplication , suspendEvent
18+ orientationChanged , orientationChangedEvent , setApplication , suspendEvent ,
19+ systemAppearanceChanged , systemAppearanceChangedEvent
1820} from "./application-common" ;
1921
2022import { profile } from "../profiling" ;
@@ -51,6 +53,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
5153 public static activityRequestPermissionsEvent = ActivityRequestPermissions ;
5254
5355 private _orientation : "portrait" | "landscape" | "unknown" ;
56+ private _systemAppearance : "light" | "dark" ;
5457 public paused : boolean ;
5558 public nativeApp : android . app . Application ;
5659 public context : android . content . Context ;
@@ -93,9 +96,8 @@ export class AndroidApplication extends Observable implements AndroidApplication
9396 if ( ! this . _orientation ) {
9497 const resources = this . context . getResources ( ) ;
9598 const configuration = < android . content . res . Configuration > resources . getConfiguration ( ) ;
96- const orientation = configuration . orientation ;
9799
98- this . _orientation = getOrientationValue ( orientation ) ;
100+ this . _orientation = getOrientationValue ( configuration ) ;
99101 }
100102
101103 return this . _orientation ;
@@ -105,6 +107,21 @@ export class AndroidApplication extends Observable implements AndroidApplication
105107 this . _orientation = value ;
106108 }
107109
110+ get systemAppearance ( ) : "light" | "dark" {
111+ if ( ! this . _systemAppearance ) {
112+ const resources = this . context . getResources ( ) ;
113+ const configuration = < android . content . res . Configuration > resources . getConfiguration ( ) ;
114+
115+ this . _systemAppearance = getSystemAppearanceValue ( configuration ) ;
116+ }
117+
118+ return this . _systemAppearance ;
119+ }
120+
121+ set systemAppearance ( value : "light" | "dark" ) {
122+ this . _systemAppearance = value ;
123+ }
124+
108125 public registerBroadcastReceiver ( intentFilter : string , onReceiveCallback : ( context : android . content . Context , intent : android . content . Intent ) => void ) {
109126 ensureBroadCastReceiverClass ( ) ;
110127 const that = this ;
@@ -131,6 +148,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
131148 }
132149 }
133150}
151+
134152export interface AndroidApplication {
135153 on ( eventNames : string , callback : ( data : AndroidActivityEventData ) => void , thisArg ?: any ) ;
136154 on ( event : "activityCreated" , callback : ( args : AndroidActivityBundleEventData ) => void , thisArg ?: any ) ;
@@ -252,12 +270,9 @@ export function orientation(): "portrait" | "landscape" | "unknown" {
252270 return androidApp . orientation ;
253271}
254272
255- on ( orientationChangedEvent , ( args : OrientationChangedEventData ) => {
256- const rootView = getRootView ( ) ;
257- if ( rootView ) {
258- orientationChanged ( rootView , args . newValue ) ;
259- }
260- } ) ;
273+ export function systemAppearance ( ) : "dark" | "light" {
274+ return androidApp . systemAppearance ;
275+ }
261276
262277global . __onLiveSync = function __onLiveSync ( context ?: ModuleContext ) {
263278 if ( androidApp && androidApp . paused ) {
@@ -268,7 +283,9 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
268283 livesync ( rootView , context ) ;
269284} ;
270285
271- function getOrientationValue ( orientation : number ) : "portrait" | "landscape" | "unknown" {
286+ function getOrientationValue ( configuration : android . content . res . Configuration ) : "portrait" | "landscape" | "unknown" {
287+ const orientation = configuration . orientation ;
288+
272289 switch ( orientation ) {
273290 case android . content . res . Configuration . ORIENTATION_LANDSCAPE :
274291 return "landscape" ;
@@ -279,6 +296,19 @@ function getOrientationValue(orientation: number): "portrait" | "landscape" | "u
279296 }
280297}
281298
299+ // https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#configuration_changes
300+ function getSystemAppearanceValue ( configuration : android . content . res . Configuration ) : "dark" | "light" {
301+ const systemAppearance = configuration . uiMode & android . content . res . Configuration . UI_MODE_NIGHT_MASK ;
302+
303+ switch ( systemAppearance ) {
304+ case android . content . res . Configuration . UI_MODE_NIGHT_YES :
305+ return "dark" ;
306+ case android . content . res . Configuration . UI_MODE_NIGHT_NO :
307+ case android . content . res . Configuration . UI_MODE_NIGHT_UNDEFINED :
308+ return "light" ;
309+ }
310+ }
311+
282312function initLifecycleCallbacks ( ) {
283313 const setThemeOnLaunch = profile ( "setThemeOnLaunch" , ( activity : androidx . appcompat . app . AppCompatActivity ) => {
284314 // Set app theme after launch screen was used during startup
@@ -380,19 +410,35 @@ function initComponentCallbacks() {
380410 // TODO: This is skipped for now, test carefully for OutOfMemory exceptions
381411 } ) ,
382412
383- onConfigurationChanged : profile ( "onConfigurationChanged" , function ( newConfig : android . content . res . Configuration ) {
384- const newConfigOrientation = newConfig . orientation ;
385- const newOrientation = getOrientationValue ( newConfigOrientation ) ;
413+ onConfigurationChanged : profile ( "onConfigurationChanged" , function ( newConfiguration : android . content . res . Configuration ) {
414+ const newOrientation = getOrientationValue ( newConfiguration ) ;
386415
387416 if ( androidApp . orientation !== newOrientation ) {
388417 androidApp . orientation = newOrientation ;
418+ orientationChanged ( getRootView ( ) , newOrientation ) ;
389419
390420 notify ( < OrientationChangedEventData > {
391421 eventName : orientationChangedEvent ,
392422 android : androidApp . nativeApp ,
393423 newValue : androidApp . orientation ,
394424 object : androidApp
395425 } ) ;
426+
427+ return ;
428+ }
429+
430+ const newSystemAppearance = getSystemAppearanceValue ( newConfiguration ) ;
431+
432+ if ( androidApp . systemAppearance !== newSystemAppearance ) {
433+ androidApp . systemAppearance = newSystemAppearance ;
434+ systemAppearanceChanged ( getRootView ( ) , newSystemAppearance ) ;
435+
436+ notify ( < SystemAppearanceChangedEventData > {
437+ eventName : systemAppearanceChangedEvent ,
438+ android : androidApp . nativeApp ,
439+ newValue : androidApp . systemAppearance ,
440+ object : androidApp
441+ } ) ;
396442 }
397443 } )
398444 } ) ;
0 commit comments