diff --git a/app/build.gradle b/app/build.gradle index bedfe9f4f..0afc8d4c3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,6 +94,7 @@ dependencies { implementation 'io.reactivex.rxjava2:rxjava:2.2.2' implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1' + implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final' implementation "androidx.room:room-runtime:${roomDbLibVersion}" implementation "androidx.room:room-rxjava2:${roomDbLibVersion}" diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 6b56e1c75..53a9ecd5a 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -18,6 +18,7 @@ -dontobfuscate -keep class org.schabi.newpipe.extractor.timeago.patterns.** { *; } +-keep class org.ocpsoft.prettytime.i18n.** { *; } -keep class org.mozilla.javascript.** { *; } diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 95e56c620..8698b3c93 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -98,6 +98,7 @@ public class App extends Application { NewPipe.init(getDownloader(), Localization.getPreferredLocalization(this), Localization.getPreferredContentCountry(this)); + Localization.init(); StateSaver.init(this); initNotificationChannel(); diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java index 6741e4403..4d94ec392 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java @@ -109,8 +109,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { } if (item.getPublishedTime() != null) { - itemPublishedTime.setText(Localization - .formatDate(item.getPublishedTime().date().getTime())); + itemPublishedTime.setText(Localization.relativeTime(item.getPublishedTime().date())); } else { itemPublishedTime.setText(item.getTextualPublishedTime()); } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java index 6b54d0168..c48934d10 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.info_list.holder; +import android.preference.PreferenceManager; import android.text.TextUtils; import android.view.ViewGroup; import android.widget.TextView; @@ -12,6 +13,8 @@ import org.schabi.newpipe.info_list.InfoItemBuilder; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.Localization; +import static org.schabi.newpipe.MainActivity.DEBUG; + /* * Created by Christian Schabesberger on 01.08.16. *

@@ -62,13 +65,30 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder { viewsAndDate = Localization.shortViewCount(itemBuilder.getContext(), infoItem.getViewCount()); } } - if (!TextUtils.isEmpty(infoItem.getTextualUploadDate())) { + + final String uploadDate = getFormattedRelativeUploadDate(infoItem); + if (!TextUtils.isEmpty(uploadDate)) { if (viewsAndDate.isEmpty()) { - viewsAndDate = infoItem.getTextualUploadDate(); - } else { - viewsAndDate += " • " + infoItem.getTextualUploadDate(); + return uploadDate; } + + return Localization.concatenateStrings(viewsAndDate, uploadDate); } + return viewsAndDate; } + + private String getFormattedRelativeUploadDate(final StreamInfoItem infoItem) { + if (infoItem.getUploadDate() != null) { + String formattedRelativeTime = Localization.relativeTime(infoItem.getUploadDate().date()); + + if (DEBUG && PreferenceManager.getDefaultSharedPreferences(itemBuilder.getContext()) + .getBoolean(itemBuilder.getContext().getString(R.string.show_original_time_ago_key), false)) { + formattedRelativeTime += " (" + infoItem.getTextualUploadDate() + ")"; + } + return formattedRelativeTime; + } else { + return infoItem.getTextualUploadDate(); + } + } } 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 9a6a9c96e..9274df848 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -2,25 +2,26 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.SharedPreferences; -import android.content.res.Resources; import android.preference.PreferenceManager; -import androidx.annotation.NonNull; -import androidx.annotation.PluralsRes; -import androidx.annotation.StringRes; import android.text.TextUtils; +import org.ocpsoft.prettytime.PrettyTime; +import org.ocpsoft.prettytime.units.Decade; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.localization.ContentCountry; import java.text.DateFormat; import java.text.NumberFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; +import androidx.annotation.NonNull; +import androidx.annotation.PluralsRes; +import androidx.annotation.StringRes; + /* * Created by chschtsch on 12/29/15. * @@ -43,11 +44,16 @@ import java.util.Locale; public class Localization { - public final static String DOT_SEPARATOR = " • "; + private static PrettyTime prettyTime; + private static final String DOT_SEPARATOR = " • "; private Localization() { } + public static void init() { + initPrettyTime(); + } + @NonNull public static String concatenateStrings(final String... strings) { return concatenateStrings(Arrays.asList(strings)); @@ -188,4 +194,26 @@ public class Localization { } return output; } + + /*////////////////////////////////////////////////////////////////////////// + // Pretty Time + //////////////////////////////////////////////////////////////////////////*/ + + private static void initPrettyTime() { + prettyTime = new PrettyTime(Locale.getDefault()); + // Do not use decades as YouTube doesn't either. + prettyTime.removeUnit(Decade.class); + } + + private static PrettyTime getPrettyTime() { + // If pretty time's Locale is different, init again with the new one. + if (!prettyTime.getLocale().equals(Locale.getDefault())) { + initPrettyTime(); + } + return prettyTime; + } + + public static String relativeTime(Calendar calendarTime) { + return getPrettyTime().formatUnrounded(calendarTime); + } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index fc7abf678..80f2bb1f4 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -111,8 +111,8 @@ debug_pref_screen_key allow_heap_dumping_key - allow_disposed_exceptions_key + show_original_time_ago_text_key theme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 78dcb5244..328128a62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -458,6 +458,10 @@ Memory leak monitoring may cause the app to become unresponsive when heap dumping Report out-of-lifecycle errors Force reporting of undeliverable Rx exceptions outside of fragment or activity lifecycle after disposal + + Show original time ago on items + Original texts from services will be visible in stream items + Import/export Import diff --git a/app/src/main/res/xml/debug_settings.xml b/app/src/main/res/xml/debug_settings.xml index 7059ee8ce..b51d4e232 100644 --- a/app/src/main/res/xml/debug_settings.xml +++ b/app/src/main/res/xml/debug_settings.xml @@ -18,4 +18,11 @@ android:key="@string/allow_disposed_exceptions_key" android:title="@string/enable_disposed_exceptions_title" android:summary="@string/enable_disposed_exceptions_summary"/> + +