diff options
author | Tom Hennen <TomHennen@users.noreply.github.com> | 2015-06-27 08:24:01 -0400 |
---|---|---|
committer | Tom Hennen <TomHennen@users.noreply.github.com> | 2015-06-27 08:24:01 -0400 |
commit | e332df4af572528f673745da2c7c13bf6ef43def (patch) | |
tree | 62c87c172a2825062427e3b54f9de64f32c78196 | |
parent | ddf34dccf385e4f2cd5a7d5d7d501b32d0010eb6 (diff) | |
parent | 8f75777b41c3ca23e8f834025cafd74c0e96f3bc (diff) | |
download | AntennaPod-e332df4af572528f673745da2c7c13bf6ef43def.zip |
Merge pull request #957 from mfietz/issue/798-update-time
Set interval or time of day for automatic updates
6 files changed, 143 insertions, 47 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/ui/PreferencesTest.java b/app/src/androidTest/java/de/test/antennapod/ui/PreferencesTest.java index b978a119d..99954bf77 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/PreferencesTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/PreferencesTest.java @@ -241,9 +241,9 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference } public void testDisableUpdateInterval() { - solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervall_title)); + solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervallOrTime_sum)); solo.waitForDialogToOpen(); - solo.clickOnText(solo.getString(R.string.pref_update_interval_hours_manual)); + solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervallOrTime_Disable)); solo.waitForCondition(new Condition() { @Override public boolean isSatisfied() { @@ -253,7 +253,9 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference } public void testSetUpdateInterval() { - solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervall_title)); + solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervallOrTime_title)); + solo.waitForDialogToOpen(); + solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervallOrTime_Interval)); solo.waitForDialogToOpen(); String search = "12 " + solo.getString(R.string.pref_update_interval_hours_plural); solo.clickOnText(search); diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index 2c6afd126..3a8abcc40 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.preferences; import android.app.Activity; import android.app.AlertDialog; +import android.app.TimePickerDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -16,15 +17,16 @@ import android.preference.Preference; import android.preference.PreferenceScreen; import android.text.Editable; import android.text.TextWatcher; +import android.text.format.DateFormat; import android.util.Log; import android.widget.EditText; +import android.widget.TimePicker; import android.widget.Toast; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.TimeUnit; import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.R; @@ -202,14 +204,10 @@ public class PreferenceController { }); ui.findPreference(UserPreferences.PREF_UPDATE_INTERVAL) - .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + .setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (newValue instanceof String) { - int hours = Integer.valueOf((String)newValue); - long millis = TimeUnit.HOURS.toMillis(hours); - UserPreferences.restartUpdateAlarm(millis, millis); - } + public boolean onPreferenceClick(Preference preference) { + showUpdateIntervalTimePreferencesDialog(); return true; } }); @@ -366,7 +364,6 @@ public class PreferenceController { return true; } }); - buildUpdateIntervalPreference(); buildSmartMarkAsPlayedPreference(); buildAutodownloadSelectedNetworsPreference(); setSelectedNetworksEnabled(UserPreferences @@ -399,11 +396,8 @@ public class PreferenceController { ui.findPreference(PreferenceController.PREF_GPODNET_HOSTNAME).setSummary(GpodnetPreferences.getHostname()); } - private void buildUpdateIntervalPreference() { + private String[] getUpdateIntervalEntries(final String[] values) { final Resources res = ui.getActivity().getResources(); - - ListPreference pref = (ListPreference) ui.findPreference(UserPreferences.PREF_UPDATE_INTERVAL); - String[] values = res.getStringArray(R.array.update_intervall_values); String[] entries = new String[values.length]; for (int x = 0; x < values.length; x++) { Integer v = Integer.parseInt(values[x]); @@ -412,19 +406,15 @@ public class PreferenceController { 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); + 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); + entries[x] = v + " " + res.getString(R.string.pref_update_interval_hours_plural); break; } } - pref.setEntries(entries); - + return entries; } private void buildSmartMarkAsPlayedPreference() { @@ -617,7 +607,63 @@ public class PreferenceController { builder.create().show(); } + private void showUpdateIntervalTimePreferencesDialog() { + final Context context = ui.getActivity(); + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.pref_autoUpdateIntervallOrTime_title); + builder.setMessage(R.string.pref_autoUpdateIntervallOrTime_message); + builder.setNegativeButton(R.string.pref_autoUpdateIntervallOrTime_Disable, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + UserPreferences.setUpdateInterval(0); + } + }); + builder.setNeutralButton(R.string.pref_autoUpdateIntervallOrTime_Interval, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(context.getString(R.string.pref_autoUpdateIntervallOrTime_Interval)); + final String[] values = context.getResources().getStringArray(R.array.update_intervall_values); + final String[] entries = getUpdateIntervalEntries(values); + builder.setSingleChoiceItems(entries, -1, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int hours = Integer.valueOf(values[which]); + UserPreferences.setUpdateInterval(hours); + dialog.dismiss(); + } + }); + builder.setNegativeButton(context.getString(R.string.cancel_label), null); + builder.show(); + } + }); + builder.setPositiveButton(R.string.pref_autoUpdateIntervallOrTime_TimeOfDay, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int hourOfDay = 7, minute = 0; + int[] updateTime = UserPreferences.getUpdateTimeOfDay(); + if (updateTime.length == 2) { + hourOfDay = updateTime[0]; + minute = updateTime[1]; + } + TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() { + @Override + public void onTimeSet(TimePicker view, int hourOfDay, int minute) { + if (view.getTag() == null) { // onTimeSet() may get called twice! + view.setTag("TAGGED"); + UserPreferences.setUpdateTimeOfDay(hourOfDay, minute); + } + } + }, hourOfDay, minute, DateFormat.is24HourFormat(context)); + timePickerDialog.setTitle(context.getString(R.string.pref_autoUpdateIntervallOrTime_TimeOfDay)); + timePickerDialog.show(); + } + } + + ); + builder.show(); + } public static interface PreferenceUI { diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 564630afc..04699bac1 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -96,13 +96,10 @@ </PreferenceCategory> <PreferenceCategory android:title="@string/network_pref"> - <ListPreference - android:defaultValue="0" - android:entries="@array/update_intervall_values" - android:entryValues="@array/update_intervall_values" + <Preference android:key="prefAutoUpdateIntervall" - android:summary="@string/pref_autoUpdateIntervall_sum" - android:title="@string/pref_autoUpdateIntervall_title"/> + android:summary="@string/pref_autoUpdateIntervallOrTime_sum" + android:title="@string/pref_autoUpdateIntervallOrTime_title"/> <CheckBoxPreference android:defaultValue="false" android:enabled="true" diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 5d53db5b3..d96d25fc2 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -206,7 +207,24 @@ public class UserPreferences { } public static long getUpdateInterval() { - return readUpdateInterval(prefs.getString(PREF_UPDATE_INTERVAL, "0")); + String updateInterval = prefs.getString(PREF_UPDATE_INTERVAL, "0"); + if(false == updateInterval.contains(":")) { + return readUpdateInterval(updateInterval); + } else { + return 0; + } + } + + public static int[] getUpdateTimeOfDay() { + String datetime = prefs.getString(PREF_UPDATE_INTERVAL, ""); + if(datetime.length() >= 3 && datetime.contains(":")) { + String[] parts = datetime.split(":"); + int hourOfDay = Integer.valueOf(parts[0]); + int minute = Integer.valueOf(parts[1]); + return new int[] { hourOfDay, minute }; + } else { + return new int[0]; + } } public static boolean isAllowMobileUpdate() { @@ -317,6 +335,16 @@ public class UserPreferences { } /** + * Sets the update interval value. Should only be used for testing purposes! + */ + public static void setUpdateTimeOfDay(int hourOfDay, int minute) { + prefs.edit() + .putString(PREF_UPDATE_INTERVAL, hourOfDay + ":" + minute) + .apply(); + restartUpdateAlarm(); + } + + /** * Change the auto-flattr settings * * @param enabled Whether automatic flattring should be enabled at all @@ -493,14 +521,19 @@ public class UserPreferences { } public static void restartUpdateAlarm() { - long hours = getUpdateInterval(); - restartUpdateAlarm(TimeUnit.SECONDS.toMillis(10), hours); + int[] timeOfDay = getUpdateTimeOfDay(); + if (timeOfDay.length == 2) { + restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1]); + } else { + long hours = getUpdateInterval(); + restartUpdateIntervalAlarm(TimeUnit.SECONDS.toMillis(10), hours); + } } /** - * Updates alarm registered with the AlarmManager service or deactivates it. + * Sets the interval in which the feeds are refreshed automatically */ - public static void restartUpdateAlarm(long triggerAtMillis, long intervalMillis) { + public static void restartUpdateIntervalAlarm(long triggerAtMillis, long intervalMillis) { Log.d(TAG, "Restarting update alarm."); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0, @@ -518,6 +551,31 @@ public class UserPreferences { } /** + * Sets time of day the feeds are refreshed automatically + */ + public static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute) { + Log.d(TAG, "Restarting update alarm."); + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0, + new Intent(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedUpdateReceiver.class), 0); + alarmManager.cancel(updateIntent); + + Calendar now = Calendar.getInstance(); + Calendar alarm = (Calendar)now.clone(); + alarm.set(Calendar.HOUR_OF_DAY, hoursOfDay); + alarm.set(Calendar.MINUTE, minute); + if(alarm.before(now)) { + alarm.add(Calendar.DATE, 1); + } + + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, + alarm.getTimeInMillis(), + AlarmManager.INTERVAL_DAY, + updateIntent); + Log.d(TAG, "Changed alarm to new time of day " + hoursOfDay + ":" + minute); + } + + /** * Reads episode cache size as it is saved in the episode_cache_size_values array. */ public static int readEpisodeCacheSize(String valueFromPrefs) { diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 4ecf2cf61..afcb31078 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -20,18 +20,7 @@ <item>60</item> </integer-array> - <string-array name="update_intervall_options"> - <item>Manual</item> - <item>1 hour</item> - <item>2 hours</item> - <item>4 hours</item> - <item>8 hours</item> - <item>12 hours</item> - <item>24 hours</item> - </string-array> - <string-array name="update_intervall_values"> - <item>0</item> <item>1</item> <item>2</item> <item>4</item> diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index c2147cccc..0f1830202 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -253,8 +253,12 @@ <string name="pref_smart_mark_as_played_title">Smart mark as played</string> <string name="playback_pref">Playback</string> <string name="network_pref">Network</string> - <string name="pref_autoUpdateIntervall_title">Update Interval</string> - <string name="pref_autoUpdateIntervall_sum">Specify an interval in which the feeds are refreshed automatically or disable it</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 feeds 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_downloadMediaOnWifiOnly_sum">Download media files only over WiFi</string> <string name="pref_followQueue_title">Continuous Playback</string> <string name="pref_downloadMediaOnWifiOnly_title">WiFi media download</string> |