Skip to content

Commit c5c8216

Browse files
committed
add debounce mechanism to not spam auto upload on start
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 7d13475 commit c5c8216

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

app/src/main/java/com/nextcloud/client/preferences/AppPreferences.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,7 @@ default void onDarkThemeModeChanged(DarkMode mode) {
396396

397397
String getLastDisplayedAccountName();
398398
void setLastDisplayedAccountName(String lastDisplayedAccountName);
399+
400+
boolean startAutoUploadOnStart();
401+
void setLastAutoUploadOnStartTime(long timeInMillisecond);
399402
}

app/src/main/java/com/nextcloud/client/preferences/AppPreferencesImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,19 @@ public final class AppPreferencesImpl implements AppPreferences {
111111

112112
private static final String PREF_LAST_DISPLAYED_ACCOUNT_NAME = "last_displayed_user";
113113

114+
private static final String AUTO_PREF__LAST_AUTO_UPLOAD_ON_START = "last_auto_upload_on_start";
115+
116+
114117
private static final String LOG_ENTRY = "log_entry";
115118

116119
private final Context context;
117120
private final SharedPreferences preferences;
118121
private final UserAccountManager userAccountManager;
119122
private final ListenerRegistry listeners;
120123

124+
private static final int AUTO_UPLOAD_ON_START_DEBOUNCE_IN_MINUTES = 10;
125+
private static final long AUTO_UPLOAD_ON_START_DEBOUNCE_MS = AUTO_UPLOAD_ON_START_DEBOUNCE_IN_MINUTES * 60 * 1000L;
126+
121127
/**
122128
* Adapter delegating raw {@link SharedPreferences.OnSharedPreferenceChangeListener} calls with key-value pairs to
123129
* respective {@link com.nextcloud.client.preferences.AppPreferences.Listener} method.
@@ -849,4 +855,16 @@ public String getLastDisplayedAccountName() {
849855
public void setLastDisplayedAccountName(String lastDisplayedAccountName) {
850856
preferences.edit().putString(PREF_LAST_DISPLAYED_ACCOUNT_NAME, lastDisplayedAccountName).apply();
851857
}
858+
859+
@Override
860+
public boolean startAutoUploadOnStart() {
861+
long lastRunTime = preferences.getLong(AUTO_PREF__LAST_AUTO_UPLOAD_ON_START, 0L);
862+
long now = System.currentTimeMillis();
863+
return lastRunTime == 0L || (now - lastRunTime) >= AUTO_UPLOAD_ON_START_DEBOUNCE_MS;
864+
}
865+
866+
@Override
867+
public void setLastAutoUploadOnStartTime(long timeInMillisecond) {
868+
preferences.edit().putLong(AUTO_PREF__LAST_AUTO_UPLOAD_ON_START, timeInMillisecond).apply();
869+
}
852870
}

app/src/main/java/com/owncloud/android/MainApp.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import android.annotation.SuppressLint;
1919
import android.app.Activity;
20-
import android.app.ActivityManager;
2120
import android.app.Application;
2221
import android.app.NotificationChannel;
2322
import android.app.NotificationManager;
@@ -31,7 +30,6 @@
3130
import android.content.pm.PackageManager;
3231
import android.content.res.Resources;
3332
import android.net.ConnectivityManager;
34-
import android.os.Build;
3533
import android.os.Bundle;
3634
import android.os.Environment;
3735
import android.os.StrictMode;
@@ -389,10 +387,14 @@ public void disableDocumentsStorageProvider() {
389387
private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> {
390388
if (event == Lifecycle.Event.ON_START) {
391389
Log_OC.d(TAG, "APP IN FOREGROUND");
392-
FilesSyncHelper.startAutoUploadForEnabledSyncedFolders(syncedFolderProvider,
393-
backgroundJobManager,
394-
new String[]{},
395-
true);
390+
391+
if (preferences.startAutoUploadOnStart()) {
392+
FilesSyncHelper.startAutoUploadForEnabledSyncedFolders(syncedFolderProvider,
393+
backgroundJobManager,
394+
new String[]{},
395+
true);
396+
preferences.setLastAutoUploadOnStartTime(System.currentTimeMillis());
397+
}
396398
} else if (event == Lifecycle.Event.ON_STOP) {
397399
passCodeManager.setCanAskPin(true);
398400
Log_OC.d(TAG, "APP IN BACKGROUND");

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,6 @@
916916
<string name="notification_channel_offline_operations_name_short">Offline operations</string>
917917
<string name="notification_channel_offline_operations_description">Shows progress of offline file operations</string>
918918

919-
<string name="content_observer_work_notification_title">Detecting content changes</string>
920919
<string name="notification_channel_content_observer_name_short">Content observer</string>
921920
<string name="notification_channel_content_observer_description">Detects local file changes</string>
922921

0 commit comments

Comments
 (0)