changed according to latest discussion
[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.util.AbstractList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.accounts.OnAccountsUpdateListener;
33 import android.app.NotificationManager;
34 import android.app.PendingIntent;
35 import android.app.Service;
36 import android.content.Intent;
37 import android.os.Binder;
38 import android.os.Handler;
39 import android.os.HandlerThread;
40 import android.os.IBinder;
41 import android.os.Looper;
42 import android.os.Message;
43 import android.os.Process;
44 import android.support.v4.app.NotificationCompat;
45 import android.util.Pair;
46 import android.webkit.MimeTypeMap;
47
48 import com.owncloud.android.R;
49 import com.owncloud.android.authentication.AccountUtils;
50 import com.owncloud.android.authentication.AuthenticatorActivity;
51 import com.owncloud.android.datamodel.FileDataStorageManager;
52 import com.owncloud.android.datamodel.OCFile;
53 import com.owncloud.android.db.DbHandler;
54 import com.owncloud.android.lib.common.OwnCloudAccount;
55 import com.owncloud.android.lib.common.OwnCloudClient;
56 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
57 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
58 import com.owncloud.android.lib.common.operations.RemoteOperation;
59 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
60 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
61 import com.owncloud.android.lib.common.utils.Log_OC;
62 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
63 import com.owncloud.android.lib.resources.files.FileUtils;
64 import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
65 import com.owncloud.android.lib.resources.files.RemoteFile;
66 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
67 import com.owncloud.android.notifications.NotificationBuilderWithProgressBar;
68 import com.owncloud.android.notifications.NotificationDelayer;
69 import com.owncloud.android.operations.CreateFolderOperation;
70 import com.owncloud.android.operations.UploadFileOperation;
71 import com.owncloud.android.operations.common.SyncOperation;
72 import com.owncloud.android.ui.activity.FileActivity;
73 import com.owncloud.android.ui.activity.FileDisplayActivity;
74 import com.owncloud.android.utils.ErrorMessageAdapter;
75 import com.owncloud.android.utils.UriUtils;
76
77
78 public class FileUploader extends Service
79 implements OnDatatransferProgressListener, OnAccountsUpdateListener {
80
81 private static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
82 public static final String EXTRA_UPLOAD_RESULT = "RESULT";
83 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
84 public static final String EXTRA_OLD_REMOTE_PATH = "OLD_REMOTE_PATH";
85 public static final String EXTRA_OLD_FILE_PATH = "OLD_FILE_PATH";
86 public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO";
87 public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
88
89 public static final String KEY_FILE = "FILE";
90 public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
91 public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
92 public static final String KEY_MIME_TYPE = "MIME_TYPE";
93
94 public static final String KEY_ACCOUNT = "ACCOUNT";
95
96 public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE";
97 public static final String KEY_FORCE_OVERWRITE = "KEY_FORCE_OVERWRITE";
98 public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD";
99 public static final String KEY_LOCAL_BEHAVIOUR = "BEHAVIOUR";
100
101 public static final int LOCAL_BEHAVIOUR_COPY = 0;
102 public static final int LOCAL_BEHAVIOUR_MOVE = 1;
103 public static final int LOCAL_BEHAVIOUR_FORGET = 2;
104 public static final int LOCAL_BEHAVIOUR_REMOVE = 3;
105
106 public static final int UPLOAD_SINGLE_FILE = 0;
107 public static final int UPLOAD_MULTIPLE_FILES = 1;
108
109 private static final String TAG = FileUploader.class.getSimpleName();
110
111 private Looper mServiceLooper;
112 private ServiceHandler mServiceHandler;
113 private IBinder mBinder;
114 private OwnCloudClient mUploadClient = null;
115 private Account mCurrentAccount = null;
116 private FileDataStorageManager mStorageManager;
117
118 private IndexedForest<UploadFileOperation> mPendingUploads = new IndexedForest<UploadFileOperation>();
119 private UploadFileOperation mCurrentUpload = null;
120
121 private NotificationManager mNotificationManager;
122 private NotificationCompat.Builder mNotificationBuilder;
123 private int mLastPercent;
124
125 private static final String MIME_TYPE_PDF = "application/pdf";
126 private static final String FILE_EXTENSION_PDF = ".pdf";
127
128
129 public static String getUploadFinishMessage() {
130 return FileUploader.class.getName() + UPLOAD_FINISH_MESSAGE;
131 }
132
133 /**
134 * Checks if an ownCloud server version should support chunked uploads.
135 *
136 * @param version OwnCloud version instance corresponding to an ownCloud
137 * server.
138 * @return 'True' if the ownCloud server with version supports chunked
139 * uploads.
140 *
141 * TODO - move to OCClient
142 */
143 private static boolean chunkedUploadIsSupported(OwnCloudVersion version) {
144 return (version != null && version.compareTo(OwnCloudVersion.owncloud_v4_5) >= 0);
145 }
146
147 /**
148 * Service initialization
149 */
150 @Override
151 public void onCreate() {
152 super.onCreate();
153 Log_OC.d(TAG, "Creating service");
154 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
155 HandlerThread thread = new HandlerThread("FileUploaderThread",
156 Process.THREAD_PRIORITY_BACKGROUND);
157 thread.start();
158 mServiceLooper = thread.getLooper();
159 mServiceHandler = new ServiceHandler(mServiceLooper, this);
160 mBinder = new FileUploaderBinder();
161
162 // add AccountsUpdatedListener
163 AccountManager am = AccountManager.get(getApplicationContext());
164 am.addOnAccountsUpdatedListener(this, null, false);
165 }
166
167 /**
168 * Service clean up
169 */
170 @Override
171 public void onDestroy() {
172 Log_OC.v(TAG, "Destroying service" );
173 mBinder = null;
174 mServiceHandler = null;
175 mServiceLooper.quit();
176 mServiceLooper = null;
177 mNotificationManager = null;
178
179 // remove AccountsUpdatedListener
180 AccountManager am = AccountManager.get(getApplicationContext());
181 am.removeOnAccountsUpdatedListener(this);
182
183 super.onDestroy();
184 }
185
186
187 /**
188 * Entry point to add one or several files to the queue of uploads.
189 *
190 * New uploads are added calling to startService(), resulting in a call to
191 * this method. This ensures the service will keep on working although the
192 * caller activity goes away.
193 */
194 @Override
195 public int onStartCommand(Intent intent, int flags, int startId) {
196 Log_OC.d(TAG, "Starting command with id " + startId);
197
198 if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
199 || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
200 Log_OC.e(TAG, "Not enough information provided in intent");
201 return Service.START_NOT_STICKY;
202 }
203 int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
204 if (uploadType == -1) {
205 Log_OC.e(TAG, "Incorrect upload type provided");
206 return Service.START_NOT_STICKY;
207 }
208 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
209 if (!AccountUtils.exists(account, getApplicationContext())) {
210 return Service.START_NOT_STICKY;
211 }
212
213 String[] localPaths = null, remotePaths = null, mimeTypes = null;
214 OCFile[] files = null;
215 if (uploadType == UPLOAD_SINGLE_FILE) {
216
217 if (intent.hasExtra(KEY_FILE)) {
218 files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };
219
220 } else {
221 localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
222 remotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
223 mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
224 }
225
226 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
227
228 if (intent.hasExtra(KEY_FILE)) {
229 files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
230 // will
231 // this
232 // casting
233 // work
234 // fine?
235
236 } else {
237 localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
238 remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
239 mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
240 }
241 }
242
243 FileDataStorageManager storageManager = new FileDataStorageManager(account,
244 getContentResolver());
245
246 boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
247 boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
248 int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);
249
250 if (intent.hasExtra(KEY_FILE) && files == null) {
251 Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
252 return Service.START_NOT_STICKY;
253
254 } else if (!intent.hasExtra(KEY_FILE)) {
255 if (localPaths == null) {
256 Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
257 return Service.START_NOT_STICKY;
258 }
259 if (remotePaths == null) {
260 Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
261 return Service.START_NOT_STICKY;
262 }
263 if (localPaths.length != remotePaths.length) {
264 Log_OC.e(TAG, "Different number of remote paths and local paths!");
265 return Service.START_NOT_STICKY;
266 }
267
268 files = new OCFile[localPaths.length];
269 for (int i = 0; i < localPaths.length; i++) {
270 files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i],
271 ((mimeTypes != null) ? mimeTypes[i] : null));
272 if (files[i] == null) {
273 // TODO @andomaex add failure Notification
274 return Service.START_NOT_STICKY;
275 }
276 }
277 }
278
279 OwnCloudVersion ocv = AccountUtils.getServerVersion(account);
280
281 boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
282 AbstractList<String> requestedUploads = new Vector<String>();
283 String uploadKey = null;
284 UploadFileOperation newUpload = null;
285 try {
286 for (int i = 0; i < files.length; i++) {
287 newUpload = new UploadFileOperation(
288 account,
289 files[i],
290 chunked,
291 isInstant,
292 forceOverwrite, localAction,
293 getApplicationContext()
294 );
295 if (isInstant) {
296 newUpload.setRemoteFolderToBeCreated();
297 }
298 newUpload.addDatatransferProgressListener(this);
299 newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
300 Pair<String, String> putResult = mPendingUploads.putIfAbsent(
301 account, files[i].getRemotePath(), newUpload
302 );
303 if (putResult != null) {
304 uploadKey = putResult.first;
305 requestedUploads.add(uploadKey);
306 } // else, file already in the queue of uploads; don't repeat the request
307 }
308
309 } catch (IllegalArgumentException e) {
310 Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
311 return START_NOT_STICKY;
312
313 } catch (IllegalStateException e) {
314 Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
315 return START_NOT_STICKY;
316
317 } catch (Exception e) {
318 Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
319 return START_NOT_STICKY;
320
321 }
322
323 if (requestedUploads.size() > 0) {
324 Message msg = mServiceHandler.obtainMessage();
325 msg.arg1 = startId;
326 msg.obj = requestedUploads;
327 mServiceHandler.sendMessage(msg);
328 }
329 return Service.START_NOT_STICKY;
330 }
331
332 /**
333 * Provides a binder object that clients can use to perform operations on
334 * the queue of uploads, excepting the addition of new files.
335 *
336 * Implemented to perform cancellation, pause and resume of existing
337 * uploads.
338 */
339 @Override
340 public IBinder onBind(Intent arg0) {
341 return mBinder;
342 }
343
344 /**
345 * Called when ALL the bound clients were onbound.
346 */
347 @Override
348 public boolean onUnbind(Intent intent) {
349 ((FileUploaderBinder)mBinder).clearListeners();
350 return false; // not accepting rebinding (default behaviour)
351 }
352
353 @Override
354 public void onAccountsUpdated(Account[] accounts) {
355 // Review current upload, and cancel it if its account doen't exist
356 if (mCurrentUpload != null &&
357 !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
358 mCurrentUpload.cancel();
359 }
360 // The rest of uploads are cancelled when they try to start
361 }
362
363 /**
364 * Binder to let client components to perform operations on the queue of
365 * uploads.
366 *
367 * It provides by itself the available operations.
368 */
369 public class FileUploaderBinder extends Binder implements OnDatatransferProgressListener {
370
371 /**
372 * Map of listeners that will be reported about progress of uploads from a
373 * {@link FileUploaderBinder} instance
374 */
375 private Map<String, OnDatatransferProgressListener> mBoundListeners =
376 new HashMap<String, OnDatatransferProgressListener>();
377
378 /**
379 * Cancels a pending or current upload of a remote file.
380 *
381 * @param account ownCloud account where the remote file will be stored.
382 * @param file A file in the queue of pending uploads
383 */
384 public void cancel(Account account, OCFile file) {
385 Pair<UploadFileOperation, String> removeResult = mPendingUploads.remove(account, file.getRemotePath());
386 UploadFileOperation upload = removeResult.first;
387 if (upload != null) {
388 upload.cancel();
389 } else {
390 if (mCurrentUpload != null && mCurrentAccount != null &&
391 mCurrentUpload.getRemotePath().startsWith(file.getRemotePath()) &&
392 account.name.equals(mCurrentAccount.name)) {
393 mCurrentUpload.cancel();
394 }
395 }
396 }
397
398 /**
399 * Cancels all the uploads for an account
400 *
401 * @param account ownCloud account.
402 */
403 public void cancel(Account account) {
404 Log_OC.d(TAG, "Account= " + account.name);
405
406 if (mCurrentUpload != null) {
407 Log_OC.d(TAG, "Current Upload Account= " + mCurrentUpload.getAccount().name);
408 if (mCurrentUpload.getAccount().name.equals(account.name)) {
409 mCurrentUpload.cancel();
410 }
411 }
412 // Cancel pending uploads
413 cancelUploadsForAccount(account);
414 }
415
416 public void clearListeners() {
417 mBoundListeners.clear();
418 }
419
420
421 /**
422 * Returns True when the file described by 'file' is being uploaded to
423 * the ownCloud account 'account' or waiting for it
424 *
425 * If 'file' is a directory, returns 'true' if some of its descendant files
426 * is uploading or waiting to upload.
427 *
428 * @param account ownCloud account where the remote file will be stored.
429 * @param file A file that could be in the queue of pending uploads
430 */
431 public boolean isUploading(Account account, OCFile file) {
432 if (account == null || file == null) return false;
433 return (mPendingUploads.contains(account, file.getRemotePath()));
434 }
435
436
437 /**
438 * Adds a listener interested in the progress of the upload for a concrete file.
439 *
440 * @param listener Object to notify about progress of transfer.
441 * @param account ownCloud account holding the file of interest.
442 * @param file {@link OCFile} of interest for listener.
443 */
444 public void addDatatransferProgressListener (OnDatatransferProgressListener listener,
445 Account account, OCFile file) {
446 if (account == null || file == null || listener == null) return;
447 String targetKey = buildRemoteName(account, file);
448 mBoundListeners.put(targetKey, listener);
449 }
450
451
452
453 /**
454 * Removes a listener interested in the progress of the upload for a concrete file.
455 *
456 * @param listener Object to notify about progress of transfer.
457 * @param account ownCloud account holding the file of interest.
458 * @param file {@link OCFile} of interest for listener.
459 */
460 public void removeDatatransferProgressListener (OnDatatransferProgressListener listener,
461 Account account, OCFile file) {
462 if (account == null || file == null || listener == null) return;
463 String targetKey = buildRemoteName(account, file);
464 if (mBoundListeners.get(targetKey) == listener) {
465 mBoundListeners.remove(targetKey);
466 }
467 }
468
469
470 @Override
471 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
472 long totalToTransfer, String fileName) {
473 String key = buildRemoteName(mCurrentUpload.getAccount(), mCurrentUpload.getFile());
474 OnDatatransferProgressListener boundListener = mBoundListeners.get(key);
475 if (boundListener != null) {
476 boundListener.onTransferProgress(progressRate, totalTransferredSoFar,
477 totalToTransfer, fileName);
478 }
479 }
480
481 /**
482 * Builds a key for the map of listeners.
483 *
484 * TODO remove and replace key with file.getFileId() after changing current policy (upload file, then
485 * add to local database) to better policy (add to local database, then upload)
486 *
487 * @param account ownCloud account where the file to upload belongs.
488 * @param file File to upload
489 * @return Key
490 */
491 private String buildRemoteName(Account account, OCFile file) {
492 return account.name + file.getRemotePath();
493 }
494
495 }
496
497 /**
498 * Upload worker. Performs the pending uploads in the order they were
499 * requested.
500 *
501 * Created with the Looper of a new thread, started in
502 * {@link FileUploader#onCreate()}.
503 */
504 private static class ServiceHandler extends Handler {
505 // don't make it a final class, and don't remove the static ; lint will
506 // warn about a possible memory leak
507 FileUploader mService;
508
509 public ServiceHandler(Looper looper, FileUploader service) {
510 super(looper);
511 if (service == null)
512 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
513 mService = service;
514 }
515
516 @Override
517 public void handleMessage(Message msg) {
518 @SuppressWarnings("unchecked")
519 AbstractList<String> requestedUploads = (AbstractList<String>) msg.obj;
520 if (msg.obj != null) {
521 Iterator<String> it = requestedUploads.iterator();
522 while (it.hasNext()) {
523 mService.uploadFile(it.next());
524 }
525 }
526 Log_OC.d(TAG, "Stopping command after id " + msg.arg1);
527 mService.stopSelf(msg.arg1);
528 }
529 }
530
531 /**
532 * Core upload method: sends the file(s) to upload
533 *
534 * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
535 */
536 public void uploadFile(String uploadKey) {
537
538 mCurrentUpload = mPendingUploads.get(uploadKey);
539
540 if (mCurrentUpload != null) {
541 // Detect if the account exists
542 if (AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
543 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().name + " exists");
544
545 notifyUploadStart(mCurrentUpload);
546
547 RemoteOperationResult uploadResult = null, grantResult;
548
549 try {
550 /// prepare client object to send the request to the ownCloud server
551 if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
552 mCurrentAccount = mCurrentUpload.getAccount();
553 mStorageManager = new FileDataStorageManager(
554 mCurrentAccount,
555 getContentResolver()
556 );
557 } // else, reuse storage manager from previous operation
558
559 // always get client from client manager, to get fresh credentials in case of update
560 OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
561 mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
562 getClientFor(ocAccount, this);
563
564
565 /// check the existence of the parent folder for the file to upload
566 String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
567 remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
568 remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
569 grantResult = grantFolderExistence(remoteParentPath);
570
571 /// perform the upload
572 if (grantResult.isSuccess()) {
573 OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
574 mCurrentUpload.getFile().setParentId(parent.getFileId());
575 uploadResult = mCurrentUpload.execute(mUploadClient);
576 if (uploadResult.isSuccess()) {
577 saveUploadedFile();
578
579 } else if (uploadResult.getCode() == ResultCode.SYNC_CONFLICT) {
580 mStorageManager.saveConflict(mCurrentUpload.getFile(),
581 mCurrentUpload.getFile().getEtagInConflict());
582 }
583 } else {
584 uploadResult = grantResult;
585 }
586
587 } catch (Exception e) {
588 Log_OC.e(TAG, "Error uploading", e);
589 uploadResult = new RemoteOperationResult(e);
590
591 } finally {
592 Pair<UploadFileOperation, String> removeResult;
593 if (mCurrentUpload.wasRenamed()) {
594 removeResult = mPendingUploads.removePayload(
595 mCurrentAccount,
596 mCurrentUpload.getOldFile().getRemotePath()
597 );
598 } else {
599 removeResult = mPendingUploads.removePayload(
600 mCurrentAccount,
601 mCurrentUpload.getRemotePath()
602 );
603 }
604
605 /// notify result
606 notifyUploadResult(mCurrentUpload, uploadResult);
607
608 sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
609 }
610
611 } else {
612 // Cancel the transfer
613 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() +
614 " doesn't exist");
615 cancelUploadsForAccount(mCurrentUpload.getAccount());
616
617 }
618 }
619
620 }
621
622 /**
623 * Checks the existence of the folder where the current file will be uploaded both
624 * in the remote server and in the local database.
625 *
626 * If the upload is set to enforce the creation of the folder, the method tries to
627 * create it both remote and locally.
628 *
629 * @param pathToGrant Full remote path whose existence will be granted.
630 * @return An {@link OCFile} instance corresponding to the folder where the file
631 * will be uploaded.
632 */
633 private RemoteOperationResult grantFolderExistence(String pathToGrant) {
634 RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, this, false);
635 RemoteOperationResult result = operation.execute(mUploadClient);
636 if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND &&
637 mCurrentUpload.isRemoteFolderToBeCreated()) {
638 SyncOperation syncOp = new CreateFolderOperation( pathToGrant, true);
639 result = syncOp.execute(mUploadClient, mStorageManager);
640 }
641 if (result.isSuccess()) {
642 OCFile parentDir = mStorageManager.getFileByPath(pathToGrant);
643 if (parentDir == null) {
644 parentDir = createLocalFolder(pathToGrant);
645 }
646 if (parentDir != null) {
647 result = new RemoteOperationResult(ResultCode.OK);
648 } else {
649 result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
650 }
651 }
652 return result;
653 }
654
655
656 private OCFile createLocalFolder(String remotePath) {
657 String parentPath = new File(remotePath).getParent();
658 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ?
659 parentPath : parentPath + OCFile.PATH_SEPARATOR;
660 OCFile parent = mStorageManager.getFileByPath(parentPath);
661 if (parent == null) {
662 parent = createLocalFolder(parentPath);
663 }
664 if (parent != null) {
665 OCFile createdFolder = new OCFile(remotePath);
666 createdFolder.setMimetype("DIR");
667 createdFolder.setParentId(parent.getFileId());
668 mStorageManager.saveFile(createdFolder);
669 return createdFolder;
670 }
671 return null;
672 }
673
674
675 /**
676 * Saves a OC File after a successful upload.
677 *
678 * A PROPFIND is necessary to keep the props in the local database
679 * synchronized with the server, specially the modification time and Etag
680 * (where available)
681 *
682 * TODO move into UploadFileOperation
683 */
684 private void saveUploadedFile() {
685 OCFile file = mCurrentUpload.getFile();
686 if (file.fileExists()) {
687 file = mStorageManager.getFileById(file.getFileId());
688 }
689 long syncDate = System.currentTimeMillis();
690 file.setLastSyncDateForData(syncDate);
691
692 // new PROPFIND to keep data consistent with server
693 // in theory, should return the same we already have
694 ReadRemoteFileOperation operation =
695 new ReadRemoteFileOperation(mCurrentUpload.getRemotePath());
696 RemoteOperationResult result = operation.execute(mUploadClient);
697 if (result.isSuccess()) {
698 updateOCFile(file, (RemoteFile) result.getData().get(0));
699 file.setLastSyncDateForProperties(syncDate);
700 } else {
701 Log_OC.e(TAG, "Error reading properties of file after successful upload; this is gonna hurt...");
702 }
703
704 // / maybe this would be better as part of UploadFileOperation... or
705 // maybe all this method
706 if (mCurrentUpload.wasRenamed()) {
707 OCFile oldFile = mCurrentUpload.getOldFile();
708 if (oldFile.fileExists()) {
709 oldFile.setStoragePath(null);
710 mStorageManager.saveFile(oldFile);
711 mStorageManager.saveConflict(oldFile, null);
712
713 } // else: it was just an automatic renaming due to a name
714 // coincidence; nothing else is needed, the storagePath is right
715 // in the instance returned by mCurrentUpload.getFile()
716 }
717 file.setNeedsUpdateThumbnail(true);
718 mStorageManager.saveFile(file);
719 mStorageManager.saveConflict(file, null);
720
721 mStorageManager.triggerMediaScan(file.getStoragePath());
722
723 }
724
725 private void updateOCFile(OCFile file, RemoteFile remoteFile) {
726 file.setCreationTimestamp(remoteFile.getCreationTimestamp());
727 file.setFileLength(remoteFile.getLength());
728 file.setMimetype(remoteFile.getMimeType());
729 file.setModificationTimestamp(remoteFile.getModifiedTimestamp());
730 file.setModificationTimestampAtLastSyncForData(remoteFile.getModifiedTimestamp());
731 file.setEtag(remoteFile.getEtag());
732 file.setRemoteId(remoteFile.getRemoteId());
733 }
734
735 private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType) {
736
737 // MIME type
738 if (mimeType == null || mimeType.length() <= 0) {
739 try {
740 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
741 remotePath.substring(remotePath.lastIndexOf('.') + 1));
742 } catch (IndexOutOfBoundsException e) {
743 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " +
744 remotePath);
745 }
746 }
747 if (mimeType == null) {
748 mimeType = "application/octet-stream";
749 }
750
751 if (isPdfFileFromContentProviderWithoutExtension(localPath, mimeType)){
752 remotePath += FILE_EXTENSION_PDF;
753 }
754
755 OCFile newFile = new OCFile(remotePath);
756 newFile.setStoragePath(localPath);
757 newFile.setLastSyncDateForProperties(0);
758 newFile.setLastSyncDateForData(0);
759
760 // size
761 if (localPath != null && localPath.length() > 0) {
762 File localFile = new File(localPath);
763 newFile.setFileLength(localFile.length());
764 newFile.setLastSyncDateForData(localFile.lastModified());
765 } // don't worry about not assigning size, the problems with localPath
766 // are checked when the UploadFileOperation instance is created
767
768
769 newFile.setMimetype(mimeType);
770
771 return newFile;
772 }
773
774 /**
775 * Creates a status notification to show the upload progress
776 *
777 * @param upload Upload operation starting.
778 */
779 private void notifyUploadStart(UploadFileOperation upload) {
780 // / create status notification with a progress bar
781 mLastPercent = 0;
782 mNotificationBuilder =
783 NotificationBuilderWithProgressBar.newNotificationBuilderWithProgressBar(this);
784 mNotificationBuilder
785 .setOngoing(true)
786 .setSmallIcon(R.drawable.notification_icon)
787 .setTicker(getString(R.string.uploader_upload_in_progress_ticker))
788 .setContentTitle(getString(R.string.uploader_upload_in_progress_ticker))
789 .setProgress(100, 0, false)
790 .setContentText(
791 String.format(getString(R.string.uploader_upload_in_progress_content), 0, upload.getFileName()));
792
793 /// includes a pending intent in the notification showing the details view of the file
794 Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class);
795 showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, upload.getFile());
796 showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, upload.getAccount());
797 showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
798 mNotificationBuilder.setContentIntent(PendingIntent.getActivity(
799 this, (int) System.currentTimeMillis(), showDetailsIntent, 0
800 ));
801
802 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build());
803 }
804
805 /**
806 * Callback method to update the progress bar in the status notification
807 */
808 @Override
809 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
810 long totalToTransfer, String filePath) {
811 int percent = (int) (100.0 * ((double) totalTransferredSoFar) / ((double) totalToTransfer));
812 if (percent != mLastPercent) {
813 mNotificationBuilder.setProgress(100, percent, false);
814 String fileName = filePath.substring(
815 filePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1);
816 String text = String.format(getString(R.string.uploader_upload_in_progress_content), percent, fileName);
817 mNotificationBuilder.setContentText(text);
818 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build());
819 }
820 mLastPercent = percent;
821 }
822
823 /**
824 * Updates the status notification with the result of an upload operation.
825 *
826 * @param uploadResult Result of the upload operation.
827 * @param upload Finished upload operation
828 */
829 private void notifyUploadResult(UploadFileOperation upload,
830 RemoteOperationResult uploadResult) {
831 Log_OC.d(TAG, "NotifyUploadResult with resultCode: " + uploadResult.getCode());
832 // / cancelled operation or success -> silent removal of progress notification
833 mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker);
834
835 // Show the result: success or fail notification
836 if (!uploadResult.isCancelled()) {
837 int tickerId = (uploadResult.isSuccess()) ? R.string.uploader_upload_succeeded_ticker :
838 R.string.uploader_upload_failed_ticker;
839
840 String content;
841
842 // check credentials error
843 boolean needsToUpdateCredentials = (
844 uploadResult.getCode() == ResultCode.UNAUTHORIZED ||
845 uploadResult.isIdPRedirection()
846 );
847 tickerId = (needsToUpdateCredentials) ?
848 R.string.uploader_upload_failed_credentials_error : tickerId;
849
850 mNotificationBuilder
851 .setTicker(getString(tickerId))
852 .setContentTitle(getString(tickerId))
853 .setAutoCancel(true)
854 .setOngoing(false)
855 .setProgress(0, 0, false);
856
857 content = ErrorMessageAdapter.getErrorCauseMessage(
858 uploadResult, upload, getResources()
859 );
860
861 if (needsToUpdateCredentials) {
862 // let the user update credentials with one click
863 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
864 updateAccountCredentials.putExtra(
865 AuthenticatorActivity.EXTRA_ACCOUNT, upload.getAccount()
866 );
867 updateAccountCredentials.putExtra(
868 AuthenticatorActivity.EXTRA_ACTION,
869 AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
870 );
871 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
872 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
873 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
874 mNotificationBuilder.setContentIntent(PendingIntent.getActivity(
875 this,
876 (int) System.currentTimeMillis(),
877 updateAccountCredentials,
878 PendingIntent.FLAG_ONE_SHOT
879 ));
880
881 mUploadClient = null;
882 // grant that future retries on the same account will get the fresh credentials
883 } else {
884 mNotificationBuilder.setContentText(content);
885
886 if (upload.isInstant()) {
887 DbHandler db = null;
888 try {
889 db = new DbHandler(this.getBaseContext());
890 String message = uploadResult.getLogMessage() + " errorCode: " +
891 uploadResult.getCode();
892 Log_OC.e(TAG, message + " Http-Code: " + uploadResult.getHttpCode());
893 if (uploadResult.getCode() == ResultCode.QUOTA_EXCEEDED) {
894 //message = getString(R.string.failed_upload_quota_exceeded_text);
895 if (db.updateFileState(
896 upload.getOriginalStoragePath(),
897 DbHandler.UPLOAD_STATUS_UPLOAD_FAILED,
898 message) == 0) {
899 db.putFileForLater(
900 upload.getOriginalStoragePath(),
901 upload.getAccount().name,
902 message
903 );
904 }
905 }
906 } finally {
907 if (db != null) {
908 db.close();
909 }
910 }
911 }
912 }
913
914 mNotificationBuilder.setContentText(content);
915 mNotificationManager.notify(tickerId, mNotificationBuilder.build());
916
917 if (uploadResult.isSuccess()) {
918
919 DbHandler db = new DbHandler(this.getBaseContext());
920 db.removeIUPendingFile(mCurrentUpload.getOriginalStoragePath());
921 db.close();
922
923 // remove success notification, with a delay of 2 seconds
924 NotificationDelayer.cancelWithDelay(
925 mNotificationManager,
926 R.string.uploader_upload_succeeded_ticker,
927 2000);
928
929 }
930 }
931 }
932
933 /**
934 * Sends a broadcast in order to the interested activities can update their
935 * view
936 *
937 * @param upload Finished upload operation
938 * @param uploadResult Result of the upload operation
939 * @param unlinkedFromRemotePath Path in the uploads tree where the upload was unlinked from
940 */
941 private void sendBroadcastUploadFinished(
942 UploadFileOperation upload,
943 RemoteOperationResult uploadResult,
944 String unlinkedFromRemotePath) {
945
946 Intent end = new Intent(getUploadFinishMessage());
947 end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath()); // real remote
948 // path, after
949 // possible
950 // automatic
951 // renaming
952 if (upload.wasRenamed()) {
953 end.putExtra(EXTRA_OLD_REMOTE_PATH, upload.getOldFile().getRemotePath());
954 }
955 end.putExtra(EXTRA_OLD_FILE_PATH, upload.getOriginalStoragePath());
956 end.putExtra(ACCOUNT_NAME, upload.getAccount().name);
957 end.putExtra(EXTRA_UPLOAD_RESULT, uploadResult.isSuccess());
958 if (unlinkedFromRemotePath != null) {
959 end.putExtra(EXTRA_LINKED_TO_PATH, unlinkedFromRemotePath);
960 }
961
962 sendStickyBroadcast(end);
963 }
964
965 /**
966 * Checks if content provider, using the content:// scheme, returns a file with mime-type
967 * 'application/pdf' but file has not extension
968 * @param localPath Full path to a file in the local file system.
969 * @param mimeType MIME type of the file.
970 * @return true if is needed to add the pdf file extension to the file
971 *
972 * TODO - move to OCFile or Utils class
973 */
974 private boolean isPdfFileFromContentProviderWithoutExtension(String localPath,
975 String mimeType) {
976 return localPath.startsWith(UriUtils.URI_CONTENT_SCHEME) &&
977 mimeType.equals(MIME_TYPE_PDF) &&
978 !localPath.endsWith(FILE_EXTENSION_PDF);
979 }
980
981 /**
982 * Remove uploads of an account
983 *
984 * @param account Downloads account to remove
985 */
986 private void cancelUploadsForAccount(Account account){
987 // Cancel pending uploads
988 mPendingUploads.remove(account);
989 }
990 }