Selaa lähdekoodia

Changed calls to FileUploader service to static methods to manage service Intents internally

Juan Carlos González Cabrero 9 vuotta sitten
vanhempi
commit
e6cfedc285

+ 1 - 1
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -540,7 +540,7 @@ public class FileOperationsHelper {
             Log_OC.w(TAG, "uploaderBinder not set. Cannot retry the upload of " + upload.getOCFile());
         }
     }
-    
+
     /**
      * Remove upload from upload list.
      */

+ 9 - 53
src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java

@@ -112,42 +112,23 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         lastUploadedPhotoPath = file_path;
         Log_OC.d(TAG, "Path: " + file_path + "");        
         
-        Intent i = new Intent(context, FileUploader.class);
-        i.putExtra(FileUploader.KEY_ACCOUNT, account);
-        i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
-        i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
-        i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
-        i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
-        i.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, true);
-        i.putExtra(FileUploader.KEY_WIFI_ONLY, instantPictureUploadViaWiFiOnly(context));
-
-// On master
-//        Intent i = new Intent(context, FileUploader.class);
-//        i.putExtra(FileUploader.KEY_ACCOUNT, account);
-//        i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
-//        i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
-//        i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
-//        i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
-//        i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
-
-        // instant upload behaviour
-        i = addInstantUploadBehaviour(i, context);
-
-        context.startService(i);
+        int behaviour = getUploadBehaviour(context);
+        FileUploader.uploadNewFile(context, account, file_path, FileStorageUtils.getInstantUploadFilePath(context,
+                file_name), behaviour, mime_type, true, instantPictureUploadViaWiFiOnly(context));
     }
 
-    private Intent addInstantUploadBehaviour(Intent i, Context context){
+    private Integer getUploadBehaviour(Context context){
         SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
         String behaviour = appPreferences.getString("prefs_instant_behaviour", "NOTHING");
 
         if (behaviour.equalsIgnoreCase("NOTHING")) {
             Log_OC.d(TAG, "upload file and do nothing");
-            i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
+            return FileUploader.LOCAL_BEHAVIOUR_FORGET;
         } else if (behaviour.equalsIgnoreCase("MOVE")) {
-            i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
             Log_OC.d(TAG, "upload file and move file to oc folder");
+            return FileUploader.LOCAL_BEHAVIOUR_MOVE;
         }
-        return i;
+        return null;
     }
 
     private void handleNewVideoAction(Context context, Intent intent) {
@@ -182,33 +163,8 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
         c.close();
         Log_OC.d(TAG, file_path + "");
 
-        Intent i = new Intent(context, FileUploader.class);
-        i.putExtra(FileUploader.KEY_ACCOUNT, account);
-        i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
-        i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantUploadFilePath(context, file_name));
-        i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
-        i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
-        i.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, true);
-        i.putExtra(FileUploader.KEY_WIFI_ONLY, instantVideoUploadViaWiFiOnly(context));
-        context.startService(i);
-// On master
-//        if (!isOnline(context) || (instantVideoUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context))) {
-//            return;
-//        }
-//
-//        Intent i = new Intent(context, FileUploader.class);
-//        i.putExtra(FileUploader.KEY_ACCOUNT, account);
-//        i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
-//        i.putExtra(FileUploader.KEY_REMOTE_FILE, FileStorageUtils.getInstantVideoUploadFilePath(context, file_name));
-//        i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
-//        i.putExtra(FileUploader.KEY_MIME_TYPE, mime_type);
-//        i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);
-
-        // instant upload behaviour
-        i = addInstantUploadBehaviour(i, context);
-
-        context.startService(i);
-
+        int behaviour = getUploadBehaviour(context);
+        FileUploader.uploadNewFile(context, account, file_path, FileStorageUtils.getInstantUploadFilePath(context, file_name), behaviour, mime_type, true, instantPictureUploadViaWiFiOnly(context));
     }
 
     public static boolean instantPictureUploadEnabled(Context context) {

+ 118 - 41
src/com/owncloud/android/files/services/FileUploader.java

@@ -166,8 +166,6 @@ public class FileUploader extends Service
     public static final int LOCAL_BEHAVIOUR_MOVE = 1;
     public static final int LOCAL_BEHAVIOUR_FORGET = 2;
 
-    public static final int UPLOAD_SINGLE_FILE = 0;
-    public static final int UPLOAD_MULTIPLE_FILES = 1;
 
     /**
      * Describes local behavior for upload.
@@ -257,6 +255,115 @@ public class FileUploader extends Service
         return FileUploader.class.getName() + UPLOAD_FINISH_MESSAGE;
     }
 
+    /**
+     * Call to retry upload identified by remotePath
+     */
+    private static void retry(Context context, Account account, OCUpload upload) {
+        Log_OC.d(TAG, "FileUploader.retry()");
+        Intent i = new Intent(context, FileUploader.class);
+        i.putExtra(FileUploader.KEY_RETRY, true);
+        if(upload!= null) {
+            i.putExtra(FileUploader.KEY_ACCOUNT, account);
+            i.putExtra(FileUploader.KEY_RETRY_UPLOAD, upload);
+        }
+        context.startService(i);
+    }
+
+    /**
+     * Call to upload a new file. Main method.
+     */
+    public static void uploadNewFile(Context context, Account account, String[] localPaths, String[] remotePaths, Integer behaviour, String mimeType, Boolean createRemoteFolder, Boolean wifiOnly) {
+        Log_OC.d(TAG, "FileUploader.uploadNewFile()");
+        Intent intent = new Intent(context, FileUploader.class);
+
+        intent.putExtra(FileUploader.KEY_ACCOUNT, account);
+        intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
+        intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
+
+        if (behaviour != null)
+            intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
+        if (mimeType != null)
+            intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
+        if (createRemoteFolder != null)
+            intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
+        if (wifiOnly != null)
+            intent.putExtra(FileUploader.KEY_WIFI_ONLY, wifiOnly);
+
+        context.startService(intent);
+    }
+
+    /**
+     * Call to upload multiple new files from the FileDisplayActivity
+     */
+    public static void uploadNewFile(Context context, Account account, String[] localPaths, String[] remotePaths, int behaviour) {
+
+        uploadNewFile(context, account, localPaths, remotePaths, behaviour, null, null, null);
+    }
+
+    /**
+     * Call to upload a new single file from the FileDisplayActivity
+     */
+    public static void uploadNewFile(Context context, String localPath, String remotePath, int resultCode, String mimeType) {
+
+        uploadNewFile(context, null, new String[]{localPath}, new String[]{remotePath}, resultCode, mimeType, null, null);
+    }
+
+    /**
+     * Call to upload multiple new files from external applications
+     */
+    public static void uploadNewFile(Context context, Account account, String[] localPaths, String[] remotePaths) {
+
+        uploadNewFile(context, account, localPaths, remotePaths, null, null, null, null);
+    }
+
+    /**
+     * Call to upload a new single file from external applications
+     */
+    public static void uploadNewFile(Context context, Account account, String localPath, String remotePath) {
+
+        uploadNewFile(context, account, new String[]{localPath}, new String[]{remotePath}, null, null, null, null);
+    }
+
+    /**
+     * Call to upload a new single file from the Instant Upload Broadcast Receiver
+     */
+    public static void uploadNewFile(Context context, Account account, String localPath, String remotePath, int behaviour, String mimeType, boolean createRemoteFile, boolean wifiOnly) {
+
+        uploadNewFile(context, account, new String[]{localPath}, new String[]{remotePath}, behaviour, mimeType,
+                createRemoteFile, wifiOnly);
+    }
+
+    /**
+     * Call to update a file already uploaded from the ConflictsResolveActivity
+     */
+    public static void uploadUpdate(Context context, Account account, OCFile[] existingFiles, Integer behaviour, Boolean forceOverwrite) {
+        Log_OC.d(TAG, "FileUploader.uploadUpdate()");
+        Intent intent = new Intent(context, FileUploader.class);
+
+        intent.putExtra(FileUploader.KEY_ACCOUNT, account);
+        intent.putExtra(FileUploader.KEY_FILE, existingFiles);
+        intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
+        intent.putExtra(FileUploader.KEY_FORCE_OVERWRITE, forceOverwrite);
+
+        context.startService(intent);
+    }
+
+    /**
+     * Call to update a file already uploaded from the ConflictsResolveActivity
+     */
+    public static void uploadUpdate(Context context, Account account, OCFile existingFile, Integer behaviour, Boolean forceOverwrite) {
+
+        uploadUpdate(context, account, new OCFile[]{existingFile}, behaviour, forceOverwrite);
+    }
+
+    /**
+     * Call to update a file already uploaded from the Synchronize File Operation
+     */
+    public static void uploadUpdate(Context context, Account account, OCFile existingFile, Boolean forceOverwrite) {
+
+        uploadUpdate(context, account, new OCFile[]{existingFile}, null, forceOverwrite);
+    }
+
     /**
      * Checks if an ownCloud server version should support chunked uploads.
      *
@@ -350,41 +457,23 @@ public class FileUploader extends Service
         boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
 
         if (!retry) {
-            if (!intent.hasExtra(KEY_UPLOAD_TYPE) || !(intent.hasExtra(KEY_LOCAL_FILE) ||
+            if ( !(intent.hasExtra(KEY_LOCAL_FILE) ||
                     intent.hasExtra(KEY_FILE))) {
                 Log_OC.e(TAG, "Not enough information provided in intent");
                 return Service.START_NOT_STICKY;
             }
-            int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
-            if (uploadType == -1) {
-                Log_OC.e(TAG, "Incorrect upload type provided");
-                return Service.START_NOT_STICKY;
-            }
 
             String[] localPaths = null, remotePaths = null, mimeTypes = null;
             OCFile[] files = null;
-            if (uploadType == UPLOAD_SINGLE_FILE) {
 
-                if (intent.hasExtra(KEY_FILE)) {
-                    files = new OCFile[]{intent.getParcelableExtra(KEY_FILE)};
+            if (intent.hasExtra(KEY_FILE)) {
+                files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE);
+                // TODO : test multiple upload, working find
 
-                } else {
-                    localPaths = new String[]{intent.getStringExtra(KEY_LOCAL_FILE)};
-                    remotePaths = new String[]{intent.getStringExtra(KEY_REMOTE_FILE)};
-                    mimeTypes = new String[]{intent.getStringExtra(KEY_MIME_TYPE)};
-                }
-
-            } else { // mUploadType == UPLOAD_MULTIPLE_FILES
-
-                if (intent.hasExtra(KEY_FILE)) {
-                    files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE);
-                    // TODO : test multiple upload, working find
-
-                } else {
-                    localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
-                    remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
-                    mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
-                }
+            } else {
+                localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
+                remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
+                mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
             }
 
             FileDataStorageManager storageManager = new FileDataStorageManager(
@@ -1318,19 +1407,7 @@ public class FileUploader extends Service
 //        retry(context, null);
 //    }
 
-    /**
-     * Call to retry upload identified by remotePath
-     */
-    private static void retry(Context context, Account account, OCUpload upload) {
-        Log_OC.d(TAG, "FileUploader.retry()");
-        Intent i = new Intent(context, FileUploader.class);
-        i.putExtra(FileUploader.KEY_RETRY, true);
-        if(upload!= null) {
-            i.putExtra(FileUploader.KEY_ACCOUNT, account);
-            i.putExtra(FileUploader.KEY_RETRY_UPLOAD, upload);
-        }
-        context.startService(i);
-    }
+
 
 
 }

+ 3 - 11
src/com/owncloud/android/operations/SynchronizeFileOperation.java

@@ -285,17 +285,9 @@ public class SynchronizeFileOperation extends SyncOperation {
      * @param file     OCFile object representing the file to upload
      */
     private void requestForUpload(OCFile file) {
-        Intent i = new Intent(mContext, FileUploader.class);
-        i.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
-        i.putExtra(FileUploader.KEY_FILE, file);
-        /*i.putExtra(FileUploader.KEY_REMOTE_FILE, mRemotePath);
-        // doing this we would lose the value of keepInSync in the road, and maybe it's not updated
-        in the database when the FileUploader service gets it!
-        i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
-        i.putExtra(FileUploader.KEY_UPLOAD_TYPE,
-                FileUploader.UPLOAD_SINGLE_FILE);
-        i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
-        mContext.startService(i);
+
+        FileUploader.uploadUpdate(mContext, mAccount, file, true);
+
         mTransferWasRequested = true;
     }
 

+ 8 - 8
src/com/owncloud/android/ui/activity/ConflictsResolveActivity.java

@@ -50,17 +50,20 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict
     @Override
     public void conflictDecisionMade(Decision decision) {
         Intent i = new Intent(getApplicationContext(), FileUploader.class);
-        
+
+        Integer behaviour = null;
+        Boolean forceOverwrite = null;
+
         switch (decision) {
             case CANCEL:
                 finish();
                 return;
             case OVERWRITE:
                 // use local version -> overwrite on server
-                i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
+                forceOverwrite = true;
                 break;
             case KEEP_BOTH:
-                i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
+                behaviour = FileUploader.LOCAL_BEHAVIOUR_MOVE;
                 break;
             case SERVER:
                 // use server version -> delete local, request download
@@ -74,11 +77,8 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict
                 Log_OC.wtf(TAG, "Unhandled conflict decision " + decision);
                 return;
         }
-        i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
-        i.putExtra(FileUploader.KEY_FILE, getFile());
-        i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
-        
-        startService(i);
+
+        FileUploader.uploadUpdate(getApplicationContext(), getAccount(), getFile(), behaviour, forceOverwrite);
         finish();
     }
 

+ 2 - 21
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -681,15 +681,7 @@ public class FileDisplayActivity extends HookActivity implements
                 remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
             }
 
-            Intent i = new Intent(this, FileUploader.class);
-            i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
-            i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
-            i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
-            i.putExtra(FileUploader.KEY_UPLOAD_TYPE,
-                    FileUploader.UPLOAD_MULTIPLE_FILES);
-            if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
-                i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
-            startService(i);
+            FileUploader.uploadNewFile(getApplicationContext(), getAccount(), filePaths, remotePaths, resultCode);
 
         } else {
             Log_OC.d(TAG, "User clicked on 'Update' with no selection");
@@ -762,18 +754,7 @@ public class FileDisplayActivity extends HookActivity implements
             remotePath += new File(filePath).getName();
         }
 
-        i.putExtra(FileUploader.KEY_LOCAL_FILE, filePath);
-        i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePath);
-        i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
-        i.putExtra(FileUploader.KEY_UPLOAD_TYPE,
-                FileUploader.UPLOAD_SINGLE_FILE);
-        if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE) {
-            i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR,
-                    FileUploader.LOCAL_BEHAVIOUR_MOVE);
-        }
-        if(startService(i) == null) {
-            Log_OC.e(TAG, "FileUploader could not be started");
-        }
+        FileUploader.uploadNewFile(getApplicationContext(), filePath, remotePath, resultCode, mimeType);
 
     }
 

+ 2 - 16
src/com/owncloud/android/ui/activity/Uploader.java

@@ -567,15 +567,7 @@ public class Uploader extends FileActivity
                     throw new SecurityException();
                 }
 
-                Intent intent = new Intent(getApplicationContext(), FileUploader.class);
-                intent.putExtra(FileUploader.KEY_UPLOAD_TYPE,
-                        FileUploader.UPLOAD_MULTIPLE_FILES);
-                intent.putExtra(FileUploader.KEY_LOCAL_FILE,
-                        local.toArray(new String[local.size()]));
-                intent.putExtra(FileUploader.KEY_REMOTE_FILE,
-                        remote.toArray(new String[remote.size()]));
-                intent.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
-                startService(intent);
+                FileUploader.uploadNewFile(getApplicationContext(), getAccount(), local.toArray(new String[local.size()]), remote.toArray(new String[remote.size()]));
 
                 //Save the path to shared preferences
                 SharedPreferences.Editor appPrefs = PreferenceManager
@@ -703,13 +695,7 @@ public class Uploader extends FileActivity
             dismissWaitingCopyDialog();
         }
         if (result != null) {
-            Intent intent = new Intent(getApplicationContext(), FileUploader.class);
-            intent.putExtra(FileUploader.KEY_UPLOAD_TYPE,
-                    FileUploader.UPLOAD_SINGLE_FILE);
-            intent.putExtra(FileUploader.KEY_LOCAL_FILE, result);
-            intent.putExtra(FileUploader.KEY_REMOTE_FILE, mRemoteCacheData.get(index));
-            intent.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
-            startService(intent);
+            FileUploader.uploadNewFile(getApplicationContext(), getAccount(), result, mRemoteCacheData.get(index));
 
         } else {
             String message = String.format(getString(R.string.uploader_error_forbidden_content),