Merge pull request #5108 from peakvalleytech/IntervalOrTime

New feed update interval or time dialog
This commit is contained in:
ByteHamster 2021-04-23 20:24:53 +02:00 committed by GitHub
commit 7b5f29e090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 220 additions and 123 deletions

View File

@ -3,10 +3,21 @@ package de.test.antennapod.ui;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import androidx.preference.PreferenceManager;
import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;
import androidx.test.espresso.matcher.RootMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.PreferenceActivity;
import de.danoeh.antennapod.core.preferences.UserPreferences;
@ -20,14 +31,8 @@ import de.danoeh.antennapod.fragment.EpisodesFragment;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.fragment.SubscriptionFragment;
import de.test.antennapod.EspressoTestUtils;
import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText;
@ -44,6 +49,7 @@ import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static de.test.antennapod.EspressoTestUtils.clickPreference;
import static de.test.antennapod.EspressoTestUtils.waitForView;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertTrue;
@ -53,18 +59,22 @@ public class PreferencesTest {
private Resources res;
@Rule
public ActivityTestRule<PreferenceActivity> mActivityRule = new ActivityTestRule<>(PreferenceActivity.class, false, false);
public ActivityTestRule<PreferenceActivity> activityTestRule =
new ActivityTestRule<>(PreferenceActivity.class,
false,
false);
@Before
public void setUp() {
EspressoTestUtils.clearDatabase();
EspressoTestUtils.clearPreferences();
mActivityRule.launchActivity(new Intent());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivityRule.getActivity());
activityTestRule.launchActivity(new Intent());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activityTestRule.getActivity());
prefs.edit().putBoolean(UserPreferences.PREF_ENABLE_AUTODL, true).commit();
res = mActivityRule.getActivity().getResources();
UserPreferences.init(mActivityRule.getActivity());
res = activityTestRule.getActivity().getResources();
UserPreferences.init(activityTestRule.getActivity());
}
@Test
@ -254,8 +264,10 @@ public class PreferencesTest {
@Test
public void testDisableUpdateInterval() {
clickPreference(R.string.network_pref);
onView(withText(R.string.pref_autoUpdateIntervallOrTime_title)).perform(click());
onView(withText(R.string.pref_autoUpdateIntervallOrTime_Disable)).perform(click());
clickPreference(R.string.feed_refresh_title);
onView(withText(R.string.feed_refresh_never)).perform(click());
onView(withId(R.id.disableRadioButton)).perform(click());
onView(withText(R.string.confirm_label)).perform(click());
Awaitility.await().atMost(1000, MILLISECONDS)
.until(() -> UserPreferences.getUpdateInterval() == 0);
}
@ -263,12 +275,14 @@ public class PreferencesTest {
@Test
public void testSetUpdateInterval() {
clickPreference(R.string.network_pref);
clickPreference(R.string.pref_autoUpdateIntervallOrTime_title);
onView(withText(R.string.pref_autoUpdateIntervallOrTime_Interval)).perform(click());
String search = "12 " + res.getString(R.string.pref_update_interval_hours_plural);
onView(withText(search)).perform(click());
clickPreference(R.string.feed_refresh_title);
onView(withId(R.id.intervalRadioButton)).perform(click());
onView(withId(R.id.spinner)).perform(click());
int position = 1; // an arbitrary position
onData(anything()).inRoot(RootMatchers.isPlatformPopup()).atPosition(position).perform(click());
onView(withText(R.string.confirm_label)).perform(click());
Awaitility.await().atMost(1000, MILLISECONDS)
.until(() -> UserPreferences.getUpdateInterval() == TimeUnit.HOURS.toMillis(12));
.until(() -> UserPreferences.getUpdateInterval() == TimeUnit.HOURS.toMillis(2));
}
@Test

View File

@ -0,0 +1,106 @@
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import androidx.appcompat.app.AlertDialog;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.databinding.FeedRefreshDialogBinding;
import org.apache.commons.lang3.ArrayUtils;
import java.util.concurrent.TimeUnit;
public class FeedRefreshIntervalDialog {
private static final int[] INTERVAL_VALUES_HOURS = {1, 2, 4, 8, 12, 24, 72};
private final Context context;
private FeedRefreshDialogBinding viewBinding;
public FeedRefreshIntervalDialog(Context context) {
this.context = context;
}
public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.feed_refresh_title);
builder.setMessage(R.string.feed_refresh_sum);
viewBinding = FeedRefreshDialogBinding.inflate(LayoutInflater.from(context));
builder.setView(viewBinding.getRoot());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item, buildSpinnerEntries());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewBinding.spinner.setAdapter(spinnerArrayAdapter);
viewBinding.timePicker.setIs24HourView(DateFormat.is24HourFormat(context));
viewBinding.spinner.setSelection(ArrayUtils.indexOf(INTERVAL_VALUES_HOURS, 24));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
viewBinding.timePicker.setHour(8);
viewBinding.timePicker.setMinute(0);
} else {
viewBinding.timePicker.setCurrentHour(8);
viewBinding.timePicker.setCurrentMinute(0);
}
long currInterval = UserPreferences.getUpdateInterval();
int[] updateTime = UserPreferences.getUpdateTimeOfDay();
if (currInterval > 0) {
viewBinding.spinner.setSelection(ArrayUtils.indexOf(INTERVAL_VALUES_HOURS,
(int) TimeUnit.MILLISECONDS.toHours(currInterval)));
viewBinding.intervalRadioButton.setChecked(true);
} else if (updateTime.length == 2 && updateTime[0] >= 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
viewBinding.timePicker.setHour(updateTime[0]);
viewBinding.timePicker.setMinute(updateTime[1]);
} else {
viewBinding.timePicker.setCurrentHour(updateTime[0]);
viewBinding.timePicker.setCurrentMinute(updateTime[1]);
}
viewBinding.timeRadioButton.setChecked(true);
} else {
viewBinding.disableRadioButton.setChecked(true);
}
updateVisibility();
viewBinding.radioGroup.setOnCheckedChangeListener((radioGroup, i) -> updateVisibility());
builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
if (viewBinding.intervalRadioButton.isChecked()) {
UserPreferences.setUpdateInterval(INTERVAL_VALUES_HOURS[viewBinding.spinner.getSelectedItemPosition()]);
} else if (viewBinding.timeRadioButton.isChecked()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
UserPreferences.setUpdateTimeOfDay(viewBinding.timePicker.getHour(),
viewBinding.timePicker.getMinute());
} else {
UserPreferences.setUpdateTimeOfDay(viewBinding.timePicker.getCurrentHour(),
viewBinding.timePicker.getCurrentMinute());
}
} else if (viewBinding.disableRadioButton.isChecked()) {
UserPreferences.disableAutoUpdate(context);
} else {
throw new IllegalStateException("Unexpected error.");
}
});
builder.setNegativeButton(R.string.cancel_label, null);
builder.show();
}
private String[] buildSpinnerEntries() {
final Resources res = context.getResources();
String[] entries = new String[INTERVAL_VALUES_HOURS.length];
for (int i = 0; i < INTERVAL_VALUES_HOURS.length; i++) {
int hours = INTERVAL_VALUES_HOURS[i];
entries[i] = res.getQuantityString(R.plurals.feed_refresh_every_x_hours, hours, hours);
}
return entries;
}
private void updateVisibility() {
viewBinding.spinner.setVisibility(viewBinding.intervalRadioButton.isChecked() ? View.VISIBLE : View.GONE);
viewBinding.timePicker.setVisibility(viewBinding.timeRadioButton.isChecked() ? View.VISIBLE : View.GONE);
}
}

View File

@ -1,23 +1,25 @@
package de.danoeh.antennapod.fragment.preferences;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceFragmentCompat;
import android.text.format.DateFormat;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.PreferenceActivity;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.dialog.FeedRefreshIntervalDialog;
import de.danoeh.antennapod.dialog.ProxyDialog;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;
public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
public class NetworkPreferencesFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String PREF_SCREEN_AUTODL = "prefAutoDownloadSettings";
private static final String PREF_PROXY = "prefProxy";
@ -31,6 +33,13 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
public void onStart() {
super.onStart();
((PreferenceActivity) getActivity()).getSupportActionBar().setTitle(R.string.network_pref);
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onStop() {
super.onStop();
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
}
@Override
@ -47,9 +56,10 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
});
findPreference(UserPreferences.PREF_UPDATE_INTERVAL)
.setOnPreferenceClickListener(preference -> {
showUpdateIntervalTimePreferencesDialog();
new FeedRefreshIntervalDialog(getContext()).show();
return true;
});
findPreference(UserPreferences.PREF_PARALLEL_DOWNLOADS)
.setOnPreferenceChangeListener(
(preference, o) -> {
@ -67,6 +77,9 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
});
}
/**
* Used to init and handle changes to view
*/
private void setUpdateIntervalText() {
Context context = getActivity().getApplicationContext();
String val;
@ -74,7 +87,7 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
if (interval > 0) {
int hours = (int) TimeUnit.MILLISECONDS.toHours(interval);
val = context.getResources().getQuantityString(
R.plurals.pref_autoUpdateIntervallOrTime_every_hours, hours, hours);
R.plurals.feed_refresh_every_x_hours, hours, hours);
} else {
int[] timeOfDay = UserPreferences.getUpdateTimeOfDay();
if (timeOfDay.length == 2) {
@ -82,94 +95,29 @@ public class NetworkPreferencesFragment extends PreferenceFragmentCompat {
cal.set(Calendar.HOUR_OF_DAY, timeOfDay[0]);
cal.set(Calendar.MINUTE, timeOfDay[1]);
String timeOfDayStr = DateFormat.getTimeFormat(context).format(cal.getTime());
val = String.format(context.getString(R.string.pref_autoUpdateIntervallOrTime_at),
val = String.format(context.getString(R.string.feed_refresh_interval_at),
timeOfDayStr);
} else {
val = context.getString(R.string.pref_smart_mark_as_played_disabled); // TODO: Is this a bug? Otherwise document why is this related to smart mark???
val = context.getString(R.string.feed_refresh_never);
}
}
String summary = context.getString(R.string.pref_autoUpdateIntervallOrTime_sum) + "\n"
String summary = context.getString(R.string.feed_refresh_sum) + "\n"
+ String.format(context.getString(R.string.pref_current_value), val);
findPreference(UserPreferences.PREF_UPDATE_INTERVAL).setSummary(summary);
}
private void setParallelDownloadsText(int downloads) {
final Resources res = getActivity().getResources();
String s = res.getString(R.string.parallel_downloads, downloads);
findPreference(UserPreferences.PREF_PARALLEL_DOWNLOADS).setSummary(s);
}
private void showUpdateIntervalTimePreferencesDialog() {
final Context context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.pref_autoUpdateIntervallOrTime_title);
builder.setMessage(R.string.pref_autoUpdateIntervallOrTime_message);
builder.setPositiveButton(R.string.pref_autoUpdateIntervallOrTime_Interval, (dialog, which) -> {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle(context.getString(R.string.pref_autoUpdateIntervallOrTime_Interval));
final String[] values = context.getResources().getStringArray(R.array.update_intervall_values);
final String[] entries = getUpdateIntervalEntries(values);
long currInterval = UserPreferences.getUpdateInterval();
int checkedItem = -1;
if(currInterval > 0) {
String currIntervalStr = String.valueOf(TimeUnit.MILLISECONDS.toHours(currInterval));
checkedItem = ArrayUtils.indexOf(values, currIntervalStr);
}
builder1.setSingleChoiceItems(entries, checkedItem, (dialog1, which1) -> {
int hours = Integer.parseInt(values[which1]);
UserPreferences.setUpdateInterval(hours);
dialog1.dismiss();
setUpdateIntervalText();
});
builder1.setNegativeButton(context.getString(R.string.cancel_label), null);
builder1.show();
});
builder.setNegativeButton(R.string.pref_autoUpdateIntervallOrTime_TimeOfDay, (dialog, which) -> {
int hourOfDay = 7;
int minute = 0;
int[] updateTime = UserPreferences.getUpdateTimeOfDay();
if (updateTime.length == 2) {
hourOfDay = updateTime[0];
minute = updateTime[1];
}
TimePickerDialog timePickerDialog = new TimePickerDialog(context,
(view, selectedHourOfDay, selectedMinute) -> {
if (view.getTag() == null) { // onTimeSet() may get called twice!
view.setTag("TAGGED");
UserPreferences.setUpdateTimeOfDay(selectedHourOfDay, selectedMinute);
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (UserPreferences.PREF_UPDATE_INTERVAL.equals(key)) {
setUpdateIntervalText();
}
}, hourOfDay, minute, DateFormat.is24HourFormat(context));
timePickerDialog.setTitle(context.getString(R.string.pref_autoUpdateIntervallOrTime_TimeOfDay));
timePickerDialog.show();
});
builder.setNeutralButton(R.string.pref_autoUpdateIntervallOrTime_Disable, (dialog, which) -> {
UserPreferences.disableAutoUpdate(context);
setUpdateIntervalText();
});
builder.show();
}
private String[] getUpdateIntervalEntries(final String[] values) {
final Resources res = getActivity().getResources();
String[] entries = new String[values.length];
for (int x = 0; x < values.length; x++) {
Integer v = Integer.parseInt(values[x]);
switch (v) {
case 0:
entries[x] = res.getString(R.string.pref_update_interval_hours_manual);
break;
case 1:
entries[x] = v + " " + res.getString(R.string.pref_update_interval_hours_singular);
break;
default:
entries[x] = v + " " + res.getString(R.string.pref_update_interval_hours_plural);
break;
}
}
return entries;
}
}

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<RadioButton
android:id="@+id/intervalRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/feed_refresh_interval"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<RadioButton
android:id="@+id/timeRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/feed_refresh_time" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:visibility="gone" />
<RadioButton
android:id="@+id/disableRadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/feed_refresh_never" />
</RadioGroup>

View File

@ -6,8 +6,8 @@
<PreferenceCategory android:title="@string/automation">
<Preference
android:key="prefAutoUpdateIntervall"
android:summary="@string/pref_autoUpdateIntervallOrTime_sum"
android:title="@string/pref_autoUpdateIntervallOrTime_title"/>
android:summary="@string/feed_refresh_sum"
android:title="@string/feed_refresh_title"/>
<Preference
android:summary="@string/pref_automatic_download_sum"
android:key="prefAutoDownloadSettings"

View File

@ -4,4 +4,8 @@
<ignore path="res/drawable/ic_settings.xml" />
<ignore path="res/drawable/ic_settings_white.xml" />
</issue>
<issue id="MissingDefaultResource">
<ignore path="**/values-**/strings.xml" />
</issue>
</lint>

View File

@ -45,16 +45,6 @@
<item>60</item>
</integer-array>
<string-array name="update_intervall_values">
<item>1</item>
<item>2</item>
<item>4</item>
<item>8</item>
<item>12</item>
<item>24</item>
<item>72</item>
</string-array>
<string-array name="episode_cache_size_entries">
<item>5</item>
<item>10</item>

View File

@ -397,14 +397,13 @@
<string name="playback_pref_sum">Headphone controls, Skip intervals, Queue</string>
<string name="network_pref">Network</string>
<string name="network_pref_sum">Update interval, Download controls, Mobile data</string>
<string name="pref_autoUpdateIntervallOrTime_title">Update Interval or Time of Day</string>
<string name="pref_autoUpdateIntervallOrTime_sum">Specify an interval or a specific time of day to refresh the podcasts automatically</string>
<string name="pref_autoUpdateIntervallOrTime_message">You can set an <i>interval</i> like \"every 2 hours\", set a specific <i>time of day</i> like \"7:00 AM\" or <i>disable</i> automatic updates altogether.\n\n<small>Please note: Update times are inexact. You may encounter a short delay.</small></string>
<string name="pref_autoUpdateIntervallOrTime_Disable">Disable</string>
<string name="pref_autoUpdateIntervallOrTime_Interval">Set Interval</string>
<string name="pref_autoUpdateIntervallOrTime_TimeOfDay">Set Time of Day</string>
<string name="pref_autoUpdateIntervallOrTime_at">at %1$s</string>
<plurals name="pref_autoUpdateIntervallOrTime_every_hours">
<string name="feed_refresh_title">Refresh podcasts</string>
<string name="feed_refresh_sum">Specify an interval or a specific time to look for new episodes automatically</string>
<string name="feed_refresh_interval">Interval</string>
<string name="feed_refresh_time">Time</string>
<string name="feed_refresh_never">Never</string>
<string name="feed_refresh_interval_at">at %1$s</string>
<plurals name="feed_refresh_every_x_hours">
<item quantity="one">Every hour</item>
<item quantity="other">Every %d hours</item>
</plurals>
@ -449,9 +448,6 @@
<string name="pref_theme_title_dark">Dark</string>
<string name="pref_theme_title_trueblack">Black (AMOLED ready)</string>
<string name="pref_episode_cache_unlimited">Unlimited</string>
<string name="pref_update_interval_hours_plural">hours</string>
<string name="pref_update_interval_hours_singular">hour</string>
<string name="pref_update_interval_hours_manual">Manual</string>
<string name="pref_gpodnet_authenticate_title">Login</string>
<string name="pref_gpodnet_authenticate_sum">Login with your gpodder.net account in order to sync your subscriptions.</string>
<string name="pref_gpodnet_logout_title">Logout</string>