Give the user the option to move all the foreign files into the ownCloud local folder...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
index b5d4a02..81c8923 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
@@ -188,7 +191,7 @@ public class FileDetailFragment extends SherlockFragment implements
             mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
         }\r
         \r
-        updateFileDetails();\r
+        updateFileDetails(false);\r
         return view;\r
     }\r
     \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,10 +272,8 @@ 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
                 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();\r
                 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();\r
                 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {\r
@@ -292,7 +305,7 @@ public class FileDetailFragment extends SherlockFragment implements
                     }\r
                     \r
                 } else {\r
-                    mLastRemoteOperation = new SynchronizeFileOperation(mFile, fdsm, mAccount, true, false, getActivity());\r
+                    mLastRemoteOperation = new SynchronizeFileOperation(mFile, null, mStorageManager, mAccount, true, false, getActivity());\r
                     WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
                     mLastRemoteOperation.execute(wc, this, mHandler);\r
                 \r
@@ -307,7 +320,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
+                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
@@ -328,8 +341,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 break;\r
             }\r
             case R.id.fdRenameBtn: {\r
-                EditNameDialog dialog = EditNameDialog.newInstance(mFile.getFileName());\r
-                dialog.setOnDismissListener(this);\r
+                EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), mFile.getFileName(), this);\r
                 dialog.show(getFragmentManager(), "nameeditdialog");\r
                 break;\r
             }   \r
@@ -360,8 +372,13 @@ public class FileDetailFragment extends SherlockFragment implements
                     try {\r
                         Intent i = new Intent(Intent.ACTION_VIEW);\r
                         mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));\r
-                        if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {\r
-                            i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
+                        if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {\r
+                            if (mimeType != null) {\r
+                                i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
+                            } else {\r
+                                // desperate try\r
+                                i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*");\r
+                            }\r
                             i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
                             startActivity(i);\r
                             toastIt = false;\r
@@ -399,11 +416,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
@@ -415,12 +431,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
@@ -456,15 +471,28 @@ 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
+        updateFileDetails(false);\r
     }\r
     \r
 \r
     /**\r
      * Updates the view with all relevant details about that file.\r
+     *\r
+     * TODO Remove parameter when the transferring state of files is kept in database. \r
+     * \r
+     * @param transferring      Flag signaling if the file should be considered as downloading or uploading, \r
+     *                          although {@link FileDownloaderBinder#isDownloading(Account, OCFile)}  and \r
+     *                          {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.\r
+     * \r
      */\r
-    public void updateFileDetails() {\r
+    public void updateFileDetails(boolean transferring) {\r
 \r
         if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {\r
             \r
@@ -486,7 +514,7 @@ public class FileDetailFragment extends SherlockFragment implements
             //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {\r
             FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();\r
             FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();\r
-            if ((downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) {\r
+            if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) {\r
                 setButtonsForTransferring();\r
                 \r
             } else if (mFile.isDown()) {\r
@@ -499,9 +527,11 @@ public class FileDetailFragment extends SherlockFragment implements
                 setButtonsForDown();\r
                 \r
             } else {\r
+                // TODO load default preview image; when the local file is removed, the preview remains there\r
                 setButtonsForRemote();\r
             }\r
         }\r
+        getView().invalidate();\r
     }\r
     \r
     \r
@@ -584,7 +614,6 @@ public class FileDetailFragment extends SherlockFragment implements
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
             downloadButton.setText(R.string.filedetails_sync_file);\r
-            //downloadButton.setEnabled(true);\r
         \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);\r
             ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);\r
@@ -667,9 +696,9 @@ 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
+                    updateFileDetails(false);    // it updates the buttons; must be called although !downloadWasFine\r
                 }\r
             }\r
         }\r
@@ -694,12 +723,19 @@ public class FileDetailFragment extends SherlockFragment implements
             if (!isEmpty() && accountName.equals(mAccount.name)) {\r
                 boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);\r
                 String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);\r
-                if (mFile.getRemotePath().equals(uploadRemotePath)) {\r
+                boolean renamedInUpload = mFile.getRemotePath().equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));\r
+                if (mFile.getRemotePath().equals(uploadRemotePath) ||\r
+                    renamedInUpload) {\r
                     if (uploadWasFine) {\r
-                        FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
-                        mFile = fdsm.getFileByPath(mFile.getRemotePath());\r
+                        mFile = mStorageManager.getFileByPath(mFile.getRemotePath());\r
+                    }\r
+                    if (renamedInUpload) {\r
+                        String newName = (new File(uploadRemotePath)).getName();\r
+                        Toast msg = Toast.makeText(getActivity().getApplicationContext(), String.format(getString(R.string.filedetails_renamed_in_upload_msg), newName), Toast.LENGTH_LONG);\r
+                        msg.show();\r
+                        getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
                     }\r
-                    updateFileDetails();    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
+                    updateFileDetails(false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
                 }\r
             }\r
         }\r
@@ -815,6 +851,7 @@ public class FileDetailFragment extends SherlockFragment implements
             String newFilename = dialog.getNewFilename();\r
             Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);\r
             mLastRemoteOperation = new RenameFileOperation( mFile, \r
+                                                            mAccount, \r
                                                             newFilename, \r
                                                             new FileDataStorageManager(mAccount, getActivity().getContentResolver()));\r
             WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
@@ -974,10 +1011,8 @@ public class FileDetailFragment extends SherlockFragment implements
         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
+                i.putExtra(ConflictsResolveActivity.EXTRA_FILE, mFile);\r
+                i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);\r
                 startActivity(i);\r
                 \r
             } else {\r
@@ -985,6 +1020,13 @@ public class FileDetailFragment extends SherlockFragment implements
                 msg.show();\r
             }\r
             \r
+            if (mFile.isDown()) {\r
+                setButtonsForDown();\r
+                \r
+            } else {\r
+                setButtonsForRemote();\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