diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 95984f02e0..3040ee33be 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -69,6 +69,12 @@ + + + + + { if (!integrationState.shortcuts) shortcutsTried = true if (!HomeApp.isDefault(this)) configureIntegration(SteamIntegration.Feature.HOME) - else integrationSettings(SteamIntegration.shortcutSettings(this), android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS) + else { + SteamIntegration.setShortcutsListed(this, true) + integrationSettings(SteamIntegration.shortcutSettings(this), android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS) + } } } } @@ -1643,6 +1646,7 @@ class FrontEndActivity : ComponentActivity() { orientation = SessionPrefs.orientation(this) launcherOrientation = com.droiddeck.launcher.core.AppUiPrefs.orientation(this) requestedOrientation = launcherOrientation + SteamIntegration.syncShortcutsListing(this) refreshWifiDiscovery() com.droiddeck.launcher.stores.StoresState.refresh(this) com.droiddeck.launcher.stores.StoresState.libraryListener = { refreshAddedGames(); refresh() } diff --git a/app/src/main/java/com/droiddeck/launcher/core/ShortcutsListingReceiver.kt b/app/src/main/java/com/droiddeck/launcher/core/ShortcutsListingReceiver.kt new file mode 100644 index 0000000000..f9ee889bdf --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/ShortcutsListingReceiver.kt @@ -0,0 +1,11 @@ +package com.droiddeck.launcher.core + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +class ShortcutsListingReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) SteamIntegration.syncShortcutsListing(context) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt b/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt index 982b609474..d3188b3492 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/SteamIntegration.kt @@ -4,6 +4,7 @@ import android.accessibilityservice.AccessibilityServiceInfo import android.content.ComponentName import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.net.Uri import android.provider.Settings import android.view.accessibility.AccessibilityManager @@ -55,6 +56,33 @@ object SteamIntegration { return mode == android.app.AppOpsManager.MODE_ERRORED || mode == android.app.AppOpsManager.MODE_IGNORED } + private fun shortcutsComponent(context: Context) = ComponentName(context, HomeButtonService::class.java) + + fun shortcutsChosen(context: Context): Boolean = shortcutsSelection(context) == true + + private fun shortcutsSelection(context: Context): Boolean? { + val target = shortcutsComponent(context) + val enabled = runCatching { + Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES) + }.getOrElse { return null }.orEmpty() + return enabled.split(':').any { ComponentName.unflattenFromString(it.trim()) == target } + } + + fun setShortcutsListed(context: Context, listed: Boolean) { + val packages = context.packageManager + val component = shortcutsComponent(context) + val state = if (listed) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED + runCatching { + if (packages.getComponentEnabledSetting(component) != state) { + packages.setComponentEnabledSetting(component, state, PackageManager.DONT_KILL_APP) + } + } + } + + fun syncShortcutsListing(context: Context) { + shortcutsSelection(context)?.let { setShortcutsListed(context, it) } + } + fun dismissSuggestion(context: Context) { SessionPrefs.sessionPrefs(context).edit().putBoolean("steam_integration_seen", true).apply() } diff --git a/app/src/main/java/com/droiddeck/launcher/input/HomeButtonService.kt b/app/src/main/java/com/droiddeck/launcher/input/HomeButtonService.kt index b85fecb120..21ca4717d2 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/HomeButtonService.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/HomeButtonService.kt @@ -60,6 +60,7 @@ class HomeButtonService : AccessibilityService(), InputManager.InputDeviceListen override fun onUnbind(intent: Intent?): Boolean { HomeButtonShortcuts.release() + com.droiddeck.launcher.core.SteamIntegration.syncShortcutsListing(this) return super.onUnbind(intent) } diff --git a/app/src/test/java/com/droiddeck/launcher/core/SteamIntegrationShortcutsTest.kt b/app/src/test/java/com/droiddeck/launcher/core/SteamIntegrationShortcutsTest.kt new file mode 100644 index 0000000000..961d3d25ab --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/SteamIntegrationShortcutsTest.kt @@ -0,0 +1,97 @@ +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()) + } +}