Show absolute time

This commit is contained in:
kyori 2018-08-16 22:51:23 +09:00
parent 4b4e58302f
commit ca3a5791e3
5 changed files with 60 additions and 26 deletions

View file

@ -34,8 +34,10 @@ public class PreferencesActivity extends BaseActivity
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private boolean restartActivitiesOnExit; private boolean restartActivitiesOnExit;
private @XmlRes int currentPreferences; private @XmlRes
private @StringRes int currentTitle; int currentPreferences;
private @StringRes
int currentTitle;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -61,7 +63,7 @@ public class PreferencesActivity extends BaseActivity
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
if(savedInstanceState == null) { if (savedInstanceState == null) {
currentPreferences = R.xml.preferences; currentPreferences = R.xml.preferences;
currentTitle = R.string.action_view_preferences; currentTitle = R.string.action_view_preferences;
} else { } else {
@ -124,6 +126,10 @@ public class PreferencesActivity extends BaseActivity
restartActivitiesOnExit = true; restartActivitiesOnExit = true;
break; break;
} }
case "absoluteTimeView": {
restartActivitiesOnExit = true;
break;
}
case "notificationsEnabled": { case "notificationsEnabled": {
boolean enabled = sharedPreferences.getBoolean("notificationsEnabled", true); boolean enabled = sharedPreferences.getBoolean("notificationsEnabled", true);
if (enabled) { if (enabled) {
@ -145,14 +151,14 @@ public class PreferencesActivity extends BaseActivity
@Override @Override
public void onBackPressed() { public void onBackPressed() {
//if we are not on the top level, show the top level. Else exit the activity //if we are not on the top level, show the top level. Else exit the activity
if(currentPreferences != R.xml.preferences) { if (currentPreferences != R.xml.preferences) {
showFragment(R.xml.preferences, R.string.action_view_preferences); showFragment(R.xml.preferences, R.string.action_view_preferences);
} else { } else {
/* Switching themes won't actually change the theme of activities on the back stack. /* Switching themes won't actually change the theme of activities on the back stack.
* Either the back stack activities need to all be recreated, or do the easier thing, which * Either the back stack activities need to all be recreated, or do the easier thing, which
* is hijack the back button press and use it to launch a new MainActivity and clear the * is hijack the back button press and use it to launch a new MainActivity and clear the
* back stack. */ * back stack. */
if (restartActivitiesOnExit) { if (restartActivitiesOnExit) {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

View file

@ -1,7 +1,9 @@
package com.keylesspalace.tusky.adapter; package com.keylesspalace.tusky.adapter;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -30,8 +32,10 @@ import com.keylesspalace.tusky.viewdata.StatusViewData;
import com.mikepenz.iconics.utils.Utils; import com.mikepenz.iconics.utils.Utils;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import at.connyduck.sparkbutton.SparkButton; import at.connyduck.sparkbutton.SparkButton;
import at.connyduck.sparkbutton.SparkEventListener; import at.connyduck.sparkbutton.SparkEventListener;
@ -126,25 +130,40 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
} }
protected void setCreatedAt(@Nullable Date createdAt) { protected void setCreatedAt(@Nullable Date createdAt) {
// This is the visible timestampInfo. SharedPreferences defPrefs = PreferenceManager.getDefaultSharedPreferences(timestampInfo.getContext());
String readout; if (defPrefs.getBoolean("absoluteTimeView", true)) {
/* This one is for screen-readers. Frequently, they would mispronounce timestamps like "17m" String time = "ERROR!";
* as 17 meters instead of minutes. */ if (createdAt != null) {
CharSequence readoutAloud; SimpleDateFormat sdf;
if (createdAt != null) { if (new Date().getTime() - createdAt.getTime() > 86400000L) {
long then = createdAt.getTime(); sdf = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.getDefault());
long now = new Date().getTime(); } else {
readout = DateUtils.getRelativeTimeSpanString(timestampInfo.getContext(), then, now); sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
readoutAloud = android.text.format.DateUtils.getRelativeTimeSpanString(then, now, }
android.text.format.DateUtils.SECOND_IN_MILLIS, time = sdf.format(createdAt);
android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE); timestampInfo.setText(time);
}
} else { } else {
// unknown minutes~ // This is the visible timestampInfo.
readout = "?m"; String readout;
readoutAloud = "? minutes"; /* This one is for screen-readers. Frequently, they would mispronounce timestamps like "17m"
* as 17 meters instead of minutes. */
CharSequence readoutAloud;
if (createdAt != null) {
long then = createdAt.getTime();
long now = new Date().getTime();
readout = DateUtils.getRelativeTimeSpanString(timestampInfo.getContext(), then, now);
readoutAloud = android.text.format.DateUtils.getRelativeTimeSpanString(then, now,
android.text.format.DateUtils.SECOND_IN_MILLIS,
android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
} else {
// unknown minutes~
readout = "?m";
readoutAloud = "? minutes";
}
timestampInfo.setText(readout);
timestampInfo.setContentDescription(readoutAloud);
} }
timestampInfo.setText(readout);
timestampInfo.setContentDescription(readoutAloud);
} }
protected void showContent(boolean show) { protected void showContent(boolean show) {
@ -256,7 +275,7 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
final int urlIndex = i; final int urlIndex = i;
previews[i].setOnClickListener(v -> { previews[i].setOnClickListener(v -> {
if(getAdapterPosition() != RecyclerView.NO_POSITION) { if (getAdapterPosition() != RecyclerView.NO_POSITION) {
listener.onViewMedia(getAdapterPosition(), urlIndex, v); listener.onViewMedia(getAdapterPosition(), urlIndex, v);
} }
}); });

View file

@ -277,4 +277,6 @@
<string name="action_set_caption">説明を設定</string> <string name="action_set_caption">説明を設定</string>
<string name="action_remove_media">消去</string> <string name="action_remove_media">消去</string>
<string name="pref_title_absolute_time">絶対時間で表示</string>
</resources> </resources>

View file

@ -347,4 +347,6 @@
<string name="profile_metadata_label_label">Label</string> <string name="profile_metadata_label_label">Label</string>
<string name="profile_metadata_content_label">Content</string> <string name="profile_metadata_content_label">Content</string>
<string name="pref_title_absolute_time">Use absolute time</string>
</resources> </resources>

View file

@ -42,6 +42,11 @@
android:key="alwaysShowSensitiveMedia" android:key="alwaysShowSensitiveMedia"
android:title="@string/pref_title_alway_show_sensitive_media" /> android:title="@string/pref_title_alway_show_sensitive_media" />
<CheckBoxPreference
android:defaultValue="true"
android:key="absoluteTimeView"
android:title="@string/pref_title_absolute_time" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_publishing"> <PreferenceCategory android:title="@string/pref_publishing">