From c7d2c2c2e072c971004189a45d2a1a4a70b2fbc9 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Tue, 22 Sep 2026 17:00:13 +0200 Subject: [PATCH] fix(auth): polling Signed-off-by: alperozturk96 --- .../authentication/AuthenticatorActivity.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index 05181e31974a..1d11950b63b8 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -63,6 +63,7 @@ import com.nextcloud.client.onboarding.OnboardingService; import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.common.PlainClient; +import com.nextcloud.model.HTTPStatusCodes; import com.nextcloud.operations.PostMethod; import com.nextcloud.utils.SnackbarUtil; import com.nextcloud.utils.extensions.BundleExtensionsKt; @@ -117,6 +118,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -401,17 +403,30 @@ private void deleteCookies() { } // region LoginFlow + private static final long LOGIN_FLOW_POLL_INTERVAL_SECONDS = 30; + private final ScheduledExecutorService loginFlowExecutorService = Executors.newSingleThreadScheduledExecutor(); + private ScheduledFuture loginFlowPollTask; private boolean isLoginProcessCompleted = false; private boolean isRedirectedToTheDefaultBrowser = false; private String baseUrl; private void poolLogin() { - loginFlowExecutorService.scheduleWithFixedDelay(() -> { - if (!isLoginProcessCompleted) { + if (loginFlowPollTask != null && !loginFlowPollTask.isDone()) { + return; + } + + loginFlowPollTask = loginFlowExecutorService.scheduleWithFixedDelay(() -> { + if (isLoginProcessCompleted) { + return; + } + + try { performLoginFlowV2(); + } catch (Exception e) { + Log_OC.e(TAG, "Error polling login flow: " + e); } - }, 0, 30, TimeUnit.SECONDS); + }, 0, LOGIN_FLOW_POLL_INTERVAL_SECONDS, TimeUnit.SECONDS); } /** @@ -553,36 +568,38 @@ private void performLoginFlowV2() { String response = post.getResponseBodyAsString(); Log_OC.d(TAG, "performLoginFlowV2 status: " + status); - Log_OC.d(TAG, "performLoginFlowV2 response: " + response); - if (!response.isEmpty()) { - runOnUiThread(() -> completeLoginFlow(response, status)); + if (status != HTTPStatusCodes.SUCCESS.getCode() || TextUtils.isEmpty(response)) { + return; } + + runOnUiThread(() -> completeLoginFlow(response, status)); } private void completeLoginFlow(String response, int status) { + final LoginUrlInfo loginUrlInfo; + try { - LoginUrlInfo loginUrlInfo = gson.fromJson(response, LoginUrlInfo.class); - if (loginUrlInfo == null) { - Log_OC.e(TAG, "cannot complete login flow loginUrl is null"); + loginUrlInfo = gson.fromJson(response, LoginUrlInfo.class); + if (loginUrlInfo == null || !loginUrlInfo.isValid(status)) { + Log_OC.d(TAG, "Login flow not granted yet, keep polling"); return; } - isLoginProcessCompleted = loginUrlInfo.isValid(status); + } catch (Exception e) { + Log_OC.e(TAG, "Error completeLoginFlow: " + e); + return; + } - if (accountSetupBinding != null) { - accountSetupBinding.hostUrlInput.setText(""); - } + isLoginProcessCompleted = true; - mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.getServer()); - webViewUser = loginUrlInfo.getLoginName(); - webViewPassword = loginUrlInfo.getAppPassword(); - } catch (Exception e) { - Log_OC.d(TAG, "Error completeLoginFlow: " + e); - mServerStatusIcon = R.drawable.ic_alert; - mServerStatusText = getString(R.string.qr_could_not_be_read); - showServerStatus(); + if (accountSetupBinding != null) { + accountSetupBinding.hostUrlInput.setText(""); } + mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.getServer()); + webViewUser = loginUrlInfo.getLoginName(); + webViewPassword = loginUrlInfo.getAppPassword(); + checkOcServer(); loginFlowExecutorService.shutdown(); ProcessLifecycleOwner.get().getLifecycle().removeObserver(lifecycleEventObserver);