Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/apk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
<activity
android:name=".ui.MainActivity"
android:exported="true"
android:banner="@drawable/ic_banner"
android:launchMode="singleTask"
android:theme="@style/SplashTheme"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Shows the app (with its banner) on the Android TV home screen -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
5 changes: 5 additions & 0 deletions app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -222,6 +226,7 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
}
)
}
}
MainActivityDialogs(
unsupportedMessages = showUnsupported.collectAsStateWithLifecycle().value,
)
Expand Down
31 changes: 26 additions & 5 deletions app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Tv.kt
Original file line number Diff line number Diff line change
@@ -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 <I> ManagedActivityResultLauncher<I, *>.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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -499,7 +501,7 @@ fun PatchComposableDialog(
onClick = {
chosenSource = PatchSource.FILE
pendingSubmitAfterPick = false
localFilePicker.launch("*/*")
localFilePicker.launchSafely(context, "*/*")
},
role = Role.RadioButton
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 = {
Expand Down
9 changes: 9 additions & 0 deletions app/core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="internalOnly">

<!-- Android TV / leanback: no touchscreen needed, and the app also stays installable on phones -->
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.software.leanback"
android:required="false" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Expand Down Expand Up @@ -36,6 +44,7 @@
<application
android:name=".App"
android:allowBackup="false"
android:banner="@drawable/ic_banner"
android:enableOnBackInvokedCallback="true"
android:icon="@drawable/ic_launcher"
android:label="Magisk"
Expand Down
11 changes: 11 additions & 0 deletions app/core/src/main/java/com/topjohnwu/magisk/core/Info.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.topjohnwu.magisk.core

import android.app.KeyguardManager
import android.app.UiModeManager
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Build
import androidx.lifecycle.MutableLiveData
import com.topjohnwu.magisk.core.ktx.getProperty
Expand Down Expand Up @@ -55,6 +58,14 @@ object Info {
|| getProperty("ro.kernel.qemu", "0") == "1"
|| getProperty("ro.boot.qemu", "0") == "1"

/** Android TV / leanback device, driven by a remote (D-pad) rather than a touchscreen */
val isTv: Boolean by lazy {
val uiMode = AppContext.getSystemService(UiModeManager::class.java)
uiMode?.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION ||
AppContext.packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK) ||
!AppContext.packageManager.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
}

val isGooglebook get() =
AppContext.packageManager.hasSystemFeature("com.google.desktop.gms")

Expand Down
Binary file added app/core/src/main/res/drawable-hdpi/ic_banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/core/src/main/res/drawable-mdpi/ic_banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.