From f5bfe7e872f9b93c25824b20603d1be3759e0304 Mon Sep 17 00:00:00 2001 From: Rui Ying Date: Sun, 27 Oct 2024 21:09:13 -0700 Subject: [PATCH] Fix background task on Android --- .../expo-modules-core+2.0.0-preview.4.patch | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/patches/expo-modules-core+2.0.0-preview.4.patch b/patches/expo-modules-core+2.0.0-preview.4.patch index 21f9d299..d6abdf43 100644 --- a/patches/expo-modules-core+2.0.0-preview.4.patch +++ b/patches/expo-modules-core+2.0.0-preview.4.patch @@ -30,3 +30,52 @@ index 246afe0..199e068 100644 'OTHER_SWIFT_FLAGS' => "$(inherited) #{new_arch_enabled ? new_arch_compiler_flags : ''}" } s.user_target_xcconfig = { +diff --git a/node_modules/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt b/node_modules/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt +index 14a88fd..54d70f4 100644 +--- a/node_modules/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt ++++ b/node_modules/expo-modules-core/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt +@@ -28,7 +28,7 @@ class RNHeadlessAppLoader @DoNotStrip constructor(private val context: Context) + // In old arch reactHost will be null + if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { + // New architecture +- val reactHost = (context as ReactApplication).reactHost ?: throw IllegalStateException("Your application does not have a valid reactHost") ++ val reactHost = (context.applicationContext as ReactApplication).reactHost ?: throw IllegalStateException("Your application does not have a valid reactHost") + reactHost.addReactInstanceEventListener( + object : ReactInstanceEventListener { + override fun onReactContextInitialized(context: ReactContext) { +@@ -42,7 +42,7 @@ class RNHeadlessAppLoader @DoNotStrip constructor(private val context: Context) + reactHost.start() + } else { + // Old architecture +- val reactInstanceManager = (context as ReactApplication).reactNativeHost.reactInstanceManager ++ val reactInstanceManager = (context.applicationContext as ReactApplication).reactNativeHost.reactInstanceManager + reactInstanceManager.addReactInstanceEventListener( + object : ReactInstanceEventListener { + override fun onReactContextInitialized(context: ReactContext) { +@@ -69,7 +69,7 @@ class RNHeadlessAppLoader @DoNotStrip constructor(private val context: Context) + val reactContext = appRecords[appScopeKey] ?: return false + if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { + // New architecture +- val reactHost = (reactContext.baseContext as ReactApplication).reactHost ?: throw IllegalStateException("Your application does not have a valid reactHost") ++ val reactHost = (reactContext.applicationContext as ReactApplication).reactHost ?: throw IllegalStateException("Your application does not have a valid reactHost") + android.os.Handler(reactContext.mainLooper).post { + reactHost.destroy("Closing headless task app", null) + HeadlessAppLoaderNotifier.notifyAppDestroyed(appScopeKey) +@@ -77,7 +77,7 @@ class RNHeadlessAppLoader @DoNotStrip constructor(private val context: Context) + } + } else { + // Old architecture +- val reactNativeHost = (reactContext as ReactApplication).reactNativeHost ++ val reactNativeHost = (reactContext.applicationContext as ReactApplication).reactNativeHost + if (reactNativeHost.hasInstance()) { + val reactInstanceManager: ReactInstanceManager = reactNativeHost.reactInstanceManager + android.os.Handler(reactContext.mainLooper).post { +@@ -106,7 +106,7 @@ class RNHeadlessAppLoader @DoNotStrip constructor(private val context: Context) + return true + } else { + // Old architecture +- val reactNativeHost = (reactContext.baseContext as ReactApplication).reactNativeHost ++ val reactNativeHost = (reactContext.applicationContext as ReactApplication).reactNativeHost + return reactNativeHost.reactInstanceManager.hasStartedCreatingInitialContext() + } + }