Browse Source

Reduce number of calls to /index.php/204 if there is nothing to do.

https://github.com/nextcloud/android/issues/2263
Signed-off-by: Tomasz <tomaszduda23@gmail.com>
Tomasz 4 years ago
parent
commit
cc69e660f0

+ 6 - 0
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -1062,6 +1062,12 @@ public class FileUploader extends Service
         @Nullable final UploadResult uploadResult
     ) {
         OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
+        if(failedUploads.length == 0)
+        {
+            //nothing to do
+            return;
+        }
+
         Account currentAccount = null;
         boolean resultMatch;
         boolean accountMatch;

+ 5 - 5
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -690,11 +690,6 @@ public class UploadFileOperation extends SyncOperation {
     private RemoteOperationResult checkConditions(File originalFile) {
         RemoteOperationResult remoteOperationResult = null;
 
-        // check that internet is not behind walled garden
-        if (!connectivityService.getConnectivity().isConnected() || connectivityService.isInternetWalled()) {
-            remoteOperationResult =  new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION);
-        }
-
         // check that connectivity conditions are met and delays the upload otherwise
         Connectivity connectivity = connectivityService.getConnectivity();
         if (mOnWifiOnly && (!connectivity.isWifi() || connectivity.isMetered())) {
@@ -721,6 +716,11 @@ public class UploadFileOperation extends SyncOperation {
             remoteOperationResult =  new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
         }
 
+        // check that internet is not behind walled garden
+        if (!connectivityService.getConnectivity().isConnected() || connectivityService.isInternetWalled()) {
+            remoteOperationResult =  new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION);
+        }
+
         return remoteOperationResult;
     }
 

+ 32 - 0
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -33,6 +33,7 @@ import android.provider.MediaStore;
 
 import com.nextcloud.client.account.UserAccountManager;
 import com.nextcloud.client.core.Clock;
+import com.nextcloud.client.device.BatteryStatus;
 import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.jobs.BackgroundJobManager;
 import com.nextcloud.client.network.ConnectivityService;
@@ -185,10 +186,20 @@ public final class FilesSyncHelper {
 
         boolean accountExists;
 
+        boolean whileChargingOnly = true;
+        boolean useWifiOnly = true;
+
         OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
 
         for (OCUpload failedUpload : failedUploads) {
             accountExists = false;
+            if(!failedUpload.isWhileChargingOnly()){
+                whileChargingOnly = false;
+            }
+            if(!failedUpload.isUseWifiOnly())
+            {
+                useWifiOnly = false;
+            }
 
             // check if accounts still exists
             for (Account account : accountManager.getAccounts()) {
@@ -203,6 +214,27 @@ public final class FilesSyncHelper {
             }
         }
 
+        failedUploads = uploadsStorageManager.getFailedUploads();
+        if(failedUploads.length == 0)
+        {
+            //nothing to do
+            return;
+        }
+
+        if(whileChargingOnly){
+            final BatteryStatus batteryStatus = powerManagementService.getBattery();
+            final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
+            if(!charging){
+                //all uploads requires charging
+                return;
+            }
+        }
+
+        if (useWifiOnly && !connectivityService.getConnectivity().isWifi()){
+            //all uploads requires wifi
+            return;
+        }
+
         new Thread(() -> {
             if (connectivityService.getConnectivity().isConnected() && !connectivityService.isInternetWalled()) {
                 FileUploader.retryFailedUploads(