Skip to content

Commit

Permalink
Allow configuring time schedule interval #960
Browse files Browse the repository at this point in the history
  • Loading branch information
ImranR98 committed Oct 28, 2024
1 parent 32208fb commit f6736e6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public static class SettingsFragment extends PreferenceFragment
private WifiSsidPreference mWifiSsidWhitelist;
private CheckBoxPreference mRunInFlightMode;
private EditTextPreference mSyncDurationMinutes;
private EditTextPreference mSyncIntervalMinutes;

/* Behaviour */
private CheckBoxPreference mStartServiceOnBoot;
Expand Down Expand Up @@ -289,6 +290,8 @@ public void onActivityCreated(Bundle savedInstanceState) {
(CheckBoxPreference) findPreference(Constants.PREF_RUN_IN_FLIGHT_MODE);
mSyncDurationMinutes =
(EditTextPreference) findPreference(Constants.PREF_SYNC_DURATION_MINUTES);
mSyncIntervalMinutes =
(EditTextPreference) findPreference(Constants.PREF_SYNC_INTERVAL_MINUTES);

mRunOnMeteredWifi.setEnabled(mRunOnWifi.isChecked());
mUseWifiWhitelist.setEnabled(mRunOnWifi.isChecked());
Expand All @@ -307,6 +310,10 @@ public void onActivityCreated(Bundle savedInstanceState) {
getString(R.string.sync_duration_minutes_summary, mSyncDurationMinutes.getText())
);

mSyncIntervalMinutes.setSummary(
getString(R.string.sync_duration_minutes_summary, mSyncIntervalMinutes.getText())
);

mCategoryRunConditions = (PreferenceScreen) findPreference("category_run_conditions");
setPreferenceCategoryChangeListener(mCategoryRunConditions, this::onRunConditionPreferenceChange);

Expand Down Expand Up @@ -605,6 +612,16 @@ public boolean onRunConditionPreferenceChange(Preference preference, Object o) {
}
preference.setSummary(getString(R.string.sync_duration_minutes_summary, durationMinutes));
break;
case Constants.PREF_SYNC_INTERVAL_MINUTES:
String intervalMinutes = o.toString();
if (TextUtils.isEmpty(intervalMinutes)) {
return false;
}
if (Integer.parseInt(intervalMinutes) <= 0) {
return false;
}
preference.setSummary(getString(R.string.sync_duration_minutes_summary, intervalMinutes));
break;
}
mPendingRunConditions = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private PowerSource() { }
public static final String PREF_RUN_IN_FLIGHT_MODE = "run_in_flight_mode";
public static final String PREF_RUN_ON_TIME_SCHEDULE = "run_on_time_schedule";
public static final String PREF_SYNC_DURATION_MINUTES = "sync_duration_minutes";
public static final String PREF_SYNC_INTERVAL_MINUTES = "sync_interval_minutes";

// Preferences - User Interface
public static final String PREF_APP_THEME = "app_theme";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public RunConditionMonitor(Context context,
*/
if (lastSyncTimeSinceBootMillisecs > elapsedRealtime) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putLong(Constants.PREF_LAST_RUN_TIME, -Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS * 1000);
editor.putLong(Constants.PREF_LAST_RUN_TIME, -Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60 * 1000);
editor.apply();
lastSyncTimeSinceBootMillisecs = 0;
}
Expand All @@ -206,7 +206,7 @@ public RunConditionMonitor(Context context,
* mTimeConditionMatch is set to true during updateShouldRunDecision().
* Thus the false case cannot be triggered if the delay for scheduleSyncTriggerServiceJob would be negative
*/
Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS - elapsedSecondsSinceLastSync,
(Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60) - elapsedSecondsSinceLastSync,
!mTimeConditionMatch
);
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public void onReceive(Context context, Intent intent) {
JobUtils.cancelAllScheduledJobs(context);
JobUtils.scheduleSyncTriggerServiceJob(
context,
Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS,
Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60,
true
);
return;
Expand Down Expand Up @@ -313,9 +313,9 @@ public void onReceive(Context context, Intent intent) {
* corrected immediately
*/
long lastRunTimeMillis = mPreferences.getLong(Constants.PREF_LAST_RUN_TIME, 0);
if (lastDeterminedShouldRun && SystemClock.elapsedRealtime() - lastRunTimeMillis > Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS * 1000) {
if (lastDeterminedShouldRun && SystemClock.elapsedRealtime() - lastRunTimeMillis > Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60 * 1000) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putLong(Constants.PREF_LAST_RUN_TIME, SystemClock.elapsedRealtime() - Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS * 1000 + 60*1000);
editor.putLong(Constants.PREF_LAST_RUN_TIME, SystemClock.elapsedRealtime() - Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60 * 1000 + 60*1000);
editor.apply();
}
}
Expand All @@ -341,7 +341,7 @@ public void onReceive(Context context, Intent intent) {
JobUtils.cancelAllScheduledJobs(context);
JobUtils.scheduleSyncTriggerServiceJob(
context,
Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS,
Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60,
true
);
} else {
Expand Down Expand Up @@ -572,7 +572,7 @@ private boolean decideShouldRun() {

// PREF_RUN_ON_TIME_SCHEDULE
// set mTimeConditionMatch to true if the last run was more than WAIT_FOR_NEXT_SYNC_DELAY_SECS ago
if (SystemClock.elapsedRealtime() - mPreferences.getLong(Constants.PREF_LAST_RUN_TIME,0) > Constants.WAIT_FOR_NEXT_SYNC_DELAY_SECS * 1000)
if (SystemClock.elapsedRealtime() - mPreferences.getLong(Constants.PREF_LAST_RUN_TIME,0) > Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_INTERVAL_MINUTES,"60")) * 60 * 1000)
mTimeConditionMatch = true;
if (prefRunOnTimeSchedule && !mTimeConditionMatch) {
// Currently, we aren't within a "SyncthingNative should run" time frame.
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,11 @@
<string name="run_in_flight_mode_summary">Enabling this option will cause Syncthing to run even when you\'re offline. Enable if your device has problems detecting manual Wi-Fi connections during flight mode.</string>

<string name="run_on_time_schedule_title">Run according to time schedule</string>
<string name="run_on_time_schedule_summary">Enabling this option will attempt to sync for a configured duration if run conditions are fulfilled and the last sync was more than 1 hour ago. This can save a lot of battery but requires sync partners to be online. Please note: This may leave incomplete temporary files behind until the next scheduled sync takes place.</string>
<string name="run_on_time_schedule_summary">Enabling this option will attempt to sync for a configured duration if run conditions are fulfilled and if some specified interval period has elapsed since the last sync. This can save a lot of battery but requires sync partners to be online. Please note: This may leave incomplete temporary files behind until the next scheduled sync takes place.</string>

<string name="sync_duration_minutes_title">Duration of the sync cycle</string>
<string name="sync_duration_minutes_summary">%1$s minutes</string>
<string name="sync_interval_minutes_title">Duration of the sync pause interval</string>

<!-- Preferences - Behaviour -->

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/app_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@
android:maxLength="9"
android:defaultValue="5" />

<EditTextPreference
android:key="sync_interval_minutes"
android:title="@string/sync_interval_minutes_title"
android:summary="@string/sync_duration_minutes_summary"
android:singleLineTitle="false"
android:inputType="number"
android:maxLength="1440"
android:defaultValue="60" />

</PreferenceScreen>

<PreferenceScreen
Expand Down

0 comments on commit f6736e6

Please sign in to comment.