Skip to content

Commit 9514316

Browse files
Isira-SeneviratneStypox
authored andcommitted
Remove Runnable variables for Handlers.
1 parent 1bb166a commit 9514316

3 files changed

Lines changed: 37 additions & 37 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/gesture/BasePlayerGestureListener.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.util.Log
66
import android.view.GestureDetector
77
import android.view.MotionEvent
88
import android.view.View
9+
import androidx.core.os.postDelayed
910
import org.schabi.newpipe.databinding.PlayerBinding
1011
import org.schabi.newpipe.player.Player
1112
import org.schabi.newpipe.player.ui.VideoPlayerUi
@@ -132,13 +133,6 @@ abstract class BasePlayerGestureListener(
132133

133134
private var doubleTapDelay = DOUBLE_TAP_DELAY
134135
private val doubleTapHandler: Handler = Handler(Looper.getMainLooper())
135-
private val doubleTapRunnable = Runnable {
136-
if (DEBUG)
137-
Log.d(TAG, "doubleTapRunnable called")
138-
139-
isDoubleTapping = false
140-
doubleTapControls?.onDoubleTapFinished()
141-
}
142136

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

157151
isDoubleTapping = true
158-
doubleTapHandler.removeCallbacks(doubleTapRunnable)
159-
doubleTapHandler.postDelayed(doubleTapRunnable, doubleTapDelay)
152+
doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
153+
doubleTapHandler.postDelayed(DOUBLE_TAP_DELAY, DOUBLE_TAP) {
154+
if (DEBUG) {
155+
Log.d(TAG, "doubleTapRunnable called")
156+
}
157+
158+
isDoubleTapping = false
159+
doubleTapControls?.onDoubleTapFinished()
160+
}
160161
}
161162

162163
fun endMultiDoubleTap() {
163164
if (DEBUG)
164165
Log.d(TAG, "endMultiDoubleTap called")
165166

166167
isDoubleTapping = false
167-
doubleTapHandler.removeCallbacks(doubleTapRunnable)
168+
doubleTapHandler.removeCallbacksAndMessages(DOUBLE_TAP)
168169
doubleTapControls?.onDoubleTapFinished()
169170
}
170171

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

185+
private const val DOUBLE_TAP = "doubleTap"
184186
private const val DOUBLE_TAP_DELAY = 550L
185187
}
186188
}

app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import androidx.core.app.NotificationCompat;
4949
import androidx.core.content.ContextCompat;
5050
import androidx.core.content.FileProvider;
51+
import androidx.core.os.HandlerCompat;
5152
import androidx.recyclerview.widget.DiffUtil;
5253
import androidx.recyclerview.widget.RecyclerView;
5354
import androidx.recyclerview.widget.RecyclerView.Adapter;
@@ -91,6 +92,10 @@ public class MissionAdapter extends Adapter<ViewHolder> implements Handler.Callb
9192
private static final String UNDEFINED_PROGRESS = "--.-%";
9293
private static final String DEFAULT_MIME_TYPE = "*/*";
9394
private static final String UNDEFINED_ETA = "--:--";
95+
96+
private static final String UPDATER = "updater";
97+
private static final String DELETE = "deleteFinishedDownloads";
98+
9499
private static final int HASH_NOTIFICATION_ID = 123790;
95100

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

113-
private final Runnable rUpdater = this::updater;
114-
private final Runnable rDelete = this::deleteFinishedDownloads;
115-
116118
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
117119

118120
public MissionAdapter(Context context, @NonNull DownloadManager downloadManager, View emptyMessage, View root) {
@@ -595,12 +597,12 @@ public void clearFinishedDownloads(boolean delete) {
595597
i.remove();
596598
}
597599
applyChanges();
598-
mHandler.removeCallbacks(rDelete);
600+
mHandler.removeCallbacksAndMessages(DELETE);
599601
});
600602
mSnackbar.setActionTextColor(Color.YELLOW);
601603
mSnackbar.show();
602604

603-
mHandler.postDelayed(rDelete, 5000);
605+
HandlerCompat.postDelayed(mHandler, this::deleteFinishedDownloads, DELETE, 5000);
604606
} else if (!delete) {
605607
mDownloadManager.forgetFinishedDownloads();
606608
applyChanges();
@@ -786,15 +788,14 @@ public void onDestroy() {
786788

787789
public void onResume() {
788790
mDeleter.resume();
789-
mHandler.post(rUpdater);
791+
HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 0);
790792
}
791793

792794
public void onPaused() {
793795
mDeleter.pause();
794-
mHandler.removeCallbacks(rUpdater);
796+
mHandler.removeCallbacksAndMessages(UPDATER);
795797
}
796798

797-
798799
public void recoverMission(DownloadMission mission) {
799800
ViewHolderItem h = getViewHolder(mission);
800801
if (h == null) return;
@@ -817,7 +818,7 @@ private void updater() {
817818
updateProgress(h);
818819
}
819820

820-
mHandler.postDelayed(rUpdater, 1000);
821+
HandlerCompat.postDelayed(mHandler, this::updater, UPDATER, 1000);
821822
}
822823

823824
private boolean isNotFinite(double value) {

app/src/main/java/us/shandian/giga/ui/common/Deleter.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.os.Handler;
77
import android.view.View;
88

9+
import androidx.core.os.HandlerCompat;
10+
911
import com.google.android.material.snackbar.Snackbar;
1012

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

2123
public class Deleter {
24+
private static final String COMMIT = "commit";
25+
private static final String NEXT = "next";
26+
private static final String SHOW = "show";
27+
2228
private static final int TIMEOUT = 5000;// ms
2329
private static final int DELAY = 350;// ms
2430
private static final int DELAY_RESUME = 400;// ms
@@ -34,10 +40,6 @@ public class Deleter {
3440
private final Handler mHandler;
3541
private final View mView;
3642

37-
private final Runnable rShow;
38-
private final Runnable rNext;
39-
private final Runnable rCommit;
40-
4143
public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIterator i, Handler h) {
4244
mView = v;
4345
mContext = c;
@@ -46,21 +48,15 @@ public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIt
4648
mIterator = i;
4749
mHandler = h;
4850

49-
// use variables to know the reference of the lambdas
50-
rShow = this::show;
51-
rNext = this::next;
52-
rCommit = this::commit;
53-
5451
items = new ArrayList<>(2);
5552
}
5653

5754
public void append(Mission item) {
58-
5955
/* If a mission is removed from the list while the Snackbar for a previously
6056
* removed item is still showing, commit the action for the previous item
6157
* immediately. This prevents Snackbars from stacking up in reverse order.
6258
*/
63-
mHandler.removeCallbacks(rCommit);
59+
mHandler.removeCallbacksAndMessages(COMMIT);
6460
commit();
6561

6662
mIterator.hide(item);
@@ -82,7 +78,7 @@ private void show() {
8278
pause();
8379
running = true;
8480

85-
mHandler.postDelayed(rNext, DELAY);
81+
HandlerCompat.postDelayed(mHandler, this::next, NEXT, DELAY);
8682
}
8783

8884
private void next() {
@@ -95,7 +91,7 @@ private void next() {
9591
snackbar.setActionTextColor(Color.YELLOW);
9692
snackbar.show();
9793

98-
mHandler.postDelayed(rCommit, TIMEOUT);
94+
HandlerCompat.postDelayed(mHandler, this::commit, COMMIT, TIMEOUT);
9995
}
10096

10197
private void commit() {
@@ -124,15 +120,16 @@ private void commit() {
124120

125121
public void pause() {
126122
running = false;
127-
mHandler.removeCallbacks(rNext);
128-
mHandler.removeCallbacks(rShow);
129-
mHandler.removeCallbacks(rCommit);
123+
mHandler.removeCallbacksAndMessages(NEXT);
124+
mHandler.removeCallbacksAndMessages(SHOW);
125+
mHandler.removeCallbacksAndMessages(COMMIT);
130126
if (snackbar != null) snackbar.dismiss();
131127
}
132128

133129
public void resume() {
134-
if (running) return;
135-
mHandler.postDelayed(rShow, DELAY_RESUME);
130+
if (!running) {
131+
HandlerCompat.postDelayed(mHandler, this::show, SHOW, DELAY_RESUME);
132+
}
136133
}
137134

138135
public void dispose() {

0 commit comments

Comments
 (0)