Fixed automatic playback of media previews after download, in portrait orientation
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 28 Feb 2013 10:42:47 +0000 (11:42 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 28 Feb 2013 10:42:47 +0000 (11:42 +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
src/com/owncloud/android/ui/fragment/FileFragment.java
src/com/owncloud/android/ui/preview/PreviewImageActivity.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.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.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
 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.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.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.ui.preview.PreviewMediaFragment;\r
 \r
+import com.owncloud.android.AccountUtils;\r
 import com.owncloud.android.R;\r
 \r
 /**\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
  * @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
     \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 OCFile mFile;\r
     private Account mAccount;\r
+\r
+    private FileDataStorageManager mStorageManager;\r
+    private DownloadFinishReceiver mDownloadFinishReceiver;\r
     \r
 \r
     @Override\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
 \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
         \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
         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
     /** 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
 \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
     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
         }\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
+            \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
+\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
 }\r
index 3311822..2be4e75 100644 (file)
@@ -561,7 +561,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
     @Override\r
     }\r
 \r
     @Override\r
-    protected void onResume() {\r
+    public void onResume() {\r
         Log.d(getClass().toString(), "onResume() start");\r
         super.onResume();\r
 \r
         Log.d(getClass().toString(), "onResume() start");\r
         super.onResume();\r
 \r
@@ -605,7 +605,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
     \r
     @Override\r
 \r
     \r
     @Override\r
-    protected void onPause() {\r
+    public void onPause() {\r
         Log.d(getClass().toString(), "onPause() start");\r
         super.onPause();\r
         if (mSyncBroadcastReceiver != null) {\r
         Log.d(getClass().toString(), "onPause() start");\r
         super.onPause();\r
         if (mSyncBroadcastReceiver != null) {\r
@@ -1433,22 +1433,6 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
 \r
     }\r
 \r
 \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
-                transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
-                transaction.commit();\r
-                mWaitingToPreview = null;\r
-            }\r
-        }\r
-        mDownloadFinishReceiver.onReceive(this, intent);\r
-         */\r
-    }\r
-\r
-\r
     private void requestForDownload() {\r
         Account account = AccountUtils.getCurrentOwnCloudAccount(this);\r
         if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {\r
     private void requestForDownload() {\r
         Account account = AccountUtils.getCurrentOwnCloudAccount(this);\r
         if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {\r
index e8ac8b2..7eb6cda 100644 (file)
@@ -69,7 +69,6 @@ import com.owncloud.android.DisplayUtils;
 import com.owncloud.android.authenticator.AccountAuthenticator;\r
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.authenticator.AccountAuthenticator;\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.FileObserverService;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileObserverService;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
@@ -230,7 +229,7 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onActivityCreated(Bundle savedInstanceState) {\r
         super.onActivityCreated(savedInstanceState);\r
         if (mAccount != null) {\r
     public void onActivityCreated(Bundle savedInstanceState) {\r
         super.onActivityCreated(savedInstanceState);\r
         if (mAccount != null) {\r
-            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;\r
+            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
         }\r
     }\r
         \r
         }\r
     }\r
         \r
@@ -815,32 +814,6 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
     \r
     }\r
     \r
     \r
-    /* *\r
-     * Once the file download has finished -> update view\r
-     * @author Bartek Przybylski\r
-     * - /\r
-    private class DownloadFinishReceiver extends BroadcastReceiver {\r
-        @Override\r
-        public void onReceive(Context context, Intent intent) {\r
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-\r
-            if (!isEmpty() && accountName.equals(mAccount.name)) {\r
-                boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);\r
-                String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
-                if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
-                    if (downloadWasFine) {\r
-                        mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
-                    }\r
-                    mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);\r
-                    getActivity().removeStickyBroadcast(intent);\r
-                    updateFileDetails(false);    // it updates the buttons; must be called although !downloadWasFine\r
-                }\r
-            }\r
-        }\r
-    }\r
-    */\r
-    \r
-    \r
     /**\r
      * Once the file upload has finished -> update view\r
      * \r
     /**\r
      * Once the file upload has finished -> update view\r
      * \r
index 9cd1b9c..c489c52 100644 (file)
@@ -69,8 +69,6 @@ public interface FileFragment {
         public void showFragmentWithDetails(OCFile file);
         
         
         public void showFragmentWithDetails(OCFile file);
         
         
-        public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success);
-        
     }
     
 }
     }
     
 }
index b090316..62dc8cf 100644 (file)
@@ -328,23 +328,6 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi
         }
     }
 
         }
     }
 
-    @Override
-    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
-        /*
-        if (success) {
-            if (mWaitingToPreview != null && mWaitingToPreview.equals(file)) {
-                mWaitingToPreview = null;
-                int position = mViewPager.getCurrentItem();
-                mPreviewImagePagerAdapter.updateFile(position, file);
-                Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
-                mPreviewImagePagerAdapter.notifyDataSetChanged();
-                Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
-            }
-        }
-        */
-    }
-
-    
     /**
      * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
      * 
     /**
      * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
      *