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 committed Nov 4, 2022
1 parent 8488739 commit c30f787
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.schabi.newpipe.player.gesture

import android.os.Handler
import android.os.Looper
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 All @@ -19,7 +19,6 @@ import org.schabi.newpipe.player.ui.VideoPlayerUi
abstract class BasePlayerGestureListener(
private val playerUi: VideoPlayerUi,
) : GestureDetector.SimpleOnGestureListener(), View.OnTouchListener {

protected val player: Player = playerUi.player
protected val binding: PlayerBinding = playerUi.binding

Expand All @@ -28,15 +27,11 @@ abstract class BasePlayerGestureListener(
return false
}

private fun onDoubleTap(
event: MotionEvent,
portion: DisplayPortion
) {
private fun onDoubleTap(event: MotionEvent, portion: DisplayPortion) {
if (DEBUG) {
Log.d(
TAG,
"onDoubleTap called with playerType = [" +
player.playerType + "], portion = [" + portion + "]"
"onDoubleTap called with playerType = [${player.playerType}], portion = [$portion]"
)
}
if (playerUi.isSomePopupMenuVisible) {
Expand Down Expand Up @@ -66,11 +61,7 @@ abstract class BasePlayerGestureListener(

open fun onScrollEnd(event: MotionEvent) {
if (DEBUG) {
Log.d(
TAG,
"onScrollEnd called with playerType = [" +
player.playerType + "]"
)
Log.d(TAG, "onScrollEnd called with playerType = [${player.playerType}]")
}
if (playerUi.isControlsVisible && player.currentState == Player.STATE_PLAYING) {
playerUi.hideControls(
Expand All @@ -85,10 +76,11 @@ abstract class BasePlayerGestureListener(
// ///////////////////////////////////////////////////////////////////

override fun onDown(e: MotionEvent): Boolean {
if (DEBUG)
if (DEBUG) {
Log.d(TAG, "onDown called with e = [$e]")
}

if (isDoubleTapping && isDoubleTapEnabled) {
if (isDoubleTapping) {
doubleTapControls?.onDoubleTapProgressDown(getDisplayPortion(e))
return true
}
Expand All @@ -107,8 +99,9 @@ abstract class BasePlayerGestureListener(
}

override fun onDoubleTap(e: MotionEvent): Boolean {
if (DEBUG)
if (DEBUG) {
Log.d(TAG, "onDoubleTap called with e = [$e]")
}

onDoubleTap(e, getDisplayPortion(e))
return true
Expand All @@ -120,51 +113,50 @@ abstract class BasePlayerGestureListener(

private var doubleTapControls: DoubleTapListener? = null

private val isDoubleTapEnabled: Boolean
get() = doubleTapDelay > 0

var isDoubleTapping = false
private set

fun doubleTapControls(listener: DoubleTapListener) = apply {
doubleTapControls = listener
}

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 val doubleTapHandler = Handler(player.context.mainLooper)

private fun startMultiDoubleTap(e: MotionEvent) {
if (!isDoubleTapping) {
if (DEBUG)
if (DEBUG) {
Log.d(TAG, "startMultiDoubleTap called with e = [$e]")
}

keepInDoubleTapMode()
doubleTapControls?.onDoubleTapStarted(getDisplayPortion(e))
}
}

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

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

isDoubleTapping = false
doubleTapControls?.onDoubleTapFinished()
}
}

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

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

Expand Down
26 changes: 14 additions & 12 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 All @@ -71,6 +72,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
Expand All @@ -91,6 +93,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 @@ -99,20 +105,17 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
private final Deleter mDeleter;
private int mLayout;
private final DownloadManager.MissionIterator mIterator;
private final ArrayList<ViewHolderItem> mPendingDownloadsItems = new ArrayList<>();
private final List<ViewHolderItem> mPendingDownloadsItems = new ArrayList<>();
private final Handler mHandler;
private MenuItem mClear;
private MenuItem mStartButton;
private MenuItem mPauseButton;
private final View mEmptyMessage;
private RecoverHelper mRecover;
private final View mView;
private final ArrayList<Mission> mHidden;
private final List<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 @@ -159,7 +162,7 @@ public void onViewRecycled(@NonNull ViewHolder view) {

if (h.item.mission instanceof DownloadMission) {
mPendingDownloadsItems.remove(h);
if (mPendingDownloadsItems.size() < 1) {
if (mPendingDownloadsItems.isEmpty()) {
checkMasterButtonsVisibility();
}
}
Expand Down Expand Up @@ -595,12 +598,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 +789,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 +819,7 @@ private void updater() {
updateProgress(h);
}

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

private boolean isNotFinite(double value) {
Expand Down
Loading

0 comments on commit c30f787

Please sign in to comment.