Merge branch 'develop' into feature_previews
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index ac70e39..aa5583d 100644 (file)
@@ -21,7 +21,9 @@ package com.owncloud.android.files.services;
 \r
 import java.io.File;\r
 import java.util.AbstractList;\r
+import java.util.HashMap;\r
 import java.util.Iterator;\r
+import java.util.Map;\r
 import java.util.Vector;\r
 import java.util.concurrent.ConcurrentHashMap;\r
 import java.util.concurrent.ConcurrentMap;\r
@@ -35,6 +37,8 @@ import com.owncloud.android.operations.DownloadFileOperation;
 import com.owncloud.android.operations.RemoteOperationResult;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
+import com.owncloud.android.ui.preview.PreviewImageActivity;\r
+import com.owncloud.android.ui.preview.PreviewImageFragment;\r
 \r
 import android.accounts.Account;\r
 import android.app.Notification;\r
@@ -52,6 +56,7 @@ import android.os.Process;
 import android.util.Log;\r
 import android.widget.RemoteViews;\r
 \r
+import com.owncloud.android.AccountUtils;\r
 import com.owncloud.android.R;\r
 import eu.alefzero.webdav.WebdavClient;\r
 \r
@@ -136,6 +141,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             DownloadFileOperation newDownload = new DownloadFileOperation(account, file); \r
             mPendingDownloads.putIfAbsent(downloadKey, newDownload);\r
             newDownload.addDatatransferProgressListener(this);\r
+            newDownload.addDatatransferProgressListener((FileDownloaderBinder)mBinder);\r
             requestedDownloads.add(downloadKey);\r
             sendBroadcastNewDownload(newDownload);\r
             \r
@@ -165,13 +171,29 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         return mBinder;\r
     }\r
 \r
+\r
+    /**\r
+     * Called when ALL the bound clients were onbound.\r
+     */\r
+    @Override\r
+    public boolean onUnbind(Intent intent) {\r
+        ((FileDownloaderBinder)mBinder).clearListeners();\r
+        return false;   // not accepting rebinding (default behaviour)\r
+    }\r
+\r
     \r
     /**\r
      *  Binder to let client components to perform operations on the queue of downloads.\r
      * \r
      *  It provides by itself the available operations.\r
      */\r
-    public class FileDownloaderBinder extends Binder {\r
+    public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {\r
+        \r
+        /** \r
+         * Map of listeners that will be reported about progress of downloads from a {@link FileDownloaderBinder} instance \r
+         */\r
+        private Map<String, OnDatatransferProgressListener> mBoundListeners = new HashMap<String, OnDatatransferProgressListener>();\r
+        \r
         \r
         /**\r
          * Cancels a pending or current download of a remote file.\r
@@ -190,6 +212,11 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }\r
         \r
         \r
+        public void clearListeners() {\r
+            mBoundListeners.clear();\r
+        }\r
+\r
+\r
         /**\r
          * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or waiting to download.\r
          * \r
@@ -215,6 +242,55 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                 }\r
             }\r
         }\r
+\r
+        \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
@@ -328,7 +404,12 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);\r
         \r
         /// includes a pending intent in the notification showing the details view of the file\r
-        Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);\r
+        Intent showDetailsIntent = null;\r
+        if (PreviewImageFragment.canBePreviewed(download.getFile())) {\r
+            showDetailsIntent = new Intent(this, PreviewImageActivity.class);\r
+        } else {\r
+            showDetailsIntent = new Intent(this, FileDetailActivity.class);\r
+        }\r
         showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());\r
         showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());\r
         showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
@@ -376,8 +457,22 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             int contentId = (downloadResult.isSuccess()) ? 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(), (int)System.currentTimeMillis(), new Intent(), 0);\r
+            Intent showDetailsIntent = null;\r
+            if (downloadResult.isSuccess()) {\r
+                if (PreviewImageFragment.canBePreviewed(download.getFile())) {\r
+                    showDetailsIntent = new Intent(this, PreviewImageActivity.class);\r
+                } else {\r
+                    showDetailsIntent = new Intent(this, FileDetailActivity.class);\r
+                }\r
+                showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, download.getFile());\r
+                showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, download.getAccount());\r
+                showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
+                \r
+            } else {\r
+                // TODO put something smart in showDetailsIntent\r
+                showDetailsIntent = new Intent();\r
+            }\r
+            finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), showDetailsIntent, 0);\r
             finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), new File(download.getSavePath()).getName()), finalNotification.contentIntent);\r
             mNotificationManager.notify(tickerId, finalNotification);\r
         }\r
@@ -407,8 +502,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      */\r
     private void sendBroadcastNewDownload(DownloadFileOperation download) {\r
         Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);\r
-        /*added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
-        added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/\r
+        added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
+        added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());\r
         added.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
         sendStickyBroadcast(added);\r
     }\r