mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install (#2654)
* fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install * fix(shortcuts): bind only same-signer relays, recheck enablement per call, drop timed-out keys
This commit is contained in:
1 parent
5dd973587f
commit
de5f0fe6df
52 files changed
+750
-296
No files matched your search
@@ -1,97 +0,0 @@
|
||||
package com.droiddeck.launcher.core
|
||||
|
||||
import android.app.Application
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.provider.Settings
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.droiddeck.launcher.input.HomeButtonService
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], application = Application::class)
|
||||
class SteamIntegrationShortcutsTest {
|
||||
private val context: Application = ApplicationProvider.getApplicationContext()
|
||||
private val service = ComponentName(context, HomeButtonService::class.java)
|
||||
|
||||
private fun listed() = context.packageManager.queryIntentServices(
|
||||
Intent("android.accessibilityservice.AccessibilityService").setPackage(context.packageName), 0,
|
||||
).any { ComponentName(it.serviceInfo.packageName, it.serviceInfo.name) == service }
|
||||
|
||||
private fun choose(value: String?) =
|
||||
Settings.Secure.putString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, value)
|
||||
|
||||
@Test fun theShortcutsServiceIsHiddenUntilChosen() {
|
||||
assertTrue(listed())
|
||||
assertFalse(SteamIntegration.shortcutsChosen(context))
|
||||
SteamIntegration.syncShortcutsListing(context)
|
||||
assertFalse(listed())
|
||||
assertEquals(PackageManager.COMPONENT_ENABLED_STATE_DISABLED, context.packageManager.getComponentEnabledSetting(service))
|
||||
}
|
||||
|
||||
@Test fun anUpdateKeepsAServiceTheUserAlreadyTurnedOn() {
|
||||
choose(service.flattenToString())
|
||||
SteamIntegration.syncShortcutsListing(context)
|
||||
assertTrue(listed())
|
||||
assertTrue(SteamIntegration.shortcutsChosen(context))
|
||||
}
|
||||
|
||||
@Test fun openingShortcutSetupListsTheServiceAndBackingOutHidesItAgain() {
|
||||
SteamIntegration.setShortcutsListed(context, true)
|
||||
assertTrue(listed())
|
||||
SteamIntegration.syncShortcutsListing(context)
|
||||
assertFalse(listed())
|
||||
assertEquals(PackageManager.COMPONENT_ENABLED_STATE_DISABLED, context.packageManager.getComponentEnabledSetting(service))
|
||||
}
|
||||
|
||||
@Test fun aChosenServiceStaysListed() {
|
||||
choose("other.app/.Reader:" + service.flattenToShortString())
|
||||
assertTrue(SteamIntegration.shortcutsChosen(context))
|
||||
SteamIntegration.syncShortcutsListing(context)
|
||||
assertTrue(listed())
|
||||
choose(service.flattenToString())
|
||||
assertTrue(SteamIntegration.shortcutsChosen(context))
|
||||
choose("other.app/.Reader")
|
||||
assertFalse(SteamIntegration.shortcutsChosen(context))
|
||||
SteamIntegration.syncShortcutsListing(context)
|
||||
assertFalse(listed())
|
||||
}
|
||||
|
||||
@Test fun anUpdateHidesAnUnchosenServiceWithoutOpeningDroidDeck() {
|
||||
val replaced = Intent(Intent.ACTION_MY_PACKAGE_REPLACED).setPackage(context.packageName)
|
||||
assertTrue(context.packageManager.queryBroadcastReceivers(replaced, 0)
|
||||
.any { it.activityInfo.name == ShortcutsListingReceiver::class.java.name })
|
||||
assertTrue(listed())
|
||||
ShortcutsListingReceiver().onReceive(context, replaced)
|
||||
assertFalse(listed())
|
||||
}
|
||||
|
||||
@Test fun anUpdateLeavesAChosenServiceListed() {
|
||||
choose(service.flattenToString())
|
||||
ShortcutsListingReceiver().onReceive(context, Intent(Intent.ACTION_MY_PACKAGE_REPLACED))
|
||||
assertTrue(listed())
|
||||
assertTrue(SteamIntegration.shortcutsChosen(context))
|
||||
}
|
||||
|
||||
@Test fun otherBroadcastsChangeNothing() {
|
||||
ShortcutsListingReceiver().onReceive(context, Intent(Intent.ACTION_BOOT_COMPLETED))
|
||||
assertTrue(listed())
|
||||
}
|
||||
|
||||
@Test fun turningTheServiceOffInSettingsHidesItAtOnce() {
|
||||
choose(service.flattenToString())
|
||||
val running = org.robolectric.Robolectric.buildService(HomeButtonService::class.java).create().get()
|
||||
running.onUnbind(null)
|
||||
assertTrue(listed())
|
||||
choose("")
|
||||
running.onUnbind(null)
|
||||
assertFalse(listed())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageInfo
|
||||
import android.graphics.Rect
|
||||
import android.os.IBinder
|
||||
import android.os.Parcel
|
||||
import android.provider.Settings
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.droiddeck.launcher.core.SteamIntegration
|
||||
import com.droiddeck.shortcuts.Relay
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowBinder
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], application = Application::class)
|
||||
class ShortcutsRelayTest {
|
||||
private val context: Application = ApplicationProvider.getApplicationContext()
|
||||
private val companionUid = 10_999
|
||||
|
||||
private fun choose(value: String?) =
|
||||
Settings.Secure.putString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, value)
|
||||
|
||||
private fun installCompanion() {
|
||||
val app = ApplicationInfo().apply { packageName = Relay.PACKAGE; uid = companionUid }
|
||||
shadowOf(context.packageManager).installPackage(PackageInfo().apply { packageName = Relay.PACKAGE; applicationInfo = app })
|
||||
shadowOf(context.packageManager).setPackagesForUid(companionUid, Relay.PACKAGE)
|
||||
}
|
||||
|
||||
private fun config(binder: IBinder, uid: Int): Int? {
|
||||
val data = Parcel.obtain()
|
||||
val reply = Parcel.obtain()
|
||||
try {
|
||||
data.writeInterfaceToken(Relay.DESCRIPTOR)
|
||||
ShadowBinder.setCallingUid(uid)
|
||||
if (!binder.transact(Relay.CONFIG, data, reply, 0)) return null
|
||||
reply.setDataPosition(0)
|
||||
return reply.readInt()
|
||||
} finally {
|
||||
ShadowBinder.reset()
|
||||
data.recycle()
|
||||
reply.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun droidDeckDeclaresNoAccessibilityService() {
|
||||
val services = context.packageManager.queryIntentServices(
|
||||
Intent("android.accessibilityservice.AccessibilityService").setPackage(context.packageName), 0)
|
||||
assertTrue(services.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun theCompanionFindsTheRelay() {
|
||||
val relays = context.packageManager.queryIntentServices(Intent(Relay.ACTION).setPackage(context.packageName), 0)
|
||||
assertEquals(listOf(ShortcutsRelay::class.java.name), relays.map { it.serviceInfo.name })
|
||||
}
|
||||
|
||||
@Test fun shortcutsAreOnOnlyWhileTheCompanionServiceIsEnabled() {
|
||||
assertFalse(SteamIntegration.shortcutsEnabled(context))
|
||||
choose("other.app/.Reader:${Relay.PACKAGE}/${Relay.SERVICE}")
|
||||
assertTrue(SteamIntegration.shortcutsEnabled(context))
|
||||
choose("${Relay.PACKAGE}/.ShortcutsService")
|
||||
assertTrue(SteamIntegration.shortcutsEnabled(context))
|
||||
choose("other.app/.Reader")
|
||||
assertFalse(SteamIntegration.shortcutsEnabled(context))
|
||||
}
|
||||
|
||||
@Test fun theRelayAnswersOnlyTheEnabledCompanion() {
|
||||
installCompanion()
|
||||
val binder = Robolectric.buildService(ShortcutsRelay::class.java).create().get().onBind(Intent(Relay.ACTION))
|
||||
assertNull(config(binder, companionUid))
|
||||
choose("${Relay.PACKAGE}/${Relay.SERVICE}")
|
||||
assertNull(config(binder, 10_998))
|
||||
assertEquals(0, config(binder, companionUid))
|
||||
}
|
||||
|
||||
@Test fun windowSnapshotsSurviveTheTrip() {
|
||||
val sent = mapOf(2 to listOf(Relay.Window(7, 1, 3, Rect(0, 0, 1080, 1240), "guest.app"), Relay.Window(8, 2, 1, Rect(1, 2, 3, 4), null)))
|
||||
val got = Relay.decode(Relay.encode(sent))
|
||||
assertEquals(setOf(2), got.keys)
|
||||
val windows = got.getValue(2)
|
||||
assertEquals(listOf(7, 8), windows.map { it.id })
|
||||
assertEquals(listOf(1, 2), windows.map { it.type })
|
||||
assertEquals(listOf(3, 1), windows.map { it.layer })
|
||||
assertEquals(listOf(Rect(0, 0, 1080, 1240), Rect(1, 2, 3, 4)), windows.map { it.bounds })
|
||||
assertEquals(listOf("guest.app", null), windows.map { it.pkg })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user