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
;
30 import java
.util
.concurrent
.ConcurrentHashMap
;
31 import java
.util
.concurrent
.ConcurrentMap
;
33 import android
.accounts
.Account
;
34 import android
.accounts
.AccountManager
;
35 import android
.accounts
.AccountsException
;
36 import android
.accounts
.OnAccountsUpdateListener
;
37 import android
.app
.NotificationManager
;
38 import android
.app
.PendingIntent
;
39 import android
.app
.Service
;
40 import android
.content
.Intent
;
41 import android
.os
.Binder
;
42 import android
.os
.Handler
;
43 import android
.os
.HandlerThread
;
44 import android
.os
.IBinder
;
45 import android
.os
.Looper
;
46 import android
.os
.Message
;
47 import android
.os
.Process
;
48 import android
.support
.v4
.app
.NotificationCompat
;
49 import android
.webkit
.MimeTypeMap
;
51 import com
.owncloud
.android
.R
;
52 import com
.owncloud
.android
.authentication
.AccountUtils
;
53 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
54 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
55 import com
.owncloud
.android
.datamodel
.OCFile
;
56 import com
.owncloud
.android
.db
.DbHandler
;
57 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
58 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
59 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
60 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
61 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
62 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
63 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
64 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
65 import com
.owncloud
.android
.lib
.resources
.files
.ExistenceCheckRemoteOperation
;
66 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
67 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
68 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
69 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
70 import com
.owncloud
.android
.notifications
.NotificationBuilderWithProgressBar
;
71 import com
.owncloud
.android
.notifications
.NotificationDelayer
;
72 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
73 import com
.owncloud
.android
.operations
.UploadFileOperation
;
74 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
75 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
76 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
77 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
78 import com
.owncloud
.android
.utils
.UriUtils
;
81 public class FileUploader
extends Service
82 implements OnDatatransferProgressListener
, OnAccountsUpdateListener
{
84 private static final String UPLOAD_FINISH_MESSAGE
= "UPLOAD_FINISH";
85 public static final String EXTRA_UPLOAD_RESULT
= "RESULT";
86 public static final String EXTRA_REMOTE_PATH
= "REMOTE_PATH";
87 public static final String EXTRA_OLD_REMOTE_PATH
= "OLD_REMOTE_PATH";
88 public static final String EXTRA_OLD_FILE_PATH
= "OLD_FILE_PATH";
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 String KEY_CANCEL_ALL
= "CANCEL_ALL";
105 public static final int LOCAL_BEHAVIOUR_COPY
= 0;
106 public static final int LOCAL_BEHAVIOUR_MOVE
= 1;
107 public static final int LOCAL_BEHAVIOUR_FORGET
= 2;
109 public static final int UPLOAD_SINGLE_FILE
= 0;
110 public static final int UPLOAD_MULTIPLE_FILES
= 1;
112 private static final String TAG
= FileUploader
.class.getSimpleName();
114 private Looper mServiceLooper
;
115 private ServiceHandler mServiceHandler
;
116 private IBinder mBinder
;
117 private OwnCloudClient mUploadClient
= null
;
118 private Account mLastAccount
= null
;
119 private FileDataStorageManager mStorageManager
;
121 private ConcurrentMap
<String
, UploadFileOperation
> mPendingUploads
=
122 new ConcurrentHashMap
<String
, UploadFileOperation
>();
123 private UploadFileOperation mCurrentUpload
= null
;
125 private NotificationManager mNotificationManager
;
126 private NotificationCompat
.Builder mNotificationBuilder
;
127 private int mLastPercent
;
129 private static final String MIME_TYPE_PDF
= "application/pdf";
130 private static final String FILE_EXTENSION_PDF
= ".pdf";
133 public static String
getUploadFinishMessage() {
134 return FileUploader
.class.getName() + UPLOAD_FINISH_MESSAGE
;
138 * Builds a key for mPendingUploads from the account and file to upload
140 * @param account Account where the file to upload is stored
141 * @param file File to upload
143 private String
buildRemoteName(Account account
, OCFile file
) {
144 return account
.name
+ file
.getRemotePath();
147 private String
buildRemoteName(Account account
, String remotePath
) {
148 return account
.name
+ remotePath
;
152 * Checks if an ownCloud server version should support chunked uploads.
154 * @param version OwnCloud version instance corresponding to an ownCloud
156 * @return 'True' if the ownCloud server with version supports chunked
159 private static boolean chunkedUploadIsSupported(OwnCloudVersion version
) {
160 return (version
!= null
&& version
.compareTo(OwnCloudVersion
.owncloud_v4_5
) >= 0);
164 * Service initialization
167 public void onCreate() {
169 Log_OC
.d(TAG
, "Creating service");
170 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
171 HandlerThread thread
= new HandlerThread("FileUploaderThread",
172 Process
.THREAD_PRIORITY_BACKGROUND
);
174 mServiceLooper
= thread
.getLooper();
175 mServiceHandler
= new ServiceHandler(mServiceLooper
, this);
176 mBinder
= new FileUploaderBinder();
178 // add AccountsUpdatedListener
179 AccountManager am
= AccountManager
.get(getApplicationContext());
180 am
.addOnAccountsUpdatedListener(this, null
, false
);
187 public void onDestroy() {
188 Log_OC
.v(TAG
, "Destroying service" );
190 mServiceHandler
= null
;
191 mServiceLooper
.quit();
192 mServiceLooper
= null
;
193 mNotificationManager
= null
;
195 // remove AccountsUpdatedListener
196 AccountManager am
= AccountManager
.get(getApplicationContext());
197 am
.removeOnAccountsUpdatedListener(this);
204 * Entry point to add one or several files to the queue of uploads.
206 * New uploads are added calling to startService(), resulting in a call to
207 * this method. This ensures the service will keep on working although the
208 * caller activity goes away.
211 public int onStartCommand(Intent intent
, int flags
, int startId
) {
212 Log_OC
.d(TAG
, "Starting command with id " + startId
);
214 if (intent
.hasExtra(KEY_CANCEL_ALL
) && intent
.hasExtra(KEY_ACCOUNT
)){
215 Account account
= intent
.getParcelableExtra(KEY_ACCOUNT
);
217 Log_OC
.d(TAG
, "Account= " + account
.name
);
219 if (mCurrentUpload
!= null
) {
220 Log_OC
.d(TAG
, "Current Upload Account= " + mCurrentUpload
.getAccount().name
);
221 if (mCurrentUpload
.getAccount().name
.equals(account
.name
)) {
222 mCurrentUpload
.cancel();
225 // Cancel pending uploads
226 cancelUploadForAccount(account
.name
);
229 if (!intent
.hasExtra(KEY_ACCOUNT
) || !intent
.hasExtra(KEY_UPLOAD_TYPE
)
230 || !(intent
.hasExtra(KEY_LOCAL_FILE
) || intent
.hasExtra(KEY_FILE
))) {
231 Log_OC
.e(TAG
, "Not enough information provided in intent");
232 return Service
.START_NOT_STICKY
;
234 int uploadType
= intent
.getIntExtra(KEY_UPLOAD_TYPE
, -1);
235 if (uploadType
== -1) {
236 Log_OC
.e(TAG
, "Incorrect upload type provided");
237 return Service
.START_NOT_STICKY
;
239 Account account
= intent
.getParcelableExtra(KEY_ACCOUNT
);
240 if (!AccountUtils
.exists(account
, getApplicationContext())) {
241 return Service
.START_NOT_STICKY
;
244 String
[] localPaths
= null
, remotePaths
= null
, mimeTypes
= null
;
245 OCFile
[] files
= null
;
246 if (uploadType
== UPLOAD_SINGLE_FILE
) {
248 if (intent
.hasExtra(KEY_FILE
)) {
249 files
= new OCFile
[] { intent
.getParcelableExtra(KEY_FILE
) };
252 localPaths
= new String
[] { intent
.getStringExtra(KEY_LOCAL_FILE
) };
253 remotePaths
= new String
[] { intent
.getStringExtra(KEY_REMOTE_FILE
) };
254 mimeTypes
= new String
[] { intent
.getStringExtra(KEY_MIME_TYPE
) };
257 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
259 if (intent
.hasExtra(KEY_FILE
)) {
260 files
= (OCFile
[]) intent
.getParcelableArrayExtra(KEY_FILE
); // TODO
268 localPaths
= intent
.getStringArrayExtra(KEY_LOCAL_FILE
);
269 remotePaths
= intent
.getStringArrayExtra(KEY_REMOTE_FILE
);
270 mimeTypes
= intent
.getStringArrayExtra(KEY_MIME_TYPE
);
274 FileDataStorageManager storageManager
= new FileDataStorageManager(account
,
275 getContentResolver());
277 boolean forceOverwrite
= intent
.getBooleanExtra(KEY_FORCE_OVERWRITE
, false
);
278 boolean isInstant
= intent
.getBooleanExtra(KEY_INSTANT_UPLOAD
, false
);
279 int localAction
= intent
.getIntExtra(KEY_LOCAL_BEHAVIOUR
, LOCAL_BEHAVIOUR_COPY
);
281 if (intent
.hasExtra(KEY_FILE
) && files
== null
) {
282 Log_OC
.e(TAG
, "Incorrect array for OCFiles provided in upload intent");
283 return Service
.START_NOT_STICKY
;
285 } else if (!intent
.hasExtra(KEY_FILE
)) {
286 if (localPaths
== null
) {
287 Log_OC
.e(TAG
, "Incorrect array for local paths provided in upload intent");
288 return Service
.START_NOT_STICKY
;
290 if (remotePaths
== null
) {
291 Log_OC
.e(TAG
, "Incorrect array for remote paths provided in upload intent");
292 return Service
.START_NOT_STICKY
;
294 if (localPaths
.length
!= remotePaths
.length
) {
295 Log_OC
.e(TAG
, "Different number of remote paths and local paths!");
296 return Service
.START_NOT_STICKY
;
299 files
= new OCFile
[localPaths
.length
];
300 for (int i
= 0; i
< localPaths
.length
; i
++) {
301 files
[i
] = obtainNewOCFileToUpload(remotePaths
[i
], localPaths
[i
],
302 ((mimeTypes
!= null
) ? mimeTypes
[i
] : null
), storageManager
);
303 if (files
[i
] == null
) {
304 // TODO @andomaex add failure Notification
305 return Service
.START_NOT_STICKY
;
310 OwnCloudVersion ocv
= AccountUtils
.getServerVersion(account
);
312 boolean chunked
= FileUploader
.chunkedUploadIsSupported(ocv
);
313 AbstractList
<String
> requestedUploads
= new Vector
<String
>();
314 String uploadKey
= null
;
315 UploadFileOperation newUpload
= null
;
317 for (int i
= 0; i
< files
.length
; i
++) {
318 uploadKey
= buildRemoteName(account
, files
[i
].getRemotePath());
319 newUpload
= new UploadFileOperation(account
, files
[i
], chunked
, isInstant
,
320 forceOverwrite
, localAction
,
321 getApplicationContext());
323 newUpload
.setRemoteFolderToBeCreated();
325 // Grants that the file only upload once time
326 mPendingUploads
.putIfAbsent(uploadKey
, newUpload
);
328 newUpload
.addDatatransferProgressListener(this);
329 newUpload
.addDatatransferProgressListener((FileUploaderBinder
)mBinder
);
330 requestedUploads
.add(uploadKey
);
333 } catch (IllegalArgumentException e
) {
334 Log_OC
.e(TAG
, "Not enough information provided in intent: " + e
.getMessage());
335 return START_NOT_STICKY
;
337 } catch (IllegalStateException e
) {
338 Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage());
339 return START_NOT_STICKY
;
341 } catch (Exception e
) {
342 Log_OC
.e(TAG
, "Unexpected exception while processing upload intent", e
);
343 return START_NOT_STICKY
;
347 if (requestedUploads
.size() > 0) {
348 Message msg
= mServiceHandler
.obtainMessage();
350 msg
.obj
= requestedUploads
;
351 mServiceHandler
.sendMessage(msg
);
353 Log_OC
.i(TAG
, "mPendingUploads size:" + mPendingUploads
.size());
354 return Service
.START_NOT_STICKY
;
358 * Provides a binder object that clients can use to perform operations on
359 * the queue of uploads, excepting the addition of new files.
361 * Implemented to perform cancellation, pause and resume of existing
365 public IBinder
onBind(Intent arg0
) {
370 * Called when ALL the bound clients were onbound.
373 public boolean onUnbind(Intent intent
) {
374 ((FileUploaderBinder
)mBinder
).clearListeners();
375 return false
; // not accepting rebinding (default behaviour)
379 public void onAccountsUpdated(Account
[] accounts
) {
380 // Review current upload, and cancel it if its account doen't exist
381 if (mCurrentUpload
!= null
&&
382 !AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
383 mCurrentUpload
.cancel();
385 // The rest of uploads are cancelled when they try to start
389 * Binder to let client components to perform operations on the queue of
392 * It provides by itself the available operations.
394 public class FileUploaderBinder
extends Binder
implements OnDatatransferProgressListener
{
397 * Map of listeners that will be reported about progress of uploads from a
398 * {@link FileUploaderBinder} instance
400 private Map
<String
, OnDatatransferProgressListener
> mBoundListeners
=
401 new HashMap
<String
, OnDatatransferProgressListener
>();
404 * Cancels a pending or current upload of a remote file.
406 * @param account Owncloud account where the remote file will be stored.
407 * @param file A file in the queue of pending uploads
409 public void cancel(Account account
, OCFile file
) {
410 UploadFileOperation upload
;
411 synchronized (mPendingUploads
) {
412 upload
= mPendingUploads
.remove(buildRemoteName(account
, file
));
414 if (upload
!= null
) {
420 * Cancels a pending or current upload for an account
422 * @param account Owncloud accountName where the remote file will be stored.
424 public void cancel(Account account
) {
425 Log_OC
.d(TAG
, "Account= " + account
.name
);
427 if (mCurrentUpload
!= null
) {
428 Log_OC
.d(TAG
, "Current Upload Account= " + mCurrentUpload
.getAccount().name
);
429 if (mCurrentUpload
.getAccount().name
.equals(account
.name
)) {
430 mCurrentUpload
.cancel();
433 // Cancel pending uploads
434 cancelUploadForAccount(account
.name
);
437 public void clearListeners() {
438 mBoundListeners
.clear();
442 * Returns True when the file described by 'file' is being uploaded to
443 * the ownCloud account 'account' or waiting for it
445 * If 'file' is a directory, returns 'true' if some of its descendant files
446 * is uploading or waiting to upload.
448 * @param account ownCloud account where the remote file will be stored.
449 * @param file A file that could be in the queue of pending uploads
451 public boolean isUploading(Account account
, OCFile file
) {
452 if (account
== null
|| file
== null
)
454 String targetKey
= buildRemoteName(account
, file
);
455 synchronized (mPendingUploads
) {
456 if (file
.isFolder()) {
457 // this can be slow if there are many uploads :(
458 Iterator
<String
> it
= mPendingUploads
.keySet().iterator();
459 boolean found
= false
;
460 while (it
.hasNext() && !found
) {
461 found
= it
.next().startsWith(targetKey
);
465 return (mPendingUploads
.containsKey(targetKey
));
472 * Adds a listener interested in the progress of the upload for a concrete file.
474 * @param listener Object to notify about progress of transfer.
475 * @param account ownCloud account holding the file of interest.
476 * @param file {@link OCFile} of interest for listener.
478 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
,
479 Account account
, OCFile file
) {
480 if (account
== null
|| file
== null
|| listener
== null
) return;
481 String targetKey
= buildRemoteName(account
, file
);
482 mBoundListeners
.put(targetKey
, listener
);
488 * Removes a listener interested in the progress of the upload for a concrete file.
490 * @param listener Object to notify about progress of transfer.
491 * @param account ownCloud account holding the file of interest.
492 * @param file {@link OCFile} of interest for listener.
494 public void removeDatatransferProgressListener (OnDatatransferProgressListener listener
,
495 Account account
, OCFile file
) {
496 if (account
== null
|| file
== null
|| listener
== null
) return;
497 String targetKey
= buildRemoteName(account
, file
);
498 if (mBoundListeners
.get(targetKey
) == listener
) {
499 mBoundListeners
.remove(targetKey
);
505 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
506 long totalToTransfer
, String fileName
) {
507 String key
= buildRemoteName(mCurrentUpload
.getAccount(), mCurrentUpload
.getFile());
508 OnDatatransferProgressListener boundListener
= mBoundListeners
.get(key
);
509 if (boundListener
!= null
) {
510 boundListener
.onTransferProgress(progressRate
, totalTransferredSoFar
,
511 totalToTransfer
, fileName
);
516 * Review uploads and cancel it if its account doesn't exist
518 public void checkAccountOfCurrentUpload() {
519 if (mCurrentUpload
!= null
&&
520 !AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
521 mCurrentUpload
.cancel();
523 // The rest of uploads are cancelled when they try to start
528 * Upload worker. Performs the pending uploads in the order they were
531 * Created with the Looper of a new thread, started in
532 * {@link FileUploader#onCreate()}.
534 private static class ServiceHandler
extends Handler
{
535 // don't make it a final class, and don't remove the static ; lint will
536 // warn about a possible memory leak
537 FileUploader mService
;
539 public ServiceHandler(Looper looper
, FileUploader service
) {
542 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
547 public void handleMessage(Message msg
) {
548 @SuppressWarnings("unchecked")
549 AbstractList
<String
> requestedUploads
= (AbstractList
<String
>) msg
.obj
;
550 if (msg
.obj
!= null
) {
551 Iterator
<String
> it
= requestedUploads
.iterator();
552 while (it
.hasNext()) {
553 mService
.uploadFile(it
.next());
556 Log_OC
.d(TAG
, "Stopping command after id " + msg
.arg1
);
557 mService
.stopSelf(msg
.arg1
);
562 * Core upload method: sends the file(s) to upload
564 * @param uploadKey Key to access the upload to perform, contained in
567 public void uploadFile(String uploadKey
) {
569 synchronized (mPendingUploads
) {
570 mCurrentUpload
= mPendingUploads
.get(uploadKey
);
573 if (mCurrentUpload
!= null
) {
575 // Detect if the account exists
576 if (AccountUtils
.exists(mCurrentUpload
.getAccount(), getApplicationContext())) {
577 Log_OC
.d(TAG
, "Account " + mCurrentUpload
.getAccount().name
+ " exists");
579 notifyUploadStart(mCurrentUpload
);
581 RemoteOperationResult uploadResult
= null
, grantResult
;
584 /// prepare client object to send requests to the ownCloud server
585 if (mUploadClient
== null
||
586 !mLastAccount
.equals(mCurrentUpload
.getAccount())) {
587 mLastAccount
= mCurrentUpload
.getAccount();
589 new FileDataStorageManager(mLastAccount
, getContentResolver());
590 OwnCloudAccount ocAccount
= new OwnCloudAccount(mLastAccount
, this);
591 mUploadClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
592 getClientFor(ocAccount
, this);
595 /// check the existence of the parent folder for the file to upload
596 String remoteParentPath
= new File(mCurrentUpload
.getRemotePath()).getParent();
597 remoteParentPath
= remoteParentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ?
598 remoteParentPath
: remoteParentPath
+ OCFile
.PATH_SEPARATOR
;
599 grantResult
= grantFolderExistence(remoteParentPath
);
601 /// perform the upload
602 if (grantResult
.isSuccess()) {
603 OCFile parent
= mStorageManager
.getFileByPath(remoteParentPath
);
604 mCurrentUpload
.getFile().setParentId(parent
.getFileId());
605 uploadResult
= mCurrentUpload
.execute(mUploadClient
);
606 if (uploadResult
.isSuccess()) {
610 uploadResult
= grantResult
;
613 } catch (AccountsException e
) {
614 Log_OC
.e(TAG
, "Error while trying to get autorization for " +
615 mLastAccount
.name
, e
);
616 uploadResult
= new RemoteOperationResult(e
);
618 } catch (IOException e
) {
619 Log_OC
.e(TAG
, "Error while trying to get autorization for " +
620 mLastAccount
.name
, e
);
621 uploadResult
= new RemoteOperationResult(e
);
624 synchronized (mPendingUploads
) {
625 mPendingUploads
.remove(uploadKey
);
626 Log_OC
.i(TAG
, "Remove CurrentUploadItem from pending upload Item Map.");
628 if (uploadResult
!= null
&& uploadResult
.isException()) {
629 // enforce the creation of a new client object for next uploads;
630 // this grant that a new socket will be created in the future if
631 // the current exception is due to an abrupt lose of network connection
632 mUploadClient
= null
;
637 notifyUploadResult(uploadResult
, mCurrentUpload
);
638 sendFinalBroadcast(mCurrentUpload
, uploadResult
);
641 // Cancel the transfer
642 Log_OC
.d(TAG
, "Account " + mCurrentUpload
.getAccount().toString() +
644 cancelUploadForAccount(mCurrentUpload
.getAccount().name
);
652 * Checks the existence of the folder where the current file will be uploaded both
653 * in the remote server and in the local database.
655 * If the upload is set to enforce the creation of the folder, the method tries to
656 * create it both remote and locally.
658 * @param pathToGrant Full remote path whose existence will be granted.
659 * @return An {@link OCFile} instance corresponding to the folder where the file
662 private RemoteOperationResult
grantFolderExistence(String pathToGrant
) {
663 RemoteOperation operation
= new ExistenceCheckRemoteOperation(pathToGrant
, this, false
);
664 RemoteOperationResult result
= operation
.execute(mUploadClient
);
665 if (!result
.isSuccess() && result
.getCode() == ResultCode
.FILE_NOT_FOUND
&&
666 mCurrentUpload
.isRemoteFolderToBeCreated()) {
667 SyncOperation syncOp
= new CreateFolderOperation( pathToGrant
, true
);
668 result
= syncOp
.execute(mUploadClient
, mStorageManager
);
670 if (result
.isSuccess()) {
671 OCFile parentDir
= mStorageManager
.getFileByPath(pathToGrant
);
672 if (parentDir
== null
) {
673 parentDir
= createLocalFolder(pathToGrant
);
675 if (parentDir
!= null
) {
676 result
= new RemoteOperationResult(ResultCode
.OK
);
678 result
= new RemoteOperationResult(ResultCode
.UNKNOWN_ERROR
);
685 private OCFile
createLocalFolder(String remotePath
) {
686 String parentPath
= new File(remotePath
).getParent();
687 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ?
688 parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
689 OCFile parent
= mStorageManager
.getFileByPath(parentPath
);
690 if (parent
== null
) {
691 parent
= createLocalFolder(parentPath
);
693 if (parent
!= null
) {
694 OCFile createdFolder
= new OCFile(remotePath
);
695 createdFolder
.setMimetype("DIR");
696 createdFolder
.setParentId(parent
.getFileId());
697 mStorageManager
.saveFile(createdFolder
);
698 return createdFolder
;
705 * Saves a OC File after a successful upload.
707 * A PROPFIND is necessary to keep the props in the local database
708 * synchronized with the server, specially the modification time and Etag
711 * TODO refactor this ugly thing
713 private void saveUploadedFile() {
714 OCFile file
= mCurrentUpload
.getFile();
715 if (file
.fileExists()) {
716 file
= mStorageManager
.getFileById(file
.getFileId());
718 long syncDate
= System
.currentTimeMillis();
719 file
.setLastSyncDateForData(syncDate
);
721 // new PROPFIND to keep data consistent with server
722 // in theory, should return the same we already have
723 ReadRemoteFileOperation operation
=
724 new ReadRemoteFileOperation(mCurrentUpload
.getRemotePath());
725 RemoteOperationResult result
= operation
.execute(mUploadClient
);
726 if (result
.isSuccess()) {
727 updateOCFile(file
, (RemoteFile
) result
.getData().get(0));
728 file
.setLastSyncDateForProperties(syncDate
);
731 // / maybe this would be better as part of UploadFileOperation... or
732 // maybe all this method
733 if (mCurrentUpload
.wasRenamed()) {
734 OCFile oldFile
= mCurrentUpload
.getOldFile();
735 if (oldFile
.fileExists()) {
736 oldFile
.setStoragePath(null
);
737 mStorageManager
.saveFile(oldFile
);
739 } // else: it was just an automatic renaming due to a name
740 // coincidence; nothing else is needed, the storagePath is right
741 // in the instance returned by mCurrentUpload.getFile()
743 file
.setNeedsUpdateThumbnail(true
);
744 mStorageManager
.saveFile(file
);
745 mStorageManager
.triggerMediaScan(file
.getStoragePath());
748 private void updateOCFile(OCFile file
, RemoteFile remoteFile
) {
749 file
.setCreationTimestamp(remoteFile
.getCreationTimestamp());
750 file
.setFileLength(remoteFile
.getLength());
751 file
.setMimetype(remoteFile
.getMimeType());
752 file
.setModificationTimestamp(remoteFile
.getModifiedTimestamp());
753 file
.setModificationTimestampAtLastSyncForData(remoteFile
.getModifiedTimestamp());
754 // file.setEtag(remoteFile.getEtag()); // TODO Etag, where available
755 file
.setRemoteId(remoteFile
.getRemoteId());
758 private OCFile
obtainNewOCFileToUpload(String remotePath
, String localPath
, String mimeType
,
759 FileDataStorageManager storageManager
) {
762 if (mimeType
== null
|| mimeType
.length() <= 0) {
764 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(
765 remotePath
.substring(remotePath
.lastIndexOf('.') + 1));
766 } catch (IndexOutOfBoundsException e
) {
767 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " +
771 if (mimeType
== null
) {
772 mimeType
= "application/octet-stream";
775 if (isPdfFileFromContentProviderWithoutExtension(localPath
, mimeType
)){
776 remotePath
+= FILE_EXTENSION_PDF
;
779 OCFile newFile
= new OCFile(remotePath
);
780 newFile
.setStoragePath(localPath
);
781 newFile
.setLastSyncDateForProperties(0);
782 newFile
.setLastSyncDateForData(0);
785 if (localPath
!= null
&& localPath
.length() > 0) {
786 File localFile
= new File(localPath
);
787 newFile
.setFileLength(localFile
.length());
788 newFile
.setLastSyncDateForData(localFile
.lastModified());
789 } // don't worry about not assigning size, the problems with localPath
790 // are checked when the UploadFileOperation instance is created
793 newFile
.setMimetype(mimeType
);
799 * Creates a status notification to show the upload progress
801 * @param upload Upload operation starting.
803 private void notifyUploadStart(UploadFileOperation upload
) {
804 // / create status notification with a progress bar
806 mNotificationBuilder
=
807 NotificationBuilderWithProgressBar
.newNotificationBuilderWithProgressBar(this);
810 .setSmallIcon(R
.drawable
.notification_icon
)
811 .setTicker(getString(R
.string
.uploader_upload_in_progress_ticker
))
812 .setContentTitle(getString(R
.string
.uploader_upload_in_progress_ticker
))
813 .setProgress(100, 0, false
)
815 String
.format(getString(R
.string
.uploader_upload_in_progress_content
), 0, upload
.getFileName()));
817 /// includes a pending intent in the notification showing the details view of the file
818 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
819 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, upload
.getFile());
820 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, upload
.getAccount());
821 showDetailsIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
822 mNotificationBuilder
.setContentIntent(PendingIntent
.getActivity(
823 this, (int) System
.currentTimeMillis(), showDetailsIntent
, 0
826 mNotificationManager
.notify(R
.string
.uploader_upload_in_progress_ticker
, mNotificationBuilder
.build());
830 * Callback method to update the progress bar in the status notification
833 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
834 long totalToTransfer
, String filePath
) {
835 int percent
= (int) (100.0 * ((double) totalTransferredSoFar
) / ((double) totalToTransfer
));
836 if (percent
!= mLastPercent
) {
837 mNotificationBuilder
.setProgress(100, percent
, false
);
838 String fileName
= filePath
.substring(
839 filePath
.lastIndexOf(FileUtils
.PATH_SEPARATOR
) + 1);
840 String text
= String
.format(getString(R
.string
.uploader_upload_in_progress_content
), percent
, fileName
);
841 mNotificationBuilder
.setContentText(text
);
842 mNotificationManager
.notify(R
.string
.uploader_upload_in_progress_ticker
, mNotificationBuilder
.build());
844 mLastPercent
= percent
;
848 * Updates the status notification with the result of an upload operation.
850 * @param uploadResult Result of the upload operation.
851 * @param upload Finished upload operation
853 private void notifyUploadResult(
854 RemoteOperationResult uploadResult
, UploadFileOperation upload
) {
855 Log_OC
.d(TAG
, "NotifyUploadResult with resultCode: " + uploadResult
.getCode());
856 // / cancelled operation or success -> silent removal of progress notification
857 mNotificationManager
.cancel(R
.string
.uploader_upload_in_progress_ticker
);
859 // Show the result: success or fail notification
860 if (!uploadResult
.isCancelled()) {
861 int tickerId
= (uploadResult
.isSuccess()) ? R
.string
.uploader_upload_succeeded_ticker
:
862 R
.string
.uploader_upload_failed_ticker
;
866 // check credentials error
867 boolean needsToUpdateCredentials
= (
868 uploadResult
.getCode() == ResultCode
.UNAUTHORIZED
||
869 uploadResult
.isIdPRedirection()
871 tickerId
= (needsToUpdateCredentials
) ?
872 R
.string
.uploader_upload_failed_credentials_error
: tickerId
;
875 .setTicker(getString(tickerId
))
876 .setContentTitle(getString(tickerId
))
879 .setProgress(0, 0, false
);
881 content
= ErrorMessageAdapter
.getErrorCauseMessage(
882 uploadResult
, upload
, getResources()
885 if (needsToUpdateCredentials
) {
886 // let the user update credentials with one click
887 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
888 updateAccountCredentials
.putExtra(
889 AuthenticatorActivity
.EXTRA_ACCOUNT
, upload
.getAccount()
891 updateAccountCredentials
.putExtra(
892 AuthenticatorActivity
.EXTRA_ACTION
,
893 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
895 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
896 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
897 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
898 mNotificationBuilder
.setContentIntent(PendingIntent
.getActivity(
900 (int) System
.currentTimeMillis(),
901 updateAccountCredentials
,
902 PendingIntent
.FLAG_ONE_SHOT
905 mUploadClient
= null
;
906 // grant that future retries on the same account will get the fresh credentials
908 mNotificationBuilder
.setContentText(content
);
910 if (upload
.isInstant()) {
913 db
= new DbHandler(this.getBaseContext());
914 String message
= uploadResult
.getLogMessage() + " errorCode: " +
915 uploadResult
.getCode();
916 Log_OC
.e(TAG
, message
+ " Http-Code: " + uploadResult
.getHttpCode());
917 if (uploadResult
.getCode() == ResultCode
.QUOTA_EXCEEDED
) {
918 //message = getString(R.string.failed_upload_quota_exceeded_text);
919 if (db
.updateFileState(
920 upload
.getOriginalStoragePath(),
921 DbHandler
.UPLOAD_STATUS_UPLOAD_FAILED
,
924 upload
.getOriginalStoragePath(),
925 upload
.getAccount().name
,
938 mNotificationBuilder
.setContentText(content
);
939 mNotificationManager
.notify(tickerId
, mNotificationBuilder
.build());
941 if (uploadResult
.isSuccess()) {
943 DbHandler db
= new DbHandler(this.getBaseContext());
944 db
.removeIUPendingFile(mCurrentUpload
.getOriginalStoragePath());
947 // remove success notification, with a delay of 2 seconds
948 NotificationDelayer
.cancelWithDelay(
949 mNotificationManager
,
950 R
.string
.uploader_upload_succeeded_ticker
,
958 * Sends a broadcast in order to the interested activities can update their
961 * @param upload Finished upload operation
962 * @param uploadResult Result of the upload operation
964 private void sendFinalBroadcast(UploadFileOperation upload
, RemoteOperationResult uploadResult
) {
965 Intent end
= new Intent(getUploadFinishMessage());
966 end
.putExtra(EXTRA_REMOTE_PATH
, upload
.getRemotePath()); // real remote
971 if (upload
.wasRenamed()) {
972 end
.putExtra(EXTRA_OLD_REMOTE_PATH
, upload
.getOldFile().getRemotePath());
974 end
.putExtra(EXTRA_OLD_FILE_PATH
, upload
.getOriginalStoragePath());
975 end
.putExtra(ACCOUNT_NAME
, upload
.getAccount().name
);
976 end
.putExtra(EXTRA_UPLOAD_RESULT
, uploadResult
.isSuccess());
977 sendStickyBroadcast(end
);
981 * Checks if content provider, using the content:// scheme, returns a file with mime-type
982 * 'application/pdf' but file has not extension
983 * @param localPath Full path to a file in the local file system.
984 * @param mimeType MIME type of the file.
985 * @return true if is needed to add the pdf file extension to the file
987 private boolean isPdfFileFromContentProviderWithoutExtension(String localPath
,
989 return localPath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
) &&
990 mimeType
.equals(MIME_TYPE_PDF
) &&
991 !localPath
.endsWith(FILE_EXTENSION_PDF
);
995 * Remove uploads of an account
996 * @param accountName Name of an OC account
998 private void cancelUploadForAccount(String accountName
){
999 // this can be slow if there are many uploads :(
1000 Iterator
<String
> it
= mPendingUploads
.keySet().iterator();
1001 Log_OC
.d(TAG
, "Number of pending updloads= " + mPendingUploads
.size());
1002 while (it
.hasNext()) {
1003 String key
= it
.next();
1004 Log_OC
.d(TAG
, "mPendingUploads CANCELLED " + key
);
1005 if (key
.startsWith(accountName
)) {
1006 synchronized (mPendingUploads
) {
1007 mPendingUploads
.remove(key
);