Automatic download of previewable files in download mode
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index 71364ba..c8f23f0 100644 (file)
@@ -140,6 +140,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
     private static int[] mMenuIdentifiersToPatch = {R.id.action_about_app};\r
     \r
+    private OCFile mWaitingToPreview;\r
+    private Handler mHandler;\r
+\r
+    \r
     @Override\r
     public void onCreate(Bundle savedInstanceState) {\r
         Log.d(getClass().toString(), "onCreate() start");\r
@@ -155,6 +159,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         if(savedInstanceState != null) {\r
             // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES\r
             mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);\r
+            mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW);\r
+\r
+        } else {\r
+            mWaitingToPreview = null;\r
         }\r
         \r
         if (!AccountUtils.accountsAreSetup(this)) {\r
@@ -293,8 +301,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) {\r
             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
             if (mCurrentFile != null) {\r
-                if (mCurrentFile.isDown() && FilePreviewFragment.canBePreviewed(mCurrentFile)) {\r
-                    transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                if (FilePreviewFragment.canBePreviewed(mCurrentFile)) {\r
+                    if (mCurrentFile.isDown()) {\r
+                        transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                    } else {\r
+                        transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                        mWaitingToPreview = mCurrentFile;\r
+                    }\r
                 } else {\r
                     transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
                 }\r
@@ -542,6 +555,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 }\r
             }\r
         }\r
+        outState.putParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\r
         Log.d(getClass().toString(), "onSaveInstanceState() end");\r
     }\r
 \r
@@ -571,7 +585,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             registerReceiver(mUploadFinishReceiver, uploadIntentFilter);\r
             \r
             // Listen for download messages\r
-            IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\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
@@ -973,12 +988,27 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         public void onReceive(Context context, Intent intent) {\r
             String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
             String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\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 (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
                 }\r
             }\r
         }\r
@@ -1027,8 +1057,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         if (mDualPane) {\r
             // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'\r
             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-            if (file != null && file.isDown() && FilePreviewFragment.canBePreviewed(file)) {\r
-                transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+            if (file != null && FilePreviewFragment.canBePreviewed(file)) {\r
+                if (file.isDown()) {\r
+                    transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                } else {\r
+                    transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                    mWaitingToPreview = file;\r
+                    requestForDownload();\r
+                }\r
             } else {\r
                 transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
             }\r
@@ -1091,6 +1127,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {\r
                 Log.d(TAG, "Download service connected");\r
                 mDownloaderBinder = (FileDownloaderBinder) service;\r
+                if (mWaitingToPreview != null) {\r
+                    requestForDownload();\r
+                }\r
+                \r
             } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {\r
                 Log.d(TAG, "Upload service connected");\r
                 mUploaderBinder = (FileUploaderBinder) service;\r
@@ -1313,9 +1353,28 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
 \r
     @Override\r
-    public void notifySuccessfulDownload(OCFile file) {\r
-        // TODO refactoring once for all and remove this stupid method\r
+    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
+        if (success) {\r
+            if (mWaitingToPreview != null) {\r
+                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                transaction.commit();\r
+                mWaitingToPreview = null;\r
+            }\r
+        }\r
+        mDownloadFinishReceiver.onReceive(this, intent);\r
     }\r
 \r
 \r
+    private void requestForDownload() {\r
+        Account account = AccountUtils.getCurrentOwnCloudAccount(this);\r
+        if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {\r
+            Intent i = new Intent(this, FileDownloader.class);\r
+            i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);\r
+            i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);\r
+            startService(i);\r
+        }\r
+    }\r
+\r
+    \r
 }\r