From 64bfbcffef1e397623b72f3e596dad0690da2fe2 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Mon, 23 Oct 2023 22:27:13 +0300 Subject: [PATCH] Fixed crash when enabling passwords sync and close settings screen (#20639) Fixed crash when enabling passwords sync and close settings screen; fixes brave/brave-browser#33782 --- .../settings/BraveManageSyncSettings.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java b/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java index cbc18c5e2b6..9f754224952 100644 --- a/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java +++ b/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java @@ -5,6 +5,7 @@ package org.chromium.chrome.browser.sync.settings; +import android.app.Activity; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableString; @@ -168,6 +169,13 @@ public class BraveManageSyncSettings extends ManageSyncSettings { updateSyncPasswordsSummary(); } + @Override + public void onDestroy() { + super.onDestroy(); + // Do not let timer run when we closed the settings + cleanupPasswordsSummaryUpdater(); + } + private void updateSyncPasswordsSummary() { if (ReauthenticationManager.isScreenLockSetUp(ContextUtils.getApplicationContext())) { if (ReauthenticationManager.authenticationStillValid( @@ -190,22 +198,24 @@ public class BraveManageSyncSettings extends ManageSyncSettings { } private void scheduleCheckForStillValidAuth() { - if (mPasswordsSummaryUpdater != null) { - mPasswordsSummaryUpdater.cancel(); - mPasswordsSummaryUpdater.purge(); - mPasswordsSummaryUpdater = null; - } + // Cancel old timer before creating new. Otherwise when we turn on/off passwords sync for + // several times, we will have several timer procedures at the same time. + cleanupPasswordsSummaryUpdater(); + mPasswordsSummaryUpdater = new Timer(); mPasswordsSummaryUpdater.schedule( new TimerTask() { @Override public void run() { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - updateSyncPasswordsSummary(); - } - }); + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + updateSyncPasswordsSummary(); + } + }); + } } }, 0, @@ -217,4 +227,12 @@ public class BraveManageSyncSettings extends ManageSyncSettings { // onResume is invoked, and the timer is cancelled. RECHECK_VALID_AUTHENTICATION_INTERVAL_MILLIS); } + + private void cleanupPasswordsSummaryUpdater() { + if (mPasswordsSummaryUpdater != null) { + mPasswordsSummaryUpdater.cancel(); + mPasswordsSummaryUpdater.purge(); + mPasswordsSummaryUpdater = null; + } + } }