Adding cancellation to uploads (WIP)
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
index ae38a0a..10b44ad 100644 (file)
@@ -35,7 +35,6 @@ import org.apache.http.protocol.HTTP;
 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;\r
 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;\r
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
-import org.json.JSONException;\r
 import org.json.JSONObject;\r
 \r
 import android.accounts.Account;\r
@@ -80,8 +79,12 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.files.services.FileUploader;\r
+import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
+import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
+import com.owncloud.android.network.OwnCloudClientUtils;\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
 import com.owncloud.android.utils.OwnCloudVersion;\r
 \r
 import com.owncloud.android.R;\r
@@ -136,7 +139,7 @@ public class FileDetailFragment extends SherlockFragment implements
      * @param fileToDetail      An {@link OCFile} to show in the fragment\r
      * @param ocAccount         An ownCloud account; needed to start downloads\r
      */\r
-    public FileDetailFragment(OCFile fileToDetail, Account ocAccount){\r
+    public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {\r
         mFile = fileToDetail;\r
         mAccount = ocAccount;\r
         mLayout = R.layout.file_details_empty;\r
@@ -147,20 +150,6 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
 \r
-    /**\r
-     * {@inheritDoc}\r
-     */\r
-    @Override\r
-    public void onAttach(Activity activity) {\r
-        super.onAttach(activity);\r
-        try {\r
-            mContainerActivity = (ContainerActivity) activity;\r
-        } catch (ClassCastException e) {\r
-            throw new ClassCastException(activity.toString() + " must implement FileListFragment.ContainerActivity");\r
-        }\r
-    }\r
-    \r
-    \r
     @Override\r
     public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
             Bundle savedInstanceState) {\r
@@ -190,6 +179,20 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
     \r
 \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public void onAttach(Activity activity) {\r
+        super.onAttach(activity);\r
+        try {\r
+            mContainerActivity = (ContainerActivity) activity;\r
+        } catch (ClassCastException e) {\r
+            throw new ClassCastException(activity.toString() + " must implement " + FileDetailFragment.ContainerActivity.class.getCanonicalName());\r
+        }\r
+    }\r
+        \r
+\r
     @Override\r
     public void onSaveInstanceState(Bundle outState) {\r
         Log.i(getClass().toString(), "onSaveInstanceState() start");\r
@@ -242,17 +245,30 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onClick(View v) {\r
         switch (v.getId()) {\r
             case R.id.fdDownloadBtn: {\r
-                Intent i = new Intent(getActivity(), FileDownloader.class);\r
-                i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\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
+                //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {\r
+                FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();\r
+                if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {\r
+                    downloaderBinder.cancel(mAccount, mFile);\r
+                    if (mFile.isDown()) {\r
+                        setButtonsForDown();\r
+                    } else {\r
+                        setButtonsForRemote();\r
+                    }\r
+                    \r
+                } else {\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
                 \r
-                // update ui \r
-                setButtonsForTransferring();\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
+                    getActivity().startService(i);\r
+                    mContainerActivity.onFileStateChanged();    // this is not working; it is performed before the fileDownloadService registers it as 'in progress'\r
+                }\r
                 break;\r
             }\r
             case R.id.fdKeepInSync: {\r
@@ -262,9 +278,19 @@ public class FileDetailFragment extends SherlockFragment implements
                 fdsm.saveFile(mFile);\r
                 if (mFile.keepInSync()) {\r
                     onClick(getView().findViewById(R.id.fdDownloadBtn));\r
-                } else {    \r
+                } else {\r
                     mContainerActivity.onFileStateChanged();    // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
                 }\r
+                /*\r
+                Intent intent = new Intent(getActivity().getApplicationContext(),\r
+                                           FileObserverService.class);\r
+                intent.putExtra(FileObserverService.KEY_FILE_CMD,\r
+                           (cb.isChecked()?\r
+                                   FileObserverService.CMD_ADD_OBSERVED_FILE:\r
+                                   FileObserverService.CMD_DEL_OBSERVED_FILE));\r
+                intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath());\r
+                getActivity().startService(intent);\r
+                */\r
                 break;\r
             }\r
             case R.id.fdRenameBtn: {\r
@@ -418,7 +444,10 @@ public class FileDetailFragment extends SherlockFragment implements
             cb.setChecked(mFile.keepInSync());\r
 \r
             // configure UI for depending upon local state of the file\r
-            if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {\r
+            //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
                 setButtonsForTransferring();\r
                 \r
             } else if (mFile.isDown()) {\r
@@ -498,8 +527,8 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setButtonsForTransferring() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            //downloadButton.setText(R.string.filedetails_download_in_progress);    // ugly\r
-            downloadButton.setEnabled(false);   // TODO replace it with a 'cancel download' button\r
+            downloadButton.setText(R.string.common_cancel);\r
+            //downloadButton.setEnabled(false);\r
         \r
             // let's protect the user from himself ;)\r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);\r
@@ -514,8 +543,8 @@ 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);      // ugly\r
-            downloadButton.setEnabled(true);\r
+            downloadButton.setText(R.string.filedetails_redownload);\r
+            //downloadButton.setEnabled(true);\r
         \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);\r
             ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);\r
@@ -529,8 +558,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private void setButtonsForRemote() {\r
         if (!isEmpty()) {\r
             Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            //downloadButton.setText(R.string.filedetails_download);    // unnecessary\r
-            downloadButton.setEnabled(true);\r
+            downloadButton.setText(R.string.filedetails_download);\r
             \r
             ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);\r
             ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);\r
@@ -564,7 +592,7 @@ public class FileDetailFragment extends SherlockFragment implements
      * \r
      * @author David A. Velasco\r
      */\r
-    public interface ContainerActivity {\r
+    public interface ContainerActivity extends TransferServiceGetter {\r
 \r
         /**\r
          * Callback method invoked when the detail fragment wants to notice its container \r
@@ -637,6 +665,7 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
 \r
     // this is a temporary class for sharing purposes, it need to be replaced in transfer service\r
+    @SuppressWarnings("unused")\r
     private class ShareRunnable implements Runnable {\r
         private String mPath;\r
 \r
@@ -661,7 +690,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 final String WEBDAV_SCRIPT = "webdav.php";\r
                 final String WEBDAV_FILES_LOCATION = "/files/";\r
                 \r
-                WebdavClient wc = new WebdavClient();\r
+                WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getActivity().getApplicationContext());\r
                 HttpConnectionManagerParams params = new HttpConnectionManagerParams();\r
                 params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5);\r
 \r
@@ -684,7 +713,6 @@ public class FileDetailFragment extends SherlockFragment implements
                     PropFindMethod find = new PropFindMethod(url+"/");\r
                     find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));\r
                     Log.d("sharer", ""+ url+"/");\r
-                    wc.setCredentials(account.name.substring(0, account.name.lastIndexOf('@')), am.getPassword(account));\r
                     \r
                     for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) {\r
                         Log.d("sharer-h", a.getName() + ":"+a.getValue());\r
@@ -730,17 +758,7 @@ public class FileDetailFragment extends SherlockFragment implements
                     String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token; \r
                     Log.d("Actions:shareFile ok", "url: " + uri);   \r
                     \r
-                } catch (HttpException e) {\r
-                    // TODO Auto-generated catch block\r
-                    e.printStackTrace();\r
-                } catch (IOException e) {\r
-                    // TODO Auto-generated catch block\r
-                    e.printStackTrace();\r
-                } catch (JSONException e) {\r
-                    // TODO Auto-generated catch block\r
-                    e.printStackTrace();\r
                 } catch (Exception e) {\r
-                    // TODO Auto-generated catch block\r
                     e.printStackTrace();\r
                 }\r
                 \r
@@ -809,8 +827,7 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
         \r
         public void run() {\r
-            WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
-            wc.allowSelfsignedCertificates();\r
+            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -825,6 +842,7 @@ public class FileDetailFragment extends SherlockFragment implements
             try {\r
                 int status = wc.executeMethod(move);\r
                 success = move.succeeded();\r
+                move.getResponseBodyAsString(); // exhaust response, although not interesting\r
                 Log.d(TAG, "Move returned status: " + status);\r
                 \r
             } catch (HttpException e) {\r
@@ -835,11 +853,14 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
             } catch (Exception e) {\r
                 Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);\r
-            }\r
+                \r
+            } finally {\r
+               move.releaseConnection();\r
+            } \r
             \r
             if (success) {\r
                 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-                fdsm.removeFile(mOld);\r
+                fdsm.removeFile(mOld, false);\r
                 fdsm.saveFile(mNew);\r
                 mFile = mNew;\r
                 mHandler.post(new Runnable() {\r
@@ -973,8 +994,7 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
         \r
         public void run() {\r
-            WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());\r
-            wc.allowSelfsignedCertificates();\r
+            WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
             AccountManager am = AccountManager.get(getSherlockActivity());\r
             String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
             OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
@@ -988,6 +1008,7 @@ public class FileDetailFragment extends SherlockFragment implements
             try {\r
                 status = wc.executeMethod(delete);\r
                 success = (delete.succeeded());\r
+                delete.getResponseBodyAsString();   // exhaust the response, although not interesting\r
                 Log.d(TAG, "Delete: returned status " + status);\r
                 \r
             } catch (HttpException e) {\r
@@ -998,11 +1019,14 @@ public class FileDetailFragment extends SherlockFragment implements
                 \r
             } catch (Exception e) {\r
                 Log.e(TAG, "Unexpected exception removing file " + mFileToRemove.getRemotePath(), e);\r
+                \r
+            } finally {\r
+                delete.releaseConnection();\r
             }\r
             \r
             if (success) {\r
                 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());\r
-                fdsm.removeFile(mFileToRemove);\r
+                fdsm.removeFile(mFileToRemove, true);\r
                 mHandler.post(new Runnable() {\r
                     @Override\r
                     public void run() {\r