+ UploadFileOperation currentUpload;
+ Account currentAccount, lastAccount = null;
+ FileDataStorageManager storageManager = null;
+ WebdavClient wc = null;
+ mSuccessCounter = 0;
+ boolean createdInstantDir = false;
+
+ for (mCurrentIndexUpload = 0; mCurrentIndexUpload < mUploads.size(); mCurrentIndexUpload++) {
+ currentUpload = mUploads.get(mCurrentIndexUpload);
+ currentAccount = mAccounts.get(mCurrentIndexUpload);
+
+ /// prepare client object to send request(s) to the ownCloud server
+ if (lastAccount == null || !lastAccount.equals(currentAccount)) {
+ storageManager = new FileDataStorageManager(currentAccount, getContentResolver());
+ wc = OwnCloudClientUtils.createOwnCloudClient(currentAccount, getApplicationContext());
+ wc.setDataTransferProgressListener(this);
+ }
+
+ if (currentUpload.isInstant() && !createdInstantDir) {
+ createdInstantDir = createRemoteFolderForInstantUploads(wc, storageManager);
+ }
+
+ /// perform the upload
+ long parentDirId = -1;
+ RemoteOperationResult uploadResult = null;
+ boolean updateResult = false;
+ try {
+ File remote = new File(currentUpload.getRemotePath());
+ parentDirId = storageManager.getFileByPath(remote.getParent().endsWith("/")?remote.getParent():remote.getParent()+"/").getFileId();
+ File local = new File(currentUpload.getLocalPath());
+ long size = local.length();
+ mUploadsInProgress.put(buildRemoteName(currentAccount.name, currentUpload.getRemotePath()), currentUpload.getLocalPath());
+ uploadResult = currentUpload.execute(wc);
+ if (uploadResult.isSuccess()) {
+ saveNewOCFile(currentUpload, storageManager, parentDirId, size);
+ mSuccessCounter++;
+ updateResult = true;
+ }
+
+ } finally {
+ mUploadsInProgress.remove(buildRemoteName(currentAccount.name, currentUpload.getRemotePath()));
+ broadcastUploadEnd(currentUpload, currentAccount, updateResult, parentDirId);
+ }
+ }
+
+ notifyUploadEndOverview();
+
+ }
+
+ /**
+ * Create remote folder for instant uploads if necessary.
+ *
+ * @param client WebdavClient to the ownCloud server.
+ * @param storageManager Interface to the local database caching the data in the server.
+ * @return 'True' if the folder exists when the methods finishes.
+ */
+ private boolean createRemoteFolderForInstantUploads(WebdavClient client, FileDataStorageManager storageManager) {
+ boolean result = true;
+ OCFile instantUploadDir = storageManager.getFileByPath(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR);
+ if (instantUploadDir == null) {
+ result = client.createDirectory(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR); // fail could just mean that it already exists, but local database is not synchronized; the upload will be started anyway
+ OCFile newDir = new OCFile(PhotoTakenBroadcastReceiver.INSTANT_UPLOAD_DIR);
+ newDir.setMimetype("DIR");
+ newDir.setParentId(storageManager.getFileByPath(OCFile.PATH_SEPARATOR).getFileId());
+ storageManager.saveFile(newDir);
+ }
+ return result;
+ }
+
+ /**
+ * Saves a new OC File after a successful upload.
+ *
+ * @param upload Upload operation completed.
+ * @param storageManager Interface to the database where the new OCFile has to be stored.
+ * @param parentDirId Id of the parent OCFile.
+ * @param size Size of the file.
+ */
+ private void saveNewOCFile(UploadFileOperation upload, FileDataStorageManager storageManager, long parentDirId, long size) {
+ OCFile newFile = new OCFile(upload.getRemotePath());
+ newFile.setMimetype(upload.getMimeType());
+ newFile.setFileLength(size);
+ newFile.setModificationTimestamp(System.currentTimeMillis());
+ newFile.setLastSyncDate(0);
+ newFile.setStoragePath(upload.getLocalPath());
+ newFile.setParentId(parentDirId);
+ if (upload.getForceOverwrite())
+ newFile.setKeepInSync(true);
+ storageManager.saveFile(newFile);
+ }
+
+ /**
+ * Creates a status notification to show the upload progress
+ */
+ private void notifyUploadStart() {