Merge remote-tracking branch 'origin/master'
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
1 package com.owncloud.android.files.services;
2
3 import java.io.File;
4 import java.util.AbstractList;
5 import java.util.Iterator;
6 import java.util.Vector;
7 import java.util.concurrent.ConcurrentHashMap;
8 import java.util.concurrent.ConcurrentMap;
9
10 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
11 import eu.alefzero.webdav.OnDatatransferProgressListener;
12
13 import com.owncloud.android.network.OwnCloudClientUtils;
14 import com.owncloud.android.operations.DownloadFileOperation;
15 import com.owncloud.android.operations.RemoteOperationResult;
16
17 import android.accounts.Account;
18 import android.app.Notification;
19 import android.app.NotificationManager;
20 import android.app.PendingIntent;
21 import android.app.Service;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.os.Binder;
26 import android.os.Environment;
27 import android.os.Handler;
28 import android.os.HandlerThread;
29 import android.os.IBinder;
30 import android.os.Looper;
31 import android.os.Message;
32 import android.os.Process;
33 import android.util.Log;
34 import android.widget.RemoteViews;
35 import com.owncloud.android.R;
36 import eu.alefzero.webdav.WebdavClient;
37
38 public class FileDownloader extends Service implements OnDatatransferProgressListener {
39 public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";
40 public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";
41 public static final String EXTRA_ACCOUNT = "ACCOUNT";
42 public static final String EXTRA_FILE_PATH = "FILE_PATH";
43 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
44 public static final String EXTRA_FILE_SIZE = "FILE_SIZE";
45 public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
46
47 private static final String TAG = "FileDownloader";
48
49 private Looper mServiceLooper;
50 private ServiceHandler mServiceHandler;
51 private IBinder mBinder;
52 private WebdavClient mDownloadClient = null;
53 private Account mLastAccount = null;
54
55 //private AbstractList<Account> mAccounts = new Vector<Account>();
56 private ConcurrentMap<String, DownloadFileOperation> mPendingDownloads = new ConcurrentHashMap<String, DownloadFileOperation>();
57 private DownloadFileOperation mCurrentDownload = null;
58
59 /*
60 private Account mAccount;
61 private String mFilePath;
62 private String mRemotePath;
63 private long mTotalDownloadSize;
64 private long mCurrentDownloadSize;
65 */
66
67 private NotificationManager mNotificationMngr;
68 private Notification mNotification;
69 private int mLastPercent;
70
71
72 /**
73 * Static map with the files being download and the path to the temporal file were are download
74 */
75 //private static Set<String> mDownloadsInProgress = Collections.synchronizedSet(new HashSet<String>());
76
77 /**
78 * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading
79 */
80 /*public static boolean isDownloading(Account account, String remotePath) {
81 return (mDownloadsInProgress.contains(buildRemoteName(account.name, remotePath)));
82 }*/
83
84 /**
85 * Builds a key for mDownloadsInProgress from the accountName and remotePath
86 */
87 private static String buildRemoteName(String accountName, String remotePath) {
88 return accountName + remotePath;
89 }
90
91 public static final String getSavePath(String accountName) {
92 File sdCard = Environment.getExternalStorageDirectory();
93 return sdCard.getAbsolutePath() + "/owncloud/" + Uri.encode(accountName, "@");
94 // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
95 }
96
97 public static final String getTemporalPath(String accountName) {
98 File sdCard = Environment.getExternalStorageDirectory();
99 return sdCard.getAbsolutePath() + "/owncloud/tmp/" + Uri.encode(accountName, "@");
100 // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
101 }
102
103
104 /**
105 * Service initialization
106 */
107 @Override
108 public void onCreate() {
109 super.onCreate();
110 mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
111 HandlerThread thread = new HandlerThread("FileDownladerThread",
112 Process.THREAD_PRIORITY_BACKGROUND);
113 thread.start();
114 mServiceLooper = thread.getLooper();
115 mServiceHandler = new ServiceHandler(mServiceLooper);
116 mBinder = new FileDownloaderBinder();
117 }
118
119
120 /**
121 * Entry point to add one or several files to the queue of downloads.
122 *
123 * New downloads are added calling to startService(), resulting in a call to this method. This ensures the service will keep on working
124 * although the caller activity goes away.
125 */
126 @Override
127 public int onStartCommand(Intent intent, int flags, int startId) {
128 if ( !intent.hasExtra(EXTRA_ACCOUNT) ||
129 !intent.hasExtra(EXTRA_FILE_PATH) ||
130 !intent.hasExtra(EXTRA_REMOTE_PATH)
131 ) {
132 Log.e(TAG, "Not enough information provided in intent");
133 return START_NOT_STICKY;
134 }
135 Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
136 String filePath = intent.getStringExtra(EXTRA_FILE_PATH);
137 String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
138 long totalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1);
139
140 AbstractList<String> requestedDownloads = new Vector<String>(); // dvelasco: now this will always contain just one element, but that can change in a near future
141 String downloadKey = buildRemoteName(account.name, remotePath);
142 try {
143 DownloadFileOperation newDownload = new DownloadFileOperation(account, filePath, remotePath, (String)null, totalDownloadSize, false);
144 mPendingDownloads.putIfAbsent(downloadKey, newDownload);
145 newDownload.addDatatransferProgressListener(this);
146 requestedDownloads.add(downloadKey);
147
148 } catch (IllegalArgumentException e) {
149 Log.e(TAG, "Not enough information provided in intent: " + e.getMessage());
150 return START_NOT_STICKY;
151 }
152
153 if (requestedDownloads.size() > 0) {
154 Message msg = mServiceHandler.obtainMessage();
155 msg.arg1 = startId;
156 msg.obj = requestedDownloads;
157 mServiceHandler.sendMessage(msg);
158 }
159
160 return START_NOT_STICKY;
161 }
162
163
164 /**
165 * Provides a binder object that clients can use to perform operations on the queue of downloads, excepting the addition of new files.
166 *
167 * Implemented to perform cancellation, pause and resume of existing downloads.
168 */
169 @Override
170 public IBinder onBind(Intent arg0) {
171 return mBinder;
172 }
173
174
175 /**
176 * Binder to let client components to perform operations on the queue of downloads.
177 *
178 * It provides by itself the available operations.
179 */
180 public class FileDownloaderBinder extends Binder {
181
182 /**
183 * Cancels a pending or current download of a remote file.
184 *
185 * @param account Owncloud account where the remote file is stored.
186 * @param remotePath URL to the remote file in the queue of downloads.
187 */
188 public void cancel(Account account, String remotePath) {
189 synchronized (mPendingDownloads) {
190 DownloadFileOperation download = mPendingDownloads.remove(buildRemoteName(account.name, remotePath));
191 if (download != null) {
192 download.cancel();
193 }
194 }
195 }
196
197
198 /**
199 * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading
200 *
201 * @param account Owncloud account where the remote file is stored.
202 * @param remotePath URL to the remote file in the queue of downloads.
203 */
204 public boolean isDownloading(Account account, String remotePath) {
205 synchronized (mPendingDownloads) {
206 return (mPendingDownloads.containsKey(buildRemoteName(account.name, remotePath)));
207 }
208 }
209 }
210
211 /**
212 * Download worker. Performs the pending downloads in the order they were requested.
213 *
214 * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
215 */
216 private final class ServiceHandler extends Handler {
217 public ServiceHandler(Looper looper) {
218 super(looper);
219 }
220
221 @Override
222 public void handleMessage(Message msg) {
223 @SuppressWarnings("unchecked")
224 AbstractList<String> requestedDownloads = (AbstractList<String>) msg.obj;
225 if (msg.obj != null) {
226 Iterator<String> it = requestedDownloads.iterator();
227 while (it.hasNext()) {
228 downloadFile(it.next());
229 }
230 }
231 stopSelf(msg.arg1);
232 }
233 }
234
235
236
237 /**
238 * Core download method: requests a file to download and stores it.
239 *
240 * @param downloadKey Key to access the download to perform, contained in mPendingDownloads
241 */
242 private void downloadFile(String downloadKey) {
243
244 synchronized(mPendingDownloads) {
245 mCurrentDownload = mPendingDownloads.get(downloadKey);
246 }
247
248 if (mCurrentDownload != null) {
249
250 notifyDownloadStart(mCurrentDownload);
251
252 /// prepare client object to send the request to the ownCloud server
253 if (mDownloadClient == null || mLastAccount != mCurrentDownload.getAccount()) {
254 mLastAccount = mCurrentDownload.getAccount();
255 mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
256 }
257
258 /// perform the download
259 //mDownloadsInProgress.add(buildRemoteName(mLastAccount.name, mCurrentDownload.getRemotePath()));
260 RemoteOperationResult downloadResult = null;
261 File newLocalFile = null;
262 //try {
263 downloadResult = mCurrentDownload.execute(mDownloadClient);
264 if (downloadResult.isSuccess()) {
265 ContentValues cv = new ContentValues();
266 newLocalFile = new File(getSavePath(mCurrentDownload.getAccount().name) + mCurrentDownload.getLocalPath());
267 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newLocalFile.getAbsolutePath());
268 getContentResolver().update(
269 ProviderTableMeta.CONTENT_URI,
270 cv,
271 ProviderTableMeta.FILE_NAME + "=? AND "
272 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
273 new String[] {
274 mCurrentDownload.getLocalPath().substring(mCurrentDownload.getLocalPath().lastIndexOf('/') + 1),
275 mLastAccount.name });
276 }
277
278 /*} finally {
279 mDownloadsInProgress.remove(buildRemoteName(mLastAccount.name, mCurrentDownload.getRemotePath()));
280 }*/
281
282 mPendingDownloads.remove(downloadKey);
283
284 /// notify result
285 notifyDownloadResult(mCurrentDownload, downloadResult);
286
287 sendFinalBroadcast(mCurrentDownload, downloadResult, (downloadResult.isSuccess())? newLocalFile.getAbsolutePath():null);
288 }
289 }
290
291
292 /**
293 * Callback method to update the progress bar in the status notification.
294 */
295 @Override
296 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
297 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
298 if (percent != mLastPercent) {
299 mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, totalToTransfer == -1);
300 mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), percent, fileName));
301 mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification);
302 }
303 mLastPercent = percent;
304 }
305
306
307 /**
308 * Callback method to update the progress bar in the status notification (old version)
309 */
310 @Override
311 public void onTransferProgress(long progressRate) {
312 // NOTHING TO DO HERE ANYMORE
313 }
314
315
316 /**
317 * Creates a status notification to show the download progress
318 *
319 * @param download Download operation starting.
320 */
321 private void notifyDownloadStart(DownloadFileOperation download) {
322 /// create status notification to show the download progress
323 mLastPercent = 0;
324 mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis());
325 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
326 mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
327 mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, download.getSize() == -1);
328 mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), 0, new File(download.getLocalPath()).getName()));
329 mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
330 // TODO put something smart in the contentIntent below
331 mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
332 mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification);
333 }
334
335
336 /**
337 * Updates the status notification with the result of a download operation.
338 *
339 * @param downloadResult Result of the download operation.
340 * @param download Finished download operation
341 */
342 private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
343 mNotificationMngr.cancel(R.string.downloader_download_in_progress_ticker);
344 if (!downloadResult.isCancelled()) {
345 int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
346 int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;
347 Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis());
348 finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;
349 // TODO put something smart in the contentIntent below
350 finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
351 finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getLocalPath()).getName()), finalNotification.contentIntent);
352 mNotificationMngr.notify(tickerId, finalNotification);
353 }
354 }
355
356
357 /**
358 * Sends a broadcast in order to the interested activities can update their view
359 *
360 * @param download Finished download operation
361 * @param downloadResult Result of the download operation
362 * @param newFilePath Absolute path to the downloaded file
363 */
364 private void sendFinalBroadcast(DownloadFileOperation download, RemoteOperationResult downloadResult, String newFilePath) {
365 Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);
366 end.putExtra(EXTRA_DOWNLOAD_RESULT, downloadResult.isSuccess());
367 end.putExtra(ACCOUNT_NAME, download.getAccount().name);
368 end.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
369 if (downloadResult.isSuccess()) {
370 end.putExtra(EXTRA_FILE_PATH, newFilePath);
371 }
372 sendBroadcast(end);
373 }
374
375 }