Skip to content

Commit

Permalink
Remove Runnable variables for Handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne authored and Stypox committed Jan 12, 2023
1 parent 1bb166a commit 9514316
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import androidx.core.os.postDelayed
import org.schabi.newpipe.databinding.PlayerBinding
import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.ui.VideoPlayerUi
Expand Down Expand Up @@ -132,13 +133,6 @@ abstract class BasePlayerGestureListener(

private var doubleTapDelay = DOUBLE_TAP_DELAY
private val doubleTapHandler: Handler = Handler(Looper.getMainLooper())
private val doubleTapRunnable = Runnable {
if (DEBUG)
Log.d(TAG, "doubleTapRunnable called")

isDoubleTapping = false
doubleTapControls?.onDoubleTapFinished()
}

private fun startMultiDoubleTap(e: MotionEvent) {
if (!isDoubleTapping) {
Expand All @@ -155,16 +149,23 @@ abstract class BasePlayerGestureListener(
Log.d(TAG, "keepInDoubleTapMode called")

isDoubleTapping = true
doubleTapHandler.removeCallbacks(doubleTapRunnable)
doubleTapHandler.postDelayed(doubleTapRunnable, doubleTapDelay)
doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
doubleTapHandler.postDelayed(DOUBLE_TAP_DELAY, DOUBLE_TAP) {
if (DEBUG) {
Log.d(TAG, "doubleTapRunnable called")
}

isDoubleTapping = false
doubleTapControls?.onDoubleTapFinished()
}
}

fun endMultiDoubleTap() {
if (DEBUG)
Log.d(TAG, "endMultiDoubleTap called")

isDoubleTapping = false
doubleTapHandler.removeCallbacks(doubleTapRunnable)
doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
doubleTapControls?.onDoubleTapFinished()
}

Expand All @@ -181,6 +182,7 @@ abstract class BasePlayerGestureListener(
private const val TAG = "BasePlayerGestListener"
private val DEBUG = Player.DEBUG

private const val DOUBLE_TAP = "doubleTap"
private const val DOUBLE_TAP_DELAY = 550L
}
}
19 changes: 10 additions & 9 deletions app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.core.os.HandlerCompat;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
Expand Down Expand Up @@ -91,6 +92,10 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
private static final String UNDEFINED_PROGRESS = "--.-%";
private static final String DEFAULT_MIME_TYPE = "*/*";
private static final String UNDEFINED_ETA = "--:--";

private static final String UPDATER = "updater";
private static final String DELETE = "deleteFinishedDownloads";

private static final int HASH_NOTIFICATION_ID = 123790;

private final Context mContext;
Expand All @@ -110,9 +115,6 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
private final ArrayList<Mission> mHidden;
private Snackbar mSnackbar;

private final Runnable rUpdater = this::updater;
private final Runnable rDelete = this::deleteFinishedDownloads;

private final CompositeDisposable compositeDisposable = new CompositeDisposable();

public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) {
Expand Down Expand Up @@ -595,12 +597,12 @@ public void clearFinishedDownloads(boolean delete) {
i.remove();
}
applyChanges();
mHandler.removeCallbacks(rDelete);
mHandler.removeCallbacksAndMessages(DELETE);
});
mSnackbar.setActionTextColor(Color.YELLOW);
mSnackbar.show();

mHandler.postDelayed(rDelete, 5000);
HandlerCompat.postDelayed(mHandler, this::deleteFinishedDownloads, DELETE, 5000);
} else if (!delete) {
mDownloadManager.forgetFinishedDownloads();
applyChanges();
Expand Down Expand Up @@ -786,15 +788,14 @@ public void onDestroy() {

public void onResume() {
mDeleter.resume();
mHandler.post(rUpdater);
HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 0);
}

public void onPaused() {
mDeleter.pause();
mHandler.removeCallbacks(rUpdater);
mHandler.removeCallbacksAndMessages(UPDATER);
}


public void recoverMission(DownloadMission mission) {
ViewHolderItem h = getViewHolder(mission);
if (h == null) return;
Expand All @@ -817,7 +818,7 @@ private void updater() {
updateProgress(h);
}

mHandler.postDelayed(rUpdater, 1000);
HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 1000);
}

private boolean isNotFinite(double value) {
Expand Down
33 changes: 15 additions & 18 deletions app/src/main/java/us/shandian/giga/ui/common/Deleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.Handler;
import android.view.View;

import androidx.core.os.HandlerCompat;

import com.google.android.material.snackbar.Snackbar;

import org.schabi.newpipe.R;
Expand All @@ -19,6 +21,10 @@
import us.shandian.giga.ui.adapter.MissionAdapter;

public class Deleter {
private static final String COMMIT = "commit";
private static final String NEXT = "next";
private static final String SHOW = "show";

private static final int TIMEOUT = 5000;// ms
private static final int DELAY = 350;// ms
private static final int DELAY_RESUME = 400;// ms
Expand All @@ -34,10 +40,6 @@ public class Deleter {
private final Handler mHandler;
private final View mView;

private final Runnable rShow;
private final Runnable rNext;
private final Runnable rCommit;

public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIterator i, Handler h) {
mView = v;
mContext = c;
Expand All @@ -46,21 +48,15 @@ public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIt
mIterator = i;
mHandler = h;

// use variables to know the reference of the lambdas
rShow = this::show;
rNext = this::next;
rCommit = this::commit;

items = new ArrayList<>(2);
}

public void append(Mission item) {

/* If a mission is removed from the list while the Snackbar for a previously
* removed item is still showing, commit the action for the previous item
* immediately. This prevents Snackbars from stacking up in reverse order.
*/
mHandler.removeCallbacks(rCommit);
mHandler.removeCallbacksAndMessages(COMMIT);
commit();

mIterator.hide(item);
Expand All @@ -82,7 +78,7 @@ private void show() {
pause();
running = true;

mHandler.postDelayed(rNext, DELAY);
HandlerCompat.postDelayed(mHandler, this::next, NEXT, DELAY);
}

private void next() {
Expand All @@ -95,7 +91,7 @@ private void next() {
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();

mHandler.postDelayed(rCommit, TIMEOUT);
HandlerCompat.postDelayed(mHandler, this::commit, COMMIT, TIMEOUT);
}

private void commit() {
Expand Down Expand Up @@ -124,15 +120,16 @@ private void commit() {

public void pause() {
running = false;
mHandler.removeCallbacks(rNext);
mHandler.removeCallbacks(rShow);
mHandler.removeCallbacks(rCommit);
mHandler.removeCallbacksAndMessages(NEXT);
mHandler.removeCallbacksAndMessages(SHOW);
mHandler.removeCallbacksAndMessages(COMMIT);
if (snackbar != null) snackbar.dismiss();
}

public void resume() {
if (running) return;
mHandler.postDelayed(rShow, DELAY_RESUME);
if (!running) {
HandlerCompat.postDelayed(mHandler, this::show, SHOW, DELAY_RESUME);
}
}

public void dispose() {
Expand Down

0 comments on commit 9514316

Please sign in to comment.