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.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
@ -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) {
@ -155,8 +149,15 @@ 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() {
@ -164,7 +165,7 @@ abstract class BasePlayerGestureListener(
Log.d(TAG, "endMultiDoubleTap called")
isDoubleTapping = false
doubleTapHandler.removeCallbacks(doubleTapRunnable)
doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
doubleTapControls?.onDoubleTapFinished()
}
@ -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
}
}

View File

@ -48,6 +48,7 @@ import androidx.appcompat.app.AlertDialog;
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;
@ -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;
@ -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) {
@ -595,12 +597,12 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
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();
@ -786,15 +788,14 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
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;
@ -817,7 +818,7 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
updateProgress(h);
}
mHandler.postDelayed(rUpdater, 1000);
HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 1000);
}
private boolean isNotFinite(double value) {

View File

@ -6,6 +6,8 @@ import android.graphics.Color;
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;
@ -19,6 +21,10 @@ import us.shandian.giga.service.DownloadManager.MissionIterator;
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
@ -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;
@ -46,21 +48,15 @@ public class Deleter {
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);
@ -82,7 +78,7 @@ public class Deleter {
pause();
running = true;
mHandler.postDelayed(rNext, DELAY);
HandlerCompat.postDelayed(mHandler, this::next, NEXT, DELAY);
}
private void next() {
@ -95,7 +91,7 @@ public class Deleter {
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
mHandler.postDelayed(rCommit, TIMEOUT);
HandlerCompat.postDelayed(mHandler, this::commit, COMMIT, TIMEOUT);
}
private void commit() {
@ -124,15 +120,16 @@ public class Deleter {
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() {