commit 07f38358647f7abbf4cf64dead6890b4bd545fbe
parent ae1aee3ce0f662db447021d51901630000d5e884
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date: Mon, 12 Aug 2024 01:14:40 +0200
fix(core/bridge): connection retry
Diffstat:
1 file changed, 52 insertions(+), 40 deletions(-)
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/bridge/BridgeClient.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/bridge/BridgeClient.kt
@@ -64,52 +64,64 @@ class BridgeClient(
return true
}
- return withTimeoutOrNull(10000L) {
- suspendCancellableCoroutine { cancellableContinuation ->
- continuation = cancellableContinuation
- with(context.androidContext) {
- //ensure the remote process is running
- runCatching {
- startActivity(Intent()
- .setClassName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.bridge.ForceStartActivity")
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
- )
- }
+ val connectionTimeout = 15000L
+ val retryDelay = 3000L
+
+ return withTimeoutOrNull(connectionTimeout) {
+ var result: Boolean? = null
+
+ for (retry in 0.. (connectionTimeout / retryDelay).toInt()) {
+ result = withTimeoutOrNull(retryDelay) {
+ suspendCancellableCoroutine { cancellableContinuation ->
+ continuation = cancellableContinuation
+ with(context.androidContext) {
+ //ensure the remote process is running
+ runCatching {
+ startActivity(Intent()
+ .setClassName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.bridge.ForceStartActivity")
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
+ )
+ }
- runCatching {
- val intent = Intent()
- .setClassName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.bridge.BridgeService")
- runCatching {
- if (this@BridgeClient::service.isInitialized) {
- unbindService(this@BridgeClient)
+ runCatching {
+ val intent = Intent()
+ .setClassName(Constants.SE_PACKAGE_NAME, "me.rhunk.snapenhance.bridge.BridgeService")
+ runCatching {
+ if (this@BridgeClient::service.isInitialized) {
+ unbindService(this@BridgeClient)
+ }
+ }
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+ bindService(
+ intent,
+ Context.BIND_AUTO_CREATE,
+ Executors.newSingleThreadExecutor(),
+ this@BridgeClient
+ )
+ } else {
+ XposedHelpers.callMethod(
+ this,
+ "bindServiceAsUser",
+ intent,
+ this@BridgeClient,
+ Context.BIND_AUTO_CREATE,
+ Handler(HandlerThread("BridgeClient").apply {
+ start()
+ }.looper),
+ Process.myUserHandle()
+ )
+ }
+ }.onFailure {
+ onFailure(it)
+ resumeContinuation(false)
}
}
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- bindService(
- intent,
- Context.BIND_AUTO_CREATE,
- Executors.newSingleThreadExecutor(),
- this@BridgeClient
- )
- } else {
- XposedHelpers.callMethod(
- this,
- "bindServiceAsUser",
- intent,
- this@BridgeClient,
- Context.BIND_AUTO_CREATE,
- Handler(HandlerThread("BridgeClient").apply {
- start()
- }.looper),
- Process.myUserHandle()
- )
- }
- }.onFailure {
- onFailure(it)
- resumeContinuation(false)
}
}
+ if (result != null) break
}
+
+ result
}
}