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 01fc186..46d8d56 100644 (file)
@@ -113,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
@@ -134,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
@@ -149,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
@@ -205,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
@@ -257,7 +272,6 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     @Override\r
     public void onClick(View v) {\r
-        FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
         switch (v.getId()) {\r
             case R.id.fdDownloadBtn: {\r
                 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {\r
@@ -292,7 +306,7 @@ public class FileDetailFragment extends SherlockFragment implements
                     }\r
                     \r
                 } else {\r
-                    mLastRemoteOperation = new SynchronizeFileOperation(mFile.getRemotePath(), fdsm, mAccount, true, false, getActivity());\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
@@ -307,20 +321,7 @@ public class FileDetailFragment extends SherlockFragment implements
             case R.id.fdKeepInSync: {\r
                 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);\r
                 mFile.setKeepInSync(cb.isChecked());\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
@@ -335,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
@@ -410,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
@@ -426,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
@@ -467,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
@@ -594,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
@@ -678,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