Merge pull request #957 from mfietz/issue/798-update-time

Set interval or time of day for automatic updates
This commit is contained in:
Tom Hennen 2015-06-27 08:24:01 -04:00
commit e332df4af5
6 changed files with 143 additions and 47 deletions

View File

@ -241,9 +241,9 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference
} }
public void testDisableUpdateInterval() { public void testDisableUpdateInterval() {
solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervall_title)); solo.clickOnText(solo.getString(R.string.pref_autoUpdateIntervallOrTime_sum));
solo.waitForDialogToOpen(); 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() { solo.waitForCondition(new Condition() {
@Override @Override
public boolean isSatisfied() { public boolean isSatisfied() {
@ -253,7 +253,9 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference
} }
public void testSetUpdateInterval() { 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(); solo.waitForDialogToOpen();
String search = "12 " + solo.getString(R.string.pref_update_interval_hours_plural); String search = "12 " + solo.getString(R.string.pref_update_interval_hours_plural);
solo.clickOnText(search); solo.clickOnText(search);

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.preferences;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -16,15 +17,16 @@ import android.preference.Preference;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast; import android.widget.Toast;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -202,14 +204,10 @@ public class PreferenceController {
}); });
ui.findPreference(UserPreferences.PREF_UPDATE_INTERVAL) ui.findPreference(UserPreferences.PREF_UPDATE_INTERVAL)
.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { .setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceClick(Preference preference) {
if (newValue instanceof String) { showUpdateIntervalTimePreferencesDialog();
int hours = Integer.valueOf((String)newValue);
long millis = TimeUnit.HOURS.toMillis(hours);
UserPreferences.restartUpdateAlarm(millis, millis);
}
return true; return true;
} }
}); });
@ -366,7 +364,6 @@ public class PreferenceController {
return true; return true;
} }
}); });
buildUpdateIntervalPreference();
buildSmartMarkAsPlayedPreference(); buildSmartMarkAsPlayedPreference();
buildAutodownloadSelectedNetworsPreference(); buildAutodownloadSelectedNetworsPreference();
setSelectedNetworksEnabled(UserPreferences setSelectedNetworksEnabled(UserPreferences
@ -399,11 +396,8 @@ public class PreferenceController {
ui.findPreference(PreferenceController.PREF_GPODNET_HOSTNAME).setSummary(GpodnetPreferences.getHostname()); ui.findPreference(PreferenceController.PREF_GPODNET_HOSTNAME).setSummary(GpodnetPreferences.getHostname());
} }
private void buildUpdateIntervalPreference() { private String[] getUpdateIntervalEntries(final String[] values) {
final Resources res = ui.getActivity().getResources(); 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]; String[] entries = new String[values.length];
for (int x = 0; x < values.length; x++) { for (int x = 0; x < values.length; x++) {
Integer v = Integer.parseInt(values[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); entries[x] = res.getString(R.string.pref_update_interval_hours_manual);
break; break;
case 1: case 1:
entries[x] = v entries[x] = v + " " + res.getString(R.string.pref_update_interval_hours_singular);
+ " "
+ res.getString(R.string.pref_update_interval_hours_singular);
break; break;
default: default:
entries[x] = v + " " entries[x] = v + " " + res.getString(R.string.pref_update_interval_hours_plural);
+ res.getString(R.string.pref_update_interval_hours_plural);
break; break;
} }
} }
pref.setEntries(entries); return entries;
} }
private void buildSmartMarkAsPlayedPreference() { private void buildSmartMarkAsPlayedPreference() {
@ -617,7 +607,63 @@ public class PreferenceController {
builder.create().show(); 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 { public static interface PreferenceUI {

View File

@ -96,13 +96,10 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/network_pref"> <PreferenceCategory android:title="@string/network_pref">
<ListPreference <Preference
android:defaultValue="0"
android:entries="@array/update_intervall_values"
android:entryValues="@array/update_intervall_values"
android:key="prefAutoUpdateIntervall" android:key="prefAutoUpdateIntervall"
android:summary="@string/pref_autoUpdateIntervall_sum" android:summary="@string/pref_autoUpdateIntervallOrTime_sum"
android:title="@string/pref_autoUpdateIntervall_title"/> android:title="@string/pref_autoUpdateIntervallOrTime_title"/>
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:enabled="true" android:enabled="true"

View File

@ -19,6 +19,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -206,7 +207,24 @@ public class UserPreferences {
} }
public static long getUpdateInterval() { 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() { public static boolean isAllowMobileUpdate() {
@ -316,6 +334,16 @@ public class UserPreferences {
restartUpdateAlarm(); 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 * Change the auto-flattr settings
* *
@ -493,14 +521,19 @@ public class UserPreferences {
} }
public static void restartUpdateAlarm() { public static void restartUpdateAlarm() {
long hours = getUpdateInterval(); int[] timeOfDay = getUpdateTimeOfDay();
restartUpdateAlarm(TimeUnit.SECONDS.toMillis(10), hours); 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."); Log.d(TAG, "Restarting update alarm.");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0, 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. * Reads episode cache size as it is saved in the episode_cache_size_values array.
*/ */

View File

@ -20,18 +20,7 @@
<item>60</item> <item>60</item>
</integer-array> </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"> <string-array name="update_intervall_values">
<item>0</item>
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>
<item>4</item> <item>4</item>

View File

@ -253,8 +253,12 @@
<string name="pref_smart_mark_as_played_title">Smart mark as played</string> <string name="pref_smart_mark_as_played_title">Smart mark as played</string>
<string name="playback_pref">Playback</string> <string name="playback_pref">Playback</string>
<string name="network_pref">Network</string> <string name="network_pref">Network</string>
<string name="pref_autoUpdateIntervall_title">Update Interval</string> <string name="pref_autoUpdateIntervallOrTime_title">Update Interval or Time of Day</string>
<string name="pref_autoUpdateIntervall_sum">Specify an interval in which the feeds are refreshed automatically or disable it</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_downloadMediaOnWifiOnly_sum">Download media files only over WiFi</string>
<string name="pref_followQueue_title">Continuous Playback</string> <string name="pref_followQueue_title">Continuous Playback</string>
<string name="pref_downloadMediaOnWifiOnly_title">WiFi media download</string> <string name="pref_downloadMediaOnWifiOnly_title">WiFi media download</string>