Bug fixed: download followed by refresh always resulted in upload, although the file...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
index 76a387f..46d8d56 100644 (file)
@@ -82,6 +82,8 @@ import com.owncloud.android.operations.RemoteOperationResult;
 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
 import com.owncloud.android.operations.RemoveFileOperation;\r
 import com.owncloud.android.operations.RenameFileOperation;\r
+import com.owncloud.android.operations.SynchronizeFileOperation;\r
+import com.owncloud.android.ui.activity.ConflictsResolveActivity;\r
 import com.owncloud.android.ui.activity.FileDetailActivity;\r
 import com.owncloud.android.ui.activity.FileDisplayActivity;\r
 import com.owncloud.android.ui.activity.TransferServiceGetter;\r
@@ -111,6 +113,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private View mView;\r
     private OCFile mFile;\r
     private Account mAccount;\r
+    private FileDataStorageManager mStorageManager;\r
     private ImageView mPreview;\r
     \r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
@@ -119,7 +122,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private Handler mHandler;\r
     private RemoteOperation mLastRemoteOperation;\r
 \r
-    private static final String TAG = "FileDetailFragment";\r
+    private static final String TAG = FileDetailFragment.class.getSimpleName();\r
     public static final String FTAG = "FileDetails"; \r
     public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";\r
 \r
@@ -132,6 +135,7 @@ public class FileDetailFragment extends SherlockFragment implements
     public FileDetailFragment() {\r
         mFile = null;\r
         mAccount = null;\r
+        mStorageManager = null;\r
         mLayout = R.layout.file_details_empty;\r
     }\r
     \r
@@ -147,6 +151,7 @@ public class FileDetailFragment extends SherlockFragment implements
     public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {\r
         mFile = fileToDetail;\r
         mAccount = ocAccount;\r
+        mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment \r
         mLayout = R.layout.file_details_empty;\r
         \r
         if(fileToDetail != null && ocAccount != null) {\r
@@ -203,6 +208,18 @@ public class FileDetailFragment extends SherlockFragment implements
             throw new ClassCastException(activity.toString() + " must implement " + FileDetailFragment.ContainerActivity.class.getSimpleName());\r
         }\r
     }\r
+    \r
+    \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onActivityCreated(Bundle savedInstanceState) {\r
+        super.onActivityCreated(savedInstanceState);\r
+        if (mAccount != null) {\r
+            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;\r
+        }\r
+    }\r
         \r
 \r
     @Override\r
@@ -289,40 +306,22 @@ public class FileDetailFragment extends SherlockFragment implements
                     }\r
                     \r
                 } else {\r
-                    // ISSUE 6: this button should be promoted to 'synchronize' if the file is DOWN, not just redownload\r
-                    Intent i = new Intent(getActivity(), FileDownloader.class);\r
-                    i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
-                    i.putExtra(FileDownloader.EXTRA_FILE, mFile);\r
-                    /*i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
-                    i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());\r
-                    i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());*/\r
+                    mLastRemoteOperation = new SynchronizeFileOperation(mFile, mStorageManager, mAccount, true, false, getActivity());\r
+                    WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
+                    mLastRemoteOperation.execute(wc, this, mHandler);\r
                 \r
                     // update ui \r
-                    setButtonsForTransferring();\r
-                \r
-                    getActivity().startService(i);\r
-                    mContainerActivity.onFileStateChanged();    // this is not working; it is performed before the fileDownloadService registers it as 'in progress'\r
+                    boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+                    getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+                    setButtonsForTransferring(); // disable button immediately, although the synchronization does not result in a file transference\r
+                    \r
                 }\r
                 break;\r
             }\r
             case R.id.fdKeepInSync: {\r
                 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);\r
                 mFile.setKeepInSync(cb.isChecked());\r
-                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
-                fdsm.saveFile(mFile);\r
-                \r
-                /* NOT HERE\r
-                 * now that FileObserverService is involved, the easiest way to coordinate it with the download service\r
-                 * in every possible case is let the FileObserverService decide if the download should be started at\r
-                 * this moment or not\r
-                 * \r
-                 * see changes at FileObserverService#addObservedFile\r
-                 \r
-                   if (mFile.keepInSync()) {\r
-                    onClick(getView().findViewById(R.id.fdDownloadBtn));\r
-                } else {\r
-                    mContainerActivity.onFileStateChanged();    // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
-                }*/\r
+                mStorageManager.saveFile(mFile);\r
                 \r
                 /// register the OCFile instance in the observer service to monitor local updates;\r
                 /// if necessary, the file is download \r
@@ -337,7 +336,9 @@ public class FileDetailFragment extends SherlockFragment implements
                 Log.e(TAG, "starting observer service");\r
                 getActivity().startService(intent);\r
                 \r
-                mContainerActivity.onFileStateChanged();                \r
+                if (mFile.keepInSync()) {\r
+                    onClick(getView().findViewById(R.id.fdDownloadBtn));    // force an immediate synchronization\r
+                }\r
                 break;\r
             }\r
             case R.id.fdRenameBtn: {\r
@@ -412,11 +413,10 @@ public class FileDetailFragment extends SherlockFragment implements
     @Override\r
     public void onConfirmation(String callerTag) {\r
         if (callerTag.equals(FTAG_CONFIRMATION)) {\r
-            FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-            if (fdsm.getFileById(mFile.getFileId()) != null) {\r
+            if (mStorageManager.getFileById(mFile.getFileId()) != null) {\r
                 mLastRemoteOperation = new RemoveFileOperation( mFile, \r
                                                                 true, \r
-                                                                new FileDataStorageManager(mAccount, getActivity().getContentResolver()));\r
+                                                                mStorageManager);\r
                 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
                 mLastRemoteOperation.execute(wc, this, mHandler);\r
                 \r
@@ -428,12 +428,11 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     @Override\r
     public void onNeutral(String callerTag) {\r
-        FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
         File f = null;\r
         if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {\r
             f.delete();\r
             mFile.setStoragePath(null);\r
-            fdsm.saveFile(mFile);\r
+            mStorageManager.saveFile(mFile);\r
             updateFileDetails(mFile, mAccount);\r
         }\r
     }\r
@@ -469,6 +468,12 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     public void updateFileDetails(OCFile file, Account ocAccount) {\r
         mFile = file;\r
+        if (ocAccount != null && ( \r
+                mStorageManager == null || \r
+                (mAccount != null && !mAccount.equals(ocAccount))\r
+           )) {\r
+            mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());\r
+        }\r
         mAccount = ocAccount;\r
         updateFileDetails();\r
     }\r
@@ -596,7 +601,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setButtonsForDown() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setText(R.string.filedetails_redownload);\r
+            downloadButton.setText(R.string.filedetails_sync_file);\r
             //downloadButton.setEnabled(true);\r
         \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);\r
@@ -680,7 +685,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
                 if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
                     if (downloadWasFine) {\r
-                        mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));    // updates the local object without accessing the database again\r
+                        mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
                     }\r
                     updateFileDetails();    // it updates the buttons; must be called although !downloadWasFine\r
                 }\r
@@ -923,6 +928,9 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
             } else if (operation instanceof RenameFileOperation) {\r
                 onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
+                \r
+            } else if (operation instanceof SynchronizeFileOperation) {\r
+                onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
             }\r
         }\r
     }\r
@@ -977,5 +985,39 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
     \r
+    private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {\r
+        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
+        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
+\r
+        if (!result.isSuccess()) {\r
+            if (result.getCode() == ResultCode.SYNC_CONFLICT) {\r
+                Intent i = new Intent(getActivity(), ConflictsResolveActivity.class);\r
+                //i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);\r
+                i.putExtra("remotepath", mFile.getRemotePath());\r
+                i.putExtra("localpath", mFile.getStoragePath());\r
+                i.putExtra("account", mAccount);\r
+                startActivity(i);\r
+                \r
+            } else {\r
+                Toast msg = Toast.makeText(getActivity(), R.string.sync_file_fail_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+            }\r
+            \r
+        } else {\r
+            if (operation.transferWasRequested()) {\r
+                mContainerActivity.onFileStateChanged();    // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so \r
+                                                            // checking the service to see if the file is downloading results in FALSE\r
+            } else {\r
+                Toast msg = Toast.makeText(getActivity(), R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG); \r
+                msg.show();\r
+                if (mFile.isDown()) {\r
+                    setButtonsForDown();\r
+                    \r
+                } else {\r
+                    setButtonsForRemote();\r
+                }\r
+            }\r
+        }\r
+    }\r
 \r
 }\r