Skip to content

Commit 19abf58

Browse files
committed
fix: system bridge autostarter uses correct time for cooldown
1 parent 8bc2592 commit 19abf58

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/promode/SystemBridgeAutoStarter.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class SystemBridgeAutoStarter @Inject constructor(
128128
private val autoStartFlow: Flow<AutoStartType?> =
129129
connectionManager.connectionState.flatMapLatest { connectionState ->
130130

131+
Timber.i("LATEST CONNECTION STATE: $connectionState")
132+
131133
// Do not autostart if it is connected or it was killed from the user
132134
if (connectionState !is SystemBridgeConnectionState.Disconnected ||
133135
connectionState.isStoppedByUser ||
@@ -137,7 +139,7 @@ class SystemBridgeAutoStarter @Inject constructor(
137139
!isAutoStartEnabled()
138140
) {
139141
flowOf(null)
140-
} else if (diedAfterAutostart(connectionState.time)) {
142+
} else if (isWithinAutoStartCooldown()) {
141143
// Do not autostart if the system bridge was killed shortly after.
142144
// This prevents infinite loops happening.
143145
Timber.w(
@@ -293,9 +295,10 @@ class SystemBridgeAutoStarter @Inject constructor(
293295
* Whether the system bridge died less than 5 minutes after the previous time it was
294296
* auto started.
295297
*/
296-
private suspend fun diedAfterAutostart(disconnectionTime: Long): Boolean {
298+
private suspend fun isWithinAutoStartCooldown(): Boolean {
297299
val lastAutoStartTime = preferences.get(Keys.systemBridgeLastAutoStartTime).first()
298-
return lastAutoStartTime != null && disconnectionTime - lastAutoStartTime < (5 * 60_000)
300+
return lastAutoStartTime != null &&
301+
clock.elapsedRealtime() - lastAutoStartTime < (5 * 60_000)
299302
}
300303

301304
private suspend fun isAutoStartEnabled(): Boolean {

base/src/test/java/io/github/sds100/keymapper/base/promode/SystemBridgeAutoStarterTest.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,27 @@ class SystemBridgeAutoStarterTest {
6262
private lateinit var mockResourceProvider: ResourceProvider
6363
private lateinit var testBuildConfig: TestBuildConfigProvider
6464

65-
private val isRootGrantedFlow = MutableStateFlow(false)
66-
private val shizukuIsStartedFlow = MutableStateFlow(false)
67-
private val connectionStateFlow = MutableStateFlow<SystemBridgeConnectionState>(
68-
SystemBridgeConnectionState.Disconnected(time = 0, isStoppedByUser = false),
69-
)
70-
private val isWifiConnectedFlow = MutableStateFlow(false)
71-
private val writeSecureSettingsGrantedFlow = MutableStateFlow(false)
72-
private val shizukuPermissionGrantedFlow = MutableStateFlow(false)
65+
private lateinit var isRootGrantedFlow: MutableStateFlow<Boolean>
66+
private lateinit var shizukuIsStartedFlow: MutableStateFlow<Boolean>
67+
private lateinit var connectionStateFlow: MutableStateFlow<SystemBridgeConnectionState>
68+
private lateinit var isWifiConnectedFlow: MutableStateFlow<Boolean>
69+
private lateinit var writeSecureSettingsGrantedFlow: MutableStateFlow<Boolean>
70+
private lateinit var shizukuPermissionGrantedFlow: MutableStateFlow<Boolean>
7371

7472
@Before
7573
fun init() {
74+
isRootGrantedFlow = MutableStateFlow(false)
75+
shizukuIsStartedFlow = MutableStateFlow(false)
76+
connectionStateFlow = MutableStateFlow(
77+
SystemBridgeConnectionState.Disconnected(
78+
time = testCoroutineScope.testScheduler.currentTime,
79+
isStoppedByUser = false,
80+
),
81+
)
82+
isWifiConnectedFlow = MutableStateFlow(false)
83+
writeSecureSettingsGrantedFlow = MutableStateFlow(false)
84+
shizukuPermissionGrantedFlow = MutableStateFlow(false)
85+
7686
mockSuAdapter = mock {
7787
on { isRootGranted } doReturn isRootGrantedFlow
7888
}
@@ -521,15 +531,14 @@ class SystemBridgeAutoStarterTest {
521531
}
522532

523533
@Test
524-
fun `show killed and not restarting notification if system bridge dies less than 30 seconds after auto starting`() =
534+
fun `show killed and not restarting notification if want to autostart again within the cooldown`() =
525535
runTest(testDispatcher) {
526536
fakePreferences.set(Keys.isSystemBridgeKeepAliveEnabled, true)
527537
fakePreferences.set(Keys.isSystemBridgeUsed, true)
528538

529539
whenever(
530540
mockResourceProvider.getString(R.string.system_bridge_died_notification_title),
531541
).thenReturn("died")
532-
533542
whenever(mockSetupController.isAdbPaired()).thenReturn(true)
534543
isWifiConnectedFlow.value = true
535544
writeSecureSettingsGrantedFlow.value = true
@@ -542,10 +551,11 @@ class SystemBridgeAutoStarterTest {
542551
verify(mockNotificationAdapter).showNotification(any())
543552
connectionStateFlow.value = SystemBridgeConnectionState.Connected(time = 10000)
544553

545-
// Set the state as connected within the timeout
554+
// Set the state as disconnected within the timeout
546555
connectionStateFlow.value =
547556
SystemBridgeConnectionState.Disconnected(time = 11000, isStoppedByUser = false)
548557
advanceUntilIdle()
558+
549559
// Show notification
550560
val argument = argumentCaptor<NotificationModel>()
551561
verify(mockNotificationAdapter).showNotification(argument.capture())

sysbridge/src/main/java/io/github/sds100/keymapper/sysbridge/service/SystemBridge.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class SystemBridge : ISystemBridge.Stub() {
436436
return inputManager.injectInputEvent(event, mode)
437437
}
438438

439-
override fun getEvdevInputDevices(): Array<out EvdevDeviceInfo?>? {
439+
override fun getEvdevInputDevices(): Array<out EvdevDeviceInfo?> {
440440
return getEvdevDevicesNative()
441441
}
442442

@@ -757,11 +757,14 @@ class SystemBridge : ISystemBridge.Stub() {
757757
tetheringConnector.registerTetheringEventCallback(callback, processPackageName)
758758

759759
// Wait for callback with timeout using Handler
760-
mainHandler.postDelayed({
761-
synchronized(lock) {
762-
lock.notify()
763-
}
764-
}, timeoutMillis)
760+
mainHandler.postDelayed(
761+
{
762+
synchronized(lock) {
763+
lock.notify()
764+
}
765+
},
766+
timeoutMillis,
767+
)
765768

766769
synchronized(lock) {
767770
lock.wait(timeoutMillis)

0 commit comments

Comments
 (0)