Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Remove org.mozilla.gecko.util.ThreadUtils usage (#3076)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin authored Apr 1, 2020
1 parent ee26bb6 commit 618045d
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 82 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 618045d

Please sign in to comment.