2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2012-2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.files
.services
;
24 import java
.util
.AbstractList
;
25 import java
.util
.HashMap
;
26 import java
.util
.Iterator
;
28 import java
.util
.Vector
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.authentication
.AccountUtils
;
32 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
33 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
34 import com
.owncloud
.android
.datamodel
.OCFile
;
36 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
37 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
38 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
39 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
40 import com
.owncloud
.android
.notifications
.NotificationBuilderWithProgressBar
;
41 import com
.owncloud
.android
.notifications
.NotificationDelayer
;
42 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
43 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
44 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
45 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
46 import com
.owncloud
.android
.operations
.DownloadFileOperation
;
47 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
48 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
49 import com
.owncloud
.android
.ui
.preview
.PreviewImageActivity
;
50 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
51 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
53 import android
.accounts
.Account
;
54 import android
.accounts
.AccountManager
;
55 import android
.accounts
.OnAccountsUpdateListener
;
56 import android
.app
.NotificationManager
;
57 import android
.app
.PendingIntent
;
58 import android
.app
.Service
;
59 import android
.content
.Intent
;
60 import android
.os
.Binder
;
61 import android
.os
.Handler
;
62 import android
.os
.HandlerThread
;
63 import android
.os
.IBinder
;
64 import android
.os
.Looper
;
65 import android
.os
.Message
;
66 import android
.os
.Process
;
67 import android
.support
.v4
.app
.NotificationCompat
;
68 import android
.util
.Pair
;
70 public class FileDownloader
extends Service
71 implements OnDatatransferProgressListener
, OnAccountsUpdateListener
{
73 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
74 public static final String EXTRA_FILE
= "FILE";
76 private static final String DOWNLOAD_ADDED_MESSAGE
= "DOWNLOAD_ADDED";
77 private static final String DOWNLOAD_FINISH_MESSAGE
= "DOWNLOAD_FINISH";
78 public static final String EXTRA_DOWNLOAD_RESULT
= "RESULT";
79 public static final String EXTRA_FILE_PATH
= "FILE_PATH";
80 public static final String EXTRA_REMOTE_PATH
= "REMOTE_PATH";
81 public static final String EXTRA_LINKED_TO_PATH
= "LINKED_TO";
82 public static final String ACCOUNT_NAME
= "ACCOUNT_NAME";
84 private static final String TAG
= "FileDownloader";
86 private Looper mServiceLooper
;
87 private ServiceHandler mServiceHandler
;
88 private IBinder mBinder
;
89 private OwnCloudClient mDownloadClient
= null
;
90 private Account mCurrentAccount
= null
;
91 private FileDataStorageManager mStorageManager
;
93 private IndexedForest
<DownloadFileOperation
> mPendingDownloads
= new IndexedForest
<DownloadFileOperation
>();
95 private DownloadFileOperation mCurrentDownload
= null
;
97 private NotificationManager mNotificationManager
;
98 private NotificationCompat
.Builder mNotificationBuilder
;
99 private int mLastPercent
;
102 public static String
getDownloadAddedMessage() {
103 return FileDownloader
.class.getName() + DOWNLOAD_ADDED_MESSAGE
;
106 public static String
getDownloadFinishMessage() {
107 return FileDownloader
.class.getName() + DOWNLOAD_FINISH_MESSAGE
;
111 * Service initialization
114 public void onCreate() {
116 Log_OC
.d(TAG
, "Creating service");
117 mNotificationManager
= (NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
118 HandlerThread thread
= new HandlerThread("FileDownloaderThread",
119 Process
.THREAD_PRIORITY_BACKGROUND
);
121 mServiceLooper
= thread
.getLooper();
122 mServiceHandler
= new ServiceHandler(mServiceLooper
, this);
123 mBinder
= new FileDownloaderBinder();
125 // add AccountsUpdatedListener
126 AccountManager am
= AccountManager
.get(getApplicationContext());
127 am
.addOnAccountsUpdatedListener(this, null
, false
);
135 public void onDestroy() {
136 Log_OC
.v(TAG
, "Destroying service");
138 mServiceHandler
= null
;
139 mServiceLooper
.quit();
140 mServiceLooper
= null
;
141 mNotificationManager
= null
;
143 // remove AccountsUpdatedListener
144 AccountManager am
= AccountManager
.get(getApplicationContext());
145 am
.removeOnAccountsUpdatedListener(this);
152 * Entry point to add one or several files to the queue of downloads.
154 * New downloads are added calling to startService(), resulting in a call to this method.
155 * This ensures the service will keep on working although the caller activity goes away.
158 public int onStartCommand(Intent intent
, int flags
, int startId
) {
159 Log_OC
.d(TAG
, "Starting command with id " + startId
);
161 if (!intent
.hasExtra(EXTRA_ACCOUNT
) ||
162 !intent
.hasExtra(EXTRA_FILE
)
164 Log_OC
.e(TAG
, "Not enough information provided in intent");
165 return START_NOT_STICKY
;
167 final Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
168 final OCFile file
= intent
.getParcelableExtra(EXTRA_FILE
);
169 AbstractList
<String
> requestedDownloads
= new Vector
<String
>();
171 DownloadFileOperation newDownload
= new DownloadFileOperation(account
, file
);
172 newDownload
.addDatatransferProgressListener(this);
173 newDownload
.addDatatransferProgressListener((FileDownloaderBinder
) mBinder
);
174 Pair
<String
, String
> putResult
= mPendingDownloads
.putIfAbsent(
175 account
, file
.getRemotePath(), newDownload
177 String downloadKey
= putResult
.first
;
178 requestedDownloads
.add(downloadKey
);
180 sendBroadcastNewDownload(newDownload
, putResult
.second
);
182 } catch (IllegalArgumentException e
) {
183 Log_OC
.e(TAG
, "Not enough information provided in intent: " + e
.getMessage());
184 return START_NOT_STICKY
;
187 if (requestedDownloads
.size() > 0) {
188 Message msg
= mServiceHandler
.obtainMessage();
190 msg
.obj
= requestedDownloads
;
191 mServiceHandler
.sendMessage(msg
);
196 return START_NOT_STICKY
;
201 * Provides a binder object that clients can use to perform operations on the queue of downloads,
202 * excepting the addition of new files.
204 * Implemented to perform cancellation, pause and resume of existing downloads.
207 public IBinder
onBind(Intent arg0
) {
213 * Called when ALL the bound clients were onbound.
216 public boolean onUnbind(Intent intent
) {
217 ((FileDownloaderBinder
) mBinder
).clearListeners();
218 return false
; // not accepting rebinding (default behaviour)
222 public void onAccountsUpdated(Account
[] accounts
) {
223 //review the current download and cancel it if its account doesn't exist
224 if (mCurrentDownload
!= null
&&
225 !AccountUtils
.exists(mCurrentDownload
.getAccount(), getApplicationContext())) {
226 mCurrentDownload
.cancel();
228 // The rest of downloads are cancelled when they try to start
233 * Binder to let client components to perform operations on the queue of downloads.
235 * It provides by itself the available operations.
237 public class FileDownloaderBinder
extends Binder
implements OnDatatransferProgressListener
{
240 * Map of listeners that will be reported about progress of downloads from a
241 * {@link FileDownloaderBinder}
244 private Map
<Long
, OnDatatransferProgressListener
> mBoundListeners
=
245 new HashMap
<Long
, OnDatatransferProgressListener
>();
249 * Cancels a pending or current download of a remote file.
251 * @param account ownCloud account where the remote file is stored.
252 * @param file A file in the queue of pending downloads
254 public void cancel(Account account
, OCFile file
) {
255 Pair
<DownloadFileOperation
, String
> removeResult
= mPendingDownloads
.remove(account
, file
.getRemotePath());
256 DownloadFileOperation download
= removeResult
.first
;
257 if (download
!= null
) {
260 if (mCurrentDownload
!= null
&& mCurrentAccount
!= null
&&
261 mCurrentDownload
.getRemotePath().startsWith(file
.getRemotePath()) &&
262 account
.name
.equals(mCurrentAccount
.name
)) {
263 mCurrentDownload
.cancel();
269 * Cancels all the downloads for an account
271 * @param account ownCloud account.
273 public void cancel(Account account
) {
274 Log_OC
.d(TAG
, "Account= " + account
.name
);
276 if (mCurrentDownload
!= null
) {
277 Log_OC
.d(TAG
, "Current Download Account= " + mCurrentDownload
.getAccount().name
);
278 if (mCurrentDownload
.getAccount().name
.equals(account
.name
)) {
279 mCurrentDownload
.cancel();
282 // Cancel pending downloads
283 cancelDownloadsForAccount(account
);
286 public void clearListeners() {
287 mBoundListeners
.clear();
292 * Returns True when the file described by 'file' in the ownCloud account 'account'
293 * is downloading or waiting to download.
295 * If 'file' is a directory, returns 'true' if any of its descendant files is downloading or
296 * waiting to download.
298 * @param account ownCloud account where the remote file is stored.
299 * @param file A file that could be in the queue of downloads.
301 public boolean isDownloading(Account account
, OCFile file
) {
302 if (account
== null
|| file
== null
) return false
;
303 return (mPendingDownloads
.contains(account
, file
.getRemotePath()));
308 * Adds a listener interested in the progress of the download for a concrete file.
310 * @param listener Object to notify about progress of transfer.
311 * @param account ownCloud account holding the file of interest.
312 * @param file {@link OCFile} of interest for listener.
314 public void addDatatransferProgressListener(
315 OnDatatransferProgressListener listener
, Account account
, OCFile file
317 if (account
== null
|| file
== null
|| listener
== null
) return;
318 mBoundListeners
.put(file
.getFileId(), listener
);
323 * Removes a listener interested in the progress of the download for a concrete file.
325 * @param listener Object to notify about progress of transfer.
326 * @param account ownCloud account holding the file of interest.
327 * @param file {@link OCFile} of interest for listener.
329 public void removeDatatransferProgressListener(
330 OnDatatransferProgressListener listener
, Account account
, OCFile file
332 if (account
== null
|| file
== null
|| listener
== null
) return;
333 Long fileId
= file
.getFileId();
334 if (mBoundListeners
.get(fileId
) == listener
) {
335 mBoundListeners
.remove(fileId
);
340 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
341 long totalToTransfer
, String fileName
) {
342 OnDatatransferProgressListener boundListener
=
343 mBoundListeners
.get(mCurrentDownload
.getFile().getFileId());
344 if (boundListener
!= null
) {
345 boundListener
.onTransferProgress(progressRate
, totalTransferredSoFar
,
346 totalToTransfer
, fileName
);
354 * Download worker. Performs the pending downloads in the order they were requested.
356 * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
358 private static class ServiceHandler
extends Handler
{
359 // don't make it a final class, and don't remove the static ; lint will warn about a
360 // possible memory leak
361 FileDownloader mService
;
363 public ServiceHandler(Looper looper
, FileDownloader service
) {
366 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
371 public void handleMessage(Message msg
) {
372 @SuppressWarnings("unchecked")
373 AbstractList
<String
> requestedDownloads
= (AbstractList
<String
>) msg
.obj
;
374 if (msg
.obj
!= null
) {
375 Iterator
<String
> it
= requestedDownloads
.iterator();
376 while (it
.hasNext()) {
377 String next
= it
.next();
378 mService
.downloadFile(next
);
381 Log_OC
.d(TAG
, "Stopping after command with id " + msg
.arg1
);
382 mService
.stopSelf(msg
.arg1
);
388 * Core download method: requests a file to download and stores it.
390 * @param downloadKey Key to access the download to perform, contained in mPendingDownloads
392 private void downloadFile(String downloadKey
) {
394 mCurrentDownload
= mPendingDownloads
.get(downloadKey
);
396 if (mCurrentDownload
!= null
) {
397 // Detect if the account exists
398 if (AccountUtils
.exists(mCurrentDownload
.getAccount(), getApplicationContext())) {
399 Log_OC
.d(TAG
, "Account " + mCurrentDownload
.getAccount().name
+ " exists");
401 notifyDownloadStart(mCurrentDownload
);
403 RemoteOperationResult downloadResult
= null
;
405 /// prepare client object to send the request to the ownCloud server
406 if (mCurrentAccount
== null
||
407 !mCurrentAccount
.equals(mCurrentDownload
.getAccount())) {
408 mCurrentAccount
= mCurrentDownload
.getAccount();
409 mStorageManager
= new FileDataStorageManager(
413 } // else, reuse storage manager from previous operation
415 // always get client from client manager, to get fresh credentials in case
417 OwnCloudAccount ocAccount
= new OwnCloudAccount(mCurrentAccount
, this);
418 mDownloadClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
419 getClientFor(ocAccount
, this);
422 /// perform the download
423 downloadResult
= mCurrentDownload
.execute(mDownloadClient
);
424 if (downloadResult
.isSuccess()) {
425 saveDownloadedFile();
428 } catch (Exception e
) {
429 Log_OC
.e(TAG
, "Error downloading", e
);
430 downloadResult
= new RemoteOperationResult(e
);
433 Pair
<DownloadFileOperation
, String
> removeResult
=
434 mPendingDownloads
.removePayload(mCurrentAccount
,
435 mCurrentDownload
.getRemotePath());
438 notifyDownloadResult(mCurrentDownload
, downloadResult
);
440 sendBroadcastDownloadFinished(mCurrentDownload
, downloadResult
, removeResult
.second
);
444 // Cancel the transfer
445 Log_OC
.d(TAG
, "Account " + mCurrentDownload
.getAccount().toString() +
447 cancelDownloadsForAccount(mCurrentDownload
.getAccount());
455 * Updates the OC File after a successful download.
457 * TODO move to DownloadFileOperation
459 private void saveDownloadedFile() {
460 OCFile file
= mStorageManager
.getFileById(mCurrentDownload
.getFile().getFileId());
461 long syncDate
= System
.currentTimeMillis();
462 file
.setLastSyncDateForProperties(syncDate
);
463 file
.setLastSyncDateForData(syncDate
);
464 file
.setNeedsUpdateThumbnail(true
);
465 file
.setModificationTimestamp(mCurrentDownload
.getModificationTimestamp());
466 file
.setModificationTimestampAtLastSyncForData(mCurrentDownload
.getModificationTimestamp());
467 file
.setEtag(mCurrentDownload
.getEtag());
468 file
.setMimetype(mCurrentDownload
.getMimeType());
469 file
.setStoragePath(mCurrentDownload
.getSavePath());
470 file
.setFileLength((new File(mCurrentDownload
.getSavePath()).length()));
471 file
.setRemoteId(mCurrentDownload
.getFile().getRemoteId());
472 mStorageManager
.saveFile(file
);
473 mStorageManager
.triggerMediaScan(file
.getStoragePath());
474 mStorageManager
.saveConflict(file
, false
);
478 * Creates a status notification to show the download progress
480 * @param download Download operation starting.
482 private void notifyDownloadStart(DownloadFileOperation download
) {
483 /// create status notification with a progress bar
485 mNotificationBuilder
=
486 NotificationBuilderWithProgressBar
.newNotificationBuilderWithProgressBar(this);
488 .setSmallIcon(R
.drawable
.notification_icon
)
489 .setTicker(getString(R
.string
.downloader_download_in_progress_ticker
))
490 .setContentTitle(getString(R
.string
.downloader_download_in_progress_ticker
))
492 .setProgress(100, 0, download
.getSize() < 0)
494 String
.format(getString(R
.string
.downloader_download_in_progress_content
), 0,
495 new File(download
.getSavePath()).getName())
498 /// includes a pending intent in the notification showing the details view of the file
499 Intent showDetailsIntent
= null
;
500 if (PreviewImageFragment
.canBePreviewed(download
.getFile())) {
501 showDetailsIntent
= new Intent(this, PreviewImageActivity
.class);
503 showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
505 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, download
.getFile());
506 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, download
.getAccount());
507 showDetailsIntent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
509 mNotificationBuilder
.setContentIntent(PendingIntent
.getActivity(
510 this, (int) System
.currentTimeMillis(), showDetailsIntent
, 0
513 mNotificationManager
.notify(R
.string
.downloader_download_in_progress_ticker
, mNotificationBuilder
.build());
518 * Callback method to update the progress bar in the status notification.
521 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
,
522 long totalToTransfer
, String filePath
) {
523 int percent
= (int) (100.0 * ((double) totalTransferredSoFar
) / ((double) totalToTransfer
));
524 if (percent
!= mLastPercent
) {
525 mNotificationBuilder
.setProgress(100, percent
, totalToTransfer
< 0);
526 String fileName
= filePath
.substring(filePath
.lastIndexOf(FileUtils
.PATH_SEPARATOR
) + 1);
527 String text
= String
.format(getString(R
.string
.downloader_download_in_progress_content
), percent
, fileName
);
528 mNotificationBuilder
.setContentText(text
);
529 mNotificationManager
.notify(R
.string
.downloader_download_in_progress_ticker
, mNotificationBuilder
.build());
531 mLastPercent
= percent
;
536 * Updates the status notification with the result of a download operation.
538 * @param downloadResult Result of the download operation.
539 * @param download Finished download operation
541 private void notifyDownloadResult(DownloadFileOperation download
,
542 RemoteOperationResult downloadResult
) {
543 mNotificationManager
.cancel(R
.string
.downloader_download_in_progress_ticker
);
544 if (!downloadResult
.isCancelled()) {
545 int tickerId
= (downloadResult
.isSuccess()) ? R
.string
.downloader_download_succeeded_ticker
:
546 R
.string
.downloader_download_failed_ticker
;
548 boolean needsToUpdateCredentials
= (
549 downloadResult
.getCode() == ResultCode
.UNAUTHORIZED
||
550 downloadResult
.isIdPRedirection()
552 tickerId
= (needsToUpdateCredentials
) ?
553 R
.string
.downloader_download_failed_credentials_error
: tickerId
;
556 .setTicker(getString(tickerId
))
557 .setContentTitle(getString(tickerId
))
560 .setProgress(0, 0, false
);
562 if (needsToUpdateCredentials
) {
564 // let the user update credentials with one click
565 Intent updateAccountCredentials
= new Intent(this, AuthenticatorActivity
.class);
566 updateAccountCredentials
.putExtra(AuthenticatorActivity
.EXTRA_ACCOUNT
,
567 download
.getAccount());
568 updateAccountCredentials
.putExtra(
569 AuthenticatorActivity
.EXTRA_ACTION
,
570 AuthenticatorActivity
.ACTION_UPDATE_EXPIRED_TOKEN
572 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
573 updateAccountCredentials
.addFlags(Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
);
574 updateAccountCredentials
.addFlags(Intent
.FLAG_FROM_BACKGROUND
);
576 .setContentIntent(PendingIntent
.getActivity(
577 this, (int) System
.currentTimeMillis(), updateAccountCredentials
,
578 PendingIntent
.FLAG_ONE_SHOT
));
581 // TODO put something smart in showDetailsIntent
582 Intent showDetailsIntent
= new Intent();
584 .setContentIntent(PendingIntent
.getActivity(
585 this, (int) System
.currentTimeMillis(), showDetailsIntent
, 0));
588 mNotificationBuilder
.setContentText(
589 ErrorMessageAdapter
.getErrorCauseMessage(downloadResult
, download
,
592 mNotificationManager
.notify(tickerId
, mNotificationBuilder
.build());
594 // Remove success notification
595 if (downloadResult
.isSuccess()) {
596 // Sleep 2 seconds, so show the notification before remove it
597 NotificationDelayer
.cancelWithDelay(
598 mNotificationManager
,
599 R
.string
.downloader_download_succeeded_ticker
,
608 * Sends a broadcast when a download finishes in order to the interested activities can
611 * @param download Finished download operation
612 * @param downloadResult Result of the download operation
613 * @param unlinkedFromRemotePath Path in the downloads tree where the download was unlinked from
615 private void sendBroadcastDownloadFinished(
616 DownloadFileOperation download
,
617 RemoteOperationResult downloadResult
,
618 String unlinkedFromRemotePath
) {
620 Intent end
= new Intent(getDownloadFinishMessage());
621 end
.putExtra(EXTRA_DOWNLOAD_RESULT
, downloadResult
.isSuccess());
622 end
.putExtra(ACCOUNT_NAME
, download
.getAccount().name
);
623 end
.putExtra(EXTRA_REMOTE_PATH
, download
.getRemotePath());
624 end
.putExtra(EXTRA_FILE_PATH
, download
.getSavePath());
625 if (unlinkedFromRemotePath
!= null
) {
626 end
.putExtra(EXTRA_LINKED_TO_PATH
, unlinkedFromRemotePath
);
628 sendStickyBroadcast(end
);
633 * Sends a broadcast when a new download is added to the queue.
635 * @param download Added download operation
636 * @param linkedToRemotePath Path in the downloads tree where the download was linked to
638 private void sendBroadcastNewDownload(DownloadFileOperation download
,
639 String linkedToRemotePath
) {
640 Intent added
= new Intent(getDownloadAddedMessage());
641 added
.putExtra(ACCOUNT_NAME
, download
.getAccount().name
);
642 added
.putExtra(EXTRA_REMOTE_PATH
, download
.getRemotePath());
643 added
.putExtra(EXTRA_FILE_PATH
, download
.getSavePath());
644 added
.putExtra(EXTRA_LINKED_TO_PATH
, linkedToRemotePath
);
645 sendStickyBroadcast(added
);
649 * Remove downloads of an account
651 * @param account Downloads account to remove
653 private void cancelDownloadsForAccount(Account account
) {
654 // Cancel pending downloads
655 mPendingDownloads
.remove(account
);