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.
Fix rescan items 7-10: transfers per TV, Undo, protected list, scan DNS
jesse-agent-3626dbcec19dbcb
diff --git a/README.md b/README.mdindex 790bb46..d2aadcb 100644--- a/README.md+++ b/README.md@@ -68,7 +68,7 @@ Tellyscope needs a phone on Android 8.0 or newer, on the same Wi-Fi as the TV, a## Is it safe?-- **Small and private.** Under 3 MB. No ads, no tracking, no accounts. It makes two internet requests: one fetches the debloat lists from this project's GitHub, or from your own fork if you point it there; the other asks GitHub every 30 minutes whether a newer version is out, and you can turn it off in Settings. Everything else stays on your Wi-Fi. Turning on Private DNS changes where the *TV* looks up addresses, not what the app sends.+- **Small and private.** Under 3 MB. No ads, no tracking, no accounts. It makes two internet requests: one fetches the debloat lists from this project's GitHub, or from your own fork if you point it there; the other asks GitHub every 30 minutes whether a newer version is out, and you can turn it off in Settings. Everything else stays on your Wi-Fi, except one lookup: the scan asks your phone's DNS server for the name of each TV it finds, which leaves your Wi-Fi only if that server is outside it, such as Private DNS on the phone. Turning on Private DNS changes where the *TV* looks up addresses, not what the app sends.- **Reversible.** Apps that came with the TV are only ever disabled, never uninstalled, so nothing is deleted. Apps you installed yourself can also be uninstalled, after a confirm that says it can't be undone. Every change goes into History, all but Uninstall with an Undo, and **Restore everything** turns back on all it turned off.- **Protected list.** Apps the TV needs to start, update, stay on Wi-Fi, show its home screen or take input can't be stopped or disabled. They sit in their own **System** section under Apps. There is no override. Apps a TV's list only says to keep can still be changed, after a warning in the confirm.- **You see it first.** Debloat shows every app it will turn off, and the confirm names each one before anything runs.diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt b/adb/src/main/kotlin/app/tellyscope/adb/Protected.ktindex 570285b..4dcd6be 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt@@ -24,7 +24,7 @@ object Protected {private val CORE = Regex("android|com\\.android\\.systemui|com\\.android\\.shell|com\\.android\\.phone|" +"com\\.(google\\.)?android\\.(permissioncontroller|packageinstaller|ext\\.services|ext\\.shared|modulemetadata)|" +- "com\\.google\\.android\\.overlay\\.modules.*|com\\.android\\.providers\\..*",+ "com\\.google\\.android\\.overlay\\.modules.*|com\\.(google\\.)?android\\.providers\\..*",)/** Providers no TV feature uses; tested lists turn them off. */private val CORE_OK = Regex("com\\.android\\.providers\\.(calendar|contacts|userdictionary|blockednumber)")@@ -32,6 +32,8 @@ object Protected {private val EXACT: Map<String, ProtectedReason> = mapOf("com.android.settings" to ProtectedReason.SETTINGS,"com.android.tv.settings" to ProtectedReason.SETTINGS,+ "com.amazon.tv.settings.v2" to ProtectedReason.SETTINGS, // Fire TV+ "org.droidtv.settings" to ProtectedReason.SETTINGS, // Philips"com.android.vending" to ProtectedReason.PLAY_SERVICES,"com.google.android.gms" to ProtectedReason.PLAY_SERVICES,"com.google.android.gsf" to ProtectedReason.PLAY_SERVICES,diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.ktindex 05bf4fe..36395c4 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt@@ -13,6 +13,12 @@ class ProtectedTest {assertNull(Protected.reason("com.android.providers.calendar"))}+ @Test fun vendorSettingsAndTheMediaModule() {+ assertEquals(ProtectedReason.SETTINGS, Protected.reason("com.amazon.tv.settings.v2"))+ assertEquals(ProtectedReason.SETTINGS, Protected.reason("org.droidtv.settings"))+ assertEquals(ProtectedReason.SYSTEM, Protected.reason("com.google.android.providers.media.module"))+ }+/** Package names from an Android TV 11 XGIMI projector. */@Test fun xgimiPackages() {assertEquals(ProtectedReason.TV_INPUTS_REMOTE, Protected.reason("com.xgimi.tvinput"))diff --git a/app/src/main/java/app/tellyscope/ui/Copy.kt b/app/src/main/java/app/tellyscope/ui/Copy.ktindex 8b4a325..74b09dd 100644--- a/app/src/main/java/app/tellyscope/ui/Copy.kt+++ b/app/src/main/java/app/tellyscope/ui/Copy.kt@@ -299,6 +299,7 @@ object Copy {const val CANCELLED_SEND = "Cancelled. Nothing was left on the TV."const val CANCELLED_SAVE = "Cancelled. Nothing was saved."const val CANT_READ = "Couldn't read that file on your phone."+ const val CANT_UNDO = "This version of Tellyscope can't undo that setting."const val FILES_LOST = "Couldn't read this folder. Check the TV is still connected."// Remotediff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.ktindex 8165d49..5999010 100644--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt@@ -50,6 +50,7 @@ import kotlinx.coroutines.launchimport kotlinx.coroutines.sync.Muteximport kotlinx.coroutines.sync.withLockimport kotlinx.coroutines.withTimeoutOrNull+import java.util.concurrent.ConcurrentHashMap/** Per-row connect state on the scan screen. */sealed interface RowState {@@ -189,7 +190,11 @@ class TellyscopeViewModel(private var scanJob: Job? = nullprivate var sideloadJob: Job? = null- private var transferJob: Job? = null+ /** File copies by TV: each dialog shows only on its own TV's page, and its Cancel stops only that copy. */+ private val transferJobs = mutableMapOf<String, Job>()+ private val transfers = ConcurrentHashMap<String, TransferUi>() // progress arrives off the main thread+ /** History entries being undone, so a second tap can't run the same Undo again. */+ private val undoInFlight = mutableSetOf<Long>()private var liveJob: Job? = nullprivate var mediumJob: Job? = nullprivate var listsJob: Job? = null@@ -324,7 +329,8 @@ class TellyscopeViewModel(scan = s.scan.copy(hits = s.scan.hits.filterNot { it.ip == device.hit.ip }),)}- select(device.id)+ // A TV that comes online mid-copy joins the list; switching pages would hide the copy's dialog and its Cancel.+ if (transferJobs[_state.value.selectedId]?.isActive != true) select(device.id)}fun select(id: String?) {@@ -332,7 +338,9 @@ class TellyscopeViewModel(mediumJob?.cancel()listsJob?.cancel()requestJob?.cancel()- _state.update { it.copy(selectedId = id, device = DeviceUi(snapshot = id?.let(lastSnapshot::get)), query = "") }+ // A copy still running on this TV keeps its dialog, after a reconnect or a look at another TV.+ val transfer = id?.let(transfers::get)+ _state.update { it.copy(selectedId = id, device = DeviceUi(snapshot = id?.let(lastSnapshot::get), transfer = transfer), query = "") }val device = _state.value.selected ?: return// History lives on the phone, so it shows (and undo is offered) even while the TV is off.viewModelScope.launch {@@ -425,11 +433,22 @@ class TellyscopeViewModel(* have switched TVs since; History only lists the selected TV's entries.*/fun undo(entry: HistoryEntry, deviceId: String? = _state.value.selectedId) {- val undo = entry.undo ?: return- if (entry.action == Action.Change) {- change(Tweak.valueOf(entry.packageName), entry.previous.orEmpty(), undoing = entry.id, id = deviceId)- } else {- run(entry.packageName, undo, undoing = entry.id, id = deviceId)+ if (deviceId == null || entry.undo == null || !undoInFlight.add(entry.id)) return+ viewModelScope.launch {+ // Both the snackbar and History offer Undo: an entry already undone from one is done.+ val now = source.history(deviceId).firstOrNull { it.id == entry.id } ?: entry+ val undo = now.undo+ val tweak = Tweak.entries.firstOrNull { it.name == now.packageName }+ when {+ now.undoneAt != null || undo == null -> undoInFlight -= entry.id+ now.action != Action.Change -> run(now.packageName, undo, undoing = now.id, id = deviceId)+ tweak != null -> change(tweak, now.previous.orEmpty(), undoing = now.id, id = deviceId)+ else -> {+ // A setting this version doesn't know, from a newer one's History.+ undoInFlight -= entry.id+ _notice.value = Copy.CANT_UNDO+ }+ }}}@@ -450,6 +469,7 @@ class TellyscopeViewModel(// A change can fail after it was recorded (a rollback that didn't take), so History is reread.val history = source.history(id)updateDevice(id) { it.copy(busy = null, history = history) }+ undoing?.let(undoInFlight::remove) // a failed Undo can be tried again_notice.value = e.message?.replaceFirstChar(Char::uppercase) ?: "That didn't work."return@launch}@@ -513,6 +533,7 @@ class TellyscopeViewModel(} catch (e: Exception) {// Anything else (a dropped socket) still must not crash the app.updateDevice(id) { it.copy(busy = null) }+ undoing?.let(undoInFlight::remove) // a failed Undo can be tried again_notice.value = e.message?.replaceFirstChar(Char::uppercase) ?: "That didn't work."return@launch}@@ -609,20 +630,20 @@ class TellyscopeViewModel(val id = _state.value.selectedId ?: returnval storage = storage ?: returnval folder = _state.value.device.files.folder- transferJob = viewModelScope.launch {+ transferJobs[id] = viewModelScope.launch {val file = try {storage.open(uri)} catch (_: Exception) {_notice.value = Copy.CANT_READreturn@launch}- setTransfer(TransferUi(file.name, toTv = true, folder = folder))+ setTransfer(id, TransferUi(file.name, toTv = true, folder = folder))try {val saved = file.source.use { src ->- source.push(id, folder, file.name, file.size, src) { p -> updateTransfer { it.copy(progress = p) } }+ source.push(id, folder, file.name, file.size, src) { p -> updateTransfer(id) { it.copy(progress = p) } }}// Close the dialog before the reload: a Cancel tapped now would report a sent file as cancelled.- setTransfer(null)+ setTransfer(id, null)_notice.value = Copy.sent(file.name, saved, folder)loadFiles()} catch (e: CancellationException) {@@ -631,7 +652,7 @@ class TellyscopeViewModel(} catch (_: Exception) {_notice.value = Copy.sendFailed(file.name)} finally {- setTransfer(null)+ setTransfer(id, null)}}}@@ -655,11 +676,11 @@ class TellyscopeViewModel(val id = _state.value.selectedId ?: returnval storage = storage ?: returnval folder = _state.value.device.files.folder- setTransfer(TransferUi(file.name, toTv = false, folder = folder, progress = Progress(0, file.size)))- transferJob = viewModelScope.launch {+ setTransfer(id, TransferUi(file.name, toTv = false, folder = folder, progress = Progress(0, file.size)))+ transferJobs[id] = viewModelScope.launch {try {storage.create(uri).use { sink ->- source.pull(id, folder, file, sink) { p -> updateTransfer { it.copy(progress = p) } }+ source.pull(id, folder, file, sink) { p -> updateTransfer(id) { it.copy(progress = p) } }}_notice.value = Copy.saved(file.name)} catch (e: CancellationException) {@@ -670,14 +691,14 @@ class TellyscopeViewModel(storage.delete(uri)_notice.value = Copy.saveFailed(file.name)} finally {- setTransfer(null)+ setTransfer(id, null)}}}/** Only the dialog's Cancel button calls this. */fun cancelTransfer() {- transferJob?.cancel()+ transferJobs[_state.value.selectedId]?.cancel()}private fun setSideload(ui: SideloadUi?) = _state.update { it.copy(device = it.device.copy(sideload = ui)) }@@ -685,10 +706,15 @@ class TellyscopeViewModel(private fun updateSideload(f: (SideloadUi) -> SideloadUi) =_state.update { s -> s.device.sideload?.let { s.copy(device = s.device.copy(sideload = f(it))) } ?: s }- private fun setTransfer(ui: TransferUi?) = _state.update { it.copy(device = it.device.copy(transfer = ui)) }+ private fun setTransfer(id: String, ui: TransferUi?) {+ if (ui == null) transfers -= id else transfers[id] = ui+ updateDevice(id) { it.copy(transfer = ui) }+ }- private fun updateTransfer(f: (TransferUi) -> TransferUi) =- _state.update { s -> s.device.transfer?.let { s.copy(device = s.device.copy(transfer = f(it))) } ?: s }+ private fun updateTransfer(id: String, f: (TransferUi) -> TransferUi) {+ val ui = transfers[id]?.let(f) ?: return+ setTransfer(id, ui)+ }fun doneShown() {_done.value = nulldiff --git a/app/src/test/java/app/tellyscope/TransfersAndUndoTest.kt b/app/src/test/java/app/tellyscope/TransfersAndUndoTest.ktnew file mode 100644index 0000000..78295b1--- /dev/null+++ b/app/src/test/java/app/tellyscope/TransfersAndUndoTest.kt@@ -0,0 +1,169 @@+// SPDX-License-Identifier: GPL-3.0-only++package app.tellyscope++import android.net.Uri+import androidx.lifecycle.viewModelScope+import app.tellyscope.adb.Progress+import app.tellyscope.adb.TvFolder+import app.tellyscope.data.Action+import app.tellyscope.data.ConnectResult+import app.tellyscope.data.Device+import app.tellyscope.data.DeviceSource+import app.tellyscope.data.DeviceState+import app.tellyscope.data.FakeDeviceSource+import app.tellyscope.data.HistoryEntry+import app.tellyscope.data.PhoneFile+import app.tellyscope.data.PhoneStorage+import app.tellyscope.data.PickedApp+import app.tellyscope.data.ScanHit+import app.tellyscope.data.ScanKind+import app.tellyscope.data.Snapshot+import app.tellyscope.ui.Copy+import app.tellyscope.ui.TellyscopeViewModel+import kotlinx.coroutines.CompletableDeferred+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.cancel+import kotlinx.coroutines.flow.emptyFlow+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 okio.Buffer+import okio.Sink+import okio.Source+import org.junit.After+import org.junit.Assert.assertEquals+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Before+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++private fun tv(id: String) = Device(id, ScanHit(id, 5555, ScanKind.Classic, id, null), DeviceState.Online, null, null, null, 0L)++/** TV A online, TV B online once connected; sends wait on [pushGate], disables on [actGate]. */+private class Src : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {+ val known = mutableListOf(tv("tv-a"))+ var pushGate = CompletableDeferred<Unit>()+ var actGate: CompletableDeferred<Unit>? = null+ val acted = mutableListOf<String>()+ var history = emptyList<HistoryEntry>()++ override suspend fun known() = known.toList()++ override fun live(deviceId: String) = emptyFlow<Snapshot>()++ override suspend fun connect(hit: ScanHit): ConnectResult {+ known += tv(hit.ip)+ return ConnectResult.Online(tv(hit.ip))+ }++ override suspend fun push(deviceId: String, folder: TvFolder, name: String, size: Long?, source: Source, onProgress: (Progress) -> Unit): String {+ pushGate.await()+ return name+ }++ override suspend fun act(deviceId: String, action: Action, packageName: String, undoing: Long?): HistoryEntry {+ acted += packageName+ actGate?.await()+ return HistoryEntry(99, deviceId, 0, action, packageName, "x", null, null)+ }++ override suspend fun history(deviceId: String) = history+}++private object Phone : PhoneStorage {+ override suspend fun readApp(uris: List<Uri>): PickedApp = error("unused")+ override fun open(uri: Uri) = PhoneFile("clip.mp4", 3, Buffer().writeUtf8("abc"))+ override fun create(uri: Uri): Sink = Buffer()+ override fun delete(uri: Uri) = Unit+ override fun clearApp() = Unit+}++@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [35])+class TransfersAndUndoTest {+ private val dispatcher = StandardTestDispatcher()+ private val test = TestScope(dispatcher)+ private val source = Src()+ private lateinit var vm: TellyscopeViewModel++ @Before fun setUp() = Dispatchers.setMain(dispatcher)++ @After fun tearDown() {+ vm.viewModelScope.cancel()+ Dispatchers.resetMain()+ }++ @Test fun aTvConnectingMidSendLeavesThePageAndItsDialog() = with(test) {+ vm = TellyscopeViewModel(source, Phone, clock = { 0L })+ runCurrent()+ assertEquals("tv-a", vm.state.value.selectedId)+ vm.push(Uri.parse("content://x/clip.mp4"))+ runCurrent()+ assertNotNull(vm.state.value.device.transfer)+ vm.connect(ScanHit("tv-b", 5555, ScanKind.Classic, "tv-b", null))+ runCurrent()+ assertEquals("tv-a", vm.state.value.selectedId)+ assertNotNull(vm.state.value.device.transfer)+ }++ @Test fun aSendEndingNeverClosesAnotherTvsDialog() = with(test) {+ vm = TellyscopeViewModel(source, Phone, clock = { 0L })+ runCurrent()+ vm.push(Uri.parse("content://x/clip.mp4"))+ runCurrent()+ val gateA = source.pushGate+ vm.connect(ScanHit("tv-b", 5555, ScanKind.Classic, "tv-b", null))+ runCurrent()+ source.pushGate = CompletableDeferred()+ vm.select("tv-b")+ runCurrent()+ assertNull(vm.state.value.device.transfer)+ vm.push(Uri.parse("content://x/clip.mp4"))+ runCurrent()+ assertNotNull(vm.state.value.device.transfer)+ gateA.complete(Unit)+ runCurrent()+ assertEquals("tv-b", vm.state.value.selectedId)+ assertNotNull(vm.state.value.device.transfer)+ }++ @Test fun anEntryAlreadyUndoneInHistoryIsNotUndoneAgain() = with(test) {+ vm = TellyscopeViewModel(source, clock = { 0L })+ runCurrent()+ val e = HistoryEntry(5, "tv-a", 0, Action.Disable, "com.x", "X", Action.Enable, null)+ source.history = listOf(e.copy(undoneAt = 1))+ vm.undo(e, "tv-a") // the snackbar's stale copy+ runCurrent()+ assertEquals(emptyList<String>(), source.acted)+ }++ @Test fun aSecondTapWhileUndoingDoesNothing() = with(test) {+ vm = TellyscopeViewModel(source, clock = { 0L })+ runCurrent()+ val e = HistoryEntry(5, "tv-a", 0, Action.Disable, "com.x", "X", Action.Enable, null)+ source.actGate = CompletableDeferred()+ vm.undo(e, "tv-a") // the snackbar+ runCurrent()+ vm.undo(e, "tv-a") // History, before the first one is back+ runCurrent()+ source.actGate!!.complete(Unit)+ runCurrent()+ assertEquals(listOf("com.x"), source.acted)+ }++ @Test fun aSettingThisVersionDoesntKnowSaysSoInsteadOfCrashing() = with(test) {+ vm = TellyscopeViewModel(source, clock = { 0L })+ runCurrent()+ vm.undo(HistoryEntry(6, "tv-a", 0, Action.Change, "NO_SUCH_TWEAK", "X", Action.Change, null, mapOf("k" to "v")), "tv-a")+ runCurrent()+ assertEquals(Copy.CANT_UNDO, vm.notice.value)+ }+}