Fixed: download of the first image in a folder is now started when clicked on
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index 57667aa..3311822 100644 (file)
@@ -21,7 +21,6 @@ package com.owncloud.android.ui.activity;
 import java.io.File;\r
 \r
 import android.accounts.Account;\r
-import android.app.Activity;\r
 import android.app.AlertDialog;\r
 import android.app.ProgressDialog;\r
 import android.app.AlertDialog.Builder;\r
@@ -92,6 +91,7 @@ import com.owncloud.android.ui.fragment.FileDetailFragment;
 import com.owncloud.android.ui.fragment.FileFragment;\r
 import com.owncloud.android.ui.fragment.OCFileListFragment;\r
 import com.owncloud.android.ui.preview.PreviewImageActivity;\r
+import com.owncloud.android.ui.preview.PreviewImageFragment;\r
 import com.owncloud.android.ui.preview.PreviewMediaFragment;\r
 \r
 import com.owncloud.android.R;\r
@@ -101,11 +101,11 @@ import eu.alefzero.webdav.WebdavClient;
  * Displays, what files the user has available in his ownCloud.\r
  * \r
  * @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\r
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
-    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {\r
+    OCFileListFragment.ContainerActivity, FileFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {\r
     \r
     private ArrayAdapter<String> mDirectories;\r
     private OCFile mCurrentDir = null;\r
@@ -587,7 +587,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             \r
             // Listen for download messages\r
             IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);\r
-            //downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
+            downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
             mDownloadFinishReceiver = new DownloadFinishReceiver();\r
             registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
         \r
@@ -982,42 +982,78 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     \r
     \r
     /**\r
-     * Once the file download has finished -> update view\r
+     * Class waiting for broadcast events from the {@link FielDownloader} service.\r
+     * \r
+     * Updates the UI when a download is started or finished, provided that it is relevant for the\r
+     * current folder.\r
      */\r
     private class DownloadFinishReceiver extends BroadcastReceiver {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
+            boolean sameAccount = isSameAccount(context, intent);\r
             String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-            if (accountName != null && AccountUtils.getCurrentOwnCloudAccount(context) != null) {\r
-                boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);\r
-                boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
-                if (sameAccount && isDescendant) {\r
-                    OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
-                    if (fileListFragment != null) { \r
-                        fileListFragment.listDirectory();\r
-                        if (    mWaitingToPreview != null && \r
-                                mWaitingToPreview.getRemotePath().equals(downloadedRemotePath) && \r
-                                intent.getAction().equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)    ) {\r
-                            \r
-                            Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-                            if (fragment != null && fragment instanceof FileDetailFragment ) {\r
-                                FileDetailFragment detailFragment = (FileDetailFragment) fragment;\r
-                                if (detailFragment.getFile().getRemotePath().equals(downloadedRemotePath)) {\r
-                                    detailFragment.listenForTransferProgress();\r
-                                    detailFragment.updateFileDetails(true);\r
-                                }\r
-                            }\r
-                        }\r
-                    }\r
+            boolean isDescendant = isDescendant(downloadedRemotePath);\r
+            \r
+            if (sameAccount && isDescendant) {\r
+                updateLeftPanel();\r
+                if (mDualPane) {\r
+                    updateRightPanel(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));\r
                 }\r
             }\r
+            \r
+            removeStickyBroadcast(intent);\r
+        }\r
+\r
+        private boolean isDescendant(String downloadedRemotePath) {\r
+            return (mCurrentDir != null && downloadedRemotePath != null && downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
+        }\r
+\r
+        private boolean isSameAccount(Context context, Intent intent) {\r
+            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
+            return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));\r
         }\r
     }\r
     \r
     \r
-    \r
-    \r
+    protected void updateLeftPanel() {\r
+        OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+        if (fileListFragment != null) { \r
+            fileListFragment.listDirectory();\r
+        }\r
+    }\r
+\r
+    protected void updateRightPanel(String downloadEvent, String downloadedRemotePath, boolean success) {\r
+        Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+        boolean waitedPreview = (mWaitingToPreview != null && mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));\r
+        if (fragment != null && fragment instanceof FileDetailFragment) {\r
+            FileDetailFragment detailsFragment = (FileDetailFragment) fragment;\r
+            OCFile fileInFragment = detailsFragment.getFile();\r
+            if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {\r
+                // the user browsed to other file ; forget the automatic preview \r
+                mWaitingToPreview = null;\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {\r
+                // grant that the right panel updates the progress bar\r
+                detailsFragment.listenForTransferProgress();\r
+                detailsFragment.updateFileDetails(true, false);\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+                //  update the right panel \r
+                if (success && waitedPreview) {\r
+                    mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId());   // update the file from database, for the local storage path\r
+                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                    transaction.replace(R.id.file_details_container, new PreviewMediaFragment(mWaitingToPreview, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                    transaction.commit();\r
+                    mWaitingToPreview = null;\r
+                    \r
+                } else {\r
+                    detailsFragment.updateFileDetails(false, (success));\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
     /**\r
      * {@inheritDoc}\r
      */\r
@@ -1026,7 +1062,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         return mStorageManager;\r
     }\r
     \r
-    \r
+\r
     /**\r
      * {@inheritDoc}\r
      */\r
@@ -1053,15 +1089,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
      */\r
     @Override\r
     public void onFileClick(OCFile file) {\r
-\r
-        if (file != null && PreviewMediaFragment.canBePreviewed(file)) {\r
-            if (file.isImage()) {\r
-                // preview image - it handles the download, if needed\r
-                startPreviewImage(file);\r
-                \r
-            } else if (file.isDown()) {\r
+        if (file != null && PreviewImageFragment.canBePreviewed(file)) {\r
+            // preview image - it handles the download, if needed\r
+            startPreviewImage(file);\r
+            \r
+        } else if (file != null && PreviewMediaFragment.canBePreviewed(file)) {\r
+            if (file.isDown()) {\r
                 // general preview\r
-                startOtherPreview(file);\r
+                startMediaPreview(file);\r
                 \r
             } else {\r
                 // automatic download, preview on finish\r
@@ -1081,7 +1116,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         startActivity(showDetailsIntent);\r
     }\r
     \r
-    private void startOtherPreview(OCFile file) {\r
+    private void startMediaPreview(OCFile file) {\r
         if (mDualPane) {\r
             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
             transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
@@ -1191,7 +1226,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 if (fragment != null && fragment instanceof FileDetailFragment) {\r
                     FileDetailFragment detailFragment = (FileDetailFragment)fragment;\r
                     detailFragment.listenForTransferProgress();\r
-                    detailFragment.updateFileDetails(false);\r
+                    detailFragment.updateFileDetails(false, false);\r
                 }\r
             }\r
         }\r
@@ -1374,7 +1409,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 if (downloading || uploading) {\r
                     ((FileDetailFragment)details).updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));\r
                 } else {\r
-                    ((FileDetailFragment)details).updateFileDetails(downloading || uploading);\r
+                    ((FileDetailFragment)details).updateFileDetails(false, true);\r
                 }\r
             }\r
         }\r
@@ -1400,6 +1435,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
     @Override\r
     public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
+        /*\r
         if (success) {\r
             if (mWaitingToPreview != null) {\r
                 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
@@ -1409,6 +1445,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             }\r
         }\r
         mDownloadFinishReceiver.onReceive(this, intent);\r
+         */\r
     }\r
 \r
 \r