2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2012-2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.files
.services
;
24 import java
.io
.IOException
;
25 import java
.util
.AbstractList
;
26 import java
.util
.HashMap
;
27 import java
.util
.Iterator
;
29 import java
.util
.Vector
;
31 import android
.accounts
.Account
;
32 import android
.accounts
.AccountManager
;
33 import android
.accounts
.AccountsException
;
34 import android
.accounts
.OnAccountsUpdateListener
;
35 import android
.app
.NotificationManager
;
36 import android
.app
.PendingIntent
;
37 import android
.app
.Service
;
38 import android
.content
.Intent
;
39 import android
.os
.Binder
;
40 import android
.os
.Handler
;
41 import android
.os
.HandlerThread
;
42 import android
.os
.IBinder
;
43 import android
.os
.Looper
;
44 import android
.os
.Message
;
45 import android
.os
.Process
;
46 import android
.support
.v4
.app
.NotificationCompat
;
47 import android
.util
.Pair
;
48 import android
.webkit
.MimeTypeMap
;
50 import com
.owncloud
.android
.R
;
51 import com
.owncloud
.android
.authentication
.AccountUtils
;
52 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
53 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
54 import com
.owncloud
.android
.datamodel
.OCFile
;
55 import com
.owncloud
.android
.db
.DbHandler
;
56 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
57 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
58 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
59 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
60 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
61 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
62 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
63 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
64 import com
.owncloud
.android
.lib
.resources
.files
.ExistenceCheckRemoteOperation
;
65 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
66 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
67 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
68 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
69 import com
.owncloud
.android
.notifications
.NotificationBuilderWithProgressBar
;
70 import com
.owncloud
.android
.notifications
.NotificationDelayer
;
71 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
72 import com
.owncloud
.android
.operations
.UploadFileOperation
;
73 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
74 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
75 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
76 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
77 import com
.owncloud
.android
.utils
.UriUtils
;
80 public class FileUploader
extends Service
81 implements OnDatatransferProgressListener
, OnAccountsUpdateListener
{
83 private static final String UPLOAD_FINISH_MESSAGE
= "UPLOAD_FINISH";
84 public static final String EXTRA_UPLOAD_RESULT
= "RESULT";
85 public static final String EXTRA_REMOTE_PATH
= "REMOTE_PATH";
86 public static final String EXTRA_OLD_REMOTE_PATH
= "OLD_REMOTE_PATH";
87 public static final String EXTRA_OLD_FILE_PATH
= "OLD_FILE_PATH";
88 public static final String EXTRA_LINKED_TO_PATH
= "LINKED_TO";
89 public static final String ACCOUNT_NAME
= "ACCOUNT_NAME";
91 public static final String KEY_FILE
= "FILE";
92 public static final String KEY_LOCAL_FILE
= "LOCAL_FILE";
93 public static final String KEY_REMOTE_FILE
= "REMOTE_FILE";
94 public static final String KEY_MIME_TYPE
= "MIME_TYPE";
96 public static final String KEY_ACCOUNT
= "ACCOUNT";
98 public static final String KEY_UPLOAD_TYPE
= "UPLOAD_TYPE";
99 public static final String KEY_FORCE_OVERWRITE
= "KEY_FORCE_OVERWRITE";
100 public static final String KEY_INSTANT_UPLOAD
= "INSTANT_UPLOAD";
101 public static final String KEY_LOCAL_BEHAVIOUR
= "BEHAVIOUR";
103 public static final int LOCAL_BEHAVIOUR_COPY
= 0;
104 public static final int LOCAL_BEHAVIOUR_MOVE
= 1;
105 public static final int LOCAL_BEHAVIOUR_FORGET
= 2;
107 public static final int UPLOAD_SINGLE_FILE
= 0;
108 public static final int UPLOAD_MULTIPLE_FILES
= 1;
110 private static final String TAG
= FileUploader
.class.getSimpleName();
112 private Looper mServiceLooper
;
113 private ServiceHandler mServiceHandler
;
114 private IBinder mBinder
;
115 private OwnCloudClient mUploadClient
= null
;
116 private Account mLastAccount
= null
;
117 private FileDataStorageManager mStorageManager
;
119 private IndexedForest
<UploadFileOperation
> mPendingUploads
= new IndexedForest
<UploadFileOperation
>();
120 private UploadFileOperation mCurrentUpload
= null
;
122 private NotificationManager mNotificationManager
;
123 private NotificationCompat
.Builder mNotificationBuilder
;
124 private int mLastPercent
;
126 private static final String MIME_TYPE_PDF
= "application/pdf";
127 private static final String FILE_EXTENSION_PDF
= ".pdf";
130 public static String
getUploadFinishMessage() {
131 return FileUploader
.class.getName() + UPLOAD_FINISH_MESSAGE
;
135 * Builds a key for mPendingUploads from the account and file to upload
137 * @param account Account where the file to upload is stored
138 * @param file File to upload
140 private String
buildRemoteName(Account account
, OCFile file
) {
141 return account
.name
+ file
.getRemotePath();
144 private String
buildRemoteName(Account account
, String remotePath
) {
145 return account
.name
+ remotePath
;
149 * Checks if an ownCloud server version should support chunked uploads.
151 * @param version OwnCloud version instance corresponding to an ownCloud
153 * @return 'True' if the ownCloud server with version supports chunked
156 private static boolean chunkedUploadIsSupported(OwnCloudVersion version
) {
157 return (version
!= null
&& version
.compareTo(OwnCloudVersion
.owncloud_v4_5
) >= 0);
161 * Service initialization
164 public void onCreate() {
166 Log_OC
.d(TAG
, "Creating service");
167 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
168 HandlerThread thread
= new HandlerThread("FileUploaderThread",
169 Process
.THREAD_PRIORITY_BACKGROUND
);
171 mServiceLooper
= thread
.getLooper();
172 mServiceHandler
= new ServiceHandler(mServiceLooper
, this);
173 mBinder
= new FileUploaderBinder();
175 // add AccountsUpdatedListener
176 AccountManager am
= AccountManager
.get(getApplicationContext());
177 am
.addOnAccountsUpdatedListener(this, null
, false
);
184 public void onDestroy() {
185 Log_OC
.v(TAG
, "Destroying service" );
187 mServiceHandler
= null
;
188 mServiceLooper
.quit();
189 mServiceLooper
= null
;
190 mNotificationManager
= null
;
192 // remove AccountsUpdatedListener
193 AccountManager am
= AccountManager
.get(getApplicationContext());
194 am
.removeOnAccountsUpdatedListener(this);
201 * Entry point to add one or several files to the queue of uploads.
203 * New uploads are added calling to startService(), resulting in a call to
204 * this method. This ensures the service will keep on working although the
205 * caller activity goes away.
208 public int onStartCommand(Intent intent
, int flags
, int startId
) {
209 Log_OC
.d(TAG
, "Starting command with id " + startId
);
211 if (!intent
.hasExtra(KEY_ACCOUNT
) || !intent
.hasExtra(KEY_UPLOAD_TYPE
)
212 || !(intent
.hasExtra(KEY_LOCAL_FILE
) || intent
.hasExtra(KEY_FILE
))) {
213 Log_OC
.e(TAG
, "Not enough information provided in intent");
214 return Service
.START_NOT_STICKY
;
216 int uploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
217 if (uploadType
== -1) {
218 Log_OC
.e(TAG
, "Incorrect upload type provided");
219 return Service
.START_NOT_STICKY
;
221 Account account
= intent
.getParcelableExtra(KEY_ACCOUNT
);
222 if (!AccountUtils
.exists(account
, getApplicationContext())) {
223 return Service
.START_NOT_STICKY
;
226 String
[] localPaths
= null
, remotePaths
= null
, mimeTypes
= null
;
227 OCFile
[] files
= null
;
228 if (uploadType
== UPLOAD_SINGLE_FILE
) {
230 if (intent
.hasExtra(KEY_FILE
)) {
231 files
= new OCFile
[] { intent
.getParcelableExtra(KEY_FILE
) };
234 localPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
235 remotePaths
= new String
[] { intent
.getStringExtra(KEY_REMOTE_FILE
) };
236 mimeTypes
= new String
[] { intent
.getStringExtra(KEY_MIME_TYPE
) };
239 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
241 if (intent
.hasExtra(KEY_FILE
)) {
242 files
= (OCFile
[]) intent
.getParcelableArrayExtra(KEY_FILE
); // TODO
250 localPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
251 remotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
252 mimeTypes
= intent
.getStringArrayExtra(KEY_MIME_TYPE
);
256 FileDataStorageManager storageManager
= new FileDataStorageManager(account
,
257 getContentResolver());
259 boolean forceOverwrite
= intent
.getBooleanExtra(KEY_FORCE_OVERWRITE
, false
);
260 boolean isInstant
= intent
.getBooleanExtra(KEY_INSTANT_UPLOAD
, false
);
261 int localAction
= intent
.getIntExtra(KEY_LOCAL_BEHAVIOUR
, LOCAL_BEHAVIOUR_COPY
);
263 if (intent
.hasExtra(KEY_FILE
) && files
== null
) {
264 Log_OC
.e(TAG
, "Incorrect array for OCFiles provided in upload intent");
265 return Service
.START_NOT_STICKY
;
267 } else if (!intent
.hasExtra(KEY_FILE
)) {
268 if (localPaths
== null
) {
269 Log_OC
.e(TAG
, "Incorrect array for local paths provided in upload intent");
270 return Service
.START_NOT_STICKY
;
272 if (remotePaths
== null
) {
273 Log_OC
.e(TAG
, "Incorrect array for remote paths provided in upload intent");
274 return Service
.START_NOT_STICKY
;
276 if (localPaths
.length
!= remotePaths
.length
) {
277 Log_OC
.e(TAG
, "Different number of remote paths and local paths!");
278 return Service
.START_NOT_STICKY
;
281 files
= new OCFile
[localPaths
.length
];
282 for (int i
= 0; i
< localPaths
.length
; i
++) {
283 files
[i
] = obtainNewOCFileToUpload(remotePaths
[i
], localPaths
[i
],
284 ((mimeTypes
!= null
) ? mimeTypes
[i
] : null
), storageManager
);
285 if (files
[i
] == null
) {
286 // TODO @andomaex add failure Notification
287 return Service
.START_NOT_STICKY
;
292 OwnCloudVersion ocv
= AccountUtils
.getServerVersion(account
);
294 boolean chunked
= FileUploader
.chunkedUploadIsSupported(ocv
);
295 AbstractList
<String
> requestedUploads
= new Vector
<String
>();
296 String uploadKey
= null
;
297 UploadFileOperation newUpload
= null
;
299 for (int i
= 0; i
< files
.length
; i
++) {
300 newUpload
= new UploadFileOperation(
305 forceOverwrite
, localAction
,
306 getApplicationContext()
309 newUpload
.setRemoteFolderToBeCreated();
311 newUpload
.addDatatransferProgressListener(this);
312 newUpload
.addDatatransferProgressListener((FileUploaderBinder
) mBinder
);
313 Pair
<String
, String
> putResult
= mPendingUploads
.putIfAbsent(
314 account
, files
[i
].getRemotePath(), newUpload
316 uploadKey
= putResult
.first
;
317 requestedUploads
.add(uploadKey
);
320 } catch (IllegalArgumentException e
) {
321 Log_OC
.e(TAG
, "Not enough information provided in intent: " + e
.getMessage());
322 return START_NOT_STICKY
;
324 } catch (IllegalStateException e
) {
325 Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage());
326 return START_NOT_STICKY
;
328 } catch (Exception e
) {
329 Log_OC
.e(TAG
, "Unexpected exception while processing upload intent", e
);
330 return START_NOT_STICKY
;
334 if (requestedUploads
.size() > 0) {
335 Message msg
= mServiceHandler
.obtainMessage();
337 msg
.obj
= requestedUploads
;
338 mServiceHandler
.sendMessage(msg
);
340 return Service
.START_NOT_STICKY
;
344 * Provides a binder object that clients can use to perform operations on
345 * the queue of uploads, excepting the addition of new files.
347 * Implemented to perform cancellation, pause and resume of existing
351 public IBinder
onBind(Intent arg0
) {
356 * Called when ALL the bound clients were onbound.
359 public boolean onUnbind(Intent intent
) {
360 ((FileUploaderBinder
)mBinder
).clearListeners();
361 return false
; // not accepting rebinding (default behaviour)
365 public void onAccountsUpdated(Account
[] accounts
) {
366 // Review current upload, and cancel it if its account doen't exist
367 if (mCurrentUpload
!= null
&&
368 !AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
369 mCurrentUpload
.cancel();
371 // The rest of uploads are cancelled when they try to start
375 * Binder to let client components to perform operations on the queue of
378 * It provides by itself the available operations.
380 public class FileUploaderBinder
extends Binder
implements OnDatatransferProgressListener
{
383 * Map of listeners that will be reported about progress of uploads from a
384 * {@link FileUploaderBinder} instance
386 private Map
<String
, OnDatatransferProgressListener
> mBoundListeners
=
387 new HashMap
<String
, OnDatatransferProgressListener
>();
390 * Cancels a pending or current upload of a remote file.
392 * @param account Owncloud account where the remote file will be stored.
393 * @param file A file in the queue of pending uploads
395 public void cancel(Account account
, OCFile file
) {
396 UploadFileOperation upload
;
397 //synchronized (mPendingUploads) {
398 Pair
<UploadFileOperation
, String
> removeResult
= mPendingUploads
.remove(account
, file
.getRemotePath());
399 upload
= removeResult
.first
;
401 if (upload
!= null
) {
407 * Cancels a pending or current upload for an account
409 * @param account Owncloud accountName where the remote file will be stored.
411 public void cancel(Account account
) {
412 Log_OC
.d(TAG
, "Account= " + account
.name
);
414 if (mCurrentUpload
!= null
) {
415 Log_OC
.d(TAG
, "Current Upload Account= " + mCurrentUpload
.getAccount().name
);
416 if (mCurrentUpload
.getAccount().name
.equals(account
.name
)) {
417 mCurrentUpload
.cancel();
420 // Cancel pending uploads
421 cancelUploadsForAccount(account
);
424 public void clearListeners() {
425 mBoundListeners
.clear();
429 * Returns True when the file described by 'file' is being uploaded to
430 * the ownCloud account 'account' or waiting for it
432 * If 'file' is a directory, returns 'true' if some of its descendant files
433 * is uploading or waiting to upload.
435 * @param account ownCloud account where the remote file will be stored.
436 * @param file A file that could be in the queue of pending uploads
438 public boolean isUploading(Account account
, OCFile file
) {
440 if (account == null || file == null)
442 String targetKey = buildRemoteName(account, file);
443 synchronized (mPendingUploads) {
444 if (file.isFolder()) {
445 // this can be slow if there are many uploads :(
446 Iterator<String> it = mPendingUploads.keySet().iterator();
447 boolean found = false;
448 while (it.hasNext() && !found) {
449 found = it.next().startsWith(targetKey);
453 return (mPendingUploads.containsKey(targetKey));
456 if (account
== null
|| file
== null
) return false
;
457 return (mPendingUploads
.contains(account
, file
.getRemotePath()));
462 * Adds a listener interested in the progress of the upload for a concrete file.
464 * @param listener Object to notify about progress of transfer.
465 * @param account ownCloud account holding the file of interest.
466 * @param file {@link OCFile} of interest for listener.
468 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
,
469 Account account
, OCFile file
) {
470 if (account
== null
|| file
== null
|| listener
== null
) return;
471 String targetKey
= buildRemoteName(account
, file
);
472 mBoundListeners
.put(targetKey
, listener
);
478 * Removes a listener interested in the progress of the upload for a concrete file.
480 * @param listener Object to notify about progress of transfer.
481 * @param account ownCloud account holding the file of interest.
482 * @param file {@link OCFile} of interest for listener.
484 public void removeDatatransferProgressListener (OnDatatransferProgressListener listener
,
485 Account account
, OCFile file
) {
486 if (account
== null
|| file
== null
|| listener
== null
) return;
487 String targetKey
= buildRemoteName(account
, file
);
488 if (mBoundListeners
.get(targetKey
) == listener
) {
489 mBoundListeners
.remove(targetKey
);
495 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
496 long totalToTransfer
, String fileName
) {
497 String key
= buildRemoteName(mCurrentUpload
.getAccount(), mCurrentUpload
.getFile());
498 OnDatatransferProgressListener boundListener
= mBoundListeners
.get(key
);
499 if (boundListener
!= null
) {
500 boundListener
.onTransferProgress(progressRate
, totalTransferredSoFar
,
501 totalToTransfer
, fileName
);
506 * Review uploads and cancel it if its account doesn't exist
508 public void checkAccountOfCurrentUpload() {
509 if (mCurrentUpload
!= null
&&
510 !AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
511 mCurrentUpload
.cancel();
513 // The rest of uploads are cancelled when they try to start
518 * Upload worker. Performs the pending uploads in the order they were
521 * Created with the Looper of a new thread, started in
522 * {@link FileUploader#onCreate()}.
524 private static class ServiceHandler
extends Handler
{
525 // don't make it a final class, and don't remove the static ; lint will
526 // warn about a possible memory leak
527 FileUploader mService
;
529 public ServiceHandler(Looper looper
, FileUploader service
) {
532 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
537 public void handleMessage(Message msg
) {
538 @SuppressWarnings("unchecked")
539 AbstractList
<String
> requestedUploads
= (AbstractList
<String
>) msg
.obj
;
540 if (msg
.obj
!= null
) {
541 Iterator
<String
> it
= requestedUploads
.iterator();
542 while (it
.hasNext()) {
543 mService
.uploadFile(it
.next());
546 Log_OC
.d(TAG
, "Stopping command after id " + msg
.arg1
);
547 mService
.stopSelf(msg
.arg1
);
552 * Core upload method: sends the file(s) to upload
554 * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
556 public void uploadFile(String uploadKey
) {
558 Log_OC
.v( "NOW " + TAG
+ ", thread " + Thread
.currentThread().getName(),
559 "Getting upload of " + uploadKey
);
560 mCurrentUpload
= mPendingUploads
.get(uploadKey
);
562 if (mCurrentUpload
!= null
) {
563 // Detect if the account exists
564 if (AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
565 Log_OC
.d(TAG
, "Account " + mCurrentUpload
.getAccount().name
+ " exists");
567 notifyUploadStart(mCurrentUpload
);
569 RemoteOperationResult uploadResult
= null
, grantResult
;
572 /// prepare client object to send the request to the ownCloud server
573 if (mLastAccount
== null
|| !mLastAccount
.equals(mCurrentUpload
.getAccount())) {
574 mLastAccount
= mCurrentUpload
.getAccount();
575 mStorageManager
= new FileDataStorageManager(
579 } // else, reuse storage manager from previous operation
581 // always get client from client manager, to get fresh credentials in case of update
582 OwnCloudAccount ocAccount
= new OwnCloudAccount(mLastAccount
, this);
583 mUploadClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
584 getClientFor(ocAccount
, this);
587 /// check the existence of the parent folder for the file to upload
588 String remoteParentPath
= new File(mCurrentUpload
.getRemotePath()).getParent();
589 remoteParentPath
= remoteParentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ?
590 remoteParentPath
: remoteParentPath
+ OCFile
.PATH_SEPARATOR
;
591 grantResult
= grantFolderExistence(remoteParentPath
);
593 /// perform the upload
594 if (grantResult
.isSuccess()) {
595 Log_OC
.v( "NOW " + TAG
+ ", thread " + Thread
.currentThread().getName(),
596 "Executing upload of " + mCurrentUpload
.getRemotePath());
597 OCFile parent
= mStorageManager
.getFileByPath(remoteParentPath
);
598 mCurrentUpload
.getFile().setParentId(parent
.getFileId());
599 uploadResult
= mCurrentUpload
.execute(mUploadClient
);
600 if (uploadResult
.isSuccess()) {
604 uploadResult
= grantResult
;
607 } catch (AccountsException e
) {
608 Log_OC
.e(TAG
, "Error while trying to get autorization for " +
609 mLastAccount
.name
, e
);
610 uploadResult
= new RemoteOperationResult(e
);
612 } catch (IOException e
) {
613 Log_OC
.e(TAG
, "Error while trying to get autorization for " +
614 mLastAccount
.name
, e
);
615 uploadResult
= new RemoteOperationResult(e
);
618 Log_OC
.v("NOW " + TAG
+ ", thread " + Thread
.currentThread().getName(),
619 "Removing payload " + mCurrentUpload
.getRemotePath());
620 Pair
<UploadFileOperation
, String
> removeResult
=
621 mPendingUploads
.removePayload(mLastAccount
, mCurrentUpload
.getRemotePath());
624 notifyUploadResult(mCurrentUpload
, uploadResult
);
626 sendBroadcastUploadFinished(mCurrentUpload
, uploadResult
, removeResult
.second
);
630 // Cancel the transfer
631 Log_OC
.d(TAG
, "Account " + mCurrentUpload
.getAccount().toString() +
633 cancelUploadsForAccount(mCurrentUpload
.getAccount());
641 * Checks the existence of the folder where the current file will be uploaded both
642 * in the remote server and in the local database.
644 * If the upload is set to enforce the creation of the folder, the method tries to
645 * create it both remote and locally.
647 * @param pathToGrant Full remote path whose existence will be granted.
648 * @return An {@link OCFile} instance corresponding to the folder where the file
651 private RemoteOperationResult
grantFolderExistence(String pathToGrant
) {
652 RemoteOperation operation
= new ExistenceCheckRemoteOperation(pathToGrant
, this, false
);
653 RemoteOperationResult result
= operation
.execute(mUploadClient
);
654 if (!result
.isSuccess() && result
.getCode() == ResultCode
.FILE_NOT_FOUND
&&
655 mCurrentUpload
.isRemoteFolderToBeCreated()) {
656 SyncOperation syncOp
= new CreateFolderOperation( pathToGrant
, true
);
657 result
= syncOp
.execute(mUploadClient
, mStorageManager
);
659 if (result
.isSuccess()) {
660 OCFile parentDir
= mStorageManager
.getFileByPath(pathToGrant
);
661 if (parentDir
== null
) {
662 parentDir
= createLocalFolder(pathToGrant
);
664 if (parentDir
!= null
) {
665 result
= new RemoteOperationResult(ResultCode
.OK
);
667 result
= new RemoteOperationResult(ResultCode
.UNKNOWN_ERROR
);
674 private OCFile
createLocalFolder(String remotePath
) {
675 String parentPath
= new File(remotePath
).getParent();
676 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ?
677 parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
678 OCFile parent
= mStorageManager
.getFileByPath(parentPath
);
679 if (parent
== null
) {
680 parent
= createLocalFolder(parentPath
);
682 if (parent
!= null
) {
683 OCFile createdFolder
= new OCFile(remotePath
);
684 createdFolder
.setMimetype("DIR");
685 createdFolder
.setParentId(parent
.getFileId());
686 mStorageManager
.saveFile(createdFolder
);
687 return createdFolder
;
694 * Saves a OC File after a successful upload.
696 * A PROPFIND is necessary to keep the props in the local database
697 * synchronized with the server, specially the modification time and Etag
700 * TODO refactor this ugly thing
702 private void saveUploadedFile() {
703 OCFile file
= mCurrentUpload
.getFile();
704 if (file
.fileExists()) {
705 file
= mStorageManager
.getFileById(file
.getFileId());
707 long syncDate
= System
.currentTimeMillis();
708 file
.setLastSyncDateForData(syncDate
);
710 // new PROPFIND to keep data consistent with server
711 // in theory, should return the same we already have
712 ReadRemoteFileOperation operation
=
713 new ReadRemoteFileOperation(mCurrentUpload
.getRemotePath());
714 RemoteOperationResult result
= operation
.execute(mUploadClient
);
715 if (result
.isSuccess()) {
716 updateOCFile(file
, (RemoteFile
) result
.getData().get(0));
717 file
.setLastSyncDateForProperties(syncDate
);
720 // / maybe this would be better as part of UploadFileOperation... or
721 // maybe all this method
722 if (mCurrentUpload
.wasRenamed()) {
723 OCFile oldFile
= mCurrentUpload
.getOldFile();
724 if (oldFile
.fileExists()) {
725 oldFile
.setStoragePath(null
);
726 mStorageManager
.saveFile(oldFile
);
728 } // else: it was just an automatic renaming due to a name
729 // coincidence; nothing else is needed, the storagePath is right
730 // in the instance returned by mCurrentUpload.getFile()
732 file
.setNeedsUpdateThumbnail(true
);
733 mStorageManager
.saveFile(file
);
736 private void updateOCFile(OCFile file
, RemoteFile remoteFile
) {
737 file
.setCreationTimestamp(remoteFile
.getCreationTimestamp());
738 file
.setFileLength(remoteFile
.getLength());
739 file
.setMimetype(remoteFile
.getMimeType());
740 file
.setModificationTimestamp(remoteFile
.getModifiedTimestamp());
741 file
.setModificationTimestampAtLastSyncForData(remoteFile
.getModifiedTimestamp());
742 // file.setEtag(remoteFile.getEtag()); // TODO Etag, where available
743 file
.setRemoteId(remoteFile
.getRemoteId());
746 private OCFile
obtainNewOCFileToUpload(String remotePath
, String localPath
, String mimeType
,
747 FileDataStorageManager storageManager
) {
750 if (mimeType
== null
|| mimeType
.length() <= 0) {
752 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
753 remotePath
.substring(remotePath
.lastIndexOf('.') + 1));
754 } catch (IndexOutOfBoundsException e
) {
755 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " +
759 if (mimeType
== null
) {
760 mimeType
= "application/octet-stream";
763 if (isPdfFileFromContentProviderWithoutExtension(localPath
, mimeType
)){
764 remotePath
+= FILE_EXTENSION_PDF
;
767 OCFile newFile
= new OCFile(remotePath
);
768 newFile
.setStoragePath(localPath
);
769 newFile
.setLastSyncDateForProperties(0);
770 newFile
.setLastSyncDateForData(0);
773 if (localPath
!= null
&& localPath
.length() > 0) {
774 File localFile
= new File(localPath
);
775 newFile
.setFileLength(localFile
.length());
776 newFile
.setLastSyncDateForData(localFile
.lastModified());
777 } // don't worry about not assigning size, the problems with localPath
778 // are checked when the UploadFileOperation instance is created
781 newFile
.setMimetype(mimeType
);
787 * Creates a status notification to show the upload progress
789 * @param upload Upload operation starting.
791 private void notifyUploadStart(UploadFileOperation upload
) {
792 // / create status notification with a progress bar
794 mNotificationBuilder
=
795 NotificationBuilderWithProgressBar
.newNotificationBuilderWithProgressBar(this);
798 .setSmallIcon(R
.drawable
.notification_icon
)
799 .setTicker(getString(R
.string
.uploader_upload_in_progress_ticker
))
800 .setContentTitle(getString(R
.string
.uploader_upload_in_progress_ticker
))
801 .setProgress(100, 0, false
)
803 String
.format(getString(R
.string
.uploader_upload_in_progress_content
), 0, upload
.getFileName()));
805 /// includes a pending intent in the notification showing the details view of the file
806 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
807 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, upload
.getFile());
808 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, upload
.getAccount());
809 showDetailsIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
810 mNotificationBuilder
.setContentIntent(PendingIntent
.getActivity(
811 this, (int) System
.currentTimeMillis(), showDetailsIntent
, 0
814 mNotificationManager
.notify(R
.string
.uploader_upload_in_progress_ticker
, mNotificationBuilder
.build());
818 * Callback method to update the progress bar in the status notification
821 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
822 long totalToTransfer
, String filePath
) {
823 int percent
= (int) (100.0 * ((double) totalTransferredSoFar
) / ((double) totalToTransfer
));
824 if (percent
!= mLastPercent
) {
825 mNotificationBuilder
.setProgress(100, percent
, false
);
826 String fileName
= filePath
.substring(
827 filePath
.lastIndexOf(FileUtils
.PATH_SEPARATOR
) + 1);
828 String text
= String
.format(getString(R
.string
.uploader_upload_in_progress_content
), percent
, fileName
);
829 mNotificationBuilder
.setContentText(text
);
830 mNotificationManager
.notify(R
.string
.uploader_upload_in_progress_ticker
, mNotificationBuilder
.build());
832 mLastPercent
= percent
;
836 * Updates the status notification with the result of an upload operation.
838 * @param uploadResult Result of the upload operation.
839 * @param upload Finished upload operation
841 private void notifyUploadResult(UploadFileOperation upload
,
842 RemoteOperationResult uploadResult
) {
843 Log_OC
.d(TAG
, "NotifyUploadResult with resultCode: " + uploadResult
.getCode());
844 // / cancelled operation or success -> silent removal of progress notification
845 mNotificationManager
.cancel(R
.string
.uploader_upload_in_progress_ticker
);
847 // Show the result: success or fail notification
848 if (!uploadResult
.isCancelled()) {
849 int tickerId
= (uploadResult
.isSuccess()) ? R
.string
.uploader_upload_succeeded_ticker
:
850 R
.string
.uploader_upload_failed_ticker
;
854 // check credentials error
855 boolean needsToUpdateCredentials
= (
856 uploadResult
.getCode() == ResultCode
.UNAUTHORIZED
||
857 uploadResult
.isIdPRedirection()
859 tickerId
= (needsToUpdateCredentials
) ?
860 R
.string
.uploader_upload_failed_credentials_error
: tickerId
;
863 .setTicker(getString(tickerId
))
864 .setContentTitle(getString(tickerId
))
867 .setProgress(0, 0, false
);
869 content
= ErrorMessageAdapter
.getErrorCauseMessage(
870 uploadResult
, upload
, getResources()
873 if (needsToUpdateCredentials
) {
874 // let the user update credentials with one click
875 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
876 updateAccountCredentials
.putExtra(
877 AuthenticatorActivity
.EXTRA_ACCOUNT
, upload
.getAccount()
879 updateAccountCredentials
.putExtra(
880 AuthenticatorActivity
.EXTRA_ACTION
,
881 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
883 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
884 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
885 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
886 mNotificationBuilder
.setContentIntent(PendingIntent
.getActivity(
888 (int) System
.currentTimeMillis(),
889 updateAccountCredentials
,
890 PendingIntent
.FLAG_ONE_SHOT
893 mUploadClient
= null
;
894 // grant that future retries on the same account will get the fresh credentials
896 mNotificationBuilder
.setContentText(content
);
898 if (upload
.isInstant()) {
901 db
= new DbHandler(this.getBaseContext());
902 String message
= uploadResult
.getLogMessage() + " errorCode: " +
903 uploadResult
.getCode();
904 Log_OC
.e(TAG
, message
+ " Http-Code: " + uploadResult
.getHttpCode());
905 if (uploadResult
.getCode() == ResultCode
.QUOTA_EXCEEDED
) {
906 //message = getString(R.string.failed_upload_quota_exceeded_text);
907 if (db
.updateFileState(
908 upload
.getOriginalStoragePath(),
909 DbHandler
.UPLOAD_STATUS_UPLOAD_FAILED
,
912 upload
.getOriginalStoragePath(),
913 upload
.getAccount().name
,
926 mNotificationBuilder
.setContentText(content
);
927 mNotificationManager
.notify(tickerId
, mNotificationBuilder
.build());
929 if (uploadResult
.isSuccess()) {
931 DbHandler db
= new DbHandler(this.getBaseContext());
932 db
.removeIUPendingFile(mCurrentUpload
.getOriginalStoragePath());
935 // remove success notification, with a delay of 2 seconds
936 NotificationDelayer
.cancelWithDelay(
937 mNotificationManager
,
938 R
.string
.uploader_upload_succeeded_ticker
,
946 * Sends a broadcast in order to the interested activities can update their
949 * @param upload Finished upload operation
950 * @param uploadResult Result of the upload operation
951 * @param unlinkedFromRemotePath Path in the uploads tree where the upload was unlinked from
953 private void sendBroadcastUploadFinished(
954 UploadFileOperation upload
,
955 RemoteOperationResult uploadResult
,
956 String unlinkedFromRemotePath
) {
958 Intent end
= new Intent(getUploadFinishMessage());
959 end
.putExtra(EXTRA_REMOTE_PATH
, upload
.getRemotePath()); // real remote
964 if (upload
.wasRenamed()) {
965 end
.putExtra(EXTRA_OLD_REMOTE_PATH
, upload
.getOldFile().getRemotePath());
967 end
.putExtra(EXTRA_OLD_FILE_PATH
, upload
.getOriginalStoragePath());
968 end
.putExtra(ACCOUNT_NAME
, upload
.getAccount().name
);
969 end
.putExtra(EXTRA_UPLOAD_RESULT
, uploadResult
.isSuccess());
970 if (unlinkedFromRemotePath
!= null
) {
971 end
.putExtra(EXTRA_LINKED_TO_PATH
, unlinkedFromRemotePath
);
974 sendStickyBroadcast(end
);
978 * Checks if content provider, using the content:// scheme, returns a file with mime-type
979 * 'application/pdf' but file has not extension
980 * @param localPath Full path to a file in the local file system.
981 * @param mimeType MIME type of the file.
982 * @return true if is needed to add the pdf file extension to the file
984 private boolean isPdfFileFromContentProviderWithoutExtension(String localPath
,
986 return localPath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
) &&
987 mimeType
.equals(MIME_TYPE_PDF
) &&
988 !localPath
.endsWith(FILE_EXTENSION_PDF
);
992 * Remove uploads of an account
994 * @param account Downloads account to remove
996 private void cancelUploadsForAccount(Account account
){
997 // Cancel pending uploads
998 mPendingUploads
.remove(account
);