Skip to content

Commit 6f86e21

Browse files
authored
Merge pull request TeamNewPipe#8724 from Isira-Seneviratne/toArray_improvements
Use toArray() with zero-length arrays.
2 parents 816154c + a9af1df commit 6f86e21

7 files changed

Lines changed: 16 additions & 29 deletions

File tree

app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,9 @@ public boolean isViewFromObject(@NonNull final View view, @NonNull final Object
282282
@Nullable
283283
public Parcelable saveState() {
284284
Bundle state = null;
285-
if (mSavedState.size() > 0) {
285+
if (!mSavedState.isEmpty()) {
286286
state = new Bundle();
287-
final Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
288-
mSavedState.toArray(fss);
289-
state.putParcelableArray("states", fss);
287+
state.putParcelableArray("states", mSavedState.toArray(new Fragment.SavedState[0]));
290288
}
291289
for (int i = 0; i < mFragments.size(); i++) {
292290
final Fragment f = mFragments.get(i);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,12 +2167,8 @@ private void showExternalPlaybackDialog() {
21672167
} else {
21682168
final int selectedVideoStreamIndexForExternalPlayers =
21692169
ListHelper.getDefaultResolutionIndex(activity, videoStreamsForExternalPlayers);
2170-
final CharSequence[] resolutions =
2171-
new CharSequence[videoStreamsForExternalPlayers.size()];
2172-
2173-
for (int i = 0; i < videoStreamsForExternalPlayers.size(); i++) {
2174-
resolutions[i] = videoStreamsForExternalPlayers.get(i).getResolution();
2175-
}
2170+
final CharSequence[] resolutions = videoStreamsForExternalPlayers.stream()
2171+
.map(VideoStream::getResolution).toArray(CharSequence[]::new);
21762172

21772173
builder.setSingleChoiceItems(resolutions, selectedVideoStreamIndexForExternalPlayers,
21782174
null);

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ private void changeContentFilter(final MenuItem item, final List<String> theCont
919919
filterItemCheckedId = item.getItemId();
920920
item.setChecked(true);
921921

922-
contentFilter = new String[]{theContentFilter.get(0)};
922+
contentFilter = theContentFilter.toArray(new String[0]);
923923

924924
if (!TextUtils.isEmpty(searchString)) {
925925
search(searchString, contentFilter, sortFilter);
@@ -980,8 +980,7 @@ public void handleResult(@NonNull final SearchInfo result) {
980980
isCorrectedSearch = result.isCorrectedSearch();
981981

982982
// List<MetaInfo> cannot be bundled without creating some containers
983-
metaInfo = new MetaInfo[result.getMetaInfo().size()];
984-
metaInfo = result.getMetaInfo().toArray(metaInfo);
983+
metaInfo = result.getMetaInfo().toArray(new MetaInfo[0]);
985984
showMetaInfoInTextView(result.getMetaInfo(), searchBinding.searchMetaInfoTextView,
986985
searchBinding.searchMetaInfoSeparator, disposables);
987986

app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.lang.annotation.Retention;
1515
import java.lang.annotation.RetentionPolicy;
1616
import java.util.ArrayList;
17-
import java.util.Arrays;
1817
import java.util.List;
1918
import java.util.SortedSet;
2019
import java.util.TreeSet;
@@ -115,7 +114,7 @@ private NotificationConstants() {
115114
};
116115

117116

118-
public static final Integer[] SLOT_COMPACT_DEFAULTS = {0, 1, 2};
117+
public static final List<Integer> SLOT_COMPACT_DEFAULTS = List.of(0, 1, 2);
119118

120119
public static final int[] SLOT_COMPACT_PREF_KEYS = {
121120
R.string.notification_slot_compact_0_key,
@@ -181,7 +180,7 @@ public static List<Integer> getCompactSlotsFromPreferences(
181180

182181
if (compactSlot == Integer.MAX_VALUE) {
183182
// settings not yet populated, return default values
184-
return new ArrayList<>(Arrays.asList(SLOT_COMPACT_DEFAULTS));
183+
return new ArrayList<>(SLOT_COMPACT_DEFAULTS);
185184
}
186185

187186
// a negative value (-1) is set when the user does not want a particular compact slot

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ private synchronized NotificationCompat.Builder createNotification() {
9999
// build the compact slot indices array (need code to convert from Integer... because Java)
100100
final List<Integer> compactSlotList = NotificationConstants.getCompactSlotsFromPreferences(
101101
player.getContext(), player.getPrefs(), nonNothingSlotCount);
102-
final int[] compactSlots = new int[compactSlotList.size()];
103-
for (int i = 0; i < compactSlotList.size(); i++) {
104-
compactSlots[i] = compactSlotList.get(i);
105-
}
102+
final int[] compactSlots = compactSlotList.stream().mapToInt(Integer::intValue).toArray();
106103

107104
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
108105
.setMediaSession(player.getMediaSessionManager().getSessionToken())

app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.schabi.newpipe.views.FocusOverlayView;
3636

3737
import java.util.List;
38+
import java.util.stream.IntStream;
3839

3940
public class NotificationActionsPreference extends Preference {
4041

@@ -74,13 +75,11 @@ public void onDetached() {
7475
////////////////////////////////////////////////////////////////////////////
7576

7677
private void setupActions(@NonNull final View view) {
77-
compactSlots =
78-
NotificationConstants.getCompactSlotsFromPreferences(
79-
getContext(), getSharedPreferences(), 5);
80-
notificationSlots = new NotificationSlot[5];
81-
for (int i = 0; i < 5; i++) {
82-
notificationSlots[i] = new NotificationSlot(i, view);
83-
}
78+
compactSlots = NotificationConstants.getCompactSlotsFromPreferences(getContext(),
79+
getSharedPreferences(), 5);
80+
notificationSlots = IntStream.range(0, 5)
81+
.mapToObj(i -> new NotificationSlot(i, view))
82+
.toArray(NotificationSlot[]::new);
8483
}
8584

8685

app/src/main/java/org/schabi/newpipe/streams/WebMReader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ private WebMTrack[] readTracks(final Element ref, final int lacingExpected) thro
348348
ensure(elemTrackEntry);
349349
}
350350

351-
final WebMTrack[] entries = new WebMTrack[trackEntries.size()];
352-
trackEntries.toArray(entries);
351+
final WebMTrack[] entries = trackEntries.toArray(new WebMTrack[0]);
353352

354353
for (final WebMTrack entry : entries) {
355354
switch (entry.trackType) {

0 commit comments

Comments
 (0)