1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2026-04-17 12:21:22 +00:00

Improve code style to be more consistent

This commit is contained in:
wb9688
2020-03-31 19:20:15 +02:00
parent 819e52cab3
commit fda5405e48
244 changed files with 10116 additions and 7222 deletions

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import androidx.annotation.StringRes;
import org.schabi.newpipe.R;
@@ -17,26 +18,31 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@SuppressWarnings("WeakerAccess")
public final class ListHelper {
// Video format in order of quality. 0=lowest quality, n=highest quality
private static final List<MediaFormat> VIDEO_FORMAT_QUALITY_RANKING =
Arrays.asList(MediaFormat.v3GPP, MediaFormat.WEBM, MediaFormat.MPEG_4);
Arrays.asList(MediaFormat.v3GPP, MediaFormat.WEBM, MediaFormat.MPEG_4);
// Audio format in order of quality. 0=lowest quality, n=highest quality
private static final List<MediaFormat> AUDIO_FORMAT_QUALITY_RANKING =
Arrays.asList(MediaFormat.MP3, MediaFormat.WEBMA, MediaFormat.M4A);
Arrays.asList(MediaFormat.MP3, MediaFormat.WEBMA, MediaFormat.M4A);
// Audio format in order of efficiency. 0=most efficient, n=least efficient
private static final List<MediaFormat> AUDIO_FORMAT_EFFICIENCY_RANKING =
Arrays.asList(MediaFormat.WEBMA, MediaFormat.M4A, MediaFormat.MP3);
private static final List<String> HIGH_RESOLUTION_LIST = Arrays.asList("1440p", "2160p", "1440p60", "2160p60");
private static final List<String> HIGH_RESOLUTION_LIST
= Arrays.asList("1440p", "2160p", "1440p60", "2160p60");
private ListHelper() { }
/**
* @see #getDefaultResolutionIndex(String, String, MediaFormat, List)
* @param context Android app context
* @param videoStreams list of the video streams to check
* @return index of the video stream with the default index
*/
public static int getDefaultResolutionIndex(Context context, List<VideoStream> videoStreams) {
public static int getDefaultResolutionIndex(final Context context,
final List<VideoStream> videoStreams) {
String defaultResolution = computeDefaultResolution(context,
R.string.default_resolution_key, R.string.default_resolution_value);
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
@@ -44,15 +50,25 @@ public final class ListHelper {
/**
* @see #getDefaultResolutionIndex(String, String, MediaFormat, List)
* @param context Android app context
* @param videoStreams list of the video streams to check
* @param defaultResolution the default resolution to look for
* @return index of the video stream with the default index
*/
public static int getResolutionIndex(Context context, List<VideoStream> videoStreams, String defaultResolution) {
public static int getResolutionIndex(final Context context,
final List<VideoStream> videoStreams,
final String defaultResolution) {
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
}
/**
* @see #getDefaultResolutionIndex(String, String, MediaFormat, List)
* @param context Android app context
* @param videoStreams list of the video streams to check
* @return index of the video stream with the default index
*/
public static int getPopupDefaultResolutionIndex(Context context, List<VideoStream> videoStreams) {
public static int getPopupDefaultResolutionIndex(final Context context,
final List<VideoStream> videoStreams) {
String defaultResolution = computeDefaultResolution(context,
R.string.default_popup_resolution_key, R.string.default_popup_resolution_value);
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
@@ -60,12 +76,19 @@ public final class ListHelper {
/**
* @see #getDefaultResolutionIndex(String, String, MediaFormat, List)
* @param context Android app context
* @param videoStreams list of the video streams to check
* @param defaultResolution the default resolution to look for
* @return index of the video stream with the default index
*/
public static int getPopupResolutionIndex(Context context, List<VideoStream> videoStreams, String defaultResolution) {
public static int getPopupResolutionIndex(final Context context,
final List<VideoStream> videoStreams,
final String defaultResolution) {
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
}
public static int getDefaultAudioFormat(Context context, List<AudioStream> audioStreams) {
public static int getDefaultAudioFormat(final Context context,
final List<AudioStream> audioStreams) {
MediaFormat defaultFormat = getDefaultFormat(context, R.string.default_audio_format_key,
R.string.default_audio_format_value);
@@ -79,8 +102,8 @@ public final class ListHelper {
}
/**
* Join the two lists of video streams (video_only and normal videos), and sort them according with default format
* chosen by the user
* Join the two lists of video streams (video_only and normal videos),
* and sort them according with default format chosen by the user.
*
* @param context context to search for the format to give preference
* @param videoStreams normal videos list
@@ -88,20 +111,28 @@ public final class ListHelper {
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest
* @return the sorted list
*/
public static List<VideoStream> getSortedStreamVideosList(Context context, List<VideoStream> videoStreams, List<VideoStream> videoOnlyStreams, boolean ascendingOrder) {
public static List<VideoStream> getSortedStreamVideosList(final Context context,
final List<VideoStream> videoStreams,
final List<VideoStream>
videoOnlyStreams,
final boolean ascendingOrder) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean showHigherResolutions = preferences.getBoolean(context.getString(R.string.show_higher_resolutions_key), false);
MediaFormat defaultFormat = getDefaultFormat(context, R.string.default_video_format_key, R.string.default_video_format_value);
boolean showHigherResolutions = preferences.getBoolean(
context.getString(R.string.show_higher_resolutions_key), false);
MediaFormat defaultFormat = getDefaultFormat(context, R.string.default_video_format_key,
R.string.default_video_format_value);
return getSortedStreamVideosList(defaultFormat, showHigherResolutions, videoStreams, videoOnlyStreams, ascendingOrder);
return getSortedStreamVideosList(defaultFormat, showHigherResolutions, videoStreams,
videoOnlyStreams, ascendingOrder);
}
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
private static String computeDefaultResolution(Context context, int key, int value) {
private static String computeDefaultResolution(final Context context, final int key,
final int value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
// Load the prefered resolution otherwise the best available
@@ -110,7 +141,8 @@ public final class ListHelper {
: context.getString(R.string.best_resolution_key);
String maxResolution = getResolutionLimit(context);
if (maxResolution != null && (resolution.equals(context.getString(R.string.best_resolution_key))
if (maxResolution != null
&& (resolution.equals(context.getString(R.string.best_resolution_key))
|| compareVideoStreamResolution(maxResolution, resolution) < 1)) {
resolution = maxResolution;
}
@@ -119,20 +151,29 @@ public final class ListHelper {
/**
* Return the index of the default stream in the list, based on the parameters
* defaultResolution and defaultFormat
* defaultResolution and defaultFormat.
*
* @param defaultResolution the default resolution to look for
* @param bestResolutionKey key of the best resolution
* @param defaultFormat the default fomat to look for
* @param videoStreams list of the video streams to check
* @return index of the default resolution&format
*/
static int getDefaultResolutionIndex(String defaultResolution, String bestResolutionKey,
MediaFormat defaultFormat, List<VideoStream> videoStreams) {
if (videoStreams == null || videoStreams.isEmpty()) return -1;
static int getDefaultResolutionIndex(final String defaultResolution,
final String bestResolutionKey,
final MediaFormat defaultFormat,
final List<VideoStream> videoStreams) {
if (videoStreams == null || videoStreams.isEmpty()) {
return -1;
}
sortStreamList(videoStreams, false);
if (defaultResolution.equals(bestResolutionKey)) {
return 0;
}
int defaultStreamIndex = getVideoStreamIndex(defaultResolution, defaultFormat, videoStreams);
int defaultStreamIndex
= getVideoStreamIndex(defaultResolution, defaultFormat, videoStreams);
// this is actually an error,
// but maybe there is really no stream fitting to the default value.
@@ -143,39 +184,53 @@ public final class ListHelper {
}
/**
* Join the two lists of video streams (video_only and normal videos), and sort them according with default format
* chosen by the user
* Join the two lists of video streams (video_only and normal videos),
* and sort them according with default format chosen by the user.
*
* @param defaultFormat format to give preference
* @param defaultFormat format to give preference
* @param showHigherResolutions show >1080p resolutions
* @param videoStreams normal videos list
* @param videoOnlyStreams video only stream list
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest @return the sorted list
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest
* @return the sorted list
*/
static List<VideoStream> getSortedStreamVideosList(MediaFormat defaultFormat, boolean showHigherResolutions, List<VideoStream> videoStreams, List<VideoStream> videoOnlyStreams, boolean ascendingOrder) {
static List<VideoStream> getSortedStreamVideosList(final MediaFormat defaultFormat,
final boolean showHigherResolutions,
final List<VideoStream> videoStreams,
final List<VideoStream> videoOnlyStreams,
final boolean ascendingOrder) {
ArrayList<VideoStream> retList = new ArrayList<>();
HashMap<String, VideoStream> hashMap = new HashMap<>();
if (videoOnlyStreams != null) {
for (VideoStream stream : videoOnlyStreams) {
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(stream.getResolution())) continue;
if (!showHigherResolutions
&& HIGH_RESOLUTION_LIST.contains(stream.getResolution())) {
continue;
}
retList.add(stream);
}
}
if (videoStreams != null) {
for (VideoStream stream : videoStreams) {
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(stream.getResolution())) continue;
if (!showHigherResolutions
&& HIGH_RESOLUTION_LIST.contains(stream.getResolution())) {
continue;
}
retList.add(stream);
}
}
// Add all to the hashmap
for (VideoStream videoStream : retList) hashMap.put(videoStream.getResolution(), videoStream);
for (VideoStream videoStream : retList) {
hashMap.put(videoStream.getResolution(), videoStream);
}
// Override the values when the key == resolution, with the defaultFormat
for (VideoStream videoStream : retList) {
if (videoStream.getFormat() == defaultFormat) hashMap.put(videoStream.getResolution(), videoStream);
if (videoStream.getFormat() == defaultFormat) {
hashMap.put(videoStream.getResolution(), videoStream);
}
}
retList.clear();
@@ -203,7 +258,8 @@ public final class ListHelper {
* @param videoStreams list that the sorting will be applied
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest
*/
private static void sortStreamList(List<VideoStream> videoStreams, final boolean ascendingOrder) {
private static void sortStreamList(final List<VideoStream> videoStreams,
final boolean ascendingOrder) {
Collections.sort(videoStreams, (o1, o2) -> {
int result = compareVideoStreamResolution(o1, o2);
return result == 0 ? 0 : (ascendingOrder ? result : -result);
@@ -214,18 +270,21 @@ public final class ListHelper {
* Get the audio from the list with the highest quality. Format will be ignored if it yields
* no results.
*
* @param format the format to look for
* @param audioStreams list the audio streams
* @return index of the audio with the highest average bitrate of the default format
*/
static int getHighestQualityAudioIndex(MediaFormat format, List<AudioStream> audioStreams) {
static int getHighestQualityAudioIndex(final MediaFormat format,
final List<AudioStream> audioStreams) {
int result = -1;
boolean hasOneFormat = false;
if (audioStreams != null) {
while(result == -1) {
while (result == -1) {
AudioStream prevStream = null;
for (int idx = 0; idx < audioStreams.size(); idx++) {
AudioStream stream = audioStreams.get(idx);
if ((format == null || stream.getFormat() == format) &&
(prevStream == null || compareAudioStreamBitrate(prevStream, stream,
if ((format == null || stream.getFormat() == format || hasOneFormat)
&& (prevStream == null || compareAudioStreamBitrate(prevStream, stream,
AUDIO_FORMAT_QUALITY_RANKING) < 0)) {
prevStream = stream;
result = idx;
@@ -234,7 +293,7 @@ public final class ListHelper {
if (result == -1 && format == null) {
break;
}
format = null;
hasOneFormat = true;
}
}
return result;
@@ -244,19 +303,21 @@ public final class ListHelper {
* Get the audio from the list with the lowest bitrate and efficient format. Format will be
* ignored if it yields no results.
*
* @param format The target format type or null if it doesn't matter
* @param audioStreams list the audio streams
* @return index of the audio stream that can produce the most compact results or -1 if not found.
* @param format The target format type or null if it doesn't matter
* @param audioStreams List of audio streams
* @return Index of audio stream that can produce the most compact results or -1 if not found
*/
static int getMostCompactAudioIndex(MediaFormat format, List<AudioStream> audioStreams) {
static int getMostCompactAudioIndex(final MediaFormat format,
final List<AudioStream> audioStreams) {
int result = -1;
boolean hasOneFormat = false;
if (audioStreams != null) {
while(result == -1) {
while (result == -1) {
AudioStream prevStream = null;
for (int idx = 0; idx < audioStreams.size(); idx++) {
AudioStream stream = audioStreams.get(idx);
if ((format == null || stream.getFormat() == format) &&
(prevStream == null || compareAudioStreamBitrate(prevStream, stream,
if ((format == null || stream.getFormat() == format || hasOneFormat)
&& (prevStream == null || compareAudioStreamBitrate(prevStream, stream,
AUDIO_FORMAT_EFFICIENCY_RANKING) > 0)) {
prevStream = stream;
result = idx;
@@ -265,7 +326,7 @@ public final class ListHelper {
if (result == -1 && format == null) {
break;
}
format = null;
hasOneFormat = true;
}
}
return result;
@@ -273,16 +334,25 @@ public final class ListHelper {
/**
* Locates a possible match for the given resolution and format in the provided list.
* In this order:
* 1. Find a format and resolution match
* 2. Find a format and resolution match and ignore the refresh
* 3. Find a resolution match
* 4. Find a resolution match and ignore the refresh
* 5. Find a resolution just below the requested resolution and ignore the refresh
* 6. Give up
*
* <p>In this order:</p>
*
* <ol>
* <li>Find a format and resolution match</li>
* <li>Find a format and resolution match and ignore the refresh</li>
* <li>Find a resolution match</li>
* <li>Find a resolution match and ignore the refresh</li>
* <li>Find a resolution just below the requested resolution and ignore the refresh</li>
* <li>Give up</li>
* </ol>
*
* @param targetResolution the resolution to look for
* @param targetFormat the format to look for
* @param videoStreams the available video streams
* @return the index of the prefered video stream
*/
static int getVideoStreamIndex(String targetResolution, MediaFormat targetFormat,
List<VideoStream> videoStreams) {
static int getVideoStreamIndex(final String targetResolution, final MediaFormat targetFormat,
final List<VideoStream> videoStreams) {
int fullMatchIndex = -1;
int fullMatchNoRefreshIndex = -1;
int resMatchOnlyIndex = -1;
@@ -307,11 +377,13 @@ public final class ListHelper {
resMatchOnlyIndex = idx;
}
if (resMatchOnlyNoRefreshIndex == -1 && resolutionNoRefresh.equals(targetResolutionNoRefresh)) {
if (resMatchOnlyNoRefreshIndex == -1
&& resolutionNoRefresh.equals(targetResolutionNoRefresh)) {
resMatchOnlyNoRefreshIndex = idx;
}
if (lowerResMatchNoRefreshIndex == -1 && compareVideoStreamResolution(resolutionNoRefresh, targetResolutionNoRefresh) < 0) {
if (lowerResMatchNoRefreshIndex == -1 && compareVideoStreamResolution(
resolutionNoRefresh, targetResolutionNoRefresh) < 0) {
lowerResMatchNoRefreshIndex = idx;
}
}
@@ -332,30 +404,44 @@ public final class ListHelper {
}
/**
* Fetches the desired resolution or returns the default if it is not found. The resolution
* will be reduced if video chocking is active.
* Fetches the desired resolution or returns the default if it is not found.
* The resolution will be reduced if video chocking is active.
*
* @param context Android app context
* @param defaultResolution the default resolution
* @param videoStreams the list of video streams to check
* @return the index of the prefered video stream
*/
private static int getDefaultResolutionWithDefaultFormat(Context context, String defaultResolution, List<VideoStream> videoStreams) {
MediaFormat defaultFormat = getDefaultFormat(context, R.string.default_video_format_key, R.string.default_video_format_value);
return getDefaultResolutionIndex(defaultResolution, context.getString(R.string.best_resolution_key), defaultFormat, videoStreams);
private static int getDefaultResolutionWithDefaultFormat(final Context context,
final String defaultResolution,
final List<VideoStream> videoStreams) {
MediaFormat defaultFormat = getDefaultFormat(context, R.string.default_video_format_key,
R.string.default_video_format_value);
return getDefaultResolutionIndex(defaultResolution,
context.getString(R.string.best_resolution_key), defaultFormat, videoStreams);
}
private static MediaFormat getDefaultFormat(Context context, @StringRes int defaultFormatKey, @StringRes int defaultFormatValueKey) {
private static MediaFormat getDefaultFormat(final Context context,
@StringRes final int defaultFormatKey,
@StringRes final int defaultFormatValueKey) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String defaultFormat = context.getString(defaultFormatValueKey);
String defaultFormatString = preferences.getString(context.getString(defaultFormatKey), defaultFormat);
String defaultFormatString = preferences.getString(
context.getString(defaultFormatKey), defaultFormat);
MediaFormat defaultMediaFormat = getMediaFormatFromKey(context, defaultFormatString);
if (defaultMediaFormat == null) {
preferences.edit().putString(context.getString(defaultFormatKey), defaultFormat).apply();
preferences.edit().putString(context.getString(defaultFormatKey), defaultFormat)
.apply();
defaultMediaFormat = getMediaFormatFromKey(context, defaultFormat);
}
return defaultMediaFormat;
}
private static MediaFormat getMediaFormatFromKey(Context context, String formatKey) {
private static MediaFormat getMediaFormatFromKey(final Context context,
final String formatKey) {
MediaFormat format = null;
if (formatKey.equals(context.getString(R.string.video_webm_key))) {
format = MediaFormat.WEBM;
@@ -372,8 +458,9 @@ public final class ListHelper {
}
// Compares the quality of two audio streams
private static int compareAudioStreamBitrate(AudioStream streamA, AudioStream streamB,
List<MediaFormat> formatRanking) {
private static int compareAudioStreamBitrate(final AudioStream streamA,
final AudioStream streamB,
final List<MediaFormat> formatRanking) {
if (streamA == null) {
return -1;
}
@@ -388,10 +475,11 @@ public final class ListHelper {
}
// Same bitrate and format
return formatRanking.indexOf(streamA.getFormat()) - formatRanking.indexOf(streamB.getFormat());
return formatRanking.indexOf(streamA.getFormat())
- formatRanking.indexOf(streamB.getFormat());
}
private static int compareVideoStreamResolution(String r1, String r2) {
private static int compareVideoStreamResolution(final String r1, final String r2) {
int res1 = Integer.parseInt(r1.replaceAll("0p\\d+$", "1")
.replaceAll("[^\\d.]", ""));
int res2 = Integer.parseInt(r2.replaceAll("0p\\d+$", "1")
@@ -400,7 +488,8 @@ public final class ListHelper {
}
// Compares the quality of two video streams.
private static int compareVideoStreamResolution(VideoStream streamA, VideoStream streamB) {
private static int compareVideoStreamResolution(final VideoStream streamA,
final VideoStream streamB) {
if (streamA == null) {
return -1;
}
@@ -408,27 +497,29 @@ public final class ListHelper {
return 1;
}
int resComp = compareVideoStreamResolution(streamA.getResolution(), streamB.getResolution());
int resComp = compareVideoStreamResolution(streamA.getResolution(),
streamB.getResolution());
if (resComp != 0) {
return resComp;
}
// Same bitrate and format
return ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamA.getFormat()) - ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamB.getFormat());
return ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamA.getFormat())
- ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamB.getFormat());
}
private static boolean isLimitingDataUsage(Context context) {
private static boolean isLimitingDataUsage(final Context context) {
return getResolutionLimit(context) != null;
}
/**
* The maximum resolution allowed
* The maximum resolution allowed.
*
* @param context App context
* @return maximum resolution allowed or null if there is no maximum
*/
private static String getResolutionLimit(Context context) {
private static String getResolutionLimit(final Context context) {
String resolutionLimit = null;
if (isMeteredNetwork(context)) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
@@ -442,13 +533,16 @@ public final class ListHelper {
/**
* The current network is metered (like mobile data)?
*
* @param context App context
* @return {@code true} if connected to a metered network
*/
private static boolean isMeteredNetwork(Context context)
{
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null || manager.getActiveNetworkInfo() == null) return false;
private static boolean isMeteredNetwork(final Context context) {
ConnectivityManager manager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null || manager.getActiveNetworkInfo() == null) {
return false;
}
return manager.isActiveNetworkMetered();
}