11package org .schabi .newpipe .util ;
22
33import android .content .Context ;
4- import android .util .SparseArray ;
54import android .view .LayoutInflater ;
65import android .view .View ;
76import android .view .ViewGroup ;
1110import android .widget .TextView ;
1211
1312import androidx .annotation .NonNull ;
13+ import androidx .annotation .Nullable ;
14+ import androidx .collection .SparseArrayCompat ;
1415
1516import org .schabi .newpipe .DownloaderImpl ;
1617import org .schabi .newpipe .R ;
3940 * @param <U> the secondary stream type's class extending {@link Stream}
4041 */
4142public class StreamItemAdapter <T extends Stream , U extends Stream > extends BaseAdapter {
42- private final Context context ;
43-
43+ @ NonNull
4444 private final StreamSizeWrapper <T > streamsWrapper ;
45- private final SparseArray <SecondaryStreamHelper <U >> secondaryStreams ;
45+ @ NonNull
46+ private final SparseArrayCompat <SecondaryStreamHelper <U >> secondaryStreams ;
4647
4748 /**
4849 * Indicates that at least one of the primary streams is an instance of {@link VideoStream},
@@ -51,25 +52,26 @@ public class StreamItemAdapter<T extends Stream, U extends Stream> extends BaseA
5152 */
5253 private final boolean hasAnyVideoOnlyStreamWithNoSecondaryStream ;
5354
54- public StreamItemAdapter (final Context context , final StreamSizeWrapper <T > streamsWrapper ,
55- final SparseArray <SecondaryStreamHelper <U >> secondaryStreams ) {
56- this .context = context ;
55+ public StreamItemAdapter (
56+ @ NonNull final StreamSizeWrapper <T > streamsWrapper ,
57+ @ NonNull final SparseArrayCompat <SecondaryStreamHelper <U >> secondaryStreams
58+ ) {
5759 this .streamsWrapper = streamsWrapper ;
5860 this .secondaryStreams = secondaryStreams ;
5961
6062 this .hasAnyVideoOnlyStreamWithNoSecondaryStream =
6163 checkHasAnyVideoOnlyStreamWithNoSecondaryStream ();
6264 }
6365
64- public StreamItemAdapter (final Context context , final StreamSizeWrapper <T > streamsWrapper ) {
65- this (context , streamsWrapper , null );
66+ public StreamItemAdapter (final StreamSizeWrapper <T > streamsWrapper ) {
67+ this (streamsWrapper , new SparseArrayCompat <>( 0 ) );
6668 }
6769
6870 public List <T > getAll () {
6971 return streamsWrapper .getStreamsList ();
7072 }
7173
72- public SparseArray <SecondaryStreamHelper <U >> getAllSecondary () {
74+ public SparseArrayCompat <SecondaryStreamHelper <U >> getAllSecondary () {
7375 return secondaryStreams ;
7476 }
7577
@@ -106,6 +108,7 @@ private View getCustomView(final int position,
106108 final View view ,
107109 final ViewGroup parent ,
108110 final boolean isDropdownItem ) {
111+ final var context = parent .getContext ();
109112 View convertView = view ;
110113 if (convertView == null ) {
111114 convertView = LayoutInflater .from (context ).inflate (
@@ -129,7 +132,7 @@ private View getCustomView(final int position,
129132
130133 if (hasAnyVideoOnlyStreamWithNoSecondaryStream ) {
131134 if (videoStream .isVideoOnly ()) {
132- woSoundIconVisibility = hasSecondaryStream (position )
135+ woSoundIconVisibility = secondaryStreams . get (position ) != null
133136 // It has a secondary stream associated with it, so check if it's a
134137 // dropdown view so it doesn't look out of place (missing margin)
135138 // compared to those that don't.
@@ -163,8 +166,7 @@ private View getCustomView(final int position,
163166 }
164167
165168 if (streamsWrapper .getSizeInBytes (position ) > 0 ) {
166- final SecondaryStreamHelper <U > secondary = secondaryStreams == null ? null
167- : secondaryStreams .get (position );
169+ final var secondary = secondaryStreams .get (position );
168170 if (secondary != null ) {
169171 final long size = secondary .getSizeInBytes ()
170172 + streamsWrapper .getSizeInBytes (position );
@@ -196,14 +198,6 @@ private View getCustomView(final int position,
196198 return convertView ;
197199 }
198200
199- /**
200- * @param position which primary stream to check.
201- * @return whether the primary stream at position has a secondary stream associated with it.
202- */
203- private boolean hasSecondaryStream (final int position ) {
204- return secondaryStreams != null && secondaryStreams .get (position ) != null ;
205- }
206-
207201 /**
208202 * @return if there are any video-only streams with no secondary stream associated with them.
209203 * @see #hasAnyVideoOnlyStreamWithNoSecondaryStream
@@ -213,7 +207,7 @@ private boolean checkHasAnyVideoOnlyStreamWithNoSecondaryStream() {
213207 final T stream = streamsWrapper .getStreamsList ().get (i );
214208 if (stream instanceof VideoStream ) {
215209 final boolean videoOnly = ((VideoStream ) stream ).isVideoOnly ();
216- if (videoOnly && ! hasSecondaryStream (i )) {
210+ if (videoOnly && secondaryStreams . get (i ) == null ) {
217211 return true ;
218212 }
219213 }
@@ -228,16 +222,15 @@ private boolean checkHasAnyVideoOnlyStreamWithNoSecondaryStream() {
228222 * @param <T> the stream type's class extending {@link Stream}
229223 */
230224 public static class StreamSizeWrapper <T extends Stream > implements Serializable {
231- private static final StreamSizeWrapper <Stream > EMPTY = new StreamSizeWrapper <>(
232- Collections .emptyList (), null );
225+ private static final StreamSizeWrapper <Stream > EMPTY =
226+ new StreamSizeWrapper <>( Collections .emptyList (), null );
233227 private final List <T > streamsList ;
234228 private final long [] streamSizes ;
235229 private final String unknownSize ;
236230
237- public StreamSizeWrapper (final List <T > sL , final Context context ) {
238- this .streamsList = sL != null
239- ? sL
240- : Collections .emptyList ();
231+ public StreamSizeWrapper (@ NonNull final List <T > streamList ,
232+ @ Nullable final Context context ) {
233+ this .streamsList = streamList ;
241234 this .streamSizes = new long [streamsList .size ()];
242235 this .unknownSize = context == null
243236 ? "--.-" : context .getString (R .string .unknown_content );
@@ -297,21 +290,13 @@ public String getFormattedSize(final int streamIndex) {
297290 return formatSize (getSizeInBytes (streamIndex ));
298291 }
299292
300- public String getFormattedSize (final T stream ) {
301- return formatSize (getSizeInBytes (stream ));
302- }
303-
304293 private String formatSize (final long size ) {
305294 if (size > -1 ) {
306295 return Utility .formatBytes (size );
307296 }
308297 return unknownSize ;
309298 }
310299
311- public void setSize (final int streamIndex , final long sizeInBytes ) {
312- streamSizes [streamIndex ] = sizeInBytes ;
313- }
314-
315300 public void setSize (final T stream , final long sizeInBytes ) {
316301 streamSizes [streamsList .indexOf (stream )] = sizeInBytes ;
317302 }
0 commit comments