Browse Source

Merge pull request #2062 from nextcloud/fix-for-autoupload

Fix for autoupload
Mario Đanić 7 years ago
parent
commit
8bf7aa24bf

+ 4 - 4
src/main/java/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -26,6 +26,7 @@ import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.net.Uri;
+import android.support.annotation.Nullable;
 
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.db.OCUpload;
@@ -322,7 +323,7 @@ public class UploadsStorageManager extends Observable {
     }
 
 
-    private OCUpload[] getUploads(String selection, String[] selectionArgs) {
+    private OCUpload[] getUploads(@Nullable String selection, @Nullable String[] selectionArgs) {
         OCUpload[] list;
 
         Cursor c = getDB().query(
@@ -411,9 +412,8 @@ public class UploadsStorageManager extends Observable {
      * Get all failed uploads.
      */
     public OCUpload[] getFailedUploads() {
-
-        return getUploads(
-                ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_FAILED.value, null);
+        return getUploads(ProviderTableMeta.UPLOADS_STATUS + "== ?", new String[]
+                          {String.valueOf(UploadStatus.UPLOAD_FAILED.value)});
     }
 
     public OCUpload[] getFinishedUploadsForCurrentAccount() {

+ 20 - 4
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -240,7 +240,11 @@ public class FileUploader extends Service
             intent.putExtra(FileUploader.KEY_WHILE_ON_WIFI_ONLY, requiresWifi);
             intent.putExtra(FileUploader.KEY_WHILE_CHARGING_ONLY, requiresCharging);
 
-            context.startService(intent);
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                context.startForegroundService(intent);
+            } else {
+                context.startService(intent);
+            }
         }
 
         public void uploadFileWithOverwrite(
@@ -269,7 +273,11 @@ public class FileUploader extends Service
             intent.putExtra(FileUploader.KEY_WHILE_CHARGING_ONLY, requiresCharging);
             intent.putExtra(FileUploader.KEY_FORCE_OVERWRITE, overwrite);
 
-            context.startService(intent);
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                context.startForegroundService(intent);
+            } else {
+                context.startService(intent);
+            }
         }
 
         /**
@@ -327,7 +335,11 @@ public class FileUploader extends Service
             intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
             intent.putExtra(FileUploader.KEY_FORCE_OVERWRITE, forceOverwrite);
 
-            context.startService(intent);
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                context.startForegroundService(intent);
+            } else {
+                context.startService(intent);
+            }
         }
 
         /**
@@ -418,7 +430,11 @@ public class FileUploader extends Service
                 i.putExtra(FileUploader.KEY_ACCOUNT, account);
                 i.putExtra(FileUploader.KEY_RETRY_UPLOAD, upload);
 
-                context.startService(i);
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                    context.startForegroundService(i);
+                } else {
+                    context.startService(i);
+                }
             }
         }
     }

+ 10 - 2
src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

@@ -428,7 +428,11 @@ public class SynchronizeFolderOperation extends SyncOperation {
                 Intent i = new Intent(mContext, FileDownloader.class);
                 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
                 i.putExtra(FileDownloader.EXTRA_FILE, file);
-                mContext.startService(i);
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                    mContext.startForegroundService(i);
+                } else {
+                    mContext.startService(i);
+                }
             }
         }
     }
@@ -507,7 +511,11 @@ public class SynchronizeFolderOperation extends SyncOperation {
         intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
         intent.putExtra(OperationsService.EXTRA_ACCOUNT, mAccount);
         intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, path);
-        mContext.startService(intent);
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+            mContext.startForegroundService(intent);
+        } else {
+            mContext.startService(intent);
+        }
     }
 
     public String getRemotePath() {

+ 5 - 1
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -136,7 +136,11 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             Intent i = new Intent(getContext(), FileDownloader.class);
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
             i.putExtra(FileDownloader.EXTRA_FILE, file);
-            context.startService(i);
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                context.startForegroundService(i);
+            } else {
+                context.startService(i);
+            }
 
             do {
                 if (!waitOrGetCancelled(cancellationSignal)) {