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