From 2d8d8b6ae90dc2f7f30e417ec5ac3fc21629072d Mon Sep 17 00:00:00 2001 From: Alan Hughes <30924086+alanjhughes@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:08:51 +0100 Subject: [PATCH] [android][splash-screen] Migrate to new modules API (#22827) --- packages/expo-splash-screen/CHANGELOG.md | 1 + .../splashscreen/SplashScreenModule.kt | 71 +++++++------------ .../splashscreen/SplashScreenPackage.kt | 9 +-- .../SplashScreenViewController.kt | 3 +- .../exceptions/NoContentViewException.kt | 9 --- .../exceptions/SplashScreenExceptions.kt | 12 ++++ .../singletons/SplashScreenStatusBar.kt | 7 -- .../build/ExpoSplashScreen.d.ts | 2 +- .../build/ExpoSplashScreen.d.ts.map | 2 +- .../build/ExpoSplashScreen.js | 4 +- .../build/ExpoSplashScreen.js.map | 2 +- .../expo-module.config.json | 7 ++ .../src/ExpoSplashScreen.ts | 5 +- packages/expo-splash-screen/unimodule.json | 4 -- 14 files changed, 57 insertions(+), 81 deletions(-) delete mode 100644 packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/NoContentViewException.kt create mode 100644 packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/SplashScreenExceptions.kt create mode 100644 packages/expo-splash-screen/expo-module.config.json delete mode 100644 packages/expo-splash-screen/unimodule.json diff --git a/packages/expo-splash-screen/CHANGELOG.md b/packages/expo-splash-screen/CHANGELOG.md index 651812d0c922fa..00aaeb12dc2809 100644 --- a/packages/expo-splash-screen/CHANGELOG.md +++ b/packages/expo-splash-screen/CHANGELOG.md @@ -9,6 +9,7 @@ ### 🎉 New features - Added support for React Native 0.72. ([#22588](https://github.com/expo/expo/pull/22588) by [@kudo](https://github.com/kudo)) +- Migrated Android codebase to use Expo modules API. ([#22827](https://github.com/expo/expo/pull/22827) by [@alanjhughes](https://github.com/alanjhughes)) ### 🐛 Bug fixes diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenModule.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenModule.kt index 1bac2ea3c532f2..f7d8d93136f56f 100644 --- a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenModule.kt +++ b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenModule.kt @@ -1,13 +1,11 @@ package expo.modules.splashscreen -import android.content.Context - -import expo.modules.core.ExportedModule -import expo.modules.core.ModuleRegistry -import expo.modules.core.Promise -import expo.modules.core.errors.CurrentActivityNotFoundException -import expo.modules.core.interfaces.ActivityProvider -import expo.modules.core.interfaces.ExpoMethod +import expo.modules.kotlin.Promise +import expo.modules.kotlin.exception.Exceptions +import expo.modules.kotlin.modules.Module +import expo.modules.kotlin.modules.ModuleDefinition +import expo.modules.splashscreen.exceptions.HideAsyncException +import expo.modules.splashscreen.exceptions.PreventAutoHideException // Below import must be kept unversioned even in versioned code to provide a redirection from // versioned code realm to unversioned code realm. @@ -15,47 +13,30 @@ import expo.modules.core.interfaces.ExpoMethod // in versioned SplashScreen kotlin object that stores no information about the ExperienceActivity. import expo.modules.splashscreen.singletons.SplashScreen -class SplashScreenModule(context: Context) : ExportedModule(context) { - companion object { - private const val NAME = "ExpoSplashScreen" - private const val ERROR_TAG = "ERR_SPLASH_SCREEN" - } - - private lateinit var activityProvider: ActivityProvider - - override fun getName(): String { - return NAME - } +class SplashScreenModule : Module() { + override fun definition() = ModuleDefinition { + Name("ExpoSplashScreen") - override fun onCreate(moduleRegistry: ModuleRegistry) { - activityProvider = moduleRegistry.getModule(ActivityProvider::class.java) - } + AsyncFunction("preventAutoHideAsync") { promise: Promise -> + val currentActivity = + appContext.currentActivity ?: throw Exceptions.MissingActivity() - @ExpoMethod - fun preventAutoHideAsync(promise: Promise) { - val activity = activityProvider.currentActivity - if (activity == null) { - promise.reject(CurrentActivityNotFoundException()) - return + SplashScreen.preventAutoHide( + currentActivity, + { hasEffect -> promise.resolve(hasEffect) }, + { m -> promise.reject(PreventAutoHideException(m)) } + ) } - SplashScreen.preventAutoHide( - activity, - { hasEffect -> promise.resolve(hasEffect) }, - { m -> promise.reject(ERROR_TAG, m) } - ) - } - @ExpoMethod - fun hideAsync(promise: Promise) { - val activity = activityProvider.currentActivity - if (activity == null) { - promise.reject(CurrentActivityNotFoundException()) - return + AsyncFunction("hideAsync") { promise: Promise -> + val currentActivity = + appContext.currentActivity ?: throw Exceptions.MissingActivity() + + SplashScreen.hide( + currentActivity, + { hasEffect -> promise.resolve(hasEffect) }, + { m -> promise.reject(HideAsyncException(m)) } + ) } - SplashScreen.hide( - activity, - { hasEffect -> promise.resolve(hasEffect) }, - { m -> promise.reject(ERROR_TAG, m) } - ) } } diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenPackage.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenPackage.kt index df81c79ef8781a..27268e1fe67642 100644 --- a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenPackage.kt +++ b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenPackage.kt @@ -2,16 +2,11 @@ package expo.modules.splashscreen import android.content.Context import expo.modules.splashscreen.singletons.SplashScreen -import expo.modules.core.BasePackage -import expo.modules.core.ExportedModule +import expo.modules.core.interfaces.Package import expo.modules.core.interfaces.ReactActivityLifecycleListener import expo.modules.core.interfaces.SingletonModule -class SplashScreenPackage : BasePackage() { - override fun createExportedModules(context: Context): List { - return listOf(SplashScreenModule(context)) - } - +class SplashScreenPackage : Package { override fun createSingletonModules(context: Context?): List { return listOf(SplashScreen) } diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenViewController.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenViewController.kt index d16406d0b302dc..23e8d4b416bb12 100644 --- a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenViewController.kt +++ b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/SplashScreenViewController.kt @@ -2,6 +2,7 @@ package expo.modules.splashscreen import android.app.Activity import android.os.Handler +import android.os.Looper import android.view.View import android.view.ViewGroup import expo.modules.splashscreen.exceptions.NoContentViewException @@ -17,7 +18,7 @@ open class SplashScreenViewController( private val weakActivity = WeakReference(activity) private val contentView: ViewGroup = activity.findViewById(android.R.id.content) ?: throw NoContentViewException() - private val handler = Handler() + private val handler = Handler(Looper.getMainLooper()) private var autoHideEnabled = true private var splashScreenShown = false diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/NoContentViewException.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/NoContentViewException.kt deleted file mode 100644 index 62843852c6afb5..00000000000000 --- a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/NoContentViewException.kt +++ /dev/null @@ -1,9 +0,0 @@ -package expo.modules.splashscreen.exceptions - -import expo.modules.core.errors.CodedException - -class NoContentViewException : CodedException("ContentView is not yet available. Call 'SplashScreen.show(...)' once 'setContentView()' is called.") { - override fun getCode(): String { - return "ERR_NO_CONTENT_VIEW_FOUND" - } -} diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/SplashScreenExceptions.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/SplashScreenExceptions.kt new file mode 100644 index 00000000000000..4e6dc5ed687bca --- /dev/null +++ b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/exceptions/SplashScreenExceptions.kt @@ -0,0 +1,12 @@ +package expo.modules.splashscreen.exceptions + +import expo.modules.kotlin.exception.CodedException + +class NoContentViewException : + CodedException("ContentView is not yet available. Call 'SplashScreen.show(...)' once 'setContentView()' is called.") + +class PreventAutoHideException(message: String) : + CodedException(message) + +class HideAsyncException(message: String) : + CodedException(message) diff --git a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/singletons/SplashScreenStatusBar.kt b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/singletons/SplashScreenStatusBar.kt index 47bba18d702b0d..01201f81b77f4f 100644 --- a/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/singletons/SplashScreenStatusBar.kt +++ b/packages/expo-splash-screen/android/src/main/java/expo/modules/splashscreen/singletons/SplashScreenStatusBar.kt @@ -1,17 +1,10 @@ package expo.modules.splashscreen.singletons -import android.annotation.SuppressLint import android.app.Activity -import android.os.Build import androidx.core.view.ViewCompat object SplashScreenStatusBar { fun configureTranslucent(activity: Activity, translucent: Boolean?) { - @SuppressLint("ObsoleteSdkInt") - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return - } - translucent?.let { activity.runOnUiThread { // If the status bar is translucent hook into the window insets calculations diff --git a/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts b/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts index b97aeef87d998d..648254b255d046 100644 --- a/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts +++ b/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts @@ -1,3 +1,3 @@ -declare const _default: import("expo-modules-core").ProxyNativeModule; +declare const _default: any; export default _default; //# sourceMappingURL=ExpoSplashScreen.d.ts.map \ No newline at end of file diff --git a/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts.map b/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts.map index ae5509a796902a..4c7e0fa09db426 100644 --- a/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts.map +++ b/packages/expo-splash-screen/build/ExpoSplashScreen.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"ExpoSplashScreen.d.ts","sourceRoot":"","sources":["../src/ExpoSplashScreen.ts"],"names":[],"mappings":";AAEA,wBAAyD"} \ No newline at end of file +{"version":3,"file":"ExpoSplashScreen.d.ts","sourceRoot":"","sources":["../src/ExpoSplashScreen.ts"],"names":[],"mappings":";AACA,wBAA6D"} \ No newline at end of file diff --git a/packages/expo-splash-screen/build/ExpoSplashScreen.js b/packages/expo-splash-screen/build/ExpoSplashScreen.js index 010d87f8ffaf80..f5fdbb93fb3418 100644 --- a/packages/expo-splash-screen/build/ExpoSplashScreen.js +++ b/packages/expo-splash-screen/build/ExpoSplashScreen.js @@ -1,3 +1,3 @@ -import { NativeModulesProxy } from 'expo-modules-core'; -export default NativeModulesProxy.ExpoSplashScreen || {}; +import { requireNativeModule } from 'expo-modules-core'; +export default requireNativeModule('ExpoSplashScreen') || {}; //# sourceMappingURL=ExpoSplashScreen.js.map \ No newline at end of file diff --git a/packages/expo-splash-screen/build/ExpoSplashScreen.js.map b/packages/expo-splash-screen/build/ExpoSplashScreen.js.map index 6167fdfb51b805..cbb2d94ea98a9f 100644 --- a/packages/expo-splash-screen/build/ExpoSplashScreen.js.map +++ b/packages/expo-splash-screen/build/ExpoSplashScreen.js.map @@ -1 +1 @@ -{"version":3,"file":"ExpoSplashScreen.js","sourceRoot":"","sources":["../src/ExpoSplashScreen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,eAAe,kBAAkB,CAAC,gBAAgB,IAAI,EAAE,CAAC","sourcesContent":["import { NativeModulesProxy } from 'expo-modules-core';\n\nexport default NativeModulesProxy.ExpoSplashScreen || {};\n"]} \ No newline at end of file +{"version":3,"file":"ExpoSplashScreen.js","sourceRoot":"","sources":["../src/ExpoSplashScreen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,eAAe,mBAAmB,CAAC,kBAAkB,CAAC,IAAI,EAAE,CAAC","sourcesContent":["import { requireNativeModule } from 'expo-modules-core';\nexport default requireNativeModule('ExpoSplashScreen') || {};\n"]} \ No newline at end of file diff --git a/packages/expo-splash-screen/expo-module.config.json b/packages/expo-splash-screen/expo-module.config.json new file mode 100644 index 00000000000000..717211217588c9 --- /dev/null +++ b/packages/expo-splash-screen/expo-module.config.json @@ -0,0 +1,7 @@ +{ + "name": "expo-splash-screen", + "platforms": ["ios", "android"], + "android": { + "modules": ["expo.modules.splashscreen.SplashScreenModule"] + } +} diff --git a/packages/expo-splash-screen/src/ExpoSplashScreen.ts b/packages/expo-splash-screen/src/ExpoSplashScreen.ts index ed22e4f53504a6..4689ae02bf661c 100644 --- a/packages/expo-splash-screen/src/ExpoSplashScreen.ts +++ b/packages/expo-splash-screen/src/ExpoSplashScreen.ts @@ -1,3 +1,2 @@ -import { NativeModulesProxy } from 'expo-modules-core'; - -export default NativeModulesProxy.ExpoSplashScreen || {}; +import { requireNativeModule } from 'expo-modules-core'; +export default requireNativeModule('ExpoSplashScreen') || {}; diff --git a/packages/expo-splash-screen/unimodule.json b/packages/expo-splash-screen/unimodule.json deleted file mode 100644 index 2a5a8e122276e4..00000000000000 --- a/packages/expo-splash-screen/unimodule.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "expo-splash-screen", - "platforms": ["ios", "android"] -}