package eu.friedcutlet.yokan import android.app.Activity import android.content.Intent import android.app.SearchManager import android.content.res.Configuration import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.result.contract.ActivityResultContracts import eu.friedcutlet.yokan.app.AndroidBootstrapState import eu.friedcutlet.yokan.services.system.AppDeepLinks import eu.friedcutlet.yokan.services.files.registerFolderPickerLauncher import eu.friedcutlet.yokan.services.media.player.AndroidPictureInPictureController import eu.friedcutlet.yokan.services.applock.AndroidAppLockBridge import eu.friedcutlet.yokan.ui.theme.applySystemBarTheme import eu.friedcutlet.yokan.ui.YokanApp import eu.friedcutlet.yokan.ui.navigation.searchDeepLink class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) AndroidPictureInPictureController.attach(this) // Edge-to-edge from the very first frame; the common Compose layer // (Scaffold contentWindowInsets) consumes the insets. // Icon contrast is re-synced with the in-app theme by // applySystemBarTheme() once Compose resolves dark mode. enableEdgeToEdge() AndroidForegroundActivity.activity = this registerFolderPickerLauncher(this) val credentialLauncher = registerForActivityResult( ActivityResultContracts.StartActivityForResult(), ) { result -> AndroidAppLockBridge.deliverResult(result.resultCode == Activity.RESULT_OK) } AndroidAppLockBridge.launch = { intent -> runCatching { credentialLauncher.launch(intent) }.isSuccess } AndroidBootstrapState.initialDeepLink = intent?.deepLink() setContent { YokanApp() } } override fun onDestroy() { AndroidPictureInPictureController.detach(this) if (AndroidForegroundActivity.activity === this) { AndroidForegroundActivity.activity = null AndroidAppLockBridge.launch = null } super.onDestroy() } override fun onUserLeaveHint() { AndroidPictureInPictureController.onUserLeaveHint(this) super.onUserLeaveHint() } override fun onPictureInPictureModeChanged( isInPictureInPictureMode: Boolean, newConfig: Configuration, ) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) AndroidPictureInPictureController.onPictureInPictureModeChanged(this, isInPictureInPictureMode) } override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setIntent(intent) val link = intent.deepLink() AndroidBootstrapState.initialDeepLink = link AppDeepLinks.dispatch(link) } } private fun Intent.deepLink(): String? = systemIntentLink(action, dataString) { getCharSequenceExtra(it)?.toString() } /** Shared text, system/assistant search and text selection open global search; everything else is a VIEW link. */ internal fun systemIntentLink(action: String?, dataString: String?, extra: (String) -> String?): String? = when (action) { Intent.ACTION_SEND -> extra(Intent.EXTRA_TEXT) Intent.ACTION_SEARCH, Intent.ACTION_WEB_SEARCH, GMS_SEARCH_ACTION -> extra(SearchManager.QUERY) Intent.ACTION_PROCESS_TEXT -> extra(Intent.EXTRA_PROCESS_TEXT) else -> return dataString }?.trim()?.takeIf(String::isNotEmpty)?.let { searchDeepLink(it) } private const val GMS_SEARCH_ACTION = "com.google.android.gms.actions.SEARCH_ACTION"