Automatic download of previewable files in download mode
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 22 Feb 2013 15:39:58 +0000 (16:39 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 22 Feb 2013 15:39:58 +0000 (16:39 +0100)
src/com/owncloud/android/files/services/FileDownloader.java
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 4f17cc0..d3317d8 100644 (file)
@@ -479,8 +479,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      */\r
     private void sendBroadcastNewDownload(DownloadFileOperation download) {\r
         Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);\r
      */\r
     private void sendBroadcastNewDownload(DownloadFileOperation download) {\r
         Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);\r
-        /*added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
-        added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/\r
+        added.putExtra(ACCOUNT_NAME, download.getAccount().name);\r
+        added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());\r
         added.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
         sendStickyBroadcast(added);\r
     }\r
         added.putExtra(EXTRA_FILE_PATH, download.getSavePath());\r
         sendStickyBroadcast(added);\r
     }\r
index fbc4cb9..6994c09 100644 (file)
@@ -61,7 +61,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     public static final int MODE_DETAILS = 0;\r
     public static final int MODE_PREVIEW = 1;\r
 \r
     public static final int MODE_DETAILS = 0;\r
     public static final int MODE_PREVIEW = 1;\r
 \r
-    private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";\r
+    public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";\r
     \r
     private boolean mConfigurationChangedToLandscape = false;\r
     private FileDownloaderBinder mDownloaderBinder = null;\r
     \r
     private boolean mConfigurationChangedToLandscape = false;\r
     private FileDownloaderBinder mDownloaderBinder = null;\r
@@ -303,12 +303,14 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     }\r
 \r
     @Override\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
+    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 FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG); \r
+                transaction.commit();\r
+                mWaitingToPreview = false;\r
+            }\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r
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
 \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
     @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
         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
         }\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 (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
                 } 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
                 }\r
             }\r
         }\r
+        outState.putParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\r
         Log.d(getClass().toString(), "onSaveInstanceState() end");\r
     }\r
 \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
             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
             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
         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
                 }\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 (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
             } 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 (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
             } 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
 \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
     }\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
 }\r
index d9ee918..2416ec2 100644 (file)
@@ -833,9 +833,9 @@ public class FileDetailFragment extends SherlockFragment implements
                 if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
                     if (downloadWasFine) {\r
                         mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
                 if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
                     if (downloadWasFine) {\r
                         mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
-                        mContainerActivity.notifySuccessfulDownload(mFile);\r
-                        getActivity().removeStickyBroadcast(intent);\r
                     }\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
                     updateFileDetails(false);    // it updates the buttons; must be called although !downloadWasFine\r
                 }\r
             }\r
index d2c0e46..9cd1b9c 100644 (file)
@@ -18,6 +18,7 @@
 
 package com.owncloud.android.ui.fragment;
 
 
 package com.owncloud.android.ui.fragment;
 
+import android.content.Intent;
 import android.support.v4.app.Fragment;
 
 import com.owncloud.android.datamodel.OCFile;
 import android.support.v4.app.Fragment;
 
 import com.owncloud.android.datamodel.OCFile;
@@ -68,7 +69,7 @@ public interface FileFragment {
         public void showFragmentWithDetails(OCFile file);
         
         
         public void showFragmentWithDetails(OCFile file);
         
         
-        public void notifySuccessfulDownload(OCFile file);
+        public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success);
         
     }
     
         
     }