OpenDelta: Allow opting out of sleep detection

This commit is contained in:
Ido Ben-Hur 2023-02-04 19:28:14 +02:00
parent 3b769805cd
commit b6a15fc567
No known key found for this signature in database
GPG Key ID: 0B827201D8C20BFE
6 changed files with 64 additions and 17 deletions

View File

@ -107,6 +107,8 @@
<string name="scheduler_weekly">Weekly</string>
<string name="scheduler_daily_time">Time</string>
<string name="scheduler_week_day">Day</string>
<string name="scheduler_sleep_enabled_title">Sleep detection</string>
<string name="scheduler_sleep_enabled_summary">Attempt to check for updates when the screen is off for 5 hours straight</string>
<string name="metered_networks_title">Metered networks</string>
<string name="metered_networks_message">Allow auto downloading on metered networks</string>
<string name="category_flashing">Flashing</string>

View File

@ -47,6 +47,11 @@
android:entryValues="@array/scheduler_mode_values"
android:key="scheduler_mode"
android:title="@string/scheduler_mode_title" />
<SwitchPreference
android:defaultValue="true"
android:key="scheduler_sleep_enabled"
android:summary="@string/scheduler_sleep_enabled_summary"
android:title="@string/scheduler_sleep_enabled_title" />
<Preference
android:key="scheduler_daily_time"

View File

@ -206,6 +206,14 @@ public class Config {
prefs.edit().putBoolean(PREF_SHOW_INFO_NAME, enable).commit();
}
public boolean getSchedulerSleepEnabled() {
return prefs.getBoolean(SettingsActivity.PREF_SCHEDULER_SLEEP, true);
}
public void setSchedulerSleepEnabled(boolean enable) {
prefs.edit().putBoolean(SettingsActivity.PREF_SCHEDULER_SLEEP, enable).commit();
}
public List<String> getFlashAfterUpdateZIPs() {
List<String> extras = new ArrayList<>();

View File

@ -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();

View File

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

View File

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