- /// notify result\r
- mNotificationMngr.cancel(R.string.downloader_download_in_progress_ticker);\r
- int tickerId = (downloadResult) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;\r
- int contentId = (downloadResult) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;\r
- Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis());\r
- finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;\r
- // TODO put something smart in the contentIntent below\r
- finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);\r
- finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), tmpFile.getName()), finalNotification.contentIntent);\r
- mNotificationMngr.notify(tickerId, finalNotification);\r
+ /**\r
+ * Adds a listener interested in the progress of the download for a concrete file.\r
+ * \r
+ * @param listener Object to notify about progress of transfer. \r
+ * @param account ownCloud account holding the file of interest.\r
+ * @param file {@link OCfile} of interest for listener. \r
+ */\r
+ public void addDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {\r
+ if (account == null || file == null || listener == null) return;\r
+ String targetKey = buildRemoteName(account, file);\r
+ mBoundListeners.put(targetKey, listener);\r
+ }\r
+ \r
+ \r
+ \r
+ /**\r
+ * Removes a listener interested in the progress of the download for a concrete file.\r
+ * \r
+ * @param listener Object to notify about progress of transfer. \r
+ * @param account ownCloud account holding the file of interest.\r
+ * @param file {@link OCfile} of interest for listener. \r
+ */\r
+ public void removeDatatransferProgressListener (OnDatatransferProgressListener listener, Account account, OCFile file) {\r
+ if (account == null || file == null || listener == null) return;\r
+ String targetKey = buildRemoteName(account, file);\r
+ if (mBoundListeners.get(targetKey) == listener) {\r
+ mBoundListeners.remove(targetKey);\r
+ }\r
+ }\r
+\r
+\r
+ @Override\r
+ public void onTransferProgress(long progressRate) {\r
+ // old way, should not be in use any more\r
+ }\r
+\r
+\r
+ @Override\r
+ public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,\r
+ String fileName) {\r
+ String key = buildRemoteName(mCurrentDownload.getAccount(), mCurrentDownload.getFile());\r
+ OnDatatransferProgressListener boundListener = mBoundListeners.get(key);\r
+ if (boundListener != null) {\r
+ boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);\r
+ }\r
+ }\r
+ \r
+ }\r
+ \r
+ \r
+ /** \r
+ * Download worker. Performs the pending downloads in the order they were requested. \r
+ * \r
+ * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}. \r
+ */\r
+ private static class ServiceHandler extends Handler {\r
+ // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak\r
+ FileDownloader mService;\r
+ public ServiceHandler(Looper looper, FileDownloader service) {\r
+ super(looper);\r
+ if (service == null)\r
+ throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");\r
+ mService = service;\r
+ }\r
+\r
+ @Override\r
+ public void handleMessage(Message msg) {\r
+ @SuppressWarnings("unchecked")\r
+ AbstractList<String> requestedDownloads = (AbstractList<String>) msg.obj;\r
+ if (msg.obj != null) {\r
+ Iterator<String> it = requestedDownloads.iterator();\r
+ while (it.hasNext()) {\r
+ mService.downloadFile(it.next());\r
+ }\r
+ }\r
+ mService.stopSelf(msg.arg1);\r
+ }\r
+ }\r
+ \r
+ \r
+\r
+ /**\r
+ * Core download method: requests a file to download and stores it.\r
+ * \r
+ * @param downloadKey Key to access the download to perform, contained in mPendingDownloads \r
+ */\r
+ private void downloadFile(String downloadKey) {\r
+ \r
+ synchronized(mPendingDownloads) {\r
+ mCurrentDownload = mPendingDownloads.get(downloadKey);\r
+ }\r
+ \r
+ if (mCurrentDownload != null) {\r
+ \r
+ notifyDownloadStart(mCurrentDownload);\r
+\r
+ /// prepare client object to send the request to the ownCloud server\r
+ if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
+ mLastAccount = mCurrentDownload.getAccount();\r
+ mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
+ mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());\r
+ }\r
+\r
+ /// perform the download\r
+ RemoteOperationResult downloadResult = null;\r
+ try {\r
+ downloadResult = mCurrentDownload.execute(mDownloadClient);\r
+ if (downloadResult.isSuccess()) {\r
+ saveDownloadedFile();\r
+ }\r
+ \r
+ } finally {\r
+ synchronized(mPendingDownloads) {\r
+ mPendingDownloads.remove(downloadKey);\r
+ }\r
+ }\r
+\r
+ \r
+ /// notify result\r
+ notifyDownloadResult(mCurrentDownload, downloadResult);\r