mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 05:02:20 +00:00
use Locale from DynamicLanguage for displaying dates
1) fixed DateUtils to use SimpleDateFormat for everything because it respects Locale 2) added getCurrentLocale() method to DynamicLanguage 3) allow PassphraseRequiredActionBarActivity.initFragment() to accept a Locale 4) updated classes that depend on DateUtils to pass down Locale from DynamicLanguage Fixes #2684 Closes #2725 // FREEBIE
This commit is contained in:
committed by
Jake McGinty
parent
424a463b21
commit
d8521637bb
@@ -24,6 +24,7 @@ import java.util.Locale;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -39,7 +40,12 @@ public class DateUtils extends android.text.format.DateUtils {
|
||||
return (int) to.convert(System.currentTimeMillis() - millis, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static String getBriefRelativeTimeSpanString(final Context c, final long timestamp) {
|
||||
private static String getFormattedDateTime(long time, String template, Locale locale) {
|
||||
String localizedPattern = new SimpleDateFormat(template, locale).toLocalizedPattern();
|
||||
return new SimpleDateFormat(localizedPattern, locale).format(new Date(time));
|
||||
}
|
||||
|
||||
public static String getBriefRelativeTimeSpanString(final Context c, final Locale locale, final long timestamp) {
|
||||
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
|
||||
return c.getString(R.string.DateUtils_now);
|
||||
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
|
||||
@@ -49,34 +55,34 @@ public class DateUtils extends android.text.format.DateUtils {
|
||||
int hours = convertDelta(timestamp, TimeUnit.HOURS);
|
||||
return c.getResources().getQuantityString(R.plurals.hours_ago, hours, hours);
|
||||
} else if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
|
||||
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY);
|
||||
return getFormattedDateTime(timestamp, "EEE", locale);
|
||||
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
|
||||
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL);
|
||||
return getFormattedDateTime(timestamp, "MMM d", locale);
|
||||
} else {
|
||||
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
|
||||
return getFormattedDateTime(timestamp, "MMM d, yyyy", locale);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getExtendedRelativeTimeSpanString(final Context c, final long timestamp) {
|
||||
public static String getExtendedRelativeTimeSpanString(final Context c, final Locale locale, final long timestamp) {
|
||||
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
|
||||
return c.getString(R.string.DateUtils_now);
|
||||
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
|
||||
int mins = (int)TimeUnit.MINUTES.convert(System.currentTimeMillis() - timestamp, TimeUnit.MILLISECONDS);
|
||||
return c.getResources().getQuantityString(R.plurals.minutes_ago, mins, mins);
|
||||
} else {
|
||||
int formatFlags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_TIME;
|
||||
if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
|
||||
formatFlags |= DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY;
|
||||
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
|
||||
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
|
||||
} else {
|
||||
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL;
|
||||
}
|
||||
return DateUtils.formatDateTime(c, timestamp, formatFlags);
|
||||
StringBuilder format = new StringBuilder();
|
||||
if (isWithin(timestamp, 6, TimeUnit.DAYS)) format.append("EEE ");
|
||||
else if (isWithin(timestamp, 365, TimeUnit.DAYS)) format.append("MMM d, ");
|
||||
else format.append("MMM d, yyyy, ");
|
||||
|
||||
if (DateFormat.is24HourFormat(c)) format.append("HH:mm");
|
||||
else format.append("hh:mm a");
|
||||
|
||||
return getFormattedDateTime(timestamp, format.toString(), locale);
|
||||
}
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getDetailedDateFormatter(Context context) {
|
||||
public static SimpleDateFormat getDetailedDateFormatter(Context context, Locale locale) {
|
||||
String dateFormatPattern;
|
||||
|
||||
if (DateFormat.is24HourFormat(context)) {
|
||||
@@ -85,7 +91,7 @@ public class DateUtils extends android.text.format.DateUtils {
|
||||
dateFormatPattern = "MMM d, yyyy hh:mm:ss a zzz";
|
||||
}
|
||||
|
||||
return new SimpleDateFormat(dateFormatPattern, Locale.getDefault());
|
||||
return new SimpleDateFormat(dateFormatPattern, locale);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ public class DynamicLanguage {
|
||||
setContextLocale(service, currentLocale);
|
||||
}
|
||||
|
||||
public Locale getCurrentLocale() {
|
||||
return currentLocale;
|
||||
}
|
||||
|
||||
private static void setContextLocale(Context context, Locale selectedLocale) {
|
||||
Configuration configuration = context.getResources().getConfiguration();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user