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 @@ - + android:summary="@string/pref_autoUpdateIntervallOrTime_sum" + android:title="@string/pref_autoUpdateIntervallOrTime_title"/> = 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() { @@ -316,6 +334,16 @@ public class UserPreferences { restartUpdateAlarm(); } + /** + * 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 * @@ -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, @@ -517,6 +550,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. */ 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 @@ 60 - - Manual - 1 hour - 2 hours - 4 hours - 8 hours - 12 hours - 24 hours - - - 0 1 2 4 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 @@ Smart mark as played Playback Network - Update Interval - Specify an interval in which the feeds are refreshed automatically or disable it + Update Interval or Time of Day + Specify an interval or a specific time of day to refresh the feeds automatically + You can set an interval like \"every 2 hours\", set a specific time of day like \"7:00 AM\" or disable automatic updates altogether.\n\nPlease note: Update times are inexact. You may encounter a short delay." + Disable + Set Interval + Set Time of Day Download media files only over WiFi Continuous Playback WiFi media download