Rewritten queue of FileUploader based on IndexedForest
[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
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;
49
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;
78
79
80 public class FileUploader extends Service
81 implements OnDatatransferProgressListener, OnAccountsUpdateListener {
82
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";
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 int LOCAL_BEHAVIOUR_COPY = 0;
104 public static final int LOCAL_BEHAVIOUR_MOVE = 1;
105 public static final int LOCAL_BEHAVIOUR_FORGET = 2;
106
107 public static final int UPLOAD_SINGLE_FILE = 0;
108 public static final int UPLOAD_MULTIPLE_FILES = 1;
109
110 private static final String TAG = FileUploader.class.getSimpleName();
111
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;
118
119 private IndexedForest<UploadFileOperation> mPendingUploads = new IndexedForest<UploadFileOperation>();
120 private UploadFileOperation mCurrentUpload = null;
121
122 private NotificationManager mNotificationManager;
123 private NotificationCompat.Builder mNotificationBuilder;
124 private int mLastPercent;
125
126 private static final String MIME_TYPE_PDF = "application/pdf";
127 private static final String FILE_EXTENSION_PDF = ".pdf";
128
129
130 public static String getUploadFinishMessage() {
131 return FileUploader.class.getName() + UPLOAD_FINISH_MESSAGE;
132 }
133
134 /**
135 * Builds a key for mPendingUploads from the account and file to upload
136 *
137 * @param account Account where the file to upload is stored
138 * @param file File to upload
139 */
140 private String buildRemoteName(Account account, OCFile file) {
141 return account.name + file.getRemotePath();
142 }
143
144 private String buildRemoteName(Account account, String remotePath) {
145 return account.name + remotePath;
146 }
147
148 /**
149 * Checks if an ownCloud server version should support chunked uploads.
150 *
151 * @param version OwnCloud version instance corresponding to an ownCloud
152 * server.
153 * @return 'True' if the ownCloud server with version supports chunked
154 * uploads.
155 */
156 private static boolean chunkedUploadIsSupported(OwnCloudVersion version) {
157 return (version != null && version.compareTo(OwnCloudVersion.owncloud_v4_5) >= 0);
158 }
159
160 /**
161 * Service initialization
162 */
163 @Override
164 public void onCreate() {
165 super.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);
170 thread.start();
171 mServiceLooper = thread.getLooper();
172 mServiceHandler = new ServiceHandler(mServiceLooper, this);
173 mBinder = new FileUploaderBinder();
174
175 // add AccountsUpdatedListener
176 AccountManager am = AccountManager.get(getApplicationContext());
177 am.addOnAccountsUpdatedListener(this, null, false);
178 }
179
180 /**
181 * Service clean up
182 */
183 @Override
184 public void onDestroy() {
185 Log_OC.v(TAG, "Destroying service" );
186 mBinder = null;
187 mServiceHandler = null;
188 mServiceLooper.quit();
189 mServiceLooper = null;
190 mNotificationManager = null;
191
192 // remove AccountsUpdatedListener
193 AccountManager am = AccountManager.get(getApplicationContext());
194 am.removeOnAccountsUpdatedListener(this);
195
196 super.onDestroy();
197 }
198
199
200 /**
201 * Entry point to add one or several files to the queue of uploads.
202 *
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.
206 */
207 @Override
208 public int onStartCommand(Intent intent, int flags, int startId) {
209 Log_OC.d(TAG, "Starting command with id " + startId);
210
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;
215 }
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;
220 }
221 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
222 if (!AccountUtils.exists(account, getApplicationContext())) {
223 return Service.START_NOT_STICKY;
224 }
225
226 String[] localPaths = null, remotePaths = null, mimeTypes = null;
227 OCFile[] files = null;
228 if (uploadType == UPLOAD_SINGLE_FILE) {
229
230 if (intent.hasExtra(KEY_FILE)) {
231 files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };
232
233 } else {
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) };
237 }
238
239 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
240
241 if (intent.hasExtra(KEY_FILE)) {
242 files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
243 // will
244 // this
245 // casting
246 // work
247 // fine?
248
249 } else {
250 localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
251 remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
252 mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
253 }
254 }
255
256 FileDataStorageManager storageManager = new FileDataStorageManager(account,
257 getContentResolver());
258
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);
262
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;
266
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;
271 }
272 if (remotePaths == null) {
273 Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
274 return Service.START_NOT_STICKY;
275 }
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;
279 }
280
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;
288 }
289 }
290 }
291
292 OwnCloudVersion ocv = AccountUtils.getServerVersion(account);
293
294 boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
295 AbstractList<String> requestedUploads = new Vector<String>();
296 String uploadKey = null;
297 UploadFileOperation newUpload = null;
298 try {
299 for (int i = 0; i < files.length; i++) {
300 newUpload = new UploadFileOperation(
301 account,
302 files[i],
303 chunked,
304 isInstant,
305 forceOverwrite, localAction,
306 getApplicationContext()
307 );
308 if (isInstant) {
309 newUpload.setRemoteFolderToBeCreated();
310 }
311 newUpload.addDatatransferProgressListener(this);
312 newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
313 Pair<String, String> putResult = mPendingUploads.putIfAbsent(
314 account, files[i].getRemotePath(), newUpload
315 );
316 uploadKey = putResult.first;
317 requestedUploads.add(uploadKey);
318 }
319
320 } catch (IllegalArgumentException e) {
321 Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
322 return START_NOT_STICKY;
323
324 } catch (IllegalStateException e) {
325 Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
326 return START_NOT_STICKY;
327
328 } catch (Exception e) {
329 Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
330 return START_NOT_STICKY;
331
332 }
333
334 if (requestedUploads.size() > 0) {
335 Message msg = mServiceHandler.obtainMessage();
336 msg.arg1 = startId;
337 msg.obj = requestedUploads;
338 mServiceHandler.sendMessage(msg);
339 }
340 return Service.START_NOT_STICKY;
341 }
342
343 /**
344 * Provides a binder object that clients can use to perform operations on
345 * the queue of uploads, excepting the addition of new files.
346 *
347 * Implemented to perform cancellation, pause and resume of existing
348 * uploads.
349 */
350 @Override
351 public IBinder onBind(Intent arg0) {
352 return mBinder;
353 }
354
355 /**
356 * Called when ALL the bound clients were onbound.
357 */
358 @Override
359 public boolean onUnbind(Intent intent) {
360 ((FileUploaderBinder)mBinder).clearListeners();
361 return false; // not accepting rebinding (default behaviour)
362 }
363
364 @Override
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();
370 }
371 // The rest of uploads are cancelled when they try to start
372 }
373
374 /**
375 * Binder to let client components to perform operations on the queue of
376 * uploads.
377 *
378 * It provides by itself the available operations.
379 */
380 public class FileUploaderBinder extends Binder implements OnDatatransferProgressListener {
381
382 /**
383 * Map of listeners that will be reported about progress of uploads from a
384 * {@link FileUploaderBinder} instance
385 */
386 private Map<String, OnDatatransferProgressListener> mBoundListeners =
387 new HashMap<String, OnDatatransferProgressListener>();
388
389 /**
390 * Cancels a pending or current upload of a remote file.
391 *
392 * @param account Owncloud account where the remote file will be stored.
393 * @param file A file in the queue of pending uploads
394 */
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;
400 //}
401 if (upload != null) {
402 upload.cancel();
403 }
404 }
405
406 /**
407 * Cancels a pending or current upload for an account
408 *
409 * @param account Owncloud accountName where the remote file will be stored.
410 */
411 public void cancel(Account account) {
412 Log_OC.d(TAG, "Account= " + account.name);
413
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();
418 }
419 }
420 // Cancel pending uploads
421 cancelUploadsForAccount(account);
422 }
423
424 public void clearListeners() {
425 mBoundListeners.clear();
426 }
427
428 /**
429 * Returns True when the file described by 'file' is being uploaded to
430 * the ownCloud account 'account' or waiting for it
431 *
432 * If 'file' is a directory, returns 'true' if some of its descendant files
433 * is uploading or waiting to upload.
434 *
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
437 */
438 public boolean isUploading(Account account, OCFile file) {
439 /*
440 if (account == null || file == null)
441 return false;
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);
450 }
451 return found;
452 } else {
453 return (mPendingUploads.containsKey(targetKey));
454 }
455 }*/
456 if (account == null || file == null) return false;
457 return (mPendingUploads.contains(account, file.getRemotePath()));
458 }
459
460
461 /**
462 * Adds a listener interested in the progress of the upload for a concrete file.
463 *
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.
467 */
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);
473 }
474
475
476
477 /**
478 * Removes a listener interested in the progress of the upload for a concrete file.
479 *
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.
483 */
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);
490 }
491 }
492
493
494 @Override
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);
502 }
503 }
504
505 /**
506 * Review uploads and cancel it if its account doesn't exist
507 */
508 public void checkAccountOfCurrentUpload() {
509 if (mCurrentUpload != null &&
510 !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
511 mCurrentUpload.cancel();
512 }
513 // The rest of uploads are cancelled when they try to start
514 }
515 }
516
517 /**
518 * Upload worker. Performs the pending uploads in the order they were
519 * requested.
520 *
521 * Created with the Looper of a new thread, started in
522 * {@link FileUploader#onCreate()}.
523 */
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;
528
529 public ServiceHandler(Looper looper, FileUploader service) {
530 super(looper);
531 if (service == null)
532 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
533 mService = service;
534 }
535
536 @Override
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());
544 }
545 }
546 Log_OC.d(TAG, "Stopping command after id " + msg.arg1);
547 mService.stopSelf(msg.arg1);
548 }
549 }
550
551 /**
552 * Core upload method: sends the file(s) to upload
553 *
554 * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
555 */
556 public void uploadFile(String uploadKey) {
557
558 Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
559 "Getting upload of " + uploadKey);
560 mCurrentUpload = mPendingUploads.get(uploadKey);
561
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");
566
567 notifyUploadStart(mCurrentUpload);
568
569 RemoteOperationResult uploadResult = null, grantResult;
570
571 try {
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(
576 mLastAccount,
577 getContentResolver()
578 );
579 } // else, reuse storage manager from previous operation
580
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);
585
586
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);
592
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()) {
601 saveUploadedFile();
602 }
603 } else {
604 uploadResult = grantResult;
605 }
606
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);
611
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);
616
617 } finally {
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());
622
623 /// notify result
624 notifyUploadResult(mCurrentUpload, uploadResult);
625
626 sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
627 }
628
629 } else {
630 // Cancel the transfer
631 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() +
632 " doesn't exist");
633 cancelUploadsForAccount(mCurrentUpload.getAccount());
634
635 }
636 }
637
638 }
639
640 /**
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.
643 *
644 * If the upload is set to enforce the creation of the folder, the method tries to
645 * create it both remote and locally.
646 *
647 * @param pathToGrant Full remote path whose existence will be granted.
648 * @return An {@link OCFile} instance corresponding to the folder where the file
649 * will be uploaded.
650 */
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);
658 }
659 if (result.isSuccess()) {
660 OCFile parentDir = mStorageManager.getFileByPath(pathToGrant);
661 if (parentDir == null) {
662 parentDir = createLocalFolder(pathToGrant);
663 }
664 if (parentDir != null) {
665 result = new RemoteOperationResult(ResultCode.OK);
666 } else {
667 result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
668 }
669 }
670 return result;
671 }
672
673
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);
681 }
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;
688 }
689 return null;
690 }
691
692
693 /**
694 * Saves a OC File after a successful upload.
695 *
696 * A PROPFIND is necessary to keep the props in the local database
697 * synchronized with the server, specially the modification time and Etag
698 * (where available)
699 *
700 * TODO refactor this ugly thing
701 */
702 private void saveUploadedFile() {
703 OCFile file = mCurrentUpload.getFile();
704 if (file.fileExists()) {
705 file = mStorageManager.getFileById(file.getFileId());
706 }
707 long syncDate = System.currentTimeMillis();
708 file.setLastSyncDateForData(syncDate);
709
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);
718 }
719
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);
727
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()
731 }
732 file.setNeedsUpdateThumbnail(true);
733 mStorageManager.saveFile(file);
734 }
735
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());
744 }
745
746 private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType,
747 FileDataStorageManager storageManager) {
748
749 // MIME type
750 if (mimeType == null || mimeType.length() <= 0) {
751 try {
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: " +
756 remotePath);
757 }
758 }
759 if (mimeType == null) {
760 mimeType = "application/octet-stream";
761 }
762
763 if (isPdfFileFromContentProviderWithoutExtension(localPath, mimeType)){
764 remotePath += FILE_EXTENSION_PDF;
765 }
766
767 OCFile newFile = new OCFile(remotePath);
768 newFile.setStoragePath(localPath);
769 newFile.setLastSyncDateForProperties(0);
770 newFile.setLastSyncDateForData(0);
771
772 // size
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
779
780
781 newFile.setMimetype(mimeType);
782
783 return newFile;
784 }
785
786 /**
787 * Creates a status notification to show the upload progress
788 *
789 * @param upload Upload operation starting.
790 */
791 private void notifyUploadStart(UploadFileOperation upload) {
792 // / create status notification with a progress bar
793 mLastPercent = 0;
794 mNotificationBuilder =
795 NotificationBuilderWithProgressBar.newNotificationBuilderWithProgressBar(this);
796 mNotificationBuilder
797 .setOngoing(true)
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)
802 .setContentText(
803 String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName()));
804
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
812 ));
813
814 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build());
815 }
816
817 /**
818 * Callback method to update the progress bar in the status notification
819 */
820 @Override
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());
831 }
832 mLastPercent = percent;
833 }
834
835 /**
836 * Updates the status notification with the result of an upload operation.
837 *
838 * @param uploadResult Result of the upload operation.
839 * @param upload Finished upload operation
840 */
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);
846
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;
851
852 String content;
853
854 // check credentials error
855 boolean needsToUpdateCredentials = (
856 uploadResult.getCode() == ResultCode.UNAUTHORIZED ||
857 uploadResult.isIdPRedirection()
858 );
859 tickerId = (needsToUpdateCredentials) ?
860 R.string.uploader_upload_failed_credentials_error : tickerId;
861
862 mNotificationBuilder
863 .setTicker(getString(tickerId))
864 .setContentTitle(getString(tickerId))
865 .setAutoCancel(true)
866 .setOngoing(false)
867 .setProgress(0, 0, false);
868
869 content = ErrorMessageAdapter.getErrorCauseMessage(
870 uploadResult, upload, getResources()
871 );
872
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()
878 );
879 updateAccountCredentials.putExtra(
880 AuthenticatorActivity.EXTRA_ACTION,
881 AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
882 );
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(
887 this,
888 (int) System.currentTimeMillis(),
889 updateAccountCredentials,
890 PendingIntent.FLAG_ONE_SHOT
891 ));
892
893 mUploadClient = null;
894 // grant that future retries on the same account will get the fresh credentials
895 } else {
896 mNotificationBuilder.setContentText(content);
897
898 if (upload.isInstant()) {
899 DbHandler db = null;
900 try {
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,
910 message) == 0) {
911 db.putFileForLater(
912 upload.getOriginalStoragePath(),
913 upload.getAccount().name,
914 message
915 );
916 }
917 }
918 } finally {
919 if (db != null) {
920 db.close();
921 }
922 }
923 }
924 }
925
926 mNotificationBuilder.setContentText(content);
927 mNotificationManager.notify(tickerId, mNotificationBuilder.build());
928
929 if (uploadResult.isSuccess()) {
930
931 DbHandler db = new DbHandler(this.getBaseContext());
932 db.removeIUPendingFile(mCurrentUpload.getOriginalStoragePath());
933 db.close();
934
935 // remove success notification, with a delay of 2 seconds
936 NotificationDelayer.cancelWithDelay(
937 mNotificationManager,
938 R.string.uploader_upload_succeeded_ticker,
939 2000);
940
941 }
942 }
943 }
944
945 /**
946 * Sends a broadcast in order to the interested activities can update their
947 * view
948 *
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
952 */
953 private void sendBroadcastUploadFinished(
954 UploadFileOperation upload,
955 RemoteOperationResult uploadResult,
956 String unlinkedFromRemotePath) {
957
958 Intent end = new Intent(getUploadFinishMessage());
959 end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath()); // real remote
960 // path, after
961 // possible
962 // automatic
963 // renaming
964 if (upload.wasRenamed()) {
965 end.putExtra(EXTRA_OLD_REMOTE_PATH, upload.getOldFile().getRemotePath());
966 }
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);
972 }
973
974 sendStickyBroadcast(end);
975 }
976
977 /**
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
983 */
984 private boolean isPdfFileFromContentProviderWithoutExtension(String localPath,
985 String mimeType) {
986 return localPath.startsWith(UriUtils.URI_CONTENT_SCHEME) &&
987 mimeType.equals(MIME_TYPE_PDF) &&
988 !localPath.endsWith(FILE_EXTENSION_PDF);
989 }
990
991 /**
992 * Remove uploads of an account
993 *
994 * @param account Downloads account to remove
995 */
996 private void cancelUploadsForAccount(Account account){
997 // Cancel pending uploads
998 mPendingUploads.remove(account);
999 }
1000 }