Fixed update of list of files when downloads finish
authorDavid A. Velasco <dvelasco@solidgear.es>
Wed, 27 Feb 2013 09:27:32 +0000 (10:27 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Wed, 27 Feb 2013 09:27:32 +0000 (10:27 +0100)
src/com/owncloud/android/ui/activity/FileDetailActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

index 2db3d7f..bdd475a 100644 (file)
@@ -169,7 +169,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
             FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;\r
             if (detailsFragment != null) {\r
                 detailsFragment.listenForTransferProgress();\r
-                detailsFragment.updateFileDetails(mWaitingToPreview);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
+                detailsFragment.updateFileDetails(mWaitingToPreview, false);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
             }\r
         }\r
 \r
@@ -225,7 +225,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         if (!mConfigurationChangedToLandscape) {\r
             Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
             if (fragment != null && fragment instanceof FileDetailFragment) {\r
-                ((FileDetailFragment) fragment).updateFileDetails(false);\r
+                ((FileDetailFragment) fragment).updateFileDetails(false, false);\r
             }\r
         }\r
     }\r
index 8657e9b..3311822 100644 (file)
@@ -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
@@ -1190,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
@@ -1373,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
@@ -1399,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
@@ -1408,6 +1445,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             }\r
         }\r
         mDownloadFinishReceiver.onReceive(this, intent);\r
+         */\r
     }\r
 \r
 \r
index 2735e5e..e8ac8b2 100644 (file)
@@ -120,7 +120,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private Account mAccount;\r
     private FileDataStorageManager mStorageManager;\r
     \r
-    private DownloadFinishReceiver mDownloadFinishReceiver;\r
+    //private DownloadFinishReceiver mDownloadFinishReceiver;\r
     private UploadFinishReceiver mUploadFinishReceiver;\r
     public ProgressListener mProgressListener;\r
     \r
@@ -203,7 +203,7 @@ public class FileDetailFragment extends SherlockFragment implements
             mProgressListener = new ProgressListener(progressBar);\r
         }\r
         \r
-        updateFileDetails(false);\r
+        updateFileDetails(false, false);\r
         return view;\r
     }\r
     \r
@@ -255,13 +255,15 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onResume() {\r
         super.onResume();\r
         \r
+        /*\r
         mDownloadFinishReceiver = new DownloadFinishReceiver();\r
         IntentFilter filter = new IntentFilter(\r
                 FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
         getActivity().registerReceiver(mDownloadFinishReceiver, filter);\r
+        */\r
         \r
         mUploadFinishReceiver = new UploadFinishReceiver();\r
-        filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
+        IntentFilter filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
         getActivity().registerReceiver(mUploadFinishReceiver, filter);\r
 \r
     }\r
@@ -271,11 +273,17 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onPause() {\r
         super.onPause();\r
         \r
-        getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
-        mDownloadFinishReceiver = null;\r
+        /*\r
+        if (mDownloadFinishReceiver != null) {\r
+            getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
+            mDownloadFinishReceiver = null;\r
+        }\r
+        */\r
         \r
-        getActivity().unregisterReceiver(mUploadFinishReceiver);\r
-        mUploadFinishReceiver = null;\r
+        if (mUploadFinishReceiver != null) {\r
+            getActivity().unregisterReceiver(mUploadFinishReceiver);\r
+            mUploadFinishReceiver = null;\r
+        }\r
     }\r
 \r
     \r
@@ -583,7 +591,7 @@ public class FileDetailFragment extends SherlockFragment implements
             mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());\r
         }\r
         mAccount = ocAccount;\r
-        updateFileDetails(false);\r
+        updateFileDetails(false, false);\r
     }\r
     \r
 \r
@@ -597,12 +605,17 @@ public class FileDetailFragment extends SherlockFragment implements
      * @param transferring      Flag signaling if the file should be considered as downloading or uploading, \r
      *                          although {@link FileDownloaderBinder#isDownloading(Account, OCFile)}  and \r
      *                          {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.\r
-     * \r
+     *                          \r
+     * @param refresh           If 'true', try to refresh the hold file from the database\r
      */\r
-    public void updateFileDetails(boolean transferring) {\r
+    public void updateFileDetails(boolean transferring, boolean refresh) {\r
 \r
         if (readyToShow()) {\r
             \r
+            if (refresh && mStorageManager != null) {\r
+                mFile = mStorageManager.getFileByPath(mFile.getRemotePath());\r
+            }\r
+            \r
             // set file details\r
             setFilename(mFile.getFileName());\r
             setFiletype(mFile.getMimetype());\r
@@ -802,10 +815,10 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
     \r
-    /**\r
+    /* *\r
      * Once the file download has finished -> update view\r
      * @author Bartek Przybylski\r
-     */\r
+     * - /\r
     private class DownloadFinishReceiver extends BroadcastReceiver {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
@@ -825,6 +838,7 @@ public class FileDetailFragment extends SherlockFragment implements
             }\r
         }\r
     }\r
+    */\r
     \r
     \r
     /**\r
@@ -857,7 +871,7 @@ public class FileDetailFragment extends SherlockFragment implements
                         msg.show();\r
                     }\r
                     getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
-                    updateFileDetails(false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
+                    updateFileDetails(false, false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
                 }\r
             }\r
         }\r