package com.t3tools.t3code.preview import expo.modules.splashscreen.SplashScreenManager import android.os.Build import android.os.Bundle import androidx.activity.OnBackPressedCallback import com.facebook.react.uimanager.DisplayMetricsHolder import android.view.View import android.util.DisplayMetrics import android.content.pm.ActivityInfo import android.content.res.Configuration import com.facebook.react.ReactActivity import com.facebook.react.ReactActivityDelegate import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled import com.facebook.react.defaults.DefaultReactActivityDelegate import expo.modules.ReactActivityDelegateWrapper class MainActivity : ReactActivity() { // Routes predictive-back gestures to JS on Android 13-15, where react-native // registers no OnBackPressedDispatcher callback of its own (it only does on // Android 16+). Registered in onCreate; added by withAndroidPredictiveBackCompat. private val predictiveBackCompatCallback = object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { isEnabled = false onBackPressed() isEnabled = true } } // Keep deprecated APIs to match React Native screen metrics on Android 24+. // Physical display metrics omit the configured font scale. @Suppress("DEPRECATION") private fun bindWindowDisplayMetrics() { val windowMetrics = resources.displayMetrics val metrics = DisplayMetrics() metrics.setTo(windowMetrics) windowManager.defaultDisplay.getRealMetrics(metrics) metrics.scaledDensity = windowMetrics.scaledDensity DisplayMetricsHolder.setScreenDisplayMetrics(metrics) } // Expo constructs the root view after onCreate; its constructor resets metrics. override fun setContentView(view: View?) { super.setContentView(view) bindWindowDisplayMetrics() } // Applied in onCreate and re-applied on fold/unfold; added by // withAndroidTabletOrientation. override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) bindWindowDisplayMetrics() applyTabletOrientation() } private fun applyTabletOrientation() { requestedOrientation = if (resources.configuration.smallestScreenWidthDp >= 600) { ActivityInfo.SCREEN_ORIENTATION_FULL_USER } else { ActivityInfo.SCREEN_ORIENTATION_PORTRAIT } } override fun onCreate(savedInstanceState: Bundle?) { // Set the theme to AppTheme BEFORE onCreate to support // coloring the background, status bar, and navigation bar. // This is required for expo-splash-screen. // setTheme(R.style.AppTheme); // @generated begin expo-splashscreen - expo prebuild (DO NOT MODIFY) sync-f3ff59a738c56c9a6119210cb55f0b613eb8b6af SplashScreenManager.registerOnActivity(this) // @generated end expo-splashscreen super.onCreate(null) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && Build.VERSION.SDK_INT < Build.VERSION_CODES.BAKLAVA) { onBackPressedDispatcher.addCallback(this, predictiveBackCompatCallback) } applyTabletOrientation() } /** * Returns the name of the main component registered from JavaScript. This is used to schedule * rendering of the component. */ override fun getMainComponentName(): String = "main" /** * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate] * which allows you to enable New Architecture with a single boolean flags [fabricEnabled] */ override fun createReactActivityDelegate(): ReactActivityDelegate { return ReactActivityDelegateWrapper( this, BuildConfig.IS_NEW_ARCHITECTURE_ENABLED, object : DefaultReactActivityDelegate( this, mainComponentName, fabricEnabled ){}) } /** * Align the back button behavior with Android S * where moving root activities to background instead of finishing activities. * @see onBackPressed */ override fun invokeDefaultOnBackPressed() { predictiveBackCompatCallback.isEnabled = false try { invokeDefaultOnBackPressedLegacy() } finally { predictiveBackCompatCallback.isEnabled = true } } private fun invokeDefaultOnBackPressedLegacy() { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) { if (!moveTaskToBack(false)) { // For non-root activities, use the default implementation to finish them. super.invokeDefaultOnBackPressed() } return } // Use the default back button implementation on Android S // because it's doing more than [Activity.moveTaskToBack] in fact. super.invokeDefaultOnBackPressed() } }