wip
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2012-2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.files.services;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.AbstractList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Vector;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32
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;
50
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;
79
80
81 public class FileUploader extends Service
82 implements OnDatatransferProgressListener, OnAccountsUpdateListener {
83
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";
90
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";
95
96 public static final String KEY_ACCOUNT = "ACCOUNT";
97
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";
102
103 public static final String KEY_CANCEL_ALL = "CANCEL_ALL";
104
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;
108
109 public static final int UPLOAD_SINGLE_FILE = 0;
110 public static final int UPLOAD_MULTIPLE_FILES = 1;
111
112 private static final String TAG = FileUploader.class.getSimpleName();
113
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;
120
121 private ConcurrentMap<String, UploadFileOperation> mPendingUploads =
122 new ConcurrentHashMap<String, UploadFileOperation>();
123 private UploadFileOperation mCurrentUpload = null;
124
125 private NotificationManager mNotificationManager;
126 private NotificationCompat.Builder mNotificationBuilder;
127 private int mLastPercent;
128
129 private static final String MIME_TYPE_PDF = "application/pdf";
130 private static final String FILE_EXTENSION_PDF = ".pdf";
131
132
133 public static String getUploadFinishMessage() {
134 return FileUploader.class.getName() + UPLOAD_FINISH_MESSAGE;
135 }
136
137 /**
138 * Builds a key for mPendingUploads from the account and file to upload
139 *
140 * @param account Account where the file to upload is stored
141 * @param file File to upload
142 */
143 private String buildRemoteName(Account account, OCFile file) {
144 return account.name + file.getRemotePath();
145 }
146
147 private String buildRemoteName(Account account, String remotePath) {
148 return account.name + remotePath;
149 }
150
151 /**
152 * Checks if an ownCloud server version should support chunked uploads.
153 *
154 * @param version OwnCloud version instance corresponding to an ownCloud
155 * server.
156 * @return 'True' if the ownCloud server with version supports chunked
157 * uploads.
158 */
159 private static boolean chunkedUploadIsSupported(OwnCloudVersion version) {
160 return (version != null && version.compareTo(OwnCloudVersion.owncloud_v4_5) >= 0);
161 }
162
163 /**
164 * Service initialization
165 */
166 @Override
167 public void onCreate() {
168 super.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);
173 thread.start();
174 mServiceLooper = thread.getLooper();
175 mServiceHandler = new ServiceHandler(mServiceLooper, this);
176 mBinder = new FileUploaderBinder();
177
178 // add AccountsUpdatedListener
179 AccountManager am = AccountManager.get(getApplicationContext());
180 am.addOnAccountsUpdatedListener(this, null, false);
181 }
182
183 /**
184 * Service clean up
185 */
186 @Override
187 public void onDestroy() {
188 Log_OC.v(TAG, "Destroying service" );
189 mBinder = null;
190 mServiceHandler = null;
191 mServiceLooper.quit();
192 mServiceLooper = null;
193 mNotificationManager = null;
194
195 // remove AccountsUpdatedListener
196 AccountManager am = AccountManager.get(getApplicationContext());
197 am.removeOnAccountsUpdatedListener(this);
198
199 super.onDestroy();
200 }
201
202
203 /**
204 * Entry point to add one or several files to the queue of uploads.
205 *
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.
209 */
210 @Override
211 public int onStartCommand(Intent intent, int flags, int startId) {
212 Log_OC.d(TAG, "Starting command with id " + startId);
213
214 if (intent.hasExtra(KEY_CANCEL_ALL) && intent.hasExtra(KEY_ACCOUNT)){
215 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
216
217 Log_OC.d(TAG, "Account= " + account.name);
218
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();
223 }
224 }
225 // Cancel pending uploads
226 cancelUploadForAccount(account.name);
227 }
228
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;
233 }
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;
238 }
239 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
240 if (!AccountUtils.exists(account, getApplicationContext())) {
241 return Service.START_NOT_STICKY;
242 }
243
244 String[] localPaths = null, remotePaths = null, mimeTypes = null;
245 OCFile[] files = null;
246 if (uploadType == UPLOAD_SINGLE_FILE) {
247
248 if (intent.hasExtra(KEY_FILE)) {
249 files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };
250
251 } else {
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) };
255 }
256
257 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
258
259 if (intent.hasExtra(KEY_FILE)) {
260 files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
261 // will
262 // this
263 // casting
264 // work
265 // fine?
266
267 } else {
268 localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
269 remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
270 mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
271 }
272 }
273
274 FileDataStorageManager storageManager = new FileDataStorageManager(account,
275 getContentResolver());
276
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);
280
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;
284
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;
289 }
290 if (remotePaths == null) {
291 Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
292 return Service.START_NOT_STICKY;
293 }
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;
297 }
298
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;
306 }
307 }
308 }
309
310 OwnCloudVersion ocv = AccountUtils.getServerVersion(account);
311
312 boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
313 AbstractList<String> requestedUploads = new Vector<String>();
314 String uploadKey = null;
315 UploadFileOperation newUpload = null;
316 try {
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());
322 if (isInstant) {
323 newUpload.setRemoteFolderToBeCreated();
324 }
325 // Grants that the file only upload once time
326 mPendingUploads.putIfAbsent(uploadKey, newUpload);
327
328 newUpload.addDatatransferProgressListener(this);
329 newUpload.addDatatransferProgressListener((FileUploaderBinder)mBinder);
330 requestedUploads.add(uploadKey);
331 }
332
333 } catch (IllegalArgumentException e) {
334 Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
335 return START_NOT_STICKY;
336
337 } catch (IllegalStateException e) {
338 Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
339 return START_NOT_STICKY;
340
341 } catch (Exception e) {
342 Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
343 return START_NOT_STICKY;
344
345 }
346
347 if (requestedUploads.size() > 0) {
348 Message msg = mServiceHandler.obtainMessage();
349 msg.arg1 = startId;
350 msg.obj = requestedUploads;
351 mServiceHandler.sendMessage(msg);
352 }
353 Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
354 return Service.START_NOT_STICKY;
355 }
356
357 /**
358 * Provides a binder object that clients can use to perform operations on
359 * the queue of uploads, excepting the addition of new files.
360 *
361 * Implemented to perform cancellation, pause and resume of existing
362 * uploads.
363 */
364 @Override
365 public IBinder onBind(Intent arg0) {
366 return mBinder;
367 }
368
369 /**
370 * Called when ALL the bound clients were onbound.
371 */
372 @Override
373 public boolean onUnbind(Intent intent) {
374 ((FileUploaderBinder)mBinder).clearListeners();
375 return false; // not accepting rebinding (default behaviour)
376 }
377
378 @Override
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();
384 }
385 // The rest of uploads are cancelled when they try to start
386 }
387
388 /**
389 * Binder to let client components to perform operations on the queue of
390 * uploads.
391 *
392 * It provides by itself the available operations.
393 */
394 public class FileUploaderBinder extends Binder implements OnDatatransferProgressListener {
395
396 /**
397 * Map of listeners that will be reported about progress of uploads from a
398 * {@link FileUploaderBinder} instance
399 */
400 private Map<String, OnDatatransferProgressListener> mBoundListeners =
401 new HashMap<String, OnDatatransferProgressListener>();
402
403 /**
404 * Cancels a pending or current upload of a remote file.
405 *
406 * @param account Owncloud account where the remote file will be stored.
407 * @param file A file in the queue of pending uploads
408 */
409 public void cancel(Account account, OCFile file) {
410 UploadFileOperation upload;
411 synchronized (mPendingUploads) {
412 upload = mPendingUploads.remove(buildRemoteName(account, file));
413 }
414 if (upload != null) {
415 upload.cancel();
416 }
417 }
418
419 /**
420 * Cancels a pending or current upload for an account
421 *
422 * @param account Owncloud accountName where the remote file will be stored.
423 */
424 public void cancel(Account account) {
425 Log_OC.d(TAG, "Account= " + account.name);
426
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();
431 }
432 }
433 // Cancel pending uploads
434 cancelUploadForAccount(account.name);
435 }
436
437 public void clearListeners() {
438 mBoundListeners.clear();
439 }
440
441 /**
442 * Returns True when the file described by 'file' is being uploaded to
443 * the ownCloud account 'account' or waiting for it
444 *
445 * If 'file' is a directory, returns 'true' if some of its descendant files
446 * is uploading or waiting to upload.
447 *
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
450 */
451 public boolean isUploading(Account account, OCFile file) {
452 if (account == null || file == null)
453 return false;
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);
462 }
463 return found;
464 } else {
465 return (mPendingUploads.containsKey(targetKey));
466 }
467 }
468 }
469
470
471 /**
472 * Adds a listener interested in the progress of the upload for a concrete file.
473 *
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.
477 */
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);
483 }
484
485
486
487 /**
488 * Removes a listener interested in the progress of the upload for a concrete file.
489 *
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.
493 */
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);
500 }
501 }
502
503
504 @Override
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);
512 }
513 }
514
515 /**
516 * Review uploads and cancel it if its account doesn't exist
517 */
518 public void checkAccountOfCurrentUpload() {
519 if (mCurrentUpload != null &&
520 !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
521 mCurrentUpload.cancel();
522 }
523 // The rest of uploads are cancelled when they try to start
524 }
525 }
526
527 /**
528 * Upload worker. Performs the pending uploads in the order they were
529 * requested.
530 *
531 * Created with the Looper of a new thread, started in
532 * {@link FileUploader#onCreate()}.
533 */
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;
538
539 public ServiceHandler(Looper looper, FileUploader service) {
540 super(looper);
541 if (service == null)
542 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
543 mService = service;
544 }
545
546 @Override
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());
554 }
555 }
556 Log_OC.d(TAG, "Stopping command after id " + msg.arg1);
557 mService.stopSelf(msg.arg1);
558 }
559 }
560
561 /**
562 * Core upload method: sends the file(s) to upload
563 *
564 * @param uploadKey Key to access the upload to perform, contained in
565 * mPendingUploads
566 */
567 public void uploadFile(String uploadKey) {
568
569 synchronized (mPendingUploads) {
570 mCurrentUpload = mPendingUploads.get(uploadKey);
571 }
572
573 if (mCurrentUpload != null) {
574
575 // Detect if the account exists
576 if (AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
577 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().name + " exists");
578
579 notifyUploadStart(mCurrentUpload);
580
581 RemoteOperationResult uploadResult = null, grantResult;
582
583 try {
584 /// prepare client object to send requests to the ownCloud server
585 if (mUploadClient == null ||
586 !mLastAccount.equals(mCurrentUpload.getAccount())) {
587 mLastAccount = mCurrentUpload.getAccount();
588 mStorageManager =
589 new FileDataStorageManager(mLastAccount, getContentResolver());
590 OwnCloudAccount ocAccount = new OwnCloudAccount(mLastAccount, this);
591 mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
592 getClientFor(ocAccount, this);
593 }
594
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);
600
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()) {
607 saveUploadedFile();
608 }
609 } else {
610 uploadResult = grantResult;
611 }
612
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);
617
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);
622
623 } finally {
624 synchronized (mPendingUploads) {
625 mPendingUploads.remove(uploadKey);
626 Log_OC.i(TAG, "Remove CurrentUploadItem from pending upload Item Map.");
627 }
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;
633 }
634 }
635
636 /// notify result
637 notifyUploadResult(uploadResult, mCurrentUpload);
638 sendFinalBroadcast(mCurrentUpload, uploadResult);
639
640 } else {
641 // Cancel the transfer
642 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() +
643 " doesn't exist");
644 cancelUploadForAccount(mCurrentUpload.getAccount().name);
645
646 }
647 }
648
649 }
650
651 /**
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.
654 *
655 * If the upload is set to enforce the creation of the folder, the method tries to
656 * create it both remote and locally.
657 *
658 * @param pathToGrant Full remote path whose existence will be granted.
659 * @return An {@link OCFile} instance corresponding to the folder where the file
660 * will be uploaded.
661 */
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);
669 }
670 if (result.isSuccess()) {
671 OCFile parentDir = mStorageManager.getFileByPath(pathToGrant);
672 if (parentDir == null) {
673 parentDir = createLocalFolder(pathToGrant);
674 }
675 if (parentDir != null) {
676 result = new RemoteOperationResult(ResultCode.OK);
677 } else {
678 result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
679 }
680 }
681 return result;
682 }
683
684
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);
692 }
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;
699 }
700 return null;
701 }
702
703
704 /**
705 * Saves a OC File after a successful upload.
706 *
707 * A PROPFIND is necessary to keep the props in the local database
708 * synchronized with the server, specially the modification time and Etag
709 * (where available)
710 *
711 * TODO refactor this ugly thing
712 */
713 private void saveUploadedFile() {
714 OCFile file = mCurrentUpload.getFile();
715 if (file.fileExists()) {
716 file = mStorageManager.getFileById(file.getFileId());
717 }
718 long syncDate = System.currentTimeMillis();
719 file.setLastSyncDateForData(syncDate);
720
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);
729 }
730
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);
738
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()
742 }
743 file.setNeedsUpdateThumbnail(true);
744 mStorageManager.saveFile(file);
745 mStorageManager.triggerMediaScan(file.getStoragePath());
746 }
747
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());
756 }
757
758 private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType,
759 FileDataStorageManager storageManager) {
760
761 // MIME type
762 if (mimeType == null || mimeType.length() <= 0) {
763 try {
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: " +
768 remotePath);
769 }
770 }
771 if (mimeType == null) {
772 mimeType = "application/octet-stream";
773 }
774
775 if (isPdfFileFromContentProviderWithoutExtension(localPath, mimeType)){
776 remotePath += FILE_EXTENSION_PDF;
777 }
778
779 OCFile newFile = new OCFile(remotePath);
780 newFile.setStoragePath(localPath);
781 newFile.setLastSyncDateForProperties(0);
782 newFile.setLastSyncDateForData(0);
783
784 // size
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
791
792
793 newFile.setMimetype(mimeType);
794
795 return newFile;
796 }
797
798 /**
799 * Creates a status notification to show the upload progress
800 *
801 * @param upload Upload operation starting.
802 */
803 private void notifyUploadStart(UploadFileOperation upload) {
804 // / create status notification with a progress bar
805 mLastPercent = 0;
806 mNotificationBuilder =
807 NotificationBuilderWithProgressBar.newNotificationBuilderWithProgressBar(this);
808 mNotificationBuilder
809 .setOngoing(true)
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)
814 .setContentText(
815 String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName()));
816
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
824 ));
825
826 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build());
827 }
828
829 /**
830 * Callback method to update the progress bar in the status notification
831 */
832 @Override
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());
843 }
844 mLastPercent = percent;
845 }
846
847 /**
848 * Updates the status notification with the result of an upload operation.
849 *
850 * @param uploadResult Result of the upload operation.
851 * @param upload Finished upload operation
852 */
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);
858
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;
863
864 String content;
865
866 // check credentials error
867 boolean needsToUpdateCredentials = (
868 uploadResult.getCode() == ResultCode.UNAUTHORIZED ||
869 uploadResult.isIdPRedirection()
870 );
871 tickerId = (needsToUpdateCredentials) ?
872 R.string.uploader_upload_failed_credentials_error : tickerId;
873
874 mNotificationBuilder
875 .setTicker(getString(tickerId))
876 .setContentTitle(getString(tickerId))
877 .setAutoCancel(true)
878 .setOngoing(false)
879 .setProgress(0, 0, false);
880
881 content = ErrorMessageAdapter.getErrorCauseMessage(
882 uploadResult, upload, getResources()
883 );
884
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()
890 );
891 updateAccountCredentials.putExtra(
892 AuthenticatorActivity.EXTRA_ACTION,
893 AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
894 );
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(
899 this,
900 (int) System.currentTimeMillis(),
901 updateAccountCredentials,
902 PendingIntent.FLAG_ONE_SHOT
903 ));
904
905 mUploadClient = null;
906 // grant that future retries on the same account will get the fresh credentials
907 } else {
908 mNotificationBuilder.setContentText(content);
909
910 if (upload.isInstant()) {
911 DbHandler db = null;
912 try {
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,
922 message) == 0) {
923 db.putFileForLater(
924 upload.getOriginalStoragePath(),
925 upload.getAccount().name,
926 message
927 );
928 }
929 }
930 } finally {
931 if (db != null) {
932 db.close();
933 }
934 }
935 }
936 }
937
938 mNotificationBuilder.setContentText(content);
939 mNotificationManager.notify(tickerId, mNotificationBuilder.build());
940
941 if (uploadResult.isSuccess()) {
942
943 DbHandler db = new DbHandler(this.getBaseContext());
944 db.removeIUPendingFile(mCurrentUpload.getOriginalStoragePath());
945 db.close();
946
947 // remove success notification, with a delay of 2 seconds
948 NotificationDelayer.cancelWithDelay(
949 mNotificationManager,
950 R.string.uploader_upload_succeeded_ticker,
951 2000);
952
953 }
954 }
955 }
956
957 /**
958 * Sends a broadcast in order to the interested activities can update their
959 * view
960 *
961 * @param upload Finished upload operation
962 * @param uploadResult Result of the upload operation
963 */
964 private void sendFinalBroadcast(UploadFileOperation upload, RemoteOperationResult uploadResult) {
965 Intent end = new Intent(getUploadFinishMessage());
966 end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath()); // real remote
967 // path, after
968 // possible
969 // automatic
970 // renaming
971 if (upload.wasRenamed()) {
972 end.putExtra(EXTRA_OLD_REMOTE_PATH, upload.getOldFile().getRemotePath());
973 }
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);
978 }
979
980 /**
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
986 */
987 private boolean isPdfFileFromContentProviderWithoutExtension(String localPath,
988 String mimeType) {
989 return localPath.startsWith(UriUtils.URI_CONTENT_SCHEME) &&
990 mimeType.equals(MIME_TYPE_PDF) &&
991 !localPath.endsWith(FILE_EXTENSION_PDF);
992 }
993
994 /**
995 * Remove uploads of an account
996 * @param accountName Name of an OC account
997 */
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);
1008 }
1009 }
1010 }
1011 }
1012 }