diff --git a/app/src/main/java/helium314/keyboard/latin/voice/VoicePluginManager.kt b/app/src/main/java/helium314/keyboard/latin/voice/VoicePluginManager.kt index 59d11b6a6..a288dbf61 100644 --- a/app/src/main/java/helium314/keyboard/latin/voice/VoicePluginManager.kt +++ b/app/src/main/java/helium314/keyboard/latin/voice/VoicePluginManager.kt @@ -117,10 +117,16 @@ class VoicePluginManager(private val context: Context) : IBinder.DeathRecipient return } cancelScheduledUnbind() - val runnable = Runnable { - pendingUnbindRunnable = null - if (canUnbind()) { - unbind() + val runnable = object : Runnable { + override fun run() { + if (canUnbind()) { + pendingUnbindRunnable = null + unbind() + } else { + // busy (e.g. dictating): look again soon, giving up here would leave the plugin bound + // until the next hide or screen off + mainHandler.postDelayed(this, UNBIND_RETRY_DELAY_MS) + } } } pendingUnbindRunnable = runnable @@ -307,18 +313,27 @@ class VoicePluginManager(private val context: Context) : IBinder.DeathRecipient } } + /** The bind state belongs to the main thread, the import runs on IO. */ + private fun scheduleIdleUnbindAfterImport() { + mainHandler.post { unbindAfterDelay(IMPORT_UNBIND_DELAY_MS) } + } + suspend fun bindAndImport(request: ModelImportRequest): Boolean = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { + // a plugin that was already connected stays under the normal keep-loaded policy + val wasConnected = isPluginConnected() val eng = getOrAwaitEngine() if (eng == null) { Log.e(TAG, "bindAndImport: unable to connect to voice plugin") try { request.file.close() } catch (_: Exception) {} + // the bind may still be in progress, do not leave it open + if (!wasConnected) scheduleIdleUnbindAfterImport() return@withContext false } return@withContext try { eng.importModel(request) Log.i(TAG, "bindAndImport: oneway importModel dispatched successfully") - unbindAfterDelay(30000L) + scheduleIdleUnbindAfterImport() true } catch (e: DeadObjectException) { Log.e(TAG, "Voice plugin service died in importModel", e) @@ -326,9 +341,11 @@ class VoicePluginManager(private val context: Context) : IBinder.DeathRecipient false } catch (e: RemoteException) { Log.e(TAG, "importModel oneway dispatch failed", e) + if (!wasConnected) scheduleIdleUnbindAfterImport() false } catch (e: Exception) { Log.e(TAG, "importModel failed", e) + if (!wasConnected) scheduleIdleUnbindAfterImport() false } finally { try { request.file.close() } catch (_: Exception) {} @@ -425,5 +442,7 @@ class VoicePluginManager(private val context: Context) : IBinder.DeathRecipient companion object { private const val TAG = "VoicePluginManager" + private const val IMPORT_UNBIND_DELAY_MS = 30_000L + private const val UNBIND_RETRY_DELAY_MS = 15_000L } } diff --git a/app/src/test/java/helium314/keyboard/latin/voice/VoicePluginManagerTest.kt b/app/src/test/java/helium314/keyboard/latin/voice/VoicePluginManagerTest.kt index 87f75e885..b40722171 100644 --- a/app/src/test/java/helium314/keyboard/latin/voice/VoicePluginManagerTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/voice/VoicePluginManagerTest.kt @@ -107,6 +107,41 @@ class VoicePluginManagerTest { assertFalse(manager.hasScheduledUnbind()) } + @Test + fun unbindAfterDelay_retriesWhileBusyInsteadOfGivingUp() { + assertTrue(manager.bindIfNeeded()) + fakeContext.simulateConnected() + var busy = true + + manager.unbindAfterDelay(60_000L) { !busy } + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(61)) + // busy when it fired: still bound, and still scheduled to look again + assertEquals(0, fakeContext.unbindCalls) + assertTrue(manager.hasScheduledUnbind()) + + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(60)) + assertEquals(0, fakeContext.unbindCalls) + + busy = false + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(16)) + assertEquals(1, fakeContext.unbindCalls) + assertFalse(manager.isPluginConnected()) + assertFalse(manager.hasScheduledUnbind()) + } + + @Test + fun unbindAfterDelay_retryStopsWhenCancelled() { + assertTrue(manager.bindIfNeeded()) + fakeContext.simulateConnected() + + manager.unbindAfterDelay(60_000L) { false } + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(61)) + manager.cancelScheduledUnbind() + assertFalse(manager.hasScheduledUnbind()) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMinutes(5)) + assertEquals(0, fakeContext.unbindCalls) + } + @Test fun bindIfNeeded_cancelsPendingUnbind() { assertTrue(manager.bindIfNeeded())