Skip to content

Commit 7bf2ec5

Browse files
committed
feat: improve bridge connection handling with automatic reconnect logic
1 parent 8889f1c commit 7bf2ec5

File tree

1 file changed

+28
-16
lines changed
  • app/src/main/java/com/wmods/wppenhacer/xposed/core

1 file changed

+28
-16
lines changed

app/src/main/java/com/wmods/wppenhacer/xposed/core/WppCore.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.wmods.wppenhacer.xposed.bridge.client.BaseClient;
1919
import com.wmods.wppenhacer.xposed.bridge.client.BridgeClient;
2020
import com.wmods.wppenhacer.xposed.bridge.client.ProviderClient;
21-
import com.wmods.wppenhacer.xposed.core.components.AlertDialogWpp;
2221
import com.wmods.wppenhacer.xposed.core.components.FMessageWpp;
2322
import com.wmods.wppenhacer.xposed.core.devkit.Unobfuscator;
2423
import com.wmods.wppenhacer.xposed.core.devkit.UnobfuscatorCache;
@@ -38,6 +37,7 @@
3837
import java.util.HashSet;
3938
import java.util.List;
4039
import java.util.concurrent.CompletableFuture;
40+
import java.util.concurrent.TimeUnit;
4141

4242
import de.robv.android.xposed.XC_MethodHook;
4343
import de.robv.android.xposed.XSharedPreferences;
@@ -680,25 +680,37 @@ public static void addListenerActivity(ActivityChangeState listener) {
680680
}
681681

682682
public static WaeIIFace getClientBridge() throws Exception {
683-
if (client == null || client.getService() == null || !client.getService().asBinder().isBinderAlive()
684-
|| !client.getService().asBinder().pingBinder()) {
685-
WppCore.getCurrentActivity().runOnUiThread(() -> {
686-
var dialog = new AlertDialogWpp(WppCore.getCurrentActivity());
687-
dialog.setTitle("Bridge Error");
688-
dialog.setMessage(
689-
"The Connection with WaEnhancer was lost, it is necessary to reconnect with WaEnhancer in order to reestablish the connection.");
690-
dialog.setPositiveButton("reconnect", (dialog1, which) -> {
691-
client.tryReconnect();
692-
dialog.dismiss();
693-
});
694-
dialog.setNegativeButton("cancel", null);
695-
dialog.show();
696-
});
697-
throw new Exception("Failed connect to Bridge");
683+
if (!isBridgeConnected()) {
684+
synchronized (WppCore.class) {
685+
if (!isBridgeConnected()) {
686+
if (client == null) {
687+
throw new Exception("Bridge client not initialized");
688+
}
689+
XposedBridge.log("Bridge disconnected. Trying automatic synchronous reconnect");
690+
boolean reconnected = false;
691+
try {
692+
reconnected = Boolean.TRUE.equals(client.connect().get(4, TimeUnit.SECONDS));
693+
} catch (Throwable e) {
694+
XposedBridge.log(e);
695+
}
696+
if (!reconnected || !isBridgeConnected()) {
697+
throw new Exception("Failed connect to Bridge");
698+
}
699+
}
700+
}
698701
}
699702
return client.getService();
700703
}
701704

705+
private static boolean isBridgeConnected() {
706+
var currentClient = client;
707+
if (currentClient == null) {
708+
return false;
709+
}
710+
var service = currentClient.getService();
711+
return service != null && service.asBinder().isBinderAlive() && service.asBinder().pingBinder();
712+
}
713+
702714
public interface ActivityChangeState {
703715

704716
void onChange(Activity activity, ChangeType type);

0 commit comments

Comments
 (0)