Skip to content

Commit

Permalink
Use App executors instead of ThreadUtils (#3091)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Apr 2, 2020
1 parent deefeeb commit a360c20
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 108 deletions.
20 changes: 17 additions & 3 deletions app/src/common/shared/org/mozilla/vrbrowser/AppExecutors.java
@@ -1,13 +1,14 @@
package org.mozilla.vrbrowser;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import androidx.annotation.NonNull;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import androidx.annotation.NonNull;

public class AppExecutors {

private final Executor mDiskIO;
Expand All @@ -16,14 +17,19 @@ public class AppExecutors {

private final Executor mMainThread;

private final HandlerThread mBackgroundThread;
private Handler mBackgroundHandler;

private AppExecutors(Executor diskIO, Executor networkIO, Executor mainThread) {
this.mDiskIO = diskIO;
this.mNetworkIO = networkIO;
this.mMainThread = mainThread;
mBackgroundThread = new HandlerThread("BackgroundThread");
}

public AppExecutors() {
this(Executors.newSingleThreadExecutor(), Executors.newFixedThreadPool(3),
this(Executors.newSingleThreadExecutor(),
Executors.newFixedThreadPool(3),
new MainThreadExecutor());
}

Expand All @@ -39,6 +45,14 @@ public Executor mainThread() {
return mMainThread;
}

public Handler backgroundThread() {
if (!mBackgroundThread.isAlive()) {
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
return mBackgroundHandler;
}

private static class MainThreadExecutor implements Executor {
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());

Expand Down
Expand Up @@ -18,12 +18,12 @@
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;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;

public class SessionStore implements GeckoSession.PermissionDelegate{
private static final String LOGTAG = SystemUtils.createLogtag(SessionStore.class);
Expand All @@ -38,6 +38,7 @@ public static SessionStore get() {
return mInstance;
}

private Executor mMainExecutor;
private Context mContext;
private GeckoRuntime mRuntime;
private ArrayList<Session> mSessions;
Expand All @@ -55,6 +56,7 @@ private SessionStore() {

public void setContext(Context context, Bundle aExtras) {
mContext = context;
mMainExecutor = ((VRBrowserApplication)context.getApplicationContext()).getExecutors().mainThread();

// FIXME: Once GeckoView has a prefs API
SessionUtils.vrPrefsWorkAround(context, aExtras);
Expand Down Expand Up @@ -199,7 +201,7 @@ void sessionActiveStateChanged() {
if (count > MAX_GECKO_SESSIONS) {
Log.d(LOGTAG, "Too many GeckoSessions. Active: " + activeCount + " Inactive: " + inactiveCount + " Suspended: " + suspendedCount);
mSuspendPending = true;
ThreadUtils.postToUiThread(this::limitInactiveSessions);
mMainExecutor.execute(this::limitInactiveSessions);
}
}

Expand Down
Expand Up @@ -16,18 +16,20 @@
import androidx.recyclerview.widget.RecyclerView;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserApplication;
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;
import java.util.List;
import java.util.concurrent.Executor;

public class LanguagesAdapter extends RecyclerView.Adapter<LanguagesAdapter.LanguageViewHolder> {

private static final int ICON_ANIMATION_DURATION = 200;

private Executor mMainExecutor;
private List<Language> mLanguagesList;
private boolean mIsPreferred;

Expand All @@ -40,6 +42,7 @@ public class LanguagesAdapter extends RecyclerView.Adapter<LanguagesAdapter.Lang
private final LanguageItemCallback mLanguageItemCallback;

public LanguagesAdapter(@NonNull Context context, @Nullable LanguageItemCallback clickCallback, boolean isPreferred) {
mMainExecutor = ((VRBrowserApplication)context.getApplicationContext()).getExecutors().mainThread();
mLanguageItemCallback = clickCallback;
mIsPreferred = isPreferred;

Expand Down Expand Up @@ -67,7 +70,7 @@ public void addItem(Language language) {
notifyItemInserted(mLanguagesList.indexOf(language));
// This shouldn't be necessary but for some reason the last list item is not refreshed
// if we don't do a full refresh. Might be another RecyclerView bug.
ThreadUtils.postToUiThread(() -> notifyDataSetChanged());
mMainExecutor.execute(this::notifyDataSetChanged);
}

public void addItemAlphabetical(Language language) {
Expand Down
Expand Up @@ -29,7 +29,6 @@
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 Expand Up @@ -150,11 +149,11 @@ public void setCurvedTooltip(boolean aEnabled) {
public boolean onHoverEvent(MotionEvent event) {
if (getTooltipText() != null) {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
ThreadUtils.postDelayedToUiThread(mShowTooltipRunnable, mTooltipDelay);
postDelayed(mShowTooltipRunnable, mTooltipDelay);

} else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
ThreadUtils.removeCallbacksFromUiThread(mShowTooltipRunnable);
ThreadUtils.postToUiThread(mHideTooltipRunnable);
removeCallbacks(mShowTooltipRunnable);
post(mHideTooltipRunnable);
}
}

Expand Down
Expand Up @@ -10,9 +10,9 @@
import androidx.annotation.StringRes;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserApplication;
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 Expand Up @@ -166,7 +166,7 @@ public static void show(int notificationId, @NonNull Notification notification)
}

Runnable hideTask = () -> hide(notificationId);
ThreadUtils.postDelayedToUiThread(hideTask, notification.mDuration);
notification.mView.postDelayed(hideTask, notification.mDuration);

mData.put(notificationId, new NotificationData(notificationView, notification, hideTask));
}
Expand All @@ -192,7 +192,7 @@ public static void hideAll() {
}

private static void hideNotification(@NonNull NotificationData data) {
ThreadUtils.removeCallbacksFromUiThread(data.mHideTask);
data.mNotificationView.removeCallbacks(data.mHideTask);

data.mNotificationView.hide(UIWidget.REMOVE_WIDGET);

Expand Down
Expand Up @@ -33,7 +33,6 @@
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 Expand Up @@ -298,7 +297,7 @@ public void show(@ShowFlags int aShowFlags) {
if (index == PromptDialogWidget.POSITIVE) {
SettingsStore.getInstance(getContext()).setSpeechDataCollectionEnabled(true);
}
ThreadUtils.postToUiThread(() -> show(aShowFlags));
post(() -> show(aShowFlags));
},
() -> {
mWidgetManager.openNewTabForeground(getResources().getString(R.string.private_policy_url));
Expand Down
Expand Up @@ -21,10 +21,6 @@
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;

public class ContentLanguageOptionsView extends SettingsView {

Expand Down Expand Up @@ -126,7 +122,7 @@ private void saveCurrentLanguages() {
}

private void refreshLanguages() {
ThreadUtils.postToUiThread(() -> {
post(() -> {
mPreferredAdapter.setLanguageList(LocaleUtils.getPreferredLanguages(getContext()));
mAvailableAdapter.setLanguageList(LocaleUtils.getAvailableLanguages(getContext()));
});
Expand Down
Expand Up @@ -121,7 +121,7 @@ public static void scaleIn(@NonNull View aView, long duration, long delay, final
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (aCallback != null)
ThreadUtils.postToUiThread(aCallback);
aView.post(aCallback);
}
}).setUpdateListener(animation -> aView.invalidate());
}
Expand All @@ -134,15 +134,15 @@ public static void scaleOut(@NonNull View aView, long duration, long delay, fina
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (aCallback != null)
ThreadUtils.postToUiThread(aCallback);
aView.post(aCallback);
}
}).setUpdateListener(animation -> aView.invalidate());
}

public static void scaleTo(@NonNull View aView, float scaleX, float scaleY, long duration, long delay, final Runnable aCallback) {
if (aView.getScaleX() == scaleX && aView.getScaleY() == scaleY) {
if (aCallback != null) {
ThreadUtils.postToUiThread(aCallback);
aView.post(aCallback);
}
return;
}
Expand All @@ -151,7 +151,7 @@ public static void scaleTo(@NonNull View aView, float scaleX, float scaleY, long
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (aCallback != null) {
ThreadUtils.postToUiThread(aCallback);
aView.post(aCallback);
}
}
}).setUpdateListener(animation -> aView.invalidate());
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.mozilla.vrbrowser.BuildConfig;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserActivity;
import org.mozilla.vrbrowser.VRBrowserApplication;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static String createLogtag(@NonNull Class aClass) {
private static final String CRASH_STATS_URL = "https://crash-stats.mozilla.com/report/index/";

private static void sendCrashFiles(@NonNull Context context, @NonNull final String aDumpFile, @NonNull final String aExtraFile) {
ThreadUtils.postToBackgroundThread(() -> {
((VRBrowserApplication)context.getApplicationContext()).getExecutors().backgroundThread().post(() -> {
try {
GeckoResult<String> result = CrashReporter.sendCrashReport(context, new File(aDumpFile), new File(aExtraFile), context.getString(R.string.crash_app_name));

Expand Down
82 changes: 0 additions & 82 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/ThreadUtils.java

This file was deleted.

0 comments on commit a360c20

Please sign in to comment.