Set update interval or time of day

This commit is contained in:
Martin Fietz 2015-06-24 17:43:50 +02:00
parent f123c61f21
commit 717a6a645e
5 changed files with 138 additions and 44 deletions

View File

@ -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 {

View File

@ -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"

View File

@ -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() {
@ -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.
*/

View File

@ -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>

View File

@ -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>