+ /**
+ * Cancel operation
+ * @param account Owncloud account where the remote file is stored.
+ * @param file File OCFile
+ */
+ public void cancel(Account account, OCFile file){
+ if(Looper.myLooper() == Looper.getMainLooper()) {
+ Log_OC.d(TAG, "Current Thread is Main Thread.");
+ } else {
+ Log_OC.d(TAG, "Current Thread is NOT Main Thread.");
+ }
+
+ DownloadFileOperation download = null;
+ String targetKey = buildRemoteName(account, file);
+ ArrayList<String> keyItems = new ArrayList<String>();
+ synchronized (mPendingDownloads) {
+ if (file.isFolder()) {
+ Log_OC.d(TAG, "Folder download. Canceling pending downloads (from folder)");
+ Iterator<String> it = mPendingDownloads.keySet().iterator();
+ boolean found = false;
+ while (it.hasNext()) {
+ String keyDownloadOperation = it.next();
+ found = keyDownloadOperation.startsWith(targetKey);
+ if (found) {
+ keyItems.add(keyDownloadOperation);
+ }
+ }
+ } else {
+ Log_OC.d(TAG, "Canceling file download");
+ keyItems.add(buildRemoteName(account, file));
+ }
+ }
+ for (String item: keyItems) {
+ download = mPendingDownloads.remove(item);
+ Log_OC.d(TAG, "Key removed: " + item);
+
+ if (download != null) {
+ download.cancel();
+ }
+ }
+ }