Skip to content

Commit 344c33d

Browse files
authored
Merge pull request TeamNewPipe#8631 from Isira-Seneviratne/Use_collection_factories
Use Java 9 collection factories.
2 parents 1594716 + c5b970c commit 344c33d

20 files changed

Lines changed: 113 additions & 165 deletions

File tree

app/src/main/java/com/google/android/material/appbar/FlingBehavior.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.schabi.newpipe.R;
1515

1616
import java.lang.reflect.Field;
17-
import java.util.Arrays;
1817
import java.util.List;
1918

2019
// See https://stackoverflow.com/questions/56849221#57997489
@@ -27,7 +26,7 @@ public FlingBehavior(final Context context, final AttributeSet attrs) {
2726

2827
private boolean allowScroll = true;
2928
private final Rect globalRect = new Rect();
30-
private final List<Integer> skipInterceptionOfElements = Arrays.asList(
29+
private final List<Integer> skipInterceptionOfElements = List.of(
3130
R.id.itemsListPanel, R.id.playbackSeekBar,
3231
R.id.playPauseButton, R.id.playPreviousButton, R.id.playNextButton);
3332

@@ -67,7 +66,7 @@ public boolean onRequestChildRectangleOnScreen(
6766
public boolean onInterceptTouchEvent(@NonNull final CoordinatorLayout parent,
6867
@NonNull final AppBarLayout child,
6968
@NonNull final MotionEvent ev) {
70-
for (final Integer element : skipInterceptionOfElements) {
69+
for (final int element : skipInterceptionOfElements) {
7170
final View view = child.findViewById(element);
7271
if (view != null) {
7372
final boolean visible = view.getGlobalVisibleRect(globalRect);

app/src/main/java/org/schabi/newpipe/App.java

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
import java.io.IOException;
2828
import java.io.InterruptedIOException;
2929
import java.net.SocketException;
30-
import java.util.ArrayList;
31-
import java.util.Collections;
3230
import java.util.List;
31+
import java.util.Objects;
3332

3433
import io.reactivex.rxjava3.exceptions.CompositeException;
3534
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
@@ -140,7 +139,7 @@ public void accept(@NonNull final Throwable throwable) {
140139
if (throwable instanceof UndeliverableException) {
141140
// As UndeliverableException is a wrapper,
142141
// get the cause of it to get the "real" exception
143-
actualThrowable = throwable.getCause();
142+
actualThrowable = Objects.requireNonNull(throwable.getCause());
144143
} else {
145144
actualThrowable = throwable;
146145
}
@@ -149,7 +148,7 @@ public void accept(@NonNull final Throwable throwable) {
149148
if (actualThrowable instanceof CompositeException) {
150149
errors = ((CompositeException) actualThrowable).getExceptions();
151150
} else {
152-
errors = Collections.singletonList(actualThrowable);
151+
errors = List.of(actualThrowable);
153152
}
154153

155154
for (final Throwable error : errors) {
@@ -213,41 +212,37 @@ protected void initACRA() {
213212
private void initNotificationChannels() {
214213
// Keep the importance below DEFAULT to avoid making noise on every notification update for
215214
// the main and update channels
216-
final List<NotificationChannelCompat> notificationChannelCompats = new ArrayList<>();
217-
notificationChannelCompats.add(new NotificationChannelCompat
218-
.Builder(getString(R.string.notification_channel_id),
215+
final List<NotificationChannelCompat> notificationChannelCompats = List.of(
216+
new NotificationChannelCompat.Builder(getString(R.string.notification_channel_id),
219217
NotificationManagerCompat.IMPORTANCE_LOW)
220-
.setName(getString(R.string.notification_channel_name))
221-
.setDescription(getString(R.string.notification_channel_description))
222-
.build());
223-
224-
notificationChannelCompats.add(new NotificationChannelCompat
225-
.Builder(getString(R.string.app_update_notification_channel_id),
218+
.setName(getString(R.string.notification_channel_name))
219+
.setDescription(getString(R.string.notification_channel_description))
220+
.build(),
221+
new NotificationChannelCompat
222+
.Builder(getString(R.string.app_update_notification_channel_id),
226223
NotificationManagerCompat.IMPORTANCE_LOW)
227-
.setName(getString(R.string.app_update_notification_channel_name))
228-
.setDescription(getString(R.string.app_update_notification_channel_description))
229-
.build());
230-
231-
notificationChannelCompats.add(new NotificationChannelCompat
232-
.Builder(getString(R.string.hash_channel_id),
224+
.setName(getString(R.string.app_update_notification_channel_name))
225+
.setDescription(
226+
getString(R.string.app_update_notification_channel_description))
227+
.build(),
228+
new NotificationChannelCompat.Builder(getString(R.string.hash_channel_id),
233229
NotificationManagerCompat.IMPORTANCE_HIGH)
234-
.setName(getString(R.string.hash_channel_name))
235-
.setDescription(getString(R.string.hash_channel_description))
236-
.build());
237-
238-
notificationChannelCompats.add(new NotificationChannelCompat
239-
.Builder(getString(R.string.error_report_channel_id),
230+
.setName(getString(R.string.hash_channel_name))
231+
.setDescription(getString(R.string.hash_channel_description))
232+
.build(),
233+
new NotificationChannelCompat.Builder(getString(R.string.error_report_channel_id),
240234
NotificationManagerCompat.IMPORTANCE_LOW)
241-
.setName(getString(R.string.error_report_channel_name))
242-
.setDescription(getString(R.string.error_report_channel_description))
243-
.build());
244-
245-
notificationChannelCompats.add(new NotificationChannelCompat
246-
.Builder(getString(R.string.streams_notification_channel_id),
247-
NotificationManagerCompat.IMPORTANCE_DEFAULT)
248-
.setName(getString(R.string.streams_notification_channel_name))
249-
.setDescription(getString(R.string.streams_notification_channel_description))
250-
.build());
235+
.setName(getString(R.string.error_report_channel_name))
236+
.setDescription(getString(R.string.error_report_channel_description))
237+
.build(),
238+
new NotificationChannelCompat
239+
.Builder(getString(R.string.streams_notification_channel_id),
240+
NotificationManagerCompat.IMPORTANCE_DEFAULT)
241+
.setName(getString(R.string.streams_notification_channel_name))
242+
.setDescription(
243+
getString(R.string.streams_notification_channel_description))
244+
.build()
245+
);
251246

252247
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
253248
notificationManager.createNotificationChannelsCompat(notificationChannelCompats);

app/src/main/java/org/schabi/newpipe/QueueItemMenuUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.schabi.newpipe.util.NavigationHelper;
1717
import org.schabi.newpipe.util.SparseItemUtil;
1818

19-
import java.util.Collections;
19+
import java.util.List;
2020

2121
public final class QueueItemMenuUtil {
2222
private QueueItemMenuUtil() {
@@ -53,7 +53,7 @@ public static void openPopupMenu(final PlayQueue playQueue,
5353
case R.id.menu_item_append_playlist:
5454
PlaylistDialog.createCorrespondingDialog(
5555
context,
56-
Collections.singletonList(new StreamEntity(item)),
56+
List.of(new StreamEntity(item)),
5757
dialog -> dialog.show(
5858
fragmentManager,
5959
"QueueItemMenuUtil@append_playlist"

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
import java.io.Serializable;
8282
import java.util.ArrayList;
8383
import java.util.Arrays;
84-
import java.util.Collections;
8584
import java.util.List;
8685

8786
import icepick.Icepick;
@@ -649,7 +648,7 @@ private void openAddToPlaylistDialog() {
649648
.subscribe(
650649
info -> PlaylistDialog.createCorrespondingDialog(
651650
getThemeWrapperContext(),
652-
Collections.singletonList(new StreamEntity(info)),
651+
List.of(new StreamEntity(info)),
653652
playlistDialog -> {
654653
playlistDialog.setOnDismissListener(dialog -> finish());
655654

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
import org.schabi.newpipe.util.external_communication.ShareUtils;
115115

116116
import java.util.ArrayList;
117-
import java.util.Collections;
118117
import java.util.Iterator;
119118
import java.util.LinkedList;
120119
import java.util.List;
@@ -453,7 +452,7 @@ public void onClick(final View v) {
453452
disposables.add(
454453
PlaylistDialog.createCorrespondingDialog(
455454
getContext(),
456-
Collections.singletonList(new StreamEntity(currentInfo)),
455+
List.of(new StreamEntity(currentInfo)),
457456
dialog -> dialog.show(getFM(), TAG)
458457
)
459458
);

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailPlayerCrasher.java

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import android.content.Context;
88
import android.util.Log;
9+
import android.util.Pair;
910
import android.view.ContextThemeWrapper;
1011
import android.view.LayoutInflater;
1112
import android.view.ViewGroup;
@@ -28,9 +29,7 @@
2829
import org.schabi.newpipe.util.ThemeHelper;
2930

3031
import java.io.IOException;
31-
import java.util.Collections;
32-
import java.util.LinkedHashMap;
33-
import java.util.Map;
32+
import java.util.List;
3433
import java.util.function.Supplier;
3534

3635
/**
@@ -43,50 +42,34 @@ public final class VideoDetailPlayerCrasher {
4342
// https://stackoverflow.com/a/54744028
4443
private static final String TAG = "VideoDetPlayerCrasher";
4544

46-
private static final Map<String, Supplier<ExoPlaybackException>> AVAILABLE_EXCEPTION_TYPES =
47-
getExceptionTypes();
45+
private static final String DEFAULT_MSG = "Dummy";
46+
47+
private static final List<Pair<String, Supplier<ExoPlaybackException>>>
48+
AVAILABLE_EXCEPTION_TYPES = List.of(
49+
new Pair<>("Source", () -> ExoPlaybackException.createForSource(
50+
new IOException(DEFAULT_MSG),
51+
ERROR_CODE_BEHIND_LIVE_WINDOW
52+
)),
53+
new Pair<>("Renderer", () -> ExoPlaybackException.createForRenderer(
54+
new Exception(DEFAULT_MSG),
55+
"Dummy renderer",
56+
0,
57+
null,
58+
C.FORMAT_HANDLED,
59+
/*isRecoverable=*/false,
60+
ERROR_CODE_DECODING_FAILED
61+
)),
62+
new Pair<>("Unexpected", () -> ExoPlaybackException.createForUnexpected(
63+
new RuntimeException(DEFAULT_MSG),
64+
ERROR_CODE_UNSPECIFIED
65+
)),
66+
new Pair<>("Remote", () -> ExoPlaybackException.createForRemote(DEFAULT_MSG))
67+
);
4868

4969
private VideoDetailPlayerCrasher() {
5070
// No impls
5171
}
5272

53-
private static Map<String, Supplier<ExoPlaybackException>> getExceptionTypes() {
54-
final String defaultMsg = "Dummy";
55-
final Map<String, Supplier<ExoPlaybackException>> exceptionTypes = new LinkedHashMap<>();
56-
exceptionTypes.put(
57-
"Source",
58-
() -> ExoPlaybackException.createForSource(
59-
new IOException(defaultMsg),
60-
ERROR_CODE_BEHIND_LIVE_WINDOW
61-
)
62-
);
63-
exceptionTypes.put(
64-
"Renderer",
65-
() -> ExoPlaybackException.createForRenderer(
66-
new Exception(defaultMsg),
67-
"Dummy renderer",
68-
0,
69-
null,
70-
C.FORMAT_HANDLED,
71-
/*isRecoverable=*/false,
72-
ERROR_CODE_DECODING_FAILED
73-
)
74-
);
75-
exceptionTypes.put(
76-
"Unexpected",
77-
() -> ExoPlaybackException.createForUnexpected(
78-
new RuntimeException(defaultMsg),
79-
ERROR_CODE_UNSPECIFIED
80-
)
81-
);
82-
exceptionTypes.put(
83-
"Remote",
84-
() -> ExoPlaybackException.createForRemote(defaultMsg)
85-
);
86-
87-
return Collections.unmodifiableMap(exceptionTypes);
88-
}
89-
9073
private static Context getThemeWrapperContext(final Context context) {
9174
return new ContextThemeWrapper(
9275
context,
@@ -121,10 +104,9 @@ public static void onCrashThePlayer(
121104
.setNegativeButton(R.string.cancel, null)
122105
.create();
123106

124-
for (final Map.Entry<String, Supplier<ExoPlaybackException>> entry
125-
: AVAILABLE_EXCEPTION_TYPES.entrySet()) {
107+
for (final Pair<String, Supplier<ExoPlaybackException>> entry : AVAILABLE_EXCEPTION_TYPES) {
126108
final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater).getRoot();
127-
radioButton.setText(entry.getKey());
109+
radioButton.setText(entry.first);
128110
radioButton.setChecked(false);
129111
radioButton.setLayoutParams(
130112
new RadioGroup.LayoutParams(
@@ -133,7 +115,7 @@ public static void onCrashThePlayer(
133115
)
134116
);
135117
radioButton.setOnClickListener(v -> {
136-
tryCrashPlayerWith(player, entry.getValue().get());
118+
tryCrashPlayerWith(player, entry.second.get());
137119
alertDialog.cancel();
138120
});
139121
binding.list.addView(radioButton);

app/src/main/java/org/schabi/newpipe/info_list/dialog/StreamDialogDefaultEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.schabi.newpipe.util.external_communication.KoreUtils;
2121
import org.schabi.newpipe.util.external_communication.ShareUtils;
2222

23-
import java.util.Collections;
23+
import java.util.List;
2424

2525
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
2626

@@ -89,7 +89,7 @@ public enum StreamDialogDefaultEntry {
8989
APPEND_PLAYLIST(R.string.add_to_playlist, (fragment, item) ->
9090
PlaylistDialog.createCorrespondingDialog(
9191
fragment.getContext(),
92-
Collections.singletonList(new StreamEntity(item)),
92+
List.of(new StreamEntity(item)),
9393
dialog -> dialog.show(
9494
fragment.getParentFragmentManager(),
9595
"StreamDialogEntry@"

app/src/main/java/org/schabi/newpipe/player/gesture/CustomBottomSheetBehavior.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import org.schabi.newpipe.R;
1616

17-
import java.util.Arrays;
1817
import java.util.List;
1918

2019
public class CustomBottomSheetBehavior extends BottomSheetBehavior<FrameLayout> {
@@ -25,7 +24,7 @@ public CustomBottomSheetBehavior(final Context context, final AttributeSet attrs
2524

2625
Rect globalRect = new Rect();
2726
private boolean skippingInterception = false;
28-
private final List<Integer> skipInterceptionOfElements = Arrays.asList(
27+
private final List<Integer> skipInterceptionOfElements = List.of(
2928
R.id.detail_content_root_layout, R.id.relatedItemsLayout,
3029
R.id.itemsListPanel, R.id.view_pager, R.id.tab_layout, R.id.bottomControls,
3130
R.id.playPauseButton, R.id.playPreviousButton, R.id.playNextButton);
@@ -57,7 +56,7 @@ public boolean onInterceptTouchEvent(@NonNull final CoordinatorLayout parent,
5756
if (getState() == BottomSheetBehavior.STATE_EXPANDED
5857
&& event.getAction() == MotionEvent.ACTION_DOWN) {
5958
// Without overriding scrolling will not work when user touches these elements
60-
for (final Integer element : skipInterceptionOfElements) {
59+
for (final int element : skipInterceptionOfElements) {
6160
final View view = child.findViewById(element);
6261
if (view != null) {
6362
final boolean visible = view.getGlobalVisibleRect(globalRect);

app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.schabi.newpipe.util.SimpleOnSeekBarChangeListener;
3131
import org.schabi.newpipe.util.SliderStrategy;
3232

33-
import java.util.HashMap;
3433
import java.util.Map;
3534
import java.util.Objects;
3635
import java.util.function.Consumer;
@@ -334,10 +333,8 @@ private void setupPitchControlModeTextView(
334333
}
335334

336335
private Map<Boolean, TextView> getPitchControlModeComponentMappings() {
337-
final Map<Boolean, TextView> mappings = new HashMap<>();
338-
mappings.put(PITCH_CTRL_MODE_PERCENT, binding.pitchControlModePercent);
339-
mappings.put(PITCH_CTRL_MODE_SEMITONE, binding.pitchControlModeSemitone);
340-
return mappings;
336+
return Map.of(PITCH_CTRL_MODE_PERCENT, binding.pitchControlModePercent,
337+
PITCH_CTRL_MODE_SEMITONE, binding.pitchControlModeSemitone);
341338
}
342339

343340
private void changePitchControlMode(final boolean semitones) {
@@ -407,13 +404,11 @@ private void setupStepTextView(
407404
}
408405

409406
private Map<Double, TextView> getStepSizeComponentMappings() {
410-
final Map<Double, TextView> mappings = new HashMap<>();
411-
mappings.put(STEP_1_PERCENT_VALUE, binding.stepSizeOnePercent);
412-
mappings.put(STEP_5_PERCENT_VALUE, binding.stepSizeFivePercent);
413-
mappings.put(STEP_10_PERCENT_VALUE, binding.stepSizeTenPercent);
414-
mappings.put(STEP_25_PERCENT_VALUE, binding.stepSizeTwentyFivePercent);
415-
mappings.put(STEP_100_PERCENT_VALUE, binding.stepSizeOneHundredPercent);
416-
return mappings;
407+
return Map.of(STEP_1_PERCENT_VALUE, binding.stepSizeOnePercent,
408+
STEP_5_PERCENT_VALUE, binding.stepSizeFivePercent,
409+
STEP_10_PERCENT_VALUE, binding.stepSizeTenPercent,
410+
STEP_25_PERCENT_VALUE, binding.stepSizeTwentyFivePercent,
411+
STEP_100_PERCENT_VALUE, binding.stepSizeOneHundredPercent);
417412
}
418413

419414
private void setStepSizeToUI(final double newStepSize) {

app/src/main/java/org/schabi/newpipe/player/mediasource/FailedMediaSource.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.schabi.newpipe.player.playqueue.PlayQueueItem;
1717

1818
import java.io.IOException;
19-
import java.util.Collections;
19+
import java.util.List;
2020
import java.util.concurrent.TimeUnit;
2121

2222
import androidx.annotation.NonNull;
@@ -56,9 +56,7 @@ public FailedMediaSource(@NonNull final PlayQueueItem playQueueItem,
5656
this.playQueueItem = playQueueItem;
5757
this.error = error;
5858
this.retryTimestamp = retryTimestamp;
59-
this.mediaItem = ExceptionTag
60-
.of(playQueueItem, Collections.singletonList(error))
61-
.withExtras(this)
59+
this.mediaItem = ExceptionTag.of(playQueueItem, List.of(error)).withExtras(this)
6260
.asMediaItem();
6361
}
6462

0 commit comments

Comments
 (0)