1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

Remove Runnable variables for Handlers.

This commit is contained in:
Isira Seneviratne 2022-08-27 11:55:31 +05:30 committed by Stypox
parent 1bb166a9e8
commit 9514316be3
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 37 additions and 37 deletions

View File

@ -6,6 +6,7 @@ import android.util.Log
import android.view.GestureDetector import android.view.GestureDetector
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import androidx.core.os.postDelayed
import org.schabi.newpipe.databinding.PlayerBinding import org.schabi.newpipe.databinding.PlayerBinding
import org.schabi.newpipe.player.Player import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.ui.VideoPlayerUi import org.schabi.newpipe.player.ui.VideoPlayerUi
@ -132,13 +133,6 @@ abstract class BasePlayerGestureListener(
private var doubleTapDelay = DOUBLE_TAP_DELAY private var doubleTapDelay = DOUBLE_TAP_DELAY
private val doubleTapHandler: Handler = Handler(Looper.getMainLooper()) 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) { private fun startMultiDoubleTap(e: MotionEvent) {
if (!isDoubleTapping) { if (!isDoubleTapping) {
@ -155,8 +149,15 @@ abstract class BasePlayerGestureListener(
Log.d(TAG, "keepInDoubleTapMode called") Log.d(TAG, "keepInDoubleTapMode called")
isDoubleTapping = true isDoubleTapping = true
doubleTapHandler.removeCallbacks(doubleTapRunnable) doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
doubleTapHandler.postDelayed(doubleTapRunnable, doubleTapDelay) doubleTapHandler.postDelayed(DOUBLE_TAP_DELAY, DOUBLE_TAP) {
if (DEBUG) {
Log.d(TAG, "doubleTapRunnable called")
}
isDoubleTapping = false
doubleTapControls?.onDoubleTapFinished()
}
} }
fun endMultiDoubleTap() { fun endMultiDoubleTap() {
@ -164,7 +165,7 @@ abstract class BasePlayerGestureListener(
Log.d(TAG, "endMultiDoubleTap called") Log.d(TAG, "endMultiDoubleTap called")
isDoubleTapping = false isDoubleTapping = false
doubleTapHandler.removeCallbacks(doubleTapRunnable) doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
doubleTapControls?.onDoubleTapFinished() doubleTapControls?.onDoubleTapFinished()
} }
@ -181,6 +182,7 @@ abstract class BasePlayerGestureListener(
private const val TAG = "BasePlayerGestListener" private const val TAG = "BasePlayerGestListener"
private val DEBUG = Player.DEBUG private val DEBUG = Player.DEBUG
private const val DOUBLE_TAP = "doubleTap"
private const val DOUBLE_TAP_DELAY = 550L private const val DOUBLE_TAP_DELAY = 550L
} }
} }

View File

@ -48,6 +48,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.core.os.HandlerCompat;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter; import androidx.recyclerview.widget.RecyclerView.Adapter;
@ -91,6 +92,10 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
private static final String UNDEFINED_PROGRESS = "--.-%"; private static final String UNDEFINED_PROGRESS = "--.-%";
private static final String DEFAULT_MIME_TYPE = "*/*"; private static final String DEFAULT_MIME_TYPE = "*/*";
private static final String UNDEFINED_ETA = "--:--"; 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 static final int HASH_NOTIFICATION_ID = 123790;
private final Context mContext; private final Context mContext;
@ -110,9 +115,6 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
private final ArrayList<Mission> mHidden; private final ArrayList<Mission> mHidden;
private Snackbar mSnackbar; private Snackbar mSnackbar;
private final Runnable rUpdater = this::updater;
private final Runnable rDelete = this::deleteFinishedDownloads;
private final CompositeDisposable compositeDisposable = new CompositeDisposable(); private final CompositeDisposable compositeDisposable = new CompositeDisposable();
public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) { public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) {
@ -595,12 +597,12 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
i.remove(); i.remove();
} }
applyChanges(); applyChanges();
mHandler.removeCallbacks(rDelete); mHandler.removeCallbacksAndMessages(DELETE);
}); });
mSnackbar.setActionTextColor(Color.YELLOW); mSnackbar.setActionTextColor(Color.YELLOW);
mSnackbar.show(); mSnackbar.show();
mHandler.postDelayed(rDelete, 5000); HandlerCompat.postDelayed(mHandler, this::deleteFinishedDownloads, DELETE, 5000);
} else if (!delete) { } else if (!delete) {
mDownloadManager.forgetFinishedDownloads(); mDownloadManager.forgetFinishedDownloads();
applyChanges(); applyChanges();
@ -786,15 +788,14 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
public void onResume() { public void onResume() {
mDeleter.resume(); mDeleter.resume();
mHandler.post(rUpdater); HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 0);
} }
public void onPaused() { public void onPaused() {
mDeleter.pause(); mDeleter.pause();
mHandler.removeCallbacks(rUpdater); mHandler.removeCallbacksAndMessages(UPDATER);
} }
public void recoverMission(DownloadMission mission) { public void recoverMission(DownloadMission mission) {
ViewHolderItem h = getViewHolder(mission); ViewHolderItem h = getViewHolder(mission);
if (h == null) return; if (h == null) return;
@ -817,7 +818,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
updateProgress(h); updateProgress(h);
} }
mHandler.postDelayed(rUpdater, 1000); HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 1000);
} }
private boolean isNotFinite(double value) { private boolean isNotFinite(double value) {

View File

@ -6,6 +6,8 @@ import android.graphics.Color;
import android.os.Handler; import android.os.Handler;
import android.view.View; import android.view.View;
import androidx.core.os.HandlerCompat;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
@ -19,6 +21,10 @@ import us.shandian.giga.service.DownloadManager.MissionIterator;
import us.shandian.giga.ui.adapter.MissionAdapter; import us.shandian.giga.ui.adapter.MissionAdapter;
public class Deleter { 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 TIMEOUT = 5000;// ms
private static final int DELAY = 350;// ms private static final int DELAY = 350;// ms
private static final int DELAY_RESUME = 400;// ms private static final int DELAY_RESUME = 400;// ms
@ -34,10 +40,6 @@ public class Deleter {
private final Handler mHandler; private final Handler mHandler;
private final View mView; 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) { public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIterator i, Handler h) {
mView = v; mView = v;
mContext = c; mContext = c;
@ -46,21 +48,15 @@ public class Deleter {
mIterator = i; mIterator = i;
mHandler = h; mHandler = h;
// use variables to know the reference of the lambdas
rShow = this::show;
rNext = this::next;
rCommit = this::commit;
items = new ArrayList<>(2); items = new ArrayList<>(2);
} }
public void append(Mission item) { public void append(Mission item) {
/* If a mission is removed from the list while the Snackbar for a previously /* 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 * removed item is still showing, commit the action for the previous item
* immediately. This prevents Snackbars from stacking up in reverse order. * immediately. This prevents Snackbars from stacking up in reverse order.
*/ */
mHandler.removeCallbacks(rCommit); mHandler.removeCallbacksAndMessages(COMMIT);
commit(); commit();
mIterator.hide(item); mIterator.hide(item);
@ -82,7 +78,7 @@ public class Deleter {
pause(); pause();
running = true; running = true;
mHandler.postDelayed(rNext, DELAY); HandlerCompat.postDelayed(mHandler, this::next, NEXT, DELAY);
} }
private void next() { private void next() {
@ -95,7 +91,7 @@ public class Deleter {
snackbar.setActionTextColor(Color.YELLOW); snackbar.setActionTextColor(Color.YELLOW);
snackbar.show(); snackbar.show();
mHandler.postDelayed(rCommit, TIMEOUT); HandlerCompat.postDelayed(mHandler, this::commit, COMMIT, TIMEOUT);
} }
private void commit() { private void commit() {
@ -124,15 +120,16 @@ public class Deleter {
public void pause() { public void pause() {
running = false; running = false;
mHandler.removeCallbacks(rNext); mHandler.removeCallbacksAndMessages(NEXT);
mHandler.removeCallbacks(rShow); mHandler.removeCallbacksAndMessages(SHOW);
mHandler.removeCallbacks(rCommit); mHandler.removeCallbacksAndMessages(COMMIT);
if (snackbar != null) snackbar.dismiss(); if (snackbar != null) snackbar.dismiss();
} }
public void resume() { public void resume() {
if (running) return; if (!running) {
mHandler.postDelayed(rShow, DELAY_RESUME); HandlerCompat.postDelayed(mHandler, this::show, SHOW, DELAY_RESUME);
}
} }
public void dispose() { public void dispose() {