diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt index e81facd2e..294a7fcd5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt @@ -121,11 +121,11 @@ class FeedLoadService : Service() { val useFeedExtractor = defaultSharedPreferences .getBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), false) - val thresholdOutdatedMinutesString = defaultSharedPreferences + val thresholdOutdatedSecondsString = defaultSharedPreferences .getString(getString(R.string.feed_update_threshold_key), getString(R.string.feed_update_threshold_default_value)) - val thresholdOutdatedMinutes = thresholdOutdatedMinutesString!!.toInt() + val thresholdOutdatedSeconds = thresholdOutdatedSecondsString!!.toInt() - startLoading(groupId, useFeedExtractor, thresholdOutdatedMinutes) + startLoading(groupId, useFeedExtractor, thresholdOutdatedSeconds) return START_NOT_STICKY } @@ -166,11 +166,11 @@ class FeedLoadService : Service() { } } - private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedMinutes: Int) { + private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedSeconds: Int) { feedResultsHolder = ResultsHolder() val outdatedThreshold = Calendar.getInstance().apply { - add(Calendar.MINUTE, -thresholdOutdatedMinutes) + add(Calendar.SECOND, -thresholdOutdatedSeconds) }.time val subscriptions = when (groupId) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java index e0003ccaa..6c765dc3d 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java +++ b/app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java @@ -23,7 +23,7 @@ package org.schabi.newpipe.settings; import android.content.Context; import android.content.SharedPreferences; import android.os.Environment; -import android.preference.PreferenceManager; +import androidx.preference.PreferenceManager; import androidx.annotation.NonNull; import org.schabi.newpipe.R; diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/DurationListPreference.kt b/app/src/main/java/org/schabi/newpipe/settings/custom/DurationListPreference.kt new file mode 100644 index 000000000..4bc59fcee --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/DurationListPreference.kt @@ -0,0 +1,46 @@ +package org.schabi.newpipe.settings.custom + +import android.content.Context +import android.util.AttributeSet +import androidx.preference.ListPreference +import org.schabi.newpipe.util.Localization + +/** + * An extension of a common ListPreference where it sets the duration values to human readable strings. + * + * The values in the entry values array will be interpreted as seconds. If the value of a specific position + * is less than or equals to zero, its original entry title will be used. + * + * If the entry values array have anything other than numbers in it, an exception will be raised. + */ +class DurationListPreference : ListPreference { + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) + constructor(context: Context?) : super(context) + + override fun onAttached() { + super.onAttached() + + val originalEntryTitles = entries + val originalEntryValues = entryValues + val newEntryTitles = arrayOfNulls(originalEntryValues.size) + + for (i in originalEntryValues.indices) { + val currentDurationValue: Int + try { + currentDurationValue = (originalEntryValues[i] as String).toInt() + } catch (e: NumberFormatException) { + throw RuntimeException("Invalid number was set in the preference entry values array", e) + } + + if (currentDurationValue <= 0) { + newEntryTitles[i] = originalEntryTitles[i] + } else { + newEntryTitles[i] = Localization.localizeDuration(context, currentDurationValue) + } + } + + entries = newEntryTitles + } +} \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index 47b914bde..9c8fc25b8 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -213,6 +213,42 @@ public class Localization { return output; } + /** + * Localize an amount of seconds into a human readable string. + * + *

The seconds will be converted to the closest whole time unit. + *

For example, 60 seconds would give "1 minute", 119 would also give "1 minute". + * + * @param context used to get plurals resources. + * @param durationInSecs an amount of seconds. + * @return duration in a human readable string. + */ + @NonNull + public static String localizeDuration(Context context, int durationInSecs) { + if (durationInSecs < 0) { + throw new IllegalArgumentException("duration can not be negative"); + } + + final int days = (int) (durationInSecs / (24 * 60 * 60L)); /* greater than a day */ + durationInSecs %= (24 * 60 * 60L); + final int hours = (int) (durationInSecs / (60 * 60L)); /* greater than an hour */ + durationInSecs %= (60 * 60L); + final int minutes = (int) (durationInSecs / 60L); + final int seconds = (int) (durationInSecs % 60L); + + final Resources resources = context.getResources(); + + if (days > 0) { + return resources.getQuantityString(R.plurals.days, days, days); + } else if (hours > 0) { + return resources.getQuantityString(R.plurals.hours, hours, hours); + } else if (minutes > 0) { + return resources.getQuantityString(R.plurals.minutes, minutes, minutes); + } else { + return resources.getQuantityString(R.plurals.seconds, seconds, seconds); + } + } + /*////////////////////////////////////////////////////////////////////////// // Pretty Time //////////////////////////////////////////////////////////////////////////*/ diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 631817ee9..e97bf11ca 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -182,8 +182,9 @@ enable_lock_screen_video_thumbnail feed_update_threshold_key - 5 + 300 + @string/feed_update_threshold_option_always_update 5 minutes @@ -193,14 +194,15 @@ 12 hours 1 day + 0 - 5 - 15 - 60 - 360 - 720 - 1440 + 300 + 900 + 3600 + 21600 + 43200 + 86400 feed_use_dedicated_fetch_method diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6b955c556..f4303ca74 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -598,6 +598,27 @@ %s seconds + + + %d second + %d seconds + + + + %d minute + %d minutes + + + + %d hour + %d hours + + + + %d day + %d days + + What\'s New Feed groups diff --git a/app/src/main/res/xml/content_settings.xml b/app/src/main/res/xml/content_settings.xml index 817aaf324..e2fbc081d 100644 --- a/app/src/main/res/xml/content_settings.xml +++ b/app/src/main/res/xml/content_settings.xml @@ -94,7 +94,7 @@ android:layout="@layout/settings_category_header_layout" android:title="@string/settings_category_feed_title"> -