Detailed changes
@@ -1,98 +0,0 @@
-package eu.siacs.conversations.ui;
-
-import android.content.Context;
-import android.content.res.TypedArray;
-import android.preference.DialogPreference;
-import android.preference.Preference;
-import android.util.AttributeSet;
-import android.view.View;
-import android.widget.TimePicker;
-
-import java.text.DateFormat;
-import java.util.Calendar;
-import java.util.Date;
-
-
-public class TimePreference extends DialogPreference implements Preference.OnPreferenceChangeListener {
- private TimePicker picker = null;
- public final static long DEFAULT_VALUE = 0;
-
- public TimePreference(final Context context, final AttributeSet attrs) {
- super(context, attrs, 0);
- this.setOnPreferenceChangeListener(this);
- }
-
- protected void setTime(final long time) {
- persistLong(time);
- notifyDependencyChange(shouldDisableDependents());
- notifyChanged();
- updateSummary(time);
- }
-
- private void updateSummary(final long time) {
- final DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getContext());
- final Date date = minutesToCalender(time).getTime();
- setSummary(dateFormat.format(date.getTime()));
- }
-
- @Override
- protected View onCreateDialogView() {
- picker = new TimePicker(getContext());
- picker.setIs24HourView(android.text.format.DateFormat.is24HourFormat(getContext()));
- return picker;
- }
-
- @SuppressWarnings("NullableProblems")
- @Override
- protected void onBindDialogView(final View v) {
- super.onBindDialogView(v);
- long time = getPersistedLong(DEFAULT_VALUE);
-
- picker.setCurrentHour((int) ((time % (24 * 60)) / 60));
- picker.setCurrentMinute((int) ((time % (24 * 60)) % 60));
- }
-
- @Override
- protected void onDialogClosed(final boolean positiveResult) {
- super.onDialogClosed(positiveResult);
-
- if (positiveResult) {
- setTime(picker.getCurrentHour() * 60 + picker.getCurrentMinute());
- }
- }
-
- private static Calendar minutesToCalender(long time) {
- final Calendar c = Calendar.getInstance();
- c.set(Calendar.HOUR_OF_DAY, (int) ((time % (24 * 60)) / 60));
- c.set(Calendar.MINUTE, (int) ((time % (24 * 60)) % 60));
- return c;
- }
-
- public static long minutesToTimestamp(long time) {
- return minutesToCalender(time).getTimeInMillis();
- }
-
- @Override
- protected Object onGetDefaultValue(final TypedArray a, final int index) {
- return a.getInteger(index, 0);
- }
-
- @Override
- protected void onSetInitialValue(final boolean restorePersistedValue, final Object defaultValue) {
- long time;
- if (defaultValue instanceof Long) {
- time = restorePersistedValue ? getPersistedLong((Long) defaultValue) : (Long) defaultValue;
- } else {
- time = restorePersistedValue ? getPersistedLong(DEFAULT_VALUE) : DEFAULT_VALUE;
- }
-
- setTime(time);
- updateSummary(time);
- }
-
- @Override
- public boolean onPreferenceChange(final Preference preference, final Object newValue) {
- ((TimePreference) preference).updateSummary((Long)newValue);
- return true;
- }
-}
@@ -3,15 +3,31 @@ package eu.siacs.conversations.ui.fragment.settings;
import android.os.Bundle;
import androidx.annotation.Nullable;
+import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;
import eu.siacs.conversations.R;
+import eu.siacs.conversations.utils.UIHelper;
-public class AttachmentsSettingsFragment extends PreferenceFragmentCompat {
+public class AttachmentsSettingsFragment extends XmppPreferenceFragment {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.preferences_attachments, rootKey);
+ final ListPreference autoAcceptFileSize = findPreference("auto_accept_file_size");
+ if (autoAcceptFileSize == null) {
+ throw new IllegalStateException("The preference resource file is missing preferences");
+ }
+ setValues(
+ autoAcceptFileSize,
+ R.array.file_size_values,
+ value -> {
+ if (value <= 0) {
+ return getString(R.string.never);
+ } else {
+ return UIHelper.filesizeToString(value);
+ }
+ });
}
@Override
@@ -71,16 +71,10 @@ public class BackupSettingsFragment extends XmppPreferenceFragment {
R.string.pref_create_backup_summary,
FileBackend.getBackupDirectory(requireContext()).getAbsolutePath()));
createOneOffBackup.setOnPreferenceClickListener(this::onBackupPreferenceClicked);
- final int[] choices = getResources().getIntArray(R.array.recurring_backup_values);
- final CharSequence[] entries = new CharSequence[choices.length];
- final CharSequence[] entryValues = new CharSequence[choices.length];
- for (int i = 0; i < choices.length; ++i) {
- entryValues[i] = String.valueOf(choices[i]);
- entries[i] = timeframeValueToName(requireContext(), choices[i]);
- }
- recurringBackup.setEntries(entries);
- recurringBackup.setEntryValues(entryValues);
- recurringBackup.setSummaryProvider(new TimeframeSummaryProvider());
+ setValues(
+ recurringBackup,
+ R.array.recurring_backup_values,
+ value -> timeframeValueToName(requireContext(), value));
}
@Override
@@ -4,6 +4,7 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
+import androidx.annotation.ArrayRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
@@ -11,7 +12,9 @@ import androidx.preference.ListPreference;
import androidx.preference.Preference;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
+import com.google.common.base.Function;
import com.google.common.base.Strings;
+import com.google.common.primitives.Ints;
import eu.siacs.conversations.AppSettings;
import eu.siacs.conversations.R;
@@ -21,6 +24,7 @@ import eu.siacs.conversations.services.MemorizingTrustManager;
import java.security.KeyStoreException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.concurrent.Callable;
public class SecuritySettingsFragment extends XmppPreferenceFragment {
@@ -36,19 +40,12 @@ public class SecuritySettingsFragment extends XmppPreferenceFragment {
throw new IllegalStateException("The preference resource file is missing preferences");
}
omemo.setSummaryProvider(new OmemoSummaryProvider());
- final int[] choices = getResources().getIntArray(R.array.automatic_message_deletion_values);
- final CharSequence[] entries = new CharSequence[choices.length];
- final CharSequence[] entryValues = new CharSequence[choices.length];
- for (int i = 0; i < choices.length; ++i) {
- entryValues[i] = String.valueOf(choices[i]);
- entries[i] = timeframeValueToName(requireContext(), choices[i]);
- }
- automaticMessageDeletion.setEntries(entries);
- automaticMessageDeletion.setEntryValues(entryValues);
- automaticMessageDeletion.setSummaryProvider(new TimeframeSummaryProvider());
+ setValues(
+ automaticMessageDeletion,
+ R.array.automatic_message_deletion_values,
+ value -> timeframeValueToName(requireContext(), value));
}
-
@Override
protected void onSharedPreferenceChanged(@NonNull String key) {
super.onSharedPreferenceChanged(key);
@@ -151,7 +148,6 @@ public class SecuritySettingsFragment extends XmppPreferenceFragment {
.show();
}
-
private static class OmemoSummaryProvider
implements Preference.SummaryProvider<ListPreference> {
@@ -4,12 +4,14 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
+import androidx.annotation.ArrayRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
+import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.primitives.Ints;
@@ -101,14 +103,29 @@ public abstract class XmppPreferenceFragment extends PreferenceFragmentCompat {
}
}
- protected static class TimeframeSummaryProvider
- implements Preference.SummaryProvider<ListPreference> {
-
- @Nullable
- @Override
- public CharSequence provideSummary(@NonNull ListPreference preference) {
- final Integer value = Ints.tryParse(Strings.nullToEmpty(preference.getValue()));
- return timeframeValueToName(preference.getContext(), value == null ? 0 : value);
+ protected void setValues(
+ final ListPreference listPreference,
+ @ArrayRes int resId,
+ final Function<Integer, String> valueToName) {
+ final int[] choices = getResources().getIntArray(resId);
+ final CharSequence[] entries = new CharSequence[choices.length];
+ final CharSequence[] entryValues = new CharSequence[choices.length];
+ for (int i = 0; i < choices.length; ++i) {
+ final int value = choices[i];
+ entryValues[i] = String.valueOf(choices[i]);
+ entries[i] = valueToName.apply(value);
}
+ listPreference.setEntries(entries);
+ listPreference.setEntryValues(entryValues);
+ listPreference.setSummaryProvider(
+ new Preference.SummaryProvider<ListPreference>() {
+ @Nullable
+ @Override
+ public CharSequence provideSummary(@NonNull ListPreference preference) {
+ final Integer value =
+ Ints.tryParse(Strings.nullToEmpty(preference.getValue()));
+ return valueToName.apply(value == null ? 0 : value);
+ }
+ });
}
}
@@ -1,22 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
- <string-array name="filesizes">
- <item>@string/never</item>
- <item>256 KiB</item>
- <item>512 KiB</item>
- <item>1 MiB</item>
- <item>5 MiB</item>
- <item>10 MiB</item>
- </string-array>
- <string-array name="filesizes_values">
+ <integer-array name="file_size_values">
<item>0</item>
- <item>262144</item>
<item>524288</item>
<item>1048576</item>
<item>5242880</item>
<item>10485760</item>
- </string-array>
+ <item>52428800</item>
+ </integer-array>
<integer-array name="mute_options_durations">
<item>1800</item>
@@ -22,8 +22,6 @@
<PreferenceCategory android:title="@string/pref_category_receiving">
<ListPreference
android:defaultValue="@integer/auto_accept_filesize"
- android:entries="@array/filesizes"
- android:entryValues="@array/filesizes_values"
android:icon="@drawable/ic_download_24dp"
android:key="auto_accept_file_size"
android:title="@string/pref_automatic_download"