ListHelper: rename functions & variables and add documentation

This should make the purpose of these functions more clear.
This commit is contained in:
Profpatsch 2024-01-06 15:12:43 +01:00
parent e5ac6824e8
commit 71d88d0bc0
2 changed files with 45 additions and 24 deletions

View File

@ -78,7 +78,7 @@ public final class ListHelper {
*/ */
public static int getDefaultResolutionIndex(final Context context, public static int getDefaultResolutionIndex(final Context context,
final List<VideoStream> videoStreams) { final List<VideoStream> videoStreams) {
final String defaultResolution = computeDefaultResolution(context, final String defaultResolution = getPreferredResolutionOrCurrentLimit(context,
R.string.default_resolution_key, R.string.default_resolution_value); R.string.default_resolution_key, R.string.default_resolution_value);
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams); return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
} }
@ -92,7 +92,7 @@ public final class ListHelper {
*/ */
public static int getPopupDefaultResolutionIndex(final Context context, public static int getPopupDefaultResolutionIndex(final Context context,
final List<VideoStream> videoStreams) { final List<VideoStream> videoStreams) {
final String defaultResolution = computeDefaultResolution(context, final String defaultResolution = getPreferredResolutionOrCurrentLimit(context,
R.string.default_popup_resolution_key, R.string.default_popup_resolution_value); R.string.default_popup_resolution_key, R.string.default_popup_resolution_value);
return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams); return getDefaultResolutionWithDefaultFormat(context, defaultResolution, videoStreams);
} }
@ -365,22 +365,42 @@ public final class ListHelper {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static String computeDefaultResolution(@NonNull final Context context, final int key, /** Lookup the preferred resolution and the current resolution limit.
final int value) { *
* @param context App context
* @param defaultResolutionKey The defaultResolution preference key
* @param defaultResolutionDefaultValue Default resolution if key does not exist
* @return The smaller resolution of either the preference or the current limit.
*/
private static String getPreferredResolutionOrCurrentLimit(
@NonNull final Context context,
final int defaultResolutionKey,
final int defaultResolutionDefaultValue
) {
final SharedPreferences preferences = final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context); PreferenceManager.getDefaultSharedPreferences(context);
// Load the preferred resolution otherwise the best available // Load the preferred resolution otherwise the best available
String resolution = final String preferredResolution = preferences.getString(
preferences.getString(context.getString(key), context.getString(value)); context.getString(defaultResolutionKey),
context.getString(defaultResolutionDefaultValue)
);
final String maxResolution = getResolutionLimit(context); // clamp to the currently maximum allowed resolution
if (maxResolution != null final String result;
&& (resolution.equals(context.getString(R.string.best_resolution_key)) final String resolutionLimit = getCurrentResolutionLimit(context);
|| compareVideoStreamResolution(maxResolution, resolution) < 1)) { if (resolutionLimit != null
resolution = maxResolution; && (
// if the preference is best_resolution
preferredResolution.equals(context.getString(R.string.best_resolution_key))
// or the preference is higher than the current max allowed resolution
|| compareVideoStreamResolution(resolutionLimit, preferredResolution) < 1
)) {
result = resolutionLimit;
} else {
result = preferredResolution;
} }
return resolution; return result;
} }
/** /**
@ -601,7 +621,7 @@ public final class ListHelper {
/** /**
* Fetches the desired resolution or returns the default if it is not found. * Fetches the desired resolution or returns the default if it is not found.
* The resolution will be reduced if video chocking is active. * The resolution will be reduced if video choking is active.
* *
* @param context Android app context * @param context Android app context
* @param defaultResolution the default resolution * @param defaultResolution the default resolution
@ -683,27 +703,28 @@ public final class ListHelper {
* @param context App context * @param context App context
* @return whether a max resolution is set * @return whether a max resolution is set
*/ */
static boolean isLimitingDataUsage(@NonNull final Context context) { static boolean isCurrentlyLimitingDataUsage(@NonNull final Context context) {
return getResolutionLimit(context) != null; return getCurrentResolutionLimit(context) != null;
} }
/** /**
* The maximum resolution allowed by application settings. * The maximum current resolution allowed by application settings.
* Takes into account whether we are on a metered network.
* *
* @param context App context * @param context App context
* @return maximum resolution allowed or null if there is no maximum * @return current maximum resolution allowed or null if there is no maximum
*/ */
private static String getResolutionLimit(@NonNull final Context context) { private static String getCurrentResolutionLimit(@NonNull final Context context) {
String resolutionLimit = null; String currentResolutionLimit = null;
if (isMeteredNetwork(context)) { if (isMeteredNetwork(context)) {
final SharedPreferences preferences = final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context); PreferenceManager.getDefaultSharedPreferences(context);
final String defValue = context.getString(R.string.limit_data_usage_none_key); final String defValue = context.getString(R.string.limit_data_usage_none_key);
final String value = preferences.getString( final String value = preferences.getString(
context.getString(R.string.limit_mobile_data_usage_key), defValue); context.getString(R.string.limit_mobile_data_usage_key), defValue);
resolutionLimit = defValue.equals(value) ? null : value; currentResolutionLimit = defValue.equals(value) ? null : value;
} }
return resolutionLimit; return currentResolutionLimit;
} }
/** /**
@ -734,7 +755,7 @@ public final class ListHelper {
final @NonNull Context context) { final @NonNull Context context) {
final MediaFormat defaultFormat = getDefaultFormat(context, final MediaFormat defaultFormat = getDefaultFormat(context,
R.string.default_audio_format_key, R.string.default_audio_format_value); R.string.default_audio_format_key, R.string.default_audio_format_value);
return getAudioFormatComparator(defaultFormat, isLimitingDataUsage(context)); return getAudioFormatComparator(defaultFormat, isCurrentlyLimitingDataUsage(context));
} }
/** /**

View File

@ -31,7 +31,7 @@ public class SecondaryStreamHelper<T extends Stream> {
* Finds an audio stream compatible with the provided video-only stream, so that the two streams * Finds an audio stream compatible with the provided video-only stream, so that the two streams
* can be combined in a single file by the downloader. If there are multiple available audio * can be combined in a single file by the downloader. If there are multiple available audio
* streams, chooses either the highest or the lowest quality one based on * streams, chooses either the highest or the lowest quality one based on
* {@link ListHelper#isLimitingDataUsage(Context)}. * {@link ListHelper#isCurrentlyLimitingDataUsage(Context)}.
* *
* @param context Android context * @param context Android context
* @param audioStreams list of audio streams * @param audioStreams list of audio streams
@ -56,7 +56,7 @@ public class SecondaryStreamHelper<T extends Stream> {
} }
final boolean m4v = mediaFormat == MediaFormat.MPEG_4; final boolean m4v = mediaFormat == MediaFormat.MPEG_4;
final boolean isLimitingDataUsage = ListHelper.isLimitingDataUsage(context); final boolean isLimitingDataUsage = ListHelper.isCurrentlyLimitingDataUsage(context);
Comparator<AudioStream> comparator = ListHelper.getAudioFormatComparator( Comparator<AudioStream> comparator = ListHelper.getAudioFormatComparator(
m4v ? MediaFormat.M4A : MediaFormat.WEBMA, isLimitingDataUsage); m4v ? MediaFormat.M4A : MediaFormat.WEBMA, isLimitingDataUsage);