Filter only '/' character in user input when version of server is 8.1 or later
[pub/Android/ownCloud.git] / src / com / owncloud / android / services / SyncFolderHandler.java
index b0116b3..33318f5 100644 (file)
@@ -1,4 +1,6 @@
-/* ownCloud Android client application
+/**
+ *   ownCloud Android client application
+ *
  *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
@@ -25,6 +27,7 @@ import android.os.Looper;
 import android.os.Message;
 import android.util.Pair;
 
 import android.os.Message;
 import android.util.Pair;
 
+import com.owncloud.android.MainApp;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
@@ -87,6 +90,7 @@ class SyncFolderHandler extends Handler {
     public void handleMessage(Message msg) {
         Pair<Account, String> itemSyncKey = (Pair<Account, String>) msg.obj;
         doOperation(itemSyncKey.first, itemSyncKey.second);
     public void handleMessage(Message msg) {
         Pair<Account, String> itemSyncKey = (Pair<Account, String>) msg.obj;
         doOperation(itemSyncKey.first, itemSyncKey.second);
+        Log_OC.d(TAG, "Stopping after command with id " + msg.arg1);
         mService.stopSelf(msg.arg1);
     }
 
         mService.stopSelf(msg.arg1);
     }
 
@@ -102,36 +106,38 @@ class SyncFolderHandler extends Handler {
             RemoteOperationResult result = null;
 
             try {
             RemoteOperationResult result = null;
 
             try {
-                if (mOwnCloudClient == null || !account.equals(mCurrentAccount)) {
-                    /// get client object to send the request to the ownCloud server, if cannot
+
+                if (mCurrentAccount == null || !mCurrentAccount.equals(account)) {
                     mCurrentAccount = account;
                     mStorageManager = new FileDataStorageManager(
                             account,
                             mService.getContentResolver()
                     );
                     mCurrentAccount = account;
                     mStorageManager = new FileDataStorageManager(
                             account,
                             mService.getContentResolver()
                     );
-                    OwnCloudAccount ocAccount = new OwnCloudAccount(account, mService);
-                    mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
-                            getClientFor(ocAccount, mService);
+                }   // else, reuse storage manager from previous operation
 
 
-                }   // else, reuse client from previous operation
+                // always get client from client manager, to get fresh credentials in case of update
+                OwnCloudAccount ocAccount = new OwnCloudAccount(account, mService);
+                mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
+                        getClientFor(ocAccount, mService);
 
                 result = mCurrentSyncOperation.execute(mOwnCloudClient, mStorageManager);
 
             } catch (AccountsException e) {
 
                 result = mCurrentSyncOperation.execute(mOwnCloudClient, mStorageManager);
 
             } catch (AccountsException e) {
-                Log_OC.e(TAG, "Error while trying to get autorization", e);
+                Log_OC.e(TAG, "Error while trying to get authorization", e);
             } catch (IOException e) {
             } catch (IOException e) {
-                Log_OC.e(TAG, "Error while trying to get autorization", e);
+                Log_OC.e(TAG, "Error while trying to get authorization", e);
             } finally {
                 mPendingOperations.removePayload(account, remotePath);
 
             } finally {
                 mPendingOperations.removePayload(account, remotePath);
 
-                mService.dispatchResultToOperationListeners(null, mCurrentSyncOperation, result);
+                mService.dispatchResultToOperationListeners(mCurrentSyncOperation, result);
 
                 sendBroadcastFinishedSyncFolder(account, remotePath, result.isSuccess());
             }
         }
     }
 
 
                 sendBroadcastFinishedSyncFolder(account, remotePath, result.isSuccess());
             }
         }
     }
 
-    public void add(Account account, String remotePath, SynchronizeFolderOperation syncFolderOperation){
+    public void add(Account account, String remotePath,
+                    SynchronizeFolderOperation syncFolderOperation){
         mPendingOperations.putIfAbsent(account, remotePath, syncFolderOperation);
         sendBroadcastNewSyncFolder(account, remotePath);    // TODO upgrade!
     }
         mPendingOperations.putIfAbsent(account, remotePath, syncFolderOperation);
         sendBroadcastNewSyncFolder(account, remotePath);    // TODO upgrade!
     }
@@ -154,9 +160,9 @@ class SyncFolderHandler extends Handler {
         if (synchronization != null) {
             synchronization.cancel();
         } else {
         if (synchronization != null) {
             synchronization.cancel();
         } else {
-            // TODO synchronize
+            // TODO synchronize?
             if (mCurrentSyncOperation != null && mCurrentAccount != null &&
             if (mCurrentSyncOperation != null && mCurrentAccount != null &&
-                    mCurrentSyncOperation.getFolderPath().startsWith(file.getRemotePath()) &&
+                    mCurrentSyncOperation.getRemotePath().startsWith(file.getRemotePath()) &&
                     account.name.equals(mCurrentAccount.name)) {
                 mCurrentSyncOperation.cancel();
             }
                     account.name.equals(mCurrentAccount.name)) {
                 mCurrentSyncOperation.cancel();
             }
@@ -166,26 +172,29 @@ class SyncFolderHandler extends Handler {
     }
 
     /**
     }
 
     /**
-     * TODO review this method when "folder synchronization" replaces "folder download"; this is a fast and ugly
-     * patch.
+     * TODO review this method when "folder synchronization" replaces "folder download";
+     * this is a fast and ugly patch.
      */
     private void sendBroadcastNewSyncFolder(Account account, String remotePath) {
         Intent added = new Intent(FileDownloader.getDownloadAddedMessage());
         added.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
         added.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
      */
     private void sendBroadcastNewSyncFolder(Account account, String remotePath) {
         Intent added = new Intent(FileDownloader.getDownloadAddedMessage());
         added.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
         added.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
-        added.putExtra(FileDownloader.EXTRA_FILE_PATH, FileStorageUtils.getSavePath(account.name) + remotePath);
+        added.putExtra(FileDownloader.EXTRA_FILE_PATH, FileStorageUtils.getSavePath(account.name)
+                + remotePath);
         mService.sendStickyBroadcast(added);
     }
 
     /**
         mService.sendStickyBroadcast(added);
     }
 
     /**
-     * TODO review this method when "folder synchronization" replaces "folder download"; this is a fast and ugly
-     * patch.
+     * TODO review this method when "folder synchronization" replaces "folder download";
+     * this is a fast and ugly patch.
      */
      */
-    private void sendBroadcastFinishedSyncFolder(Account account, String remotePath, boolean success) {
+    private void sendBroadcastFinishedSyncFolder(Account account, String remotePath,
+                                                 boolean success) {
         Intent finished = new Intent(FileDownloader.getDownloadFinishMessage());
         finished.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
         finished.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
         Intent finished = new Intent(FileDownloader.getDownloadFinishMessage());
         finished.putExtra(FileDownloader.ACCOUNT_NAME, account.name);
         finished.putExtra(FileDownloader.EXTRA_REMOTE_PATH, remotePath);
-        finished.putExtra(FileDownloader.EXTRA_FILE_PATH, FileStorageUtils.getSavePath(account.name) + remotePath);
+        finished.putExtra(FileDownloader.EXTRA_FILE_PATH,
+                FileStorageUtils.getSavePath(account.name) + remotePath);
         finished.putExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, success);
         mService.sendStickyBroadcast(finished);
     }
         finished.putExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, success);
         mService.sendStickyBroadcast(finished);
     }