Copyright note fixes
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.files.services;
20
21 import java.io.File;
22 import java.util.AbstractList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Vector;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import com.owncloud.android.datamodel.FileDataStorageManager;
31 import com.owncloud.android.datamodel.OCFile;
32 import eu.alefzero.webdav.OnDatatransferProgressListener;
33
34 import com.owncloud.android.network.OwnCloudClientUtils;
35 import com.owncloud.android.operations.DownloadFileOperation;
36 import com.owncloud.android.operations.RemoteOperationResult;
37 import com.owncloud.android.ui.activity.FileDetailActivity;
38 import com.owncloud.android.ui.fragment.FileDetailFragment;
39 import com.owncloud.android.ui.preview.PreviewImageActivity;
40 import com.owncloud.android.ui.preview.PreviewImageFragment;
41
42 import android.accounts.Account;
43 import android.app.Notification;
44 import android.app.NotificationManager;
45 import android.app.PendingIntent;
46 import android.app.Service;
47 import android.content.Intent;
48 import android.os.Binder;
49 import android.os.Handler;
50 import android.os.HandlerThread;
51 import android.os.IBinder;
52 import android.os.Looper;
53 import android.os.Message;
54 import android.os.Process;
55 import android.widget.RemoteViews;
56
57 import com.owncloud.android.Log_OC;
58 import com.owncloud.android.R;
59 import eu.alefzero.webdav.WebdavClient;
60
61 public class FileDownloader extends Service implements OnDatatransferProgressListener {
62
63 public static final String EXTRA_ACCOUNT = "ACCOUNT";
64 public static final String EXTRA_FILE = "FILE";
65
66 public static final String DOWNLOAD_ADDED_MESSAGE = "DOWNLOAD_ADDED";
67 public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";
68 public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";
69 public static final String EXTRA_FILE_PATH = "FILE_PATH";
70 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
71 public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
72
73 private static final String TAG = "FileDownloader";
74
75 private Looper mServiceLooper;
76 private ServiceHandler mServiceHandler;
77 private IBinder mBinder;
78 private WebdavClient mDownloadClient = null;
79 private Account mLastAccount = null;
80 private FileDataStorageManager mStorageManager;
81
82 private ConcurrentMap<String, DownloadFileOperation> mPendingDownloads = new ConcurrentHashMap<String, DownloadFileOperation>();
83 private DownloadFileOperation mCurrentDownload = null;
84
85 private NotificationManager mNotificationManager;
86 private Notification mNotification;
87 private int mLastPercent;
88
89
90 /**
91 * Builds a key for mPendingDownloads from the account and file to download
92 *
93 * @param account Account where the file to download is stored
94 * @param file File to download
95 */
96 private String buildRemoteName(Account account, OCFile file) {
97 return account.name + file.getRemotePath();
98 }
99
100
101 /**
102 * Service initialization
103 */
104 @Override
105 public void onCreate() {
106 super.onCreate();
107 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
108 HandlerThread thread = new HandlerThread("FileDownloaderThread",
109 Process.THREAD_PRIORITY_BACKGROUND);
110 thread.start();
111 mServiceLooper = thread.getLooper();
112 mServiceHandler = new ServiceHandler(mServiceLooper, this);
113 mBinder = new FileDownloaderBinder();
114 }
115
116
117 /**
118 * Entry point to add one or several files to the queue of downloads.
119 *
120 * New downloads are added calling to startService(), resulting in a call to this method. This ensures the service will keep on working
121 * although the caller activity goes away.
122 */
123 @Override
124 public int onStartCommand(Intent intent, int flags, int startId) {
125 if ( !intent.hasExtra(EXTRA_ACCOUNT) ||
126 !intent.hasExtra(EXTRA_FILE)
127 /*!intent.hasExtra(EXTRA_FILE_PATH) ||
128 !intent.hasExtra(EXTRA_REMOTE_PATH)*/
129 ) {
130 Log_OC.e(TAG, "Not enough information provided in intent");
131 return START_NOT_STICKY;
132 }
133 Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
134 OCFile file = intent.getParcelableExtra(EXTRA_FILE);
135
136 AbstractList<String> requestedDownloads = new Vector<String>(); // dvelasco: now this always contains just one element, but that can change in a near future (download of multiple selection)
137 String downloadKey = buildRemoteName(account, file);
138 try {
139 DownloadFileOperation newDownload = new DownloadFileOperation(account, file);
140 mPendingDownloads.putIfAbsent(downloadKey, newDownload);
141 newDownload.addDatatransferProgressListener(this);
142 newDownload.addDatatransferProgressListener((FileDownloaderBinder)mBinder);
143 requestedDownloads.add(downloadKey);
144 sendBroadcastNewDownload(newDownload);
145
146 } catch (IllegalArgumentException e) {
147 Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
148 return START_NOT_STICKY;
149 }
150
151 if (requestedDownloads.size() > 0) {
152 Message msg = mServiceHandler.obtainMessage();
153 msg.arg1 = startId;
154 msg.obj = requestedDownloads;
155 mServiceHandler.sendMessage(msg);
156 }
157
158 return START_NOT_STICKY;
159 }
160
161
162 /**
163 * Provides a binder object that clients can use to perform operations on the queue of downloads, excepting the addition of new files.
164 *
165 * Implemented to perform cancellation, pause and resume of existing downloads.
166 */
167 @Override
168 public IBinder onBind(Intent arg0) {
169 return mBinder;
170 }
171
172
173 /**
174 * Called when ALL the bound clients were onbound.
175 */
176 @Override
177 public boolean onUnbind(Intent intent) {
178 ((FileDownloaderBinder)mBinder).clearListeners();
179 return false; // not accepting rebinding (default behaviour)
180 }
181
182
183 /**
184 * Binder to let client components to perform operations on the queue of downloads.
185 *
186 * It provides by itself the available operations.
187 */
188 public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {
189
190 /**
191 * Map of listeners that will be reported about progress of downloads from a {@link FileDownloaderBinder} instance
192 */
193 private Map<String, OnDatatransferProgressListener> mBoundListeners = new HashMap<String, OnDatatransferProgressListener>();
194
195
196 /**
197 * Cancels a pending or current download of a remote file.
198 *
199 * @param account Owncloud account where the remote file is stored.
200 * @param file A file in the queue of pending downloads
201 */
202 public void cancel(Account account, OCFile file) {
203 DownloadFileOperation download = null;
204 synchronized (mPendingDownloads) {
205 download = mPendingDownloads.remove(buildRemoteName(account, file));
206 }
207 if (download != null) {
208 download.cancel();
209 }
210 }
211
212
213 public void clearListeners() {
214 mBoundListeners.clear();
215 }
216
217
218 /**
219 * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download.
220 *
221 * If 'file' is a directory, returns 'true' if some of its descendant files is downloading or waiting to download.
222 *
223 * @param account Owncloud account where the remote file is stored.
224 * @param file A file that could be in the queue of downloads.
225 */
226 public boolean isDownloading(Account account, OCFile file) {
227 if (account == null || file == null) return false;
228 String targetKey = buildRemoteName(account, file);
229 synchronized (mPendingDownloads) {
230 if (file.isDirectory()) {
231 // this can be slow if there are many downloads :(
232 Iterator<String> it = mPendingDownloads.keySet().iterator();
233 boolean found = false;
234 while (it.hasNext() && !found) {
235 found = it.next().startsWith(targetKey);
236 }
237 return found;
238 } else {
239 return (mPendingDownloads.containsKey(targetKey));
240 }
241 }
242 }
243
244
245 /**
246 * Adds a listener interested in the progress of the download for a concrete file.
247 *
248 * @param listener Object to notify about progress of transfer.
249 * @param account ownCloud account holding the file of interest.
250 * @param file {@link OCfile} of interest for listener.
251 */
252 public void addDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
253 if (account == null || file == null || listener == null) return;
254 String targetKey = buildRemoteName(account, file);
255 mBoundListeners.put(targetKey, listener);
256 }
257
258
259
260 /**
261 * Removes a listener interested in the progress of the download for a concrete file.
262 *
263 * @param listener Object to notify about progress of transfer.
264 * @param account ownCloud account holding the file of interest.
265 * @param file {@link OCfile} of interest for listener.
266 */
267 public void removeDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {
268 if (account == null || file == null || listener == null) return;
269 String targetKey = buildRemoteName(account, file);
270 if (mBoundListeners.get(targetKey) == listener) {
271 mBoundListeners.remove(targetKey);
272 }
273 }
274
275
276 @Override
277 public void onTransferProgress(long progressRate) {
278 // old way, should not be in use any more
279 }
280
281
282 @Override
283 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,
284 String fileName) {
285 String key = buildRemoteName(mCurrentDownload.getAccount(), mCurrentDownload.getFile());
286 OnDatatransferProgressListener boundListener = mBoundListeners.get(key);
287 if (boundListener != null) {
288 boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);
289 }
290 }
291
292 }
293
294
295 /**
296 * Download worker. Performs the pending downloads in the order they were requested.
297 *
298 * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
299 */
300 private static class ServiceHandler extends Handler {
301 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
302 FileDownloader mService;
303 public ServiceHandler(Looper looper, FileDownloader service) {
304 super(looper);
305 if (service == null)
306 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
307 mService = service;
308 }
309
310 @Override
311 public void handleMessage(Message msg) {
312 @SuppressWarnings("unchecked")
313 AbstractList<String> requestedDownloads = (AbstractList<String>) msg.obj;
314 if (msg.obj != null) {
315 Iterator<String> it = requestedDownloads.iterator();
316 while (it.hasNext()) {
317 mService.downloadFile(it.next());
318 }
319 }
320 mService.stopSelf(msg.arg1);
321 }
322 }
323
324
325
326 /**
327 * Core download method: requests a file to download and stores it.
328 *
329 * @param downloadKey Key to access the download to perform, contained in mPendingDownloads
330 */
331 private void downloadFile(String downloadKey) {
332
333 synchronized(mPendingDownloads) {
334 mCurrentDownload = mPendingDownloads.get(downloadKey);
335 }
336
337 if (mCurrentDownload != null) {
338
339 notifyDownloadStart(mCurrentDownload);
340
341 /// prepare client object to send the request to the ownCloud server
342 if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {
343 mLastAccount = mCurrentDownload.getAccount();
344 mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
345 mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
346 }
347
348 /// perform the download
349 RemoteOperationResult downloadResult = null;
350 try {
351 downloadResult = mCurrentDownload.execute(mDownloadClient);
352 if (downloadResult.isSuccess()) {
353 saveDownloadedFile();
354 }
355
356 } finally {
357 synchronized(mPendingDownloads) {
358 mPendingDownloads.remove(downloadKey);
359 }
360 }
361
362
363 /// notify result
364 notifyDownloadResult(mCurrentDownload, downloadResult);
365
366 sendBroadcastDownloadFinished(mCurrentDownload, downloadResult);
367 }
368 }
369
370
371 /**
372 * Updates the OC File after a successful download.
373 */
374 private void saveDownloadedFile() {
375 OCFile file = mCurrentDownload.getFile();
376 long syncDate = System.currentTimeMillis();
377 file.setLastSyncDateForProperties(syncDate);
378 file.setLastSyncDateForData(syncDate);
379 file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp());
380 file.setModificationTimestampAtLastSyncForData(mCurrentDownload.getModificationTimestamp());
381 // file.setEtag(mCurrentDownload.getEtag()); // TODO Etag, where available
382 file.setMimetype(mCurrentDownload.getMimeType());
383 file.setStoragePath(mCurrentDownload.getSavePath());
384 file.setFileLength((new File(mCurrentDownload.getSavePath()).length()));
385 mStorageManager.saveFile(file);
386 }
387
388
389 /**
390 * Creates a status notification to show the download progress
391 *
392 * @param download Download operation starting.
393 */
394 private void notifyDownloadStart(DownloadFileOperation download) {
395 /// create status notification with a progress bar
396 mLastPercent = 0;
397 mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis());
398 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
399 mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
400 mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, download.getSize() < 0);
401 mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), 0, new File(download.getSavePath()).getName()));
402 mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
403
404 /// includes a pending intent in the notification showing the details view of the file
405 Intent showDetailsIntent = null;
406 if (PreviewImageFragment.canBePreviewed(download.getFile())) {
407 showDetailsIntent = new Intent(this, PreviewImageActivity.class);
408 } else {
409 showDetailsIntent = new Intent(this, FileDetailActivity.class);
410 }
411 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());
412 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());
413 showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
414 mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);
415
416 mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification);
417 }
418
419
420 /**
421 * Callback method to update the progress bar in the status notification.
422 */
423 @Override
424 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
425 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
426 if (percent != mLastPercent) {
427 mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, totalToTransfer < 0);
428 String text = String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName);
429 mNotification.contentView.setTextViewText(R.id.status_text, text);
430 mNotificationManager.notify(R.string.downloader_download_in_progress_ticker, mNotification);
431 }
432 mLastPercent = percent;
433 }
434
435
436 /**
437 * Callback method to update the progress bar in the status notification (old version)
438 */
439 @Override
440 public void onTransferProgress(long progressRate) {
441 // NOTHING TO DO HERE ANYMORE
442 }
443
444
445 /**
446 * Updates the status notification with the result of a download operation.
447 *
448 * @param downloadResult Result of the download operation.
449 * @param download Finished download operation
450 */
451 private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
452 mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
453 if (!downloadResult.isCancelled()) {
454 int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
455 int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;
456 Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis());
457 finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;
458 Intent showDetailsIntent = null;
459 if (downloadResult.isSuccess()) {
460 if (PreviewImageFragment.canBePreviewed(download.getFile())) {
461 showDetailsIntent = new Intent(this, PreviewImageActivity.class);
462 } else {
463 showDetailsIntent = new Intent(this, FileDetailActivity.class);
464 }
465 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());
466 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());
467 showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
468
469 } else {
470 // TODO put something smart in showDetailsIntent
471 showDetailsIntent = new Intent();
472 }
473 finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);
474 finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent);
475 mNotificationManager.notify(tickerId, finalNotification);
476 }
477 }
478
479
480 /**
481 * Sends a broadcast when a download finishes in order to the interested activities can update their view
482 *
483 * @param download Finished download operation
484 * @param downloadResult Result of the download operation
485 */
486 private void sendBroadcastDownloadFinished(DownloadFileOperation download, RemoteOperationResult downloadResult) {
487 Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
488 end.putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess());
489 end.putExtra(ACCOUNT_NAME, download.getAccount().name);
490 end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
491 end.putExtra(EXTRA_FILE_PATH, download.getSavePath());
492 sendStickyBroadcast(end);
493 }
494
495
496 /**
497 * Sends a broadcast when a new download is added to the queue.
498 *
499 * @param download Added download operation
500 */
501 private void sendBroadcastNewDownload(DownloadFileOperation download) {
502 Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);
503 added.putExtra(ACCOUNT_NAME, download.getAccount().name);
504 added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
505 added.putExtra(EXTRA_FILE_PATH, download.getSavePath());
506 sendStickyBroadcast(added);
507 }
508
509 }