Bug 1295643 - Implement DurationFormat.formatDurationSince() for simple duration strings r?sebastian
Neither Java nor the Android framework seem to provide a concise duration formatting of the kind we
desire ("now"/"1m"/"2h"/"3d"/"3w"), so we have to roll our own.
MozReview-Commit-ID: 7cmD6WNn8l1
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/DurationFormat.java
@@ -0,0 +1,60 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+package org.mozilla.gecko.home.activitystream;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.text.format.DateUtils;
+
+import org.mozilla.gecko.R;
+
+import java.text.DateFormat;
+
+public class DurationFormat {
+
+ /**
+ * Format a date as time duration since that date. I.e. for a given input time, return
+ * "now" for recent times, strings such as "1m", "3h", "4d", "2w", for intermediate durations,
+ * or an absolute date (12 Jan 2015) for dates more than 4 weeks ago.
+ *
+ * Will also return "now" for dates in the future.
+ */
+ public static String formatDurationSince(final Context context, final long date) {
+ final Resources resources = context.getResources();
+
+ final long duration = System.currentTimeMillis() - date;
+
+ final String formatString;
+ final long formatTime;
+
+ if (duration < DateUtils.MINUTE_IN_MILLIS) {
+ // Less than one minute ago -> now
+ // This case also covers illogical (negative) durations, maybe that can
+ // happen with sync?
+ return resources.getString(R.string.duration_now);
+ } else if (duration < DateUtils.HOUR_IN_MILLIS) {
+ // Less than 1 hour ago -> "5m"
+ formatString = resources.getString(R.string.duration_minutes);
+ formatTime = (duration / DateUtils.MINUTE_IN_MILLIS);
+ } else if (duration < DateUtils.DAY_IN_MILLIS) {
+ // Less than 1 day ago -> "5h"
+ formatString = resources.getString(R.string.duration_hours);
+ formatTime = (duration / DateUtils.HOUR_IN_MILLIS);
+ } else if (duration < DateUtils.WEEK_IN_MILLIS) {
+ // Less than 1 week ago -> "5d"
+ formatString = resources.getString(R.string.duration_days);
+ formatTime = (duration / DateUtils.DAY_IN_MILLIS);
+ } else if (duration < 4 * DateUtils.WEEK_IN_MILLIS) {
+ // Less than 4 weeks ago -> "3w"
+ formatString = resources.getString(R.string.duration_weeks);
+ formatTime = (duration / DateUtils.WEEK_IN_MILLIS);
+ } else {
+ // Otherwise: show the date
+ return DateFormat.getDateInstance(DateFormat.SHORT).format(date);
+ }
+
+ return String.format(formatString, formatTime);
+ }
+}
--- a/mobile/android/base/locales/en-US/android_strings.dtd
+++ b/mobile/android/base/locales/en-US/android_strings.dtd
@@ -826,8 +826,13 @@ just addresses the organization to follo
<!ENTITY helper_first_offline_bookmark_title "Read offline">
<!ENTITY helper_first_offline_bookmark_message "Find your Reader View items in Bookmarks, even offline.">
<!ENTITY helper_first_offline_bookmark_button "Go to Bookmarks">
<!ENTITY helper_triple_readerview_open_title "Available offline">
<!ENTITY helper_triple_readerview_open_message "Bookmark Reader View items to read them offline.">
<!ENTITY helper_triple_readerview_open_button "Add to Bookmarks">
+<!ENTITY duration_now "now">
+<!ENTITY duration_minutes "&formatD;m">
+<!ENTITY duration_hours "&formatD;h">
+<!ENTITY duration_days "&formatD;d">
+<!ENTITY duration_weeks "&formatD;w">
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -434,16 +434,17 @@ gbjar.sources += ['java/org/mozilla/geck
'GeckoProfilesProvider.java',
'GeckoUpdateReceiver.java',
'GlobalHistory.java',
'GuestSession.java',
'health/HealthRecorder.java',
'health/SessionInformation.java',
'health/StubbedHealthRecorder.java',
'home/activitystream/ActivityStream.java',
+ 'home/activitystream/DurationFormat.java',
'home/BookmarkFolderView.java',
'home/BookmarkScreenshotRow.java',
'home/BookmarksListAdapter.java',
'home/BookmarksListView.java',
'home/BookmarksPanel.java',
'home/BrowserSearch.java',
'home/ClientsAdapter.java',
'home/CombinedHistoryAdapter.java',
--- a/mobile/android/base/strings.xml.in
+++ b/mobile/android/base/strings.xml.in
@@ -629,9 +629,14 @@
<string name="helper_first_offline_bookmark_title">&helper_first_offline_bookmark_title;</string>
<string name="helper_first_offline_bookmark_message">&helper_first_offline_bookmark_message;</string>
<string name="helper_first_offline_bookmark_button">&helper_first_offline_bookmark_button;</string>
<string name="helper_triple_readerview_open_title">&helper_triple_readerview_open_title;</string>
<string name="helper_triple_readerview_open_message">&helper_triple_readerview_open_message;</string>
<string name="helper_triple_readerview_open_button">&helper_triple_readerview_open_button;</string>
+ <string name="duration_now">&duration_now;</string>
+ <string name="duration_minutes">&duration_minutes;</string>
+ <string name="duration_hours">&duration_hours;</string>
+ <string name="duration_days">&duration_days;</string>
+ <string name="duration_weeks">&duration_weeks;</string>
</resources>