Browse Source

Fixed automatic playback of media previews after download, in portrait orientation

David A. Velasco 12 years ago
parent
commit
d0767f1a4f

+ 98 - 25
src/com/owncloud/android/ui/activity/FileDetailActivity.java

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

+ 2 - 18
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -561,7 +561,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }
 
     @Override
-    protected void onResume() {
+    public void onResume() {
         Log.d(getClass().toString(), "onResume() start");
         super.onResume();
 
@@ -605,7 +605,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 
     
     @Override
-    protected void onPause() {
+    public void onPause() {
         Log.d(getClass().toString(), "onPause() start");
         super.onPause();
         if (mSyncBroadcastReceiver != null) {
@@ -1433,22 +1433,6 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }
 
 
-    @Override
-    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
-        /*
-        if (success) {
-            if (mWaitingToPreview != null) {
-                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
-                transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
-                transaction.commit();
-                mWaitingToPreview = null;
-            }
-        }
-        mDownloadFinishReceiver.onReceive(this, intent);
-         */
-    }
-
-
     private void requestForDownload() {
         Account account = AccountUtils.getCurrentOwnCloudAccount(this);
         if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {

+ 1 - 28
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -69,7 +69,6 @@ import com.owncloud.android.DisplayUtils;
 import com.owncloud.android.authenticator.AccountAuthenticator;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileObserverService;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
@@ -230,7 +229,7 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         if (mAccount != null) {
-            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
+            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
         }
     }
         
@@ -815,32 +814,6 @@ public class FileDetailFragment extends SherlockFragment implements
     }
     
     
-    /* *
-     * Once the file download has finished -> update view
-     * @author Bartek Przybylski
-     * - /
-    private class DownloadFinishReceiver extends BroadcastReceiver {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
-
-            if (!isEmpty() && accountName.equals(mAccount.name)) {
-                boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
-                String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
-                if (mFile.getRemotePath().equals(downloadedRemotePath)) {
-                    if (downloadWasFine) {
-                        mFile = mStorageManager.getFileByPath(downloadedRemotePath);
-                    }
-                    mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);
-                    getActivity().removeStickyBroadcast(intent);
-                    updateFileDetails(false);    // it updates the buttons; must be called although !downloadWasFine
-                }
-            }
-        }
-    }
-    */
-    
-    
     /**
      * Once the file upload has finished -> update view
      * 

+ 0 - 2
src/com/owncloud/android/ui/fragment/FileFragment.java

@@ -69,8 +69,6 @@ public interface FileFragment {
         public void showFragmentWithDetails(OCFile file);
         
         
-        public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success);
-        
     }
     
 }

+ 0 - 17
src/com/owncloud/android/ui/preview/PreviewImageActivity.java

@@ -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.
      *