Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -307,28 +313,39 @@ 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)
handleDisconnection()
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) {}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading