From 618045df1896daf84b18f2dabfe01d9c22bf1f88 Mon Sep 17 00:00:00 2001 From: "Randall E. Barker" Date: Wed, 1 Apr 2020 11:42:04 -0700 Subject: [PATCH] Remove org.mozilla.gecko.util.ThreadUtils usage (#3076) --- .../browser/engine/SessionStore.java | 2 +- .../ui/adapters/LanguagesAdapter.java | 2 +- .../mozilla/vrbrowser/ui/views/UIButton.java | 2 +- .../ui/widgets/NotificationManager.java | 2 +- .../ui/widgets/dialogs/VoiceSearchWidget.java | 2 +- .../settings/ContentLanguageOptionsView.java | 3 +- .../vrbrowser/utils/AnimationHelper.java | 3 - .../mozilla/vrbrowser/utils/SystemUtils.java | 1 - .../mozilla/vrbrowser/utils/ThreadUtils.java | 82 +++++++++++++++++++ versions.gradle | 2 +- 10 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java index 4699593be..3f0bfec52 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionStore.java @@ -8,7 +8,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.geckoview.GeckoRuntime; import org.mozilla.geckoview.GeckoSession; import org.mozilla.vrbrowser.VRBrowserApplication; @@ -19,6 +18,7 @@ import org.mozilla.vrbrowser.browser.content.TrackingProtectionStore; import org.mozilla.vrbrowser.db.SitePermission; import org.mozilla.vrbrowser.utils.SystemUtils; +import org.mozilla.vrbrowser.utils.ThreadUtils; import org.mozilla.vrbrowser.utils.UrlUtils; import java.util.ArrayList; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/adapters/LanguagesAdapter.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/adapters/LanguagesAdapter.java index e1a790096..2625ef528 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/adapters/LanguagesAdapter.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/adapters/LanguagesAdapter.java @@ -15,10 +15,10 @@ import androidx.databinding.DataBindingUtil; import androidx.recyclerview.widget.RecyclerView; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.databinding.LanguageItemBinding; import org.mozilla.vrbrowser.ui.callbacks.LanguageItemCallback; +import org.mozilla.vrbrowser.utils.ThreadUtils; import org.mozilla.vrbrowser.utils.ViewUtils; import java.util.Collections; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java index 8994884eb..2983a2b57 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/views/UIButton.java @@ -25,11 +25,11 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatImageButton; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.ui.widgets.TooltipWidget; import org.mozilla.vrbrowser.ui.widgets.UIWidget; import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement; +import org.mozilla.vrbrowser.utils.ThreadUtils; import org.mozilla.vrbrowser.utils.ViewUtils; public class UIButton extends AppCompatImageButton implements CustomUIButton { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NotificationManager.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NotificationManager.java index 98bcbf4ec..802b10c79 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NotificationManager.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NotificationManager.java @@ -9,10 +9,10 @@ import androidx.annotation.NonNull; import androidx.annotation.StringRes; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.ui.views.UIButton; import org.mozilla.vrbrowser.ui.widgets.NotificationManager.Notification.NotificationPosition; +import org.mozilla.vrbrowser.utils.ThreadUtils; import java.util.HashMap; import java.util.Iterator; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java index 6ede2ab3d..a89f1a369 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/dialogs/VoiceSearchWidget.java @@ -25,7 +25,6 @@ import com.mozilla.speechlibrary.MozillaSpeechService; import com.mozilla.speechlibrary.STTResult; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.browser.engine.EngineProvider; @@ -34,6 +33,7 @@ import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement; import org.mozilla.vrbrowser.utils.LocaleUtils; +import org.mozilla.vrbrowser.utils.ThreadUtils; public class VoiceSearchWidget extends UIDialog implements WidgetManagerDelegate.PermissionListener, Application.ActivityLifecycleCallbacks { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/ContentLanguageOptionsView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/ContentLanguageOptionsView.java index 5db6c2a2b..acb67aae0 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/ContentLanguageOptionsView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/ContentLanguageOptionsView.java @@ -12,9 +12,7 @@ import androidx.databinding.DataBindingUtil; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.vrbrowser.R; -import org.mozilla.vrbrowser.browser.SettingsStore; import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.databinding.OptionsLanguageContentBinding; import org.mozilla.vrbrowser.ui.adapters.Language; @@ -23,6 +21,7 @@ import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement; import org.mozilla.vrbrowser.utils.LocaleUtils; +import org.mozilla.vrbrowser.utils.ThreadUtils; import java.util.Collections; import java.util.List; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/utils/AnimationHelper.java b/app/src/common/shared/org/mozilla/vrbrowser/utils/AnimationHelper.java index 0179e8584..51762e8f7 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/utils/AnimationHelper.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/utils/AnimationHelper.java @@ -3,7 +3,6 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; -import android.util.Log; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AccelerateInterpolator; @@ -13,8 +12,6 @@ import androidx.annotation.NonNull; -import org.mozilla.gecko.util.ThreadUtils; - public class AnimationHelper { public static final long FADE_ANIMATION_DURATION = 150; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java b/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java index 5ab608c6f..6e4fdac14 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java @@ -8,7 +8,6 @@ import androidx.annotation.NonNull; -import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.geckoview.CrashReporter; import org.mozilla.geckoview.GeckoResult; import org.mozilla.vrbrowser.BuildConfig; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java b/app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java new file mode 100644 index 000000000..33e0433a3 --- /dev/null +++ b/app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java @@ -0,0 +1,82 @@ +package org.mozilla.vrbrowser.utils; + +import android.os.Handler; +import android.os.Looper; + +public class ThreadUtils extends Thread { + private static final Handler sUiHandler = new Handler(Looper.getMainLooper()); + + private static final String LOOPER_NAME = "VRBBackgroundThread"; + + // Guarded by 'ThreadUtils.class'. + private static Handler mBackgroundHandler; + private static Thread mBackgroundThread; + + // The initial Runnable to run on the new mBackgroundThread. Its purpose + // is to avoid us having to wait for the new mBackgroundThread to start. + private Runnable mInitialRunnable; + + // Singleton, so private constructor. + private ThreadUtils(final Runnable initialRunnable) { + mInitialRunnable = initialRunnable; + } + + @Override + public void run() { + setName(LOOPER_NAME); + Looper.prepare(); + + synchronized (ThreadUtils.class) { + mBackgroundHandler = new Handler(); + ThreadUtils.class.notifyAll(); + } + + if (mInitialRunnable != null) { + mInitialRunnable.run(); + mInitialRunnable = null; + } + + Looper.loop(); + } + + private static void startThread(final Runnable initialRunnable) { + mBackgroundThread = new ThreadUtils(initialRunnable); + mBackgroundThread.setDaemon(true); + mBackgroundThread.start(); + } + + // Get a Handler for a looper mBackgroundThread, or create one if it doesn't yet exist. + /*package*/ static synchronized Handler getHandler() { + if (mBackgroundThread == null) { + startThread(null); + } + + while (mBackgroundHandler == null) { + try { + ThreadUtils.class.wait(); + } catch (final InterruptedException e) { + } + } + return mBackgroundHandler; + } + + public static synchronized void postToBackgroundThread(final Runnable runnable) { + if (mBackgroundThread == null) { + startThread(runnable); + return; + } + getHandler().post(runnable); + } + + public static void postToUiThread(final Runnable runnable) { + sUiHandler.post(runnable); + } + + public static void postDelayedToUiThread(final Runnable runnable, final long timeout) { + sUiHandler.postDelayed(runnable, timeout); + } + + public static void removeCallbacksFromUiThread(final Runnable runnable) { + sUiHandler.removeCallbacks(runnable); + } +} diff --git a/versions.gradle b/versions.gradle index f87b6b795..46e9573bc 100644 --- a/versions.gradle +++ b/versions.gradle @@ -24,7 +24,7 @@ ext.deps = [:] def versions = [:] // GeckoView versions can be found here: // https://maven.mozilla.org/?prefix=maven2/org/mozilla/geckoview/ -versions.gecko_view = "76.0.20200324093140" +versions.gecko_view = "76.0.20200330094747" versions.android_components = "28.0.1" // Note that android-components also depends on application-services, // and in fact is our main source of appservices-related functionality.