MainActivity.kt
composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/MainActivity.kt
1package eu.friedcutlet.yokan23import android.app.Activity4import android.content.Intent5import android.app.SearchManager6import android.content.res.Configuration7import android.os.Bundle8import androidx.activity.ComponentActivity9import androidx.activity.compose.setContent10import androidx.activity.enableEdgeToEdge11import androidx.activity.result.contract.ActivityResultContracts12import eu.friedcutlet.yokan.app.AndroidBootstrapState13import eu.friedcutlet.yokan.services.system.AppDeepLinks14import eu.friedcutlet.yokan.services.files.registerFolderPickerLauncher15import eu.friedcutlet.yokan.services.media.player.AndroidPictureInPictureController16import eu.friedcutlet.yokan.services.applock.AndroidAppLockBridge17import eu.friedcutlet.yokan.ui.theme.applySystemBarTheme18import eu.friedcutlet.yokan.ui.YokanApp19import eu.friedcutlet.yokan.ui.navigation.searchDeepLink2021class MainActivity : ComponentActivity() {22 override fun onCreate(savedInstanceState: Bundle?) {23 super.onCreate(savedInstanceState)24 AndroidPictureInPictureController.attach(this)25 // Edge-to-edge from the very first frame; the common Compose layer26 // (Scaffold contentWindowInsets) consumes the insets.27 // Icon contrast is re-synced with the in-app theme by28 // applySystemBarTheme() once Compose resolves dark mode.29 enableEdgeToEdge()30 AndroidForegroundActivity.activity = this31 registerFolderPickerLauncher(this)32 val credentialLauncher = registerForActivityResult(33 ActivityResultContracts.StartActivityForResult(),34 ) { result ->35 AndroidAppLockBridge.deliverResult(result.resultCode == Activity.RESULT_OK)36 }37 AndroidAppLockBridge.launch = { intent ->38 runCatching { credentialLauncher.launch(intent) }.isSuccess39 }40 AndroidBootstrapState.initialDeepLink = intent?.deepLink()41 setContent {42 YokanApp()43 }44 }4546 override fun onDestroy() {47 AndroidPictureInPictureController.detach(this)48 if (AndroidForegroundActivity.activity === this) {49 AndroidForegroundActivity.activity = null50 AndroidAppLockBridge.launch = null51 }52 super.onDestroy()53 }5455 override fun onUserLeaveHint() {56 AndroidPictureInPictureController.onUserLeaveHint(this)57 super.onUserLeaveHint()58 }5960 override fun onPictureInPictureModeChanged(61 isInPictureInPictureMode: Boolean,62 newConfig: Configuration,63 ) {64 super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)65 AndroidPictureInPictureController.onPictureInPictureModeChanged(this, isInPictureInPictureMode)66 }6768 override fun onNewIntent(intent: Intent) {69 super.onNewIntent(intent)70 setIntent(intent)71 val link = intent.deepLink()72 AndroidBootstrapState.initialDeepLink = link73 AppDeepLinks.dispatch(link)74 }75}7677private fun Intent.deepLink(): String? = systemIntentLink(action, dataString) { getCharSequenceExtra(it)?.toString() }7879/** Shared text, system/assistant search and text selection open global search; everything else is a VIEW link. */80internal fun systemIntentLink(action: String?, dataString: String?, extra: (String) -> String?): String? = when (action) {81 Intent.ACTION_SEND -> extra(Intent.EXTRA_TEXT)82 Intent.ACTION_SEARCH, Intent.ACTION_WEB_SEARCH, GMS_SEARCH_ACTION -> extra(SearchManager.QUERY)83 Intent.ACTION_PROCESS_TEXT -> extra(Intent.EXTRA_PROCESS_TEXT)84 else -> return dataString85}?.trim()?.takeIf(String::isNotEmpty)?.let { searchDeepLink(it) }8687private const val GMS_SEARCH_ACTION = "com.google.android.gms.actions.SEARCH_ACTION"88