OpenDelta: add weekly schedule period

Change-Id: I529f07d8f7aa01e4bb43181cf27f023ce6b18aec
This commit is contained in:
maxwen 2015-09-23 23:25:24 +02:00
parent d725ac5409
commit 96c15d28eb
5 changed files with 99 additions and 23 deletions

View File

@ -26,10 +26,21 @@
<string-array name="scheduler_mode_keys" translatable="false">
<item>@string/scheduler_smart</item>
<item>@string/scheduler_daily</item>
<item>@string/scheduler_weekly</item>
</string-array>
<string-array name="scheduler_mode_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="week_start_values" translatable="false">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
</resources>

View File

@ -93,7 +93,9 @@
<string name="scheduler_mode_title">Scheduler mode</string>
<string name="scheduler_smart">Smart</string>
<string name="scheduler_daily">Daily</string>
<string name="scheduler_daily_time">Daily time</string>
<string name="scheduler_weekly">Weekly</string>
<string name="scheduler_daily_time">Time</string>
<string name="scheduler_week_day">Day</string>
<string name="networks_title">Networks</string>
<string name="networks_message">Download only on networks types</string>
<string name="category_flashing">Flashing</string>

View File

@ -45,6 +45,12 @@
android:key="scheduler_daily_time"
android:persistent="false"
android:title="@string/scheduler_daily_time" />
<ListPreference
android:defaultValue="1"
android:entryValues="@array/week_start_values"
android:key="scheduler_week_day"
android:title="@string/scheduler_week_day" />
</PreferenceCategory>
<PreferenceCategory
android:key="category_download"

View File

@ -81,10 +81,10 @@ public class Scheduler implements OnScreenStateListener,
private PendingIntent alarmInterval = null;
private PendingIntent alarmSecondaryWake = null;
private PendingIntent alarmDetectSleep = null;
private PendingIntent alarmDaily = null;
private PendingIntent alarmCustom = null;
private boolean stopped;
private boolean dailyAlarm;
private boolean customAlarm;
private SimpleDateFormat sdfLog = (new SimpleDateFormat("HH:mm",
Locale.ENGLISH));
@ -99,7 +99,7 @@ public class Scheduler implements OnScreenStateListener,
alarmInterval = UpdateService.alarmPending(context, 1);
alarmSecondaryWake = UpdateService.alarmPending(context, 2);
alarmDetectSleep = UpdateService.alarmPending(context, 3);
alarmDaily = UpdateService.alarmPending(context, 4);
alarmCustom = UpdateService.alarmPending(context, 4);
stopped = true;
}
@ -136,7 +136,7 @@ public class Scheduler implements OnScreenStateListener,
@Override
public void onScreenState(boolean state) {
if (!stopped && !dailyAlarm) {
if (!stopped && !customAlarm) {
Logger.d("isScreenStateEnabled = " + isScreenStateEnabled(state));
if (!state) {
setDetectSleepAlarm();
@ -212,7 +212,7 @@ public class Scheduler implements OnScreenStateListener,
// Reset fallback wakeup command, we don't need to be called for another
// few hours
if (!dailyAlarm) {
if (!customAlarm) {
cancelSecondaryWakeAlarm();
setSecondaryWakeAlarm();
}
@ -223,16 +223,17 @@ public class Scheduler implements OnScreenStateListener,
cancelSecondaryWakeAlarm();
cancelDetectSleepAlarm();
alarmManager.cancel(alarmInterval);
alarmManager.cancel(alarmDaily);
alarmManager.cancel(alarmCustom);
stopped = true;
}
public void start() {
Logger.i("Starting scheduler");
dailyAlarm = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, SettingsActivity.PREF_SCHEDULER_MODE_SMART)
.equals(SettingsActivity.PREF_SCHEDULER_MODE_DAILY);
if (dailyAlarm) {
setDailyAlarmFromPrefs();
String alarmType = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, SettingsActivity.PREF_SCHEDULER_MODE_SMART);
customAlarm = alarmType.equals(SettingsActivity.PREF_SCHEDULER_MODE_DAILY) || alarmType.equals(SettingsActivity.PREF_SCHEDULER_MODE_WEEKLY);
if (customAlarm) {
setCustomAlarmFromPrefs();
} else {
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + ALARM_INTERVAL_START,
@ -243,11 +244,17 @@ public class Scheduler implements OnScreenStateListener,
stopped = false;
}
private void setDailyAlarmFromPrefs() {
if (dailyAlarm) {
String dailyAlarmTime = prefs.getString(
private void setCustomAlarmFromPrefs() {
if (customAlarm) {
final String dailyAlarmTime = prefs.getString(
SettingsActivity.PREF_SCHEDULER_DAILY_TIME, "00:00");
if (dailyAlarmTime != null) {
final String weeklyAlarmDay = prefs.getString(
SettingsActivity.PREF_SCHEDULER_WEEK_DAY, "1");
final String alarmType = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, SettingsActivity.PREF_SCHEDULER_MODE_SMART);
final boolean dailyAlarm = alarmType.equals(SettingsActivity.PREF_SCHEDULER_MODE_DAILY);
final boolean weeklyAlarm = alarmType.equals(SettingsActivity.PREF_SCHEDULER_MODE_WEEKLY);
if (dailyAlarm && dailyAlarmTime != null) {
try {
String[] timeParts = dailyAlarmTime.split(":");
int hour = Integer.valueOf(timeParts[0]);
@ -256,12 +263,37 @@ public class Scheduler implements OnScreenStateListener,
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
Logger.i("Setting daily alarm to %s", dailyAlarmTime);
SimpleDateFormat format = new SimpleDateFormat("kk:mm");
Logger.i("Setting daily alarm to %s", format.format(c.getTime()));
alarmManager.cancel(alarmDaily);
alarmManager.cancel(alarmCustom);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmDaily);
alarmCustom);
} catch (Exception e) {
}
}
if (weeklyAlarm && dailyAlarmTime != null && weeklyAlarmDay != null) {
try {
String[] timeParts = dailyAlarmTime.split(":");
int hour = Integer.valueOf(timeParts[0]);
int minute = Integer.valueOf(timeParts[1]);
final Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Integer.valueOf(weeklyAlarmDay));
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
// next week
if (c.getTimeInMillis() < Calendar.getInstance().getTimeInMillis()) {
c.set(Calendar.WEEK_OF_YEAR, Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) + 1);
}
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' kk:mm");
Logger.i("Setting weekly alarm to %s", format.format(c.getTime()));
alarmManager.cancel(alarmCustom);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
alarmCustom);
} catch (Exception e) {
}
}
@ -277,8 +309,8 @@ public class Scheduler implements OnScreenStateListener,
start();
}
}
if (key.equals(SettingsActivity.PREF_SCHEDULER_DAILY_TIME)) {
setDailyAlarmFromPrefs();
if (key.equals(SettingsActivity.PREF_SCHEDULER_DAILY_TIME) || key.equals(SettingsActivity.PREF_SCHEDULER_WEEK_DAY)) {
setCustomAlarmFromPrefs();
}
}
}

View File

@ -18,7 +18,11 @@
package eu.chainfire.opendelta;
import java.io.File;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;
@ -58,8 +62,10 @@ public class SettingsActivity extends PreferenceActivity implements
public static final String PREF_SCHEDULER_MODE = "scheduler_mode";
public static final String PREF_SCHEDULER_MODE_SMART = String.valueOf(0);
public static final String PREF_SCHEDULER_MODE_DAILY = String.valueOf(1);
public static final String PREF_SCHEDULER_MODE_WEEKLY = String.valueOf(2);
public static final String PREF_SCHEDULER_DAILY_TIME = "scheduler_daily_time";
public static final String PREF_SCHEDULER_WEEK_DAY = "scheduler_week_day";
private Preference mNetworksConfig;
private ListPreference mAutoDownload;
@ -71,6 +77,7 @@ public class SettingsActivity extends PreferenceActivity implements
private ListPreference mSchedulerMode;
private Preference mSchedulerDailyTime;
private Preference mCleanFiles;
private ListPreference mScheduleWeekDay;
@Override
public void onPause() {
@ -124,11 +131,17 @@ public class SettingsActivity extends PreferenceActivity implements
String schedulerMode = prefs.getString(PREF_SCHEDULER_MODE, PREF_SCHEDULER_MODE_SMART);
mSchedulerDailyTime = (Preference) findPreference(PREF_SCHEDULER_DAILY_TIME);
mSchedulerDailyTime.setEnabled(schedulerMode.equals(PREF_SCHEDULER_MODE_DAILY));
mSchedulerDailyTime.setEnabled(!schedulerMode.equals(PREF_SCHEDULER_MODE_SMART));
mSchedulerDailyTime.setSummary(prefs.getString(
PREF_SCHEDULER_DAILY_TIME, "00:00"));
mCleanFiles = (Preference) findPreference(PREF_CLEAN_FILES);
mScheduleWeekDay = (ListPreference) findPreference(PREF_SCHEDULER_WEEK_DAY);
mScheduleWeekDay.setEntries(getWeekdays());
mScheduleWeekDay.setSummary(mScheduleWeekDay.getEntry());
mScheduleWeekDay.setOnPreferenceChangeListener(this);
mScheduleWeekDay.setEnabled(schedulerMode.equals(PREF_SCHEDULER_MODE_WEEKLY));
}
@Override
@ -206,7 +219,12 @@ public class SettingsActivity extends PreferenceActivity implements
int idx = mSchedulerMode.findIndexOfValue(value);
mSchedulerMode.setSummary(mSchedulerMode.getEntries()[idx]);
mSchedulerMode.setValueIndex(idx);
mSchedulerDailyTime.setEnabled(!value.equals("0"));
mSchedulerDailyTime.setEnabled(!value.equals(PREF_SCHEDULER_MODE_SMART));
mScheduleWeekDay.setEnabled(value.equals(PREF_SCHEDULER_MODE_WEEKLY));
return true;
} else if (preference == mScheduleWeekDay) {
int idx = mScheduleWeekDay.findIndexOfValue((String) newValue);
mScheduleWeekDay.setSummary(mScheduleWeekDay.getEntries()[idx]);
return true;
}
return false;
@ -310,4 +328,11 @@ public class SettingsActivity extends PreferenceActivity implements
}
return deletedFiles;
}
private String[] getWeekdays() {
DateFormatSymbols dfs = new DateFormatSymbols();
List<String> weekDayList = new ArrayList<String>();
weekDayList.addAll(Arrays.asList(dfs.getWeekdays()).subList(1, 7));
return weekDayList.toArray(new String[weekDayList.size()]);
}
}