Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

Scan: a Connect to Wi-Fi screen when the phone isn't on Wi-Fi

jesse-agent-3621863996adb7a
diff --git a/NOTICE b/NOTICE
index 21b90d6..dd3a6b6 100644
--- a/NOTICE
+++ b/NOTICE
@@ -22,8 +22,8 @@ debugging pairing.
Material Icons — https://github.com/google/material-design-icons
License: Apache License 2.0
The TV and HDMI (streaming stick) device icons use the path data of the
-"tv" and "settings_input_hdmi" icons, and the Debloat button that of
-"cleaning_services".
+"tv" and "settings_input_hdmi" icons, the Debloat button that of
+"cleaning_services", and the off-Wi-Fi screen that of "wifi_off".
--------------------------------------------------------------------------------
cleanyourtv — https://github.com/Gathin23/cleanyourtv
diff --git a/app/src/main/java/app/tellyscope/MainActivity.kt b/app/src/main/java/app/tellyscope/MainActivity.kt
index 5767177..1261ab6 100644
--- a/app/src/main/java/app/tellyscope/MainActivity.kt
+++ b/app/src/main/java/app/tellyscope/MainActivity.kt
@@ -2,6 +2,7 @@
package app.tellyscope
+import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
@@ -24,6 +25,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
+import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
@@ -136,6 +138,7 @@ class MainActivity : ComponentActivity() {
fun TellyscopeApp(vm: TellyscopeViewModel) {
val state by vm.state.collectAsStateWithLifecycle()
val browser = LocalUriHandler.current
+ val context = LocalContext.current
val snackbar = remember { SnackbarHostState() }
var adding by rememberSaveable { mutableStateOf(false) }
var remote by rememberSaveable { mutableStateOf(false) }
@@ -172,6 +175,7 @@ fun TellyscopeApp(vm: TellyscopeViewModel) {
onPair = vm::pair,
onAddByIp = vm::addByIp,
onBack = if (adding) ({ adding = false }) else null,
+ onOpenWifi = { context.openWifiSettings() },
)
} else if (remote) {
BackHandler { remote = false }
@@ -282,3 +286,10 @@ internal fun Snackbars(
}
}
}
+
+/** The Wi-Fi panel over the app on Android 10+, else the full Wi-Fi settings. */
+private fun Context.openWifiSettings() {
+ val panel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Settings.Panel.ACTION_WIFI else Settings.ACTION_WIFI_SETTINGS
+ runCatching { startActivity(Intent(panel)) }
+ .recoverCatching { startActivity(Intent(Settings.ACTION_WIFI_SETTINGS)) }
+}
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index d54c3c0..8578e27 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -4,6 +4,8 @@ package app.tellyscope.data
import android.content.Context
import android.net.ConnectivityManager
+import android.net.LinkProperties
+import android.net.Network
import android.net.NetworkCapabilities
import app.tellyscope.adb.ActionResult
import app.tellyscope.adb.ApkInfo
@@ -38,7 +40,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
+import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
@@ -174,6 +178,18 @@ class AdbDeviceSource(context: Context) : DeviceSource {
.distinct()
}
+ override fun lanUp(): Flow<Unit> = callbackFlow {
+ val cm = appContext.getSystemService(ConnectivityManager::class.java) ?: run { close(); return@callbackFlow }
+ val callback = object : ConnectivityManager.NetworkCallback() {
+ // Capabilities can arrive before the address, so check again on each.
+ override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) = check()
+ override fun onLinkPropertiesChanged(network: Network, link: LinkProperties) = check()
+ private fun check() { if (lanHosts().isNotEmpty()) trySend(Unit) }
+ }
+ cm.registerDefaultNetworkCallback(callback)
+ awaitClose { cm.unregisterNetworkCallback(callback) }
+ }
+
override fun scan(): Flow<ScanEvent> = flow {
val hosts = lanHosts()
if (hosts.isEmpty()) emit(ScanEvent.NoLan)
diff --git a/app/src/main/java/app/tellyscope/data/DeviceSource.kt b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
index 88b08c3..edd1e17 100644
--- a/app/src/main/java/app/tellyscope/data/DeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
@@ -12,6 +12,7 @@ import app.tellyscope.adb.TvFile
import app.tellyscope.adb.TvFolder
import app.tellyscope.adb.Tweak
import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.emptyFlow
import okio.Sink
import okio.Source
import java.io.File
@@ -26,6 +27,9 @@ interface DeviceSource {
/** LAN sweep for 5555 plus mDNS for wireless debugging. Emits hits as they answer, then [ScanEvent.Done]. */
fun scan(): Flow<ScanEvent>
+ /** Emits whenever the phone is on a Wi-Fi or Ethernet network a [scan] can sweep. */
+ fun lanUp(): Flow<Unit> = emptyFlow()
+
/** Classic connect. Suspends while the TV shows the Allow prompt; the caller owns the timeout. */
suspend fun connect(hit: ScanHit): ConnectResult
diff --git a/app/src/main/java/app/tellyscope/ui/DeviceIcons.kt b/app/src/main/java/app/tellyscope/ui/DeviceIcons.kt
index ad81ef7..0149efa 100644
--- a/app/src/main/java/app/tellyscope/ui/DeviceIcons.kt
+++ b/app/src/main/java/app/tellyscope/ui/DeviceIcons.kt
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-only
-// Tv and Stick are the Material Icons "tv" and "settings_input_hdmi" (Apache-2.0), see NOTICE.
+// Tv, Stick and WifiOff are the Material Icons "tv", "settings_input_hdmi" and "wifi_off" (Apache-2.0), see NOTICE.
package app.tellyscope.ui
@@ -33,10 +33,16 @@ object DeviceIcons {
"M17 8.25a1.75 1.75 0 1 0 0 3.5a1.75 1.75 0 1 0 0-3.5z" +
"M15.5 14h3v1.5h-3z M15.5 17h3v1.5h-3z"
+ private const val WIFI_OFF =
+ "M22.99 9C19.15 5.16 13.8 3.76 8.84 4.78l2.52 2.52c3.47-.17 6.99 1.05 9.63 3.7l2-2zm-4 4c-1.29-1.29-2.84-2.2-4.49-2.73l3.52 3.52.97-.79z" +
+ "M2 3.05L5.07 6.1C3.6 6.82 2.22 7.78 1 9l1.99 2c1.24-1.24 2.67-2.16 4.2-2.77l2.24 2.24C7.81 10.89 6.27 11.73 5 13v.01L6.99 15" +
+ "c1.36-1.36 3.14-2.04 4.92-2.06L18.98 20l1.27-1.26L3.29 1.79 2 3.05zM9 17l3 3 3-3c-1.65-1.66-4.34-1.66-6 0z"
+
val Tv = icon("Tv", TV)
val FireTv = icon("FireTv", FIRE_TV)
val Stick = icon("Stick", STICK)
val Projector = icon("Projector", PROJECTOR)
+ val WifiOff = icon("WifiOff", WIFI_OFF)
fun of(kind: DeviceKind) = when (kind) {
DeviceKind.Tv -> Tv
diff --git a/app/src/main/java/app/tellyscope/ui/ScanScreen.kt b/app/src/main/java/app/tellyscope/ui/ScanScreen.kt
index c00c0a5..60102ed 100644
--- a/app/src/main/java/app/tellyscope/ui/ScanScreen.kt
+++ b/app/src/main/java/app/tellyscope/ui/ScanScreen.kt
@@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
@@ -50,6 +51,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.KeyboardType
+import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import app.tellyscope.data.ScanHit
import app.tellyscope.data.ScanKind
@@ -67,6 +69,7 @@ fun ScanScreen(
onPair: (ScanHit) -> Unit,
onAddByIp: (String) -> String?,
onBack: (() -> Unit)? = null,
+ onOpenWifi: () -> Unit = {},
) {
var adding by remember { mutableStateOf(false) }
Scaffold(
@@ -82,7 +85,9 @@ fun ScanScreen(
)
},
) { pad ->
- LazyColumn(
+ if (scan.offWifi) {
+ OffWifi(Modifier.padding(pad), onOpenWifi, onAddByIp = { adding = true })
+ } else LazyColumn(
Modifier.fillMaxSize().padding(pad),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
@@ -114,6 +119,29 @@ fun ScanScreen(
if (adding) AddByIpDialog(subnet = scan.progress?.subnet, onDismiss = { adding = false }, onSubmit = { onAddByIp(it).also { e -> if (e == null) adding = false } })
}
+/** Off Wi-Fi with no TV announcing itself: one way forward instead of an empty list. */
+@Composable
+private fun OffWifi(modifier: Modifier, onOpenWifi: () -> Unit, onAddByIp: () -> Unit) {
+ Column(
+ modifier.fillMaxSize().padding(32.dp),
+ verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterVertically),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ ) {
+ Icon(DeviceIcons.WifiOff, null, Modifier.size(64.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant)
+ Text("Connect to Wi-Fi", style = MaterialTheme.typography.headlineSmall, textAlign = TextAlign.Center)
+ Text(
+ "Connect this phone to the same Wi-Fi as your TV.",
+ style = MaterialTheme.typography.bodyMedium,
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ textAlign = TextAlign.Center,
+ )
+ Spacer(Modifier.height(4.dp))
+ Button(onClick = onOpenWifi) { Text("Open Wi-Fi settings") }
+ // VPNs and odd setups can still reach a TV by its address.
+ TextButton(onClick = onAddByIp) { Text("Add by IP address") }
+ }
+}
+
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
private fun ScanStatus(scan: ScanUi) {
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index a4593cd..a52c1c6 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -66,7 +66,10 @@ data class ScanUi(
val noLan: Boolean = false,
val hits: List<ScanHit> = emptyList(),
val rows: Map<String, RowState> = emptyMap(),
-)
+) {
+ /** Off Wi-Fi with nothing found: the scan screen asks the user to join one instead of listing. */
+ val offWifi get() = noLan && hits.isEmpty()
+}
data class DeviceUi(
val snapshot: Snapshot? = null,
@@ -212,6 +215,15 @@ class TellyscopeViewModel(
viewModelScope.launch(Dispatchers.IO) { _state.update { it.copy(checkUpdates = u.enabled, alphaBuild = u.alpha) } }
}
rescan()
+ viewModelScope.launch {
+ // Off Wi-Fi a scan has nothing to sweep; start over the moment the phone joins one.
+ source.lanUp().collect {
+ if (_state.value.scan.noLan) {
+ scanJob?.cancel()
+ rescan()
+ }
+ }
+ }
}
fun rescan() {
diff --git a/app/src/test/java/app/tellyscope/OffWifiTest.kt b/app/src/test/java/app/tellyscope/OffWifiTest.kt
new file mode 100644
index 0000000..afa3be4
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/OffWifiTest.kt
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope
+
+import androidx.compose.ui.test.assertIsDisplayed
+import androidx.compose.ui.test.junit4.createComposeRule
+import androidx.compose.ui.test.onNodeWithText
+import androidx.compose.ui.test.performClick
+import androidx.lifecycle.viewModelScope
+import app.tellyscope.data.Device
+import app.tellyscope.data.DeviceSource
+import app.tellyscope.data.FakeDeviceSource
+import app.tellyscope.data.ScanEvent
+import app.tellyscope.data.ScanHit
+import app.tellyscope.data.ScanKind
+import app.tellyscope.ui.RowState
+import app.tellyscope.ui.ScanScreen
+import app.tellyscope.ui.ScanUi
+import app.tellyscope.ui.TellyscopeViewModel
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runCurrent
+import kotlinx.coroutines.test.setMain
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.annotation.Config
+
+/** Off Wi-Fi on the first scan, on it from the second; [lan] stands in for the network callback. */
+private class JoinsWifi : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {
+ var scans = 0
+ val lan = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
+
+ override suspend fun known() = emptyList<Device>()
+
+ override fun scan() = flow {
+ if (++scans == 1) emit(ScanEvent.NoLan)
+ emit(ScanEvent.Done)
+ }
+
+ override fun lanUp() = lan
+}
+
+@OptIn(ExperimentalCoroutinesApi::class)
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [35])
+class OffWifiTest {
+ @get:Rule val compose = createComposeRule()
+
+ private val tv = ScanHit("192.0.2.7", 37211, ScanKind.Wireless, "chromecast", "Google")
+
+ private fun show(scan: ScanUi, onOpenWifi: () -> Unit = {}) = compose.setContent {
+ ScanScreen(scan, 0L, {}, {}, { _, _ -> }, {}, { null }, onOpenWifi = onOpenWifi)
+ }
+
+ @Test fun noLanShowsTheWifiPrompt() {
+ var opened = false
+ show(ScanUi(noLan = true)) { opened = true }
+ compose.onNodeWithText("Connect to Wi-Fi").assertIsDisplayed()
+ compose.onNodeWithText("Add by IP address").assertIsDisplayed()
+ compose.onNodeWithText("Open Wi-Fi settings").performClick()
+ assertTrue(opened)
+ }
+
+ @Test fun aTvThatAnnouncedItselfStillShows() {
+ show(ScanUi(noLan = true, hits = listOf(tv), rows = mapOf(tv.ip to RowState.Found)))
+ compose.onNodeWithText("chromecast").assertIsDisplayed()
+ compose.onNodeWithText("Connect to Wi-Fi").assertDoesNotExist()
+ }
+
+ @Test fun onWifiShowsTheScan() {
+ show(ScanUi())
+ compose.onNodeWithText("What's running on your TV?").assertIsDisplayed()
+ compose.onNodeWithText("Connect to Wi-Fi").assertDoesNotExist()
+ }
+
+ @Test fun joiningWifiScansAgain() {
+ val dispatcher = StandardTestDispatcher()
+ Dispatchers.setMain(dispatcher)
+ val source = JoinsWifi()
+ val vm = TellyscopeViewModel(source, clock = { 0L })
+ try {
+ with(TestScope(dispatcher)) {
+ runCurrent()
+ assertTrue(vm.state.value.scan.noLan)
+
+ source.lan.tryEmit(Unit)
+ runCurrent()
+ assertEquals(2, source.scans)
+ assertFalse(vm.state.value.scan.noLan)
+
+ // Already on the LAN: a network change doesn't restart the scan.
+ source.lan.tryEmit(Unit)
+ runCurrent()
+ assertEquals(2, source.scans)
+ }
+ } finally {
+ vm.viewModelScope.cancel()
+ Dispatchers.resetMain()
+ }
+ }
+}
diff --git a/app/src/test/java/app/tellyscope/ScreenshotTest.kt b/app/src/test/java/app/tellyscope/ScreenshotTest.kt
index 6a50caa..c8e332d 100644
--- a/app/src/test/java/app/tellyscope/ScreenshotTest.kt
+++ b/app/src/test/java/app/tellyscope/ScreenshotTest.kt
@@ -486,6 +486,10 @@ class ScreenshotTest {
name = "10_scan_off_wifi",
)
+ @Test fun scan_offWifiEmpty() = scan(ScanUi(noLan = true), name = "10_scan_off_wifi_empty")
+
+ @Test fun scan_offWifiEmpty_dark() = scan(ScanUi(noLan = true), dark = true, name = "10_scan_off_wifi_empty")
+
@Test fun device_notAllowed() {
val base = deviceState()
val off = base.devices.last().copy(state = DeviceState.NotAllowed)