diff --git a/app/apk/src/main/AndroidManifest.xml b/app/apk/src/main/AndroidManifest.xml index a24eeb08da824..96db1a1ff974b 100644 --- a/app/apk/src/main/AndroidManifest.xml +++ b/app/apk/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ @@ -13,6 +14,11 @@ + + + + + diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index 9410192f6bbe9..3a826060e2cce 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -15,6 +15,7 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.animation.AnimatedContentTransitionScope.SlideDirection import androidx.compose.animation.togetherWith +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Surface import androidx.compose.runtime.Composable @@ -45,6 +46,7 @@ import com.topjohnwu.magisk.core.base.SplashScreenHost import com.topjohnwu.magisk.core.wrap import com.topjohnwu.magisk.ui.component.ConfirmResult import com.topjohnwu.magisk.ui.component.rememberConfirmDialog +import com.topjohnwu.magisk.ui.component.tvSafeArea import com.topjohnwu.magisk.ui.deny.DenyListScreen import com.topjohnwu.magisk.ui.deny.DenyListViewModel import com.topjohnwu.magisk.ui.flash.FlashScreen @@ -125,6 +127,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost { MagiskTheme { Surface(modifier = Modifier.fillMaxSize()) { val navigator = rememberNavigator(Route.Main) + // Keep content inside the TV title-safe area; no-op on phones and tablets + Box(modifier = Modifier.fillMaxSize().tvSafeArea()) { CompositionLocalProvider(LocalNavigator provides navigator) { HandleFlashIntent(navigator) @@ -222,6 +226,7 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } ) } + } MainActivityDialogs( unsupportedMessages = showUnsupported.collectAsStateWithLifecycle().value, ) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt index 1ea0f0f593e75..dcde9274afeff 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt @@ -36,6 +36,7 @@ import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.withFrameNanos import androidx.navigationevent.NavigationEventInfo import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner import androidx.navigationevent.compose.NavigationBackHandler @@ -125,6 +126,17 @@ fun MainScreen( val superuserContentFocusRequester = remember { FocusRequester() } var moduleFabAction by remember { mutableStateOf<(() -> Unit)?>(null) } var superuserFabAction by remember { mutableStateOf<(() -> Unit)?>(null) } + // One requester per nav item, so focus can be put back on the nav after the page it was + // in goes away (D-pad / TV: otherwise focus is lost and the next key press does nothing) + val tabFocusRequesters = remember(visibleTabs) { + visibleTabs.map { tab -> + when (tab) { + Tab.MODULES -> navModulesFocusRequester + Tab.SUPERUSER -> navSuperuserFocusRequester + else -> FocusRequester() + } + } + } val isModulesTab = visibleTabs.getOrNull(pagerState.currentPage) == Tab.MODULES val isSuperuserTab = visibleTabs.getOrNull(pagerState.currentPage) == Tab.SUPERUSER @@ -137,10 +149,23 @@ fun MainScreen( onBackCompleted = { scope.launch { pagerState.animateScrollToPage(homePageIndex) + if (Info.isTv) { + // The focused content just left composition; don't leave focus nowhere + runCatching { tabFocusRequesters[homePageIndex].requestFocus() } + } } } ) + // On TV nothing is focused at start, and the first D-pad press is easily swallowed. + // Start on the nav item of the current tab so the remote works right away. + if (Info.isTv) { + LaunchedEffect(Unit) { + withFrameNanos { } + runCatching { tabFocusRequesters[pagerState.currentPage].requestFocus() } + } + } + // The collapsed rail needs 520dp (44dp top space, 96dp FAB header, 5 items, 44dp bottom) to // show every item. In shorter windows use the bar, like the library does below 480dp. val systemBars = WindowInsets.systemBars @@ -170,11 +195,7 @@ fun MainScreen( selected = pagerState.currentPage == index, onClick = { scope.launch { pagerState.animateScrollToPage(index) } }, modifier = Modifier - .then( - if (isModulesItem) Modifier.focusRequester(navModulesFocusRequester) - else if (isSuperuserItem) Modifier.focusRequester(navSuperuserFocusRequester) - else Modifier - ) + .focusRequester(tabFocusRequesters[index]) .focusProperties { if (isBar && ((isModulesTab && isModulesItem) || (isSuperuserTab && isSuperuserItem))) { up = fabFocusRequester diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Tv.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Tv.kt new file mode 100644 index 0000000000000..5a48240d90e22 --- /dev/null +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Tv.kt @@ -0,0 +1,33 @@ +package com.topjohnwu.magisk.ui.component + +import android.content.ActivityNotFoundException +import android.content.Context +import android.widget.Toast +import androidx.activity.compose.ManagedActivityResultLauncher +import androidx.compose.foundation.layout.padding +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.topjohnwu.magisk.core.Info +import com.topjohnwu.magisk.core.ktx.toast +import com.topjohnwu.magisk.core.R as CoreR + +/** + * Many Android TV images ship without a document picker, so launching one throws + * [ActivityNotFoundException]. Report it instead of crashing the app. + */ +fun ManagedActivityResultLauncher.launchSafely(context: Context, input: I): Boolean { + return try { + launch(input) + true + } catch (e: ActivityNotFoundException) { + context.toast(CoreR.string.app_not_found, Toast.LENGTH_LONG) + false + } +} + +/** + * TVs may overscan, so keep content inside the title-safe area (about 5% + * of a 960x540dp screen). No-op on phones and tablets. + */ +fun Modifier.tvSafeArea(): Modifier = + if (Info.isTv) padding(horizontal = 48.dp, vertical = 27.dp) else this diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt index dafb09c3ccc82..3edd8610c1656 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/install/InstallDialog.kt @@ -86,6 +86,7 @@ import com.topjohnwu.magisk.ui.component.MagiskDialog import com.topjohnwu.magisk.ui.component.MarkdownText import com.topjohnwu.magisk.ui.component.SettingsArrow import com.topjohnwu.magisk.ui.component.SettingsSwitch +import com.topjohnwu.magisk.ui.component.launchSafely import com.topjohnwu.magisk.ui.component.rememberConfirmDialog import com.topjohnwu.magisk.ui.component.verticalScrollbar import com.topjohnwu.magisk.core.R as CoreR @@ -372,7 +373,8 @@ fun PatchComposableDialog( PatchSource.CANDIDATE -> candidate?.url?.toUri() ?: return@submit PatchSource.FILE -> localFileUri ?: run { pendingSubmitAfterPick = true - localFilePicker.launch("*/*") + // No document picker (common on TVs): don't wait for a result that never comes + if (!localFilePicker.launchSafely(context, "*/*")) pendingSubmitAfterPick = false return@submit } PatchSource.URL -> parseHttpsUrl(url.trim()) ?: run { @@ -499,7 +501,7 @@ fun PatchComposableDialog( onClick = { chosenSource = PatchSource.FILE pendingSubmitAfterPick = false - localFilePicker.launch("*/*") + localFilePicker.launchSafely(context, "*/*") }, role = Role.RadioButton ) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt index 962bc5ece1c34..070805a14e67a 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/module/ModuleScreen.kt @@ -82,6 +82,7 @@ import com.topjohnwu.magisk.core.download.DownloadEngine import com.topjohnwu.magisk.core.model.module.OnlineModule import com.topjohnwu.magisk.ui.MainActivity import com.topjohnwu.magisk.ui.component.ConfirmResult +import com.topjohnwu.magisk.ui.component.launchSafely import com.topjohnwu.magisk.ui.component.MagiskDialog import com.topjohnwu.magisk.ui.component.MarkdownTextAsync import com.topjohnwu.magisk.ui.component.onlyHorizontal @@ -132,7 +133,7 @@ fun ModuleScreen( } DisposableEffect(onRegisterFab) { - onRegisterFab?.invoke { filePicker.launch("application/zip") } + onRegisterFab?.invoke { filePicker.launchSafely(context, "application/zip") } onDispose { onRegisterFab?.invoke(null) } @@ -169,7 +170,7 @@ fun ModuleScreen( floatingActionButton = { if (onRegisterFab == null) { FloatingActionButton( - onClick = { filePicker.launch("application/zip") }, + onClick = { filePicker.launchSafely(context, "application/zip") }, containerColor = MaterialTheme.colorScheme.primaryContainer, contentColor = MaterialTheme.colorScheme.onPrimaryContainer, content = { diff --git a/app/core/src/main/AndroidManifest.xml b/app/core/src/main/AndroidManifest.xml index 600bf98ea4aaf..be673c720747b 100644 --- a/app/core/src/main/AndroidManifest.xml +++ b/app/core/src/main/AndroidManifest.xml @@ -3,6 +3,14 @@ xmlns:tools="http://schemas.android.com/tools" android:installLocation="internalOnly"> + + + + @@ -36,6 +44,7 @@