From c2d940cf0685483d180610f01b4686f15e76ef72 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Wed, 22 Jul 2020 09:48:36 -0700 Subject: [PATCH] Use AC RunWhenReadyQueue (#12800) --- .../java/org/mozilla/fenix/HomeActivity.kt | 2 +- .../fenix/components/BackgroundServices.kt | 2 +- .../fenix/components/PerformanceComponent.kt | 2 +- .../PerformanceActivityLifecycleCallbacks.kt | 2 +- .../mozilla/fenix/utils/RunWhenReadyQueue.kt | 57 ------------------- 5 files changed, 4 insertions(+), 61 deletions(-) delete mode 100644 app/src/main/java/org/mozilla/fenix/utils/RunWhenReadyQueue.kt diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 0efa5e962..9c7f3cfab 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -52,6 +52,7 @@ import mozilla.components.support.ktx.android.content.share import mozilla.components.support.ktx.kotlin.isUrl import mozilla.components.support.ktx.kotlin.toNormalizedUrl import mozilla.components.support.locale.LocaleAwareAppCompatActivity +import mozilla.components.support.utils.RunWhenReadyQueue import mozilla.components.support.utils.SafeIntent import mozilla.components.support.utils.toSafeIntent import mozilla.components.support.webextensions.WebExtensionPopupFeature @@ -97,7 +98,6 @@ import org.mozilla.fenix.theme.DefaultThemeManager import org.mozilla.fenix.theme.ThemeManager import org.mozilla.fenix.trackingprotectionexceptions.TrackingProtectionExceptionsFragmentDirections import org.mozilla.fenix.utils.BrowsersCache -import org.mozilla.fenix.utils.RunWhenReadyQueue /** * The main activity of the application. The application is primarily a single Activity (this one) diff --git a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt index feb74728c..4ac0145d9 100644 --- a/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt +++ b/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt @@ -30,6 +30,7 @@ import mozilla.components.service.fxa.manager.SCOPE_SYNC import mozilla.components.service.fxa.manager.SyncEnginesStorage import mozilla.components.service.fxa.sync.GlobalSyncableStoreProvider import mozilla.components.service.sync.logins.SyncableLoginsStorage +import mozilla.components.support.utils.RunWhenReadyQueue import org.mozilla.fenix.Config import org.mozilla.fenix.FeatureFlags import org.mozilla.fenix.R @@ -39,7 +40,6 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings import org.mozilla.fenix.sync.SyncedTabsIntegration import org.mozilla.fenix.utils.Mockable -import org.mozilla.fenix.utils.RunWhenReadyQueue import org.mozilla.fenix.utils.Settings /** diff --git a/app/src/main/java/org/mozilla/fenix/components/PerformanceComponent.kt b/app/src/main/java/org/mozilla/fenix/components/PerformanceComponent.kt index 88f03aa40..615d348a8 100644 --- a/app/src/main/java/org/mozilla/fenix/components/PerformanceComponent.kt +++ b/app/src/main/java/org/mozilla/fenix/components/PerformanceComponent.kt @@ -4,7 +4,7 @@ package org.mozilla.fenix.components -import org.mozilla.fenix.utils.RunWhenReadyQueue +import mozilla.components.support.utils.RunWhenReadyQueue /** * Component group for all functionality related to performance. diff --git a/app/src/main/java/org/mozilla/fenix/session/PerformanceActivityLifecycleCallbacks.kt b/app/src/main/java/org/mozilla/fenix/session/PerformanceActivityLifecycleCallbacks.kt index 63a14cf5e..793a222cb 100644 --- a/app/src/main/java/org/mozilla/fenix/session/PerformanceActivityLifecycleCallbacks.kt +++ b/app/src/main/java/org/mozilla/fenix/session/PerformanceActivityLifecycleCallbacks.kt @@ -7,11 +7,11 @@ package org.mozilla.fenix.session import android.app.Activity import android.app.Application import android.os.Bundle +import mozilla.components.support.utils.RunWhenReadyQueue import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.IntentReceiverActivity import org.mozilla.fenix.browser.BrowserPerformanceTestActivity import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity -import org.mozilla.fenix.utils.RunWhenReadyQueue import org.mozilla.fenix.widget.VoiceSearchActivity /** diff --git a/app/src/main/java/org/mozilla/fenix/utils/RunWhenReadyQueue.kt b/app/src/main/java/org/mozilla/fenix/utils/RunWhenReadyQueue.kt deleted file mode 100644 index 9b7167d1c..000000000 --- a/app/src/main/java/org/mozilla/fenix/utils/RunWhenReadyQueue.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix.utils - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import java.util.concurrent.CopyOnWriteArrayList -import java.util.concurrent.atomic.AtomicBoolean - -/** - * A queue that acts as a gate, either executing tasks right away if the queue is marked as "ready", - * i.e. gate is open, or queues them to be executed whenever the queue is marked as ready in the - * future, i.e. gate becomes open. - */ -class RunWhenReadyQueue { - private val tasks = CopyOnWriteArrayList<() -> Unit>() - private val isReady = AtomicBoolean(false) - - /** - * Was this queue ever marked as 'ready' via a call to [ready]? - * - * @return Boolean value indicating if this queue is 'ready'. - */ - fun isReady(): Boolean = isReady.get() - - /** - * Runs the [task] if this queue is marked as ready, or queues it for later execution. - * Task will be executed on the main thread. - * - * @param task: The task to run now if queue is ready or queue for later execution. - */ - fun runIfReadyOrQueue(task: () -> Unit) { - if (isReady.get()) { - CoroutineScope(Dispatchers.Main).launch { task.invoke() } - } else { - tasks.add(task) - } - } - - /** - * Mark queue as ready. Pending tasks will execute, and all tasks passed to [runIfReadyOrQueue] - * after this point will be executed immediately. - */ - fun ready() { - // Make sure that calls to `ready` are idempotent. - if (!isReady.compareAndSet(false, true)) { - return - } - - CoroutineScope(Dispatchers.Main).launch { - tasks.forEach { it.invoke() }.also { tasks.clear() } - } - } -}