+ }
+
+ /**
+ * Saves a OC File after a successful upload.
+ *
+ * A PROPFIND is necessary to keep the props in the local database synchronized with the server,
+ * specially the modification time and Etag (where available)
+ *
+ * TODO refactor this ugly thing
+ */
+ private void saveUploadedFile() {
+ OCFile file = mCurrentUpload.getFile();
+ long syncDate = System.currentTimeMillis();
+ file.setLastSyncDateForData(syncDate);
+
+ /// new PROPFIND to keep data consistent with server in theory, should return the same we already have
+ PropFindMethod propfind = null;
+ RemoteOperationResult result = null;
+ try {
+ propfind = new PropFindMethod(mUploadClient.getBaseUri() + WebdavUtils.encodePath(mCurrentUpload.getRemotePath()));
+ int status = mUploadClient.executeMethod(propfind);
+ boolean isMultiStatus = (status == HttpStatus.SC_MULTI_STATUS);
+ if (isMultiStatus) {
+ MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
+ WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
+ mUploadClient.getBaseUri().getPath());
+ updateOCFile(file, we);
+ file.setLastSyncDateForProperties(syncDate);
+
+ } else {
+ mUploadClient.exhaustResponse(propfind.getResponseBodyAsStream());
+ }
+
+ result = new RemoteOperationResult(isMultiStatus, status);
+ Log.i(TAG, "Update: synchronizing properties for uploaded " + mCurrentUpload.getRemotePath() + ": " + result.getLogMessage());
+
+ } catch (Exception e) {
+ result = new RemoteOperationResult(e);
+ Log.e(TAG, "Update: synchronizing properties for uploaded " + mCurrentUpload.getRemotePath() + ": " + result.getLogMessage(), e);
+
+ } finally {
+ if (propfind != null)
+ propfind.releaseConnection();
+ }
+
+ /// maybe this would be better as part of UploadFileOperation... or maybe all this method
+ if (mCurrentUpload.wasRenamed()) {
+ OCFile oldFile = mCurrentUpload.getOldFile();
+ if (oldFile.fileExists()) {
+ oldFile.setStoragePath(null);
+ mStorageManager.saveFile(oldFile);
+
+ } // else: it was just an automatic renaming due to a name coincidence; nothing else is needed, the storagePath is right in the instance returned by mCurrentUpload.getFile()
+ }
+
+ mStorageManager.saveFile(file);
+ }
+
+
+ private void updateOCFile(OCFile file, WebdavEntry we) {
+ file.setCreationTimestamp(we.createTimestamp());
+ file.setFileLength(we.contentLength());
+ file.setMimetype(we.contentType());
+ file.setModificationTimestamp(we.modifiedTimesamp());
+ // file.setEtag(mCurrentDownload.getEtag()); // TODO Etag, where available
+ }
+
+
+ private boolean checkAndFixInstantUploadDirectory(FileDataStorageManager storageManager) {
+ OCFile instantUploadDir = storageManager.getFileByPath(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR);
+ if (instantUploadDir == null) {
+ // first instant upload in the account, or never account not synchronized after the remote InstantUpload folder was created
+ OCFile newDir = new OCFile(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR);
+ newDir.setMimetype("DIR");
+ newDir.setParentId(storageManager.getFileByPath(OCFile.PATH_SEPARATOR).getFileId());
+ storageManager.saveFile(newDir);
+ return true;
+ }
+ return false;
+ }
+
+
+ private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType, FileDataStorageManager storageManager) {
+ OCFile newFile = new OCFile(remotePath);
+ newFile.setStoragePath(localPath);
+ newFile.setLastSyncDateForProperties(0);
+ newFile.setLastSyncDateForData(0);
+
+ // size
+ if (localPath != null && localPath.length() > 0) {
+ File localFile = new File(localPath);
+ newFile.setFileLength(localFile.length());
+ newFile.setLastSyncDateForData(localFile.lastModified());
+ } // don't worry about not assigning size, the problems with localPath are checked when the UploadFileOperation instance is created
+
+ // MIME type
+ if (mimeType == null || mimeType.length() <= 0) {
+ try {
+ mimeType = MimeTypeMap.getSingleton()
+ .getMimeTypeFromExtension(
+ remotePath.substring(remotePath.lastIndexOf('.') + 1));
+ } catch (IndexOutOfBoundsException e) {
+ Log.e(TAG, "Trying to find out MIME type of a file without extension: " + remotePath);
+ }
+ }
+ if (mimeType == null) {
+ mimeType = "application/octet-stream";
+ }
+ newFile.setMimetype(mimeType);
+
+ // parent dir
+ String parentPath = new File(remotePath).getParent();
+ parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR ;
+ OCFile parentDir = storageManager.getFileByPath(parentPath);
+ if (parentDir == null) {
+ throw new IllegalStateException("Can not upload a file to a non existing remote location: " + parentPath);
+ }
+ long parentDirId = parentDir.getFileId();
+ newFile.setParentId(parentDirId);
+ return newFile;
+ }
+
+
+ /**
+ * Creates a status notification to show the upload progress
+ *
+ * @param upload Upload operation starting.
+ */
+ private void notifyUploadStart(UploadFileOperation upload) {
+ /// create status notification with a progress bar
+ mLastPercent = 0;
+ mNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_in_progress_ticker), System.currentTimeMillis());
+ mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
+ mDefaultNotificationContentView = mNotification.contentView;
+ mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
+ mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
+ mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.uploader_upload_in_progress_content), 0, new File(upload.getStoragePath()).getName()));
+ mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
+
+ /// includes a pending intent in the notification showing the details view of the file
+ Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
+ showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, upload.getFile());
+ showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, upload.getAccount());
+ showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);
+
+ mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
+ }
+
+
+ /**
+ * Callback method to update the progress bar in the status notification
+ */
+ @Override
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
+ int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
+ if (percent != mLastPercent) {
+ mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false);
+ String text = String.format(getString(R.string.uploader_upload_in_progress_content), percent, fileName);
+ mNotification.contentView.setTextViewText(R.id.status_text, text);
+ mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
+ }
+ mLastPercent = percent;
+ }
+
+
+ /**
+ * Callback method to update the progress bar in the status notification (old version)
+ */
+ @Override
+ public void onTransferProgress(long progressRate) {
+ // NOTHING TO DO HERE ANYMORE
+ }
+
+
+ /**
+ * Updates the status notification with the result of an upload operation.
+ *
+ * @param uploadResult Result of the upload operation.
+ * @param upload Finished upload operation
+ */
+ private void notifyUploadResult(RemoteOperationResult uploadResult, UploadFileOperation upload) {
+ if (uploadResult.isCancelled()) {
+ /// cancelled operation -> silent removal of progress notification
+ mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker);
+
+ } else if (uploadResult.isSuccess()) {
+ /// success -> silent update of progress notification to success message