From b6a15fc56705aba46db91c3f1ec80bbd262ce2ea Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Sat, 4 Feb 2023 19:28:14 +0200 Subject: [PATCH] OpenDelta: Allow opting out of sleep detection --- res/values/strings.xml | 2 + res/xml/settings.xml | 5 ++ src/eu/chainfire/opendelta/Config.java | 8 +++ src/eu/chainfire/opendelta/Scheduler.java | 12 ++++- .../chainfire/opendelta/SettingsActivity.java | 1 + .../chainfire/opendelta/SettingsFragment.java | 53 +++++++++++++------ 6 files changed, 64 insertions(+), 17 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 7158e01..52c8789 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -107,6 +107,8 @@ Weekly Time Day + Sleep detection + Attempt to check for updates when the screen is off for 5 hours straight Metered networks Allow auto downloading on metered networks Flashing diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 5d8eeb3..e30ed7c 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -47,6 +47,11 @@ android:entryValues="@array/scheduler_mode_values" android:key="scheduler_mode" android:title="@string/scheduler_mode_title" /> + getFlashAfterUpdateZIPs() { List extras = new ArrayList<>(); diff --git a/src/eu/chainfire/opendelta/Scheduler.java b/src/eu/chainfire/opendelta/Scheduler.java index 81801ca..50cc265 100644 --- a/src/eu/chainfire/opendelta/Scheduler.java +++ b/src/eu/chainfire/opendelta/Scheduler.java @@ -156,12 +156,16 @@ public class Scheduler extends Service implements OnScreenStateListener { } // smart mode - mScreenState.start(this, this); final long time = getMaxTime(ALARM_INTERVAL); Logger.i("Setting a repeating alarm (inexact) for %s", mSdf.format(new Date(time))); mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, time, ALARM_INTERVAL, mAlarmInterval); setSecondaryWakeAlarm(); + if (!Config.getInstance(this).getSchedulerSleepEnabled()) { + mScreenState.stop(); + return START_NOT_STICKY; + } + mScreenState.start(this, this); return START_REDELIVER_INTENT; } @@ -304,7 +308,11 @@ public class Scheduler extends Service implements OnScreenStateListener { @Override public void onScreenState(boolean state) { - if (mIsStopped || mIsCustomAlarm) return; + final boolean enabled = Config.getInstance(this).getSchedulerSleepEnabled(); + if (!enabled) + mScreenState.stop(); + if (mIsStopped || mIsCustomAlarm || !enabled) + return; Logger.d("onScreenState = " + state); if (!state) { setDetectSleepAlarm(); diff --git a/src/eu/chainfire/opendelta/SettingsActivity.java b/src/eu/chainfire/opendelta/SettingsActivity.java index 079fd0c..ecf511f 100644 --- a/src/eu/chainfire/opendelta/SettingsActivity.java +++ b/src/eu/chainfire/opendelta/SettingsActivity.java @@ -37,6 +37,7 @@ public class SettingsActivity extends CollapsingToolbarBaseActivity { public static final String PREF_SCHEDULER_DAILY_TIME = "scheduler_daily_time"; public static final String PREF_SCHEDULER_WEEK_DAY = "scheduler_week_day"; + public static final String PREF_SCHEDULER_SLEEP = "scheduler_sleep_enabled"; @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/src/eu/chainfire/opendelta/SettingsFragment.java b/src/eu/chainfire/opendelta/SettingsFragment.java index a31eae7..2e2e366 100644 --- a/src/eu/chainfire/opendelta/SettingsFragment.java +++ b/src/eu/chainfire/opendelta/SettingsFragment.java @@ -63,6 +63,7 @@ public class SettingsFragment extends PreferenceFragment implements private Config mConfig; private PreferenceCategory mAutoDownloadCategory; private ListPreference mSchedulerMode; + private SwitchPreference mSchedulerSleep; private Preference mSchedulerDailyTime; private Preference mCleanFiles; private ListPreference mScheduleWeekDay; @@ -70,17 +71,16 @@ public class SettingsFragment extends PreferenceFragment implements @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(getContext()); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); mConfig = Config.getInstance(getContext()); addPreferencesFromResource(R.xml.settings); mNetworksConfig = findPreference(KEY_NETWORKS); mNetworksConfig.setChecked(prefs.getBoolean(UpdateService.PREF_AUTO_UPDATE_METERED_NETWORKS, false)); - String autoDownload = prefs.getString(SettingsActivity.PREF_AUTO_DOWNLOAD, + final String autoDownload = prefs.getString(SettingsActivity.PREF_AUTO_DOWNLOAD, Integer.toString(UpdateService.PREF_AUTO_DOWNLOAD_CHECK)); - int autoDownloadValue = Integer.parseInt(autoDownload); + final int autoDownloadValue = Integer.parseInt(autoDownload); mAutoDownload = findPreference(SettingsActivity.PREF_AUTO_DOWNLOAD); mAutoDownload.setOnPreferenceChangeListener(this); mAutoDownload.setValue(autoDownload); @@ -111,21 +111,28 @@ public class SettingsFragment extends PreferenceFragment implements } mAutoDownloadCategory = findPreference(KEY_CATEGORY_DOWNLOAD); - mAutoDownloadCategory - .setEnabled(autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_CHECK); + mAutoDownloadCategory.setEnabled( + autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_CHECK); + final boolean schedulerEnabled = autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_DISABLED; mSchedulerMode = findPreference(SettingsActivity.PREF_SCHEDULER_MODE); mSchedulerMode.setOnPreferenceChangeListener(this); mSchedulerMode.setSummary(mSchedulerMode.getEntry()); - mSchedulerMode - .setEnabled(autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_DISABLED); + mSchedulerMode.setEnabled(schedulerEnabled); - String schedulerMode = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, + final String schedulerMode = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, SettingsActivity.PREF_SCHEDULER_MODE_SMART); mSchedulerDailyTime = findPreference(SettingsActivity.PREF_SCHEDULER_DAILY_TIME); mSchedulerDailyTime.setEnabled(!schedulerMode.equals(SettingsActivity.PREF_SCHEDULER_MODE_SMART)); mSchedulerDailyTime.setSummary(prefs.getString( SettingsActivity.PREF_SCHEDULER_DAILY_TIME, "00:00")); + + final boolean sleepEnabled = prefs.getBoolean(SettingsActivity.PREF_SCHEDULER_SLEEP, true); + mSchedulerSleep = findPreference(SettingsActivity.PREF_SCHEDULER_SLEEP); + mSchedulerSleep.setChecked(sleepEnabled); + mSchedulerSleep.setOnPreferenceChangeListener(this); + mSchedulerSleep.setEnabled(schedulerEnabled && + schedulerMode.equals(SettingsActivity.PREF_SCHEDULER_MODE_SMART)); mCleanFiles = findPreference(PREF_CLEAN_FILES); @@ -171,10 +178,11 @@ public class SettingsFragment extends PreferenceFragment implements mAutoDownload.setSummary(mAutoDownload.getEntries()[idx]); mAutoDownload.setValueIndex(idx); int autoDownloadValue = Integer.parseInt(value); - mAutoDownloadCategory - .setEnabled(autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_CHECK); - mSchedulerMode - .setEnabled(autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_DISABLED); + mAutoDownloadCategory.setEnabled( + autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_CHECK); + mSchedulerMode.setEnabled( + autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_DISABLED); + updateSleepEnablement(); return true; } else if (preference == mBatteryLevel) { String value = (String) newValue; @@ -189,6 +197,10 @@ public class SettingsFragment extends PreferenceFragment implements mSchedulerMode.setValueIndex(idx); mSchedulerDailyTime.setEnabled(!value.equals(SettingsActivity.PREF_SCHEDULER_MODE_SMART)); mScheduleWeekDay.setEnabled(value.equals(SettingsActivity.PREF_SCHEDULER_MODE_WEEKLY)); + updateSleepEnablement(); + return true; + } else if (preference == mSchedulerSleep) { + mConfig.setSchedulerSleepEnabled((boolean) newValue); return true; } else if (preference == mScheduleWeekDay) { int idx = mScheduleWeekDay.findIndexOfValue((String) newValue); @@ -219,14 +231,25 @@ public class SettingsFragment extends PreferenceFragment implements @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(getContext()); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); String prefValue = String.format(Locale.ENGLISH, "%02d:%02d", hourOfDay, minute); prefs.edit().putString(SettingsActivity.PREF_SCHEDULER_DAILY_TIME, prefValue).apply(); mSchedulerDailyTime.setSummary(prefValue); } + private void updateSleepEnablement() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + final String autoDownload = prefs.getString(SettingsActivity.PREF_AUTO_DOWNLOAD, + Integer.toString(UpdateService.PREF_AUTO_DOWNLOAD_CHECK)); + final int autoDownloadValue = Integer.parseInt(autoDownload); + final String schedulerMode = prefs.getString(SettingsActivity.PREF_SCHEDULER_MODE, + SettingsActivity.PREF_SCHEDULER_MODE_SMART); + mSchedulerSleep.setEnabled( + autoDownloadValue > UpdateService.PREF_AUTO_DOWNLOAD_DISABLED && + schedulerMode.equals(SettingsActivity.PREF_SCHEDULER_MODE_SMART)); + } + private void showTimePicker() { final Calendar c = Calendar.getInstance(); final int hour = c.get(Calendar.HOUR_OF_DAY);