Fixed automatic playback of media previews after download, in portrait orientation
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
index bdd475a..2ab8bcc 100644 (file)
@@ -20,9 +20,11 @@ package com.owncloud.android.ui.activity;
 import android.accounts.Account;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
+import android.content.BroadcastReceiver;\r
 import android.content.ComponentName;\r
 import android.content.Context;\r
 import android.content.Intent;\r
+import android.content.IntentFilter;\r
 import android.content.ServiceConnection;\r
 import android.content.res.Configuration;\r
 import android.os.Bundle;\r
@@ -34,14 +36,17 @@ import android.util.Log;
 import com.actionbarsherlock.app.ActionBar;\r
 import com.actionbarsherlock.app.SherlockFragmentActivity;\r
 import com.actionbarsherlock.view.MenuItem;\r
+import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
+import com.owncloud.android.ui.fragment.FileFragment;\r
 import com.owncloud.android.ui.preview.PreviewMediaFragment;\r
 \r
+import com.owncloud.android.AccountUtils;\r
 import com.owncloud.android.R;\r
 \r
 /**\r
@@ -51,7 +56,7 @@ import com.owncloud.android.R;
  * @author Bartek Przybylski\r
  * @author David A. Velasco\r
  */\r
-public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {\r
+public class FileDetailActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity {\r
     \r
     public static final int DIALOG_SHORT_WAIT = 0;\r
 \r
@@ -71,6 +76,9 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     \r
     private OCFile mFile;\r
     private Account mAccount;\r
+\r
+    private FileDataStorageManager mStorageManager;\r
+    private DownloadFinishReceiver mDownloadFinishReceiver;\r
     \r
 \r
     @Override\r
@@ -79,6 +87,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
 \r
         mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
         mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
+        mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());\r
         \r
         // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity \r
         Configuration conf = getResources().getConfiguration();\r
@@ -143,8 +152,36 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         super.onSaveInstanceState(outState);\r
         outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\r
     }\r
-\r
-\r
+    \r
+    \r
+    @Override\r
+    public void onPause() {\r
+        super.onPause();\r
+        if (mDownloadFinishReceiver != null) {\r
+            unregisterReceiver(mDownloadFinishReceiver);\r
+            mDownloadFinishReceiver = null;\r
+        }\r
+    }\r
+    \r
+    \r
+    @Override\r
+    public void onResume() {\r
+        super.onResume();\r
+        if (!mConfigurationChangedToLandscape) {\r
+            // TODO this is probably unnecessary\r
+            Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+            if (fragment != null && fragment instanceof FileDetailFragment) {\r
+                ((FileDetailFragment) fragment).updateFileDetails(false, false);\r
+            }\r
+        }\r
+        // Listen for download messages\r
+        IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);\r
+        downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
+        mDownloadFinishReceiver = new DownloadFinishReceiver();\r
+        registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
+    }\r
+    \r
+    \r
     /** Defines callbacks for service binding, passed to bindService() */\r
     private class DetailsServiceConnection implements ServiceConnection {\r
 \r
@@ -218,19 +255,6 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
 \r
 \r
 \r
-    @Override\r
-    protected void onResume() {\r
-        \r
-        super.onResume();\r
-        if (!mConfigurationChangedToLandscape) {\r
-            Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-            if (fragment != null && fragment instanceof FileDetailFragment) {\r
-                ((FileDetailFragment) fragment).updateFileDetails(false, false);\r
-            }\r
-        }\r
-    }\r
-    \r
-\r
     private void backToDisplayActivity() {\r
         Intent intent = new Intent(this, FileDisplayActivity.class);\r
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
@@ -302,16 +326,65 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         }\r
     }\r
 \r
-    @Override\r
-    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
-        if (success) {\r
-            if (mWaitingToPreview) {\r
-                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-                transaction.replace(R.id.fragment, new PreviewMediaFragment(file, mAccount), FileDetailFragment.FTAG); \r
-                transaction.commit();\r
-                mWaitingToPreview = false;\r
+    \r
+    /**\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 file.\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
+            boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));\r
+            \r
+            if (sameAccount && samePath) {\r
+                updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));\r
             }\r
+            \r
+            removeStickyBroadcast(intent);\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
+    public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {\r
+        Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\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
+                // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver \r
+                mWaitingToPreview = false;\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {\r
+                // grants that the progress bar is updated\r
+                detailsFragment.listenForTransferProgress();\r
+                detailsFragment.updateFileDetails(true, false);\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+                //  refresh the details fragment \r
+                if (success && mWaitingToPreview) {\r
+                    mFile = mStorageManager.getFileById(mFile.getFileId());   // update the file from database, for the local storage path\r
+                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                    transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);\r
+                    transaction.commit();\r
+                    mWaitingToPreview = false;\r
+                    \r
+                } else {\r
+                    detailsFragment.updateFileDetails(false, (success));\r
+                    // TODO error message if !success ¿?\r
+                }\r
+            }\r
+        } // TODO else if (fragment != null && fragment )\r
+        \r
+        \r
+    }\r
+\r
 }\r