Automatic download of previewable files in portrait mode
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 22 Feb 2013 14:27:22 +0000 (15:27 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 22 Feb 2013 14:27:22 +0000 (15: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
src/com/owncloud/android/ui/fragment/FileFragment.java

index 97f719f..fbc4cb9 100644 (file)
@@ -17,8 +17,6 @@
  */\r
 package com.owncloud.android.ui.activity;\r
 \r
-import java.lang.ref.WeakReference;\r
-\r
 import android.accounts.Account;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
@@ -32,7 +30,6 @@ import android.os.IBinder;
 import android.support.v4.app.Fragment;\r
 import android.support.v4.app.FragmentTransaction;\r
 import android.util.Log;\r
-import android.widget.ProgressBar;\r
 \r
 import com.actionbarsherlock.app.ActionBar;\r
 import com.actionbarsherlock.app.SherlockFragmentActivity;\r
@@ -47,8 +44,6 @@ import com.owncloud.android.ui.fragment.FilePreviewFragment;
 \r
 import com.owncloud.android.R;\r
 \r
-import eu.alefzero.webdav.OnDatatransferProgressListener;\r
-\r
 /**\r
  * This activity displays the details of a file like its name, its size and so\r
  * on.\r
@@ -74,11 +69,17 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     private FileUploaderBinder mUploaderBinder = null;\r
     private boolean mWaitingToPreview;\r
     \r
+    private OCFile mFile;\r
+    private Account mAccount;\r
+    \r
 \r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
 \r
+        mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
+        mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\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
         mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && \r
@@ -86,11 +87,6 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
                                            );\r
 \r
         if (!mConfigurationChangedToLandscape) {\r
-            mDownloadConnection = new DetailsServiceConnection();\r
-            bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
-            mUploadConnection = new DetailsServiceConnection();\r
-            bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
-            \r
             setContentView(R.layout.file_activity_details);\r
         \r
             ActionBar actionBar = getSupportActionBar();\r
@@ -103,6 +99,12 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
                 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);\r
             }\r
             \r
+            mDownloadConnection = new DetailsServiceConnection();\r
+            bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
+            mUploadConnection = new DetailsServiceConnection();\r
+            bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
+            \r
+            \r
         }  else {\r
             backToDisplayActivity();   // the 'back' won't be effective until this.onStart() and this.onResume() are completed;\r
         }\r
@@ -115,22 +117,20 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
      * the requested {@link Intent}.\r
      */\r
     private void createChildFragment() {\r
-        OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
-        Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
         int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW); \r
         \r
         Fragment newFragment = null;\r
-        if (FilePreviewFragment.canBePreviewed(file) && mode == MODE_PREVIEW) {\r
-            if (file.isDown()) {\r
-                newFragment = new FilePreviewFragment(file, account);\r
+        if (FilePreviewFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {\r
+            if (mFile.isDown()) {\r
+                newFragment = new FilePreviewFragment(mFile, mAccount);\r
             \r
             } else {\r
-                newFragment = new FileDetailFragment(file, account);\r
+                newFragment = new FileDetailFragment(mFile, mAccount);\r
                 mWaitingToPreview = true;\r
             }\r
             \r
         } else {\r
-            newFragment = new FileDetailFragment(file, account);\r
+            newFragment = new FileDetailFragment(mFile, mAccount);\r
         }\r
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();\r
         ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);\r
@@ -154,6 +154,10 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
             if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {\r
                 Log.d(TAG, "Download service connected");\r
                 mDownloaderBinder = (FileDownloaderBinder) service;\r
+                if (mWaitingToPreview) {\r
+                    requestForDownload();\r
+                }\r
+                    \r
             } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {\r
                 Log.d(TAG, "Upload service connected");\r
                 mUploaderBinder = (FileUploaderBinder) service;\r
@@ -165,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(false);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
+                detailsFragment.updateFileDetails(mWaitingToPreview);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
             }\r
         }\r
 \r
@@ -230,8 +234,8 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     private void backToDisplayActivity() {\r
         Intent intent = new Intent(this, FileDisplayActivity.class);\r
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
-        intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));\r
-        intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));\r
+        intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);\r
+        intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);\r
         startActivity(intent);\r
         finish();\r
     }\r
@@ -284,8 +288,28 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     @Override\r
     public void showFragmentWithDetails(OCFile file) {\r
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-        transaction.replace(R.id.fragment, new FileDetailFragment(file, (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT)), FileDetailFragment.FTAG); \r
+        transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG); \r
         transaction.commit();\r
     }\r
+\r
+    \r
+    private void requestForDownload() {\r
+        if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {\r
+            Intent i = new Intent(this, FileDownloader.class);\r
+            i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
+            i.putExtra(FileDownloader.EXTRA_FILE, mFile);\r
+            startService(i);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void notifySuccessfulDownload(OCFile file) {\r
+        if (mWaitingToPreview) {\r
+            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+            transaction.replace(R.id.fragment, new FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG); \r
+            transaction.commit();\r
+            mWaitingToPreview = false;\r
+        }\r
+    }\r
     \r
 }\r
index 215ef86..71364ba 100644 (file)
@@ -1312,4 +1312,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
 \r
+    @Override\r
+    public void notifySuccessfulDownload(OCFile file) {\r
+        // TODO refactoring once for all and remove this stupid method\r
+    }\r
+\r
+\r
 }\r
index 85312b9..d9ee918 100644 (file)
@@ -833,6 +833,8 @@ public class FileDetailFragment extends SherlockFragment implements
                 if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
                     if (downloadWasFine) {\r
                         mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
+                        mContainerActivity.notifySuccessfulDownload(mFile);\r
+                        getActivity().removeStickyBroadcast(intent);\r
                     }\r
                     updateFileDetails(false);    // it updates the buttons; must be called although !downloadWasFine\r
                 }\r
index 24e1251..d2c0e46 100644 (file)
@@ -67,6 +67,9 @@ public interface FileFragment {
          */
         public void showFragmentWithDetails(OCFile file);
         
+        
+        public void notifySuccessfulDownload(OCFile file);
+        
     }
     
 }