Execute the file to upload copy in an AsyncTask
authormasensio <masensio@solidgear.es>
Wed, 29 Apr 2015 10:42:10 +0000 (12:42 +0200)
committermasensio <masensio@solidgear.es>
Wed, 29 Apr 2015 10:42:10 +0000 (12:42 +0200)
src/com/owncloud/android/ui/activity/Uploader.java
src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java [new file with mode: 0644]

index 60b6ae5..3845d7b 100644 (file)
@@ -34,6 +34,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Stack;
 import java.util.Vector;
+import java.util.concurrent.ExecutionException;
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
@@ -78,6 +79,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.CreateFolderOperation;
 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
+import com.owncloud.android.utils.CopyTmpFileAsyncTask;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.FileStorageUtils;
@@ -88,7 +90,8 @@ import com.owncloud.android.utils.UriUtils;
  * This can be used to upload things to an ownCloud instance.
  */
 public class Uploader extends FileActivity
-        implements OnItemClickListener, android.view.View.OnClickListener {
+        implements OnItemClickListener, android.view.View.OnClickListener,
+        CopyTmpFileAsyncTask.OnCopyTmpFileTaskListener {
 
     private static final String TAG = Uploader.class.getSimpleName();
 
@@ -453,50 +456,17 @@ public class Uploader extends FileActivity
                            String data = c.getString(index);
                            String filePath = mUploadPath +
                                    c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME));
-                           String fullTempPath = FileStorageUtils.getTemporalPath(getAccount().name)
-                                   + filePath;
-                           InputStream inputStream = null;
-                           FileOutputStream outputStream = null;
+
                            if (data == null) {
-                             try {
-                                 inputStream = getContentResolver().openInputStream(uri);
-                                 File cacheFile = new File(fullTempPath);
-                                 cacheFile.createNewFile();
-                                 outputStream = new FileOutputStream(fullTempPath);
-                                 byte[]  buffer = new byte[4096];
-
-                                 int count = 0;
-
-                                 while ((count = inputStream.read(buffer)) > 0) {
-                                     outputStream.write(buffer, 0, count);
-                                 }
-
-                                 outputStream.close();
-                                 inputStream.close();
-
-                                 data = fullTempPath;
-                             }catch (Exception e) {
-                                 if (inputStream != null) {
-                                     try {
-                                         inputStream.close();
-                                     } catch (Exception e1) {
-
-                                     }
-                                 }
-
-                                 if (outputStream != null) {
-                                     try {
-                                         outputStream.close();
-                                     } catch (Exception e1) {
-
-                                     }
-                                 }
-
-                                 if (fullTempPath != null) {
-                                     File    f = new File(fullTempPath);
-                                     f.delete();
-                                 }
-                             }
+                               CopyTmpFileAsyncTask copyTask = new CopyTmpFileAsyncTask(this);
+                               Object[] params = { uri, filePath };
+                               try {
+                                   data = copyTask.execute(params).get();
+                               } catch (ExecutionException e) {
+                                   Log_OC.e(TAG, "ExecutionException " + e);
+                               } catch (InterruptedException e) {
+                                   Log_OC.e(TAG, "InterruptedException " + e);
+                               }
                            }
 
                            local.add(data);
@@ -645,16 +615,15 @@ public class Uploader extends FileActivity
         // "/" equals root-directory
         if(last_path.equals("/")) {
             mParents.add("");
-        }
-        else{
+        } else{
             String[] dir_names = last_path.split("/");
             for (String dir : dir_names)
                 mParents.add(dir);
         }
         //Make sure that path still exists, if it doesn't pop the stack and try the previous path
-            while(!getStorageManager().fileExists(generatePath(mParents)) && mParents.size() > 1){
-                mParents.pop();
-            }
+        while(!getStorageManager().fileExists(generatePath(mParents)) && mParents.size() > 1){
+            mParents.pop();
+        }
     }
 
     
@@ -662,17 +631,26 @@ public class Uploader extends FileActivity
     public boolean onOptionsItemSelected(MenuItem item) {
         boolean retval = true;
         switch (item.getItemId()) {
-        case android.R.id.home: {
-            if((mParents.size() > 1)) {                
-                onBackPressed(); 
-            }
-            break;
-        }
-        default:
-            retval = super.onOptionsItemSelected(item);
+            case android.R.id.home:
+                if((mParents.size() > 1)) {
+                    onBackPressed();
+                }
+                break;
+
+            default:
+                retval = super.onOptionsItemSelected(item);
         }
         return retval;
     }
 
-    
+
+    /**
+     * Process the result of CopyTmpFileAsyncTask
+     * @param result
+     */
+    @Override
+    public void OnCopyTmpFileTaskListener(String result) {
+
+    }
+
 }
diff --git a/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java b/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java
new file mode 100644 (file)
index 0000000..a680d0d
--- /dev/null
@@ -0,0 +1,132 @@
+/**
+ *   ownCloud Android client application
+ *
+ *   Copyright (C) 2015 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.utils;
+
+import android.app.Activity;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.net.Uri;
+import android.os.AsyncTask;
+
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.ui.activity.FileActivity;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.lang.ref.WeakReference;
+
+/**
+ * AsyncTask to copy a file from a uri in a temporal file
+ */
+public class CopyTmpFileAsyncTask  extends AsyncTask<Object, Void, String> {
+
+    private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName();
+    private final WeakReference<OnCopyTmpFileTaskListener> mListener;
+    private String mAccountName;
+    private ContentResolver mContentResolver;
+
+    public CopyTmpFileAsyncTask(Activity activity) {
+        mContentResolver = ((FileActivity) activity).getContentResolver();
+        mAccountName = ((FileActivity) activity).getAccount().name;
+        mListener = new WeakReference<OnCopyTmpFileTaskListener>((OnCopyTmpFileTaskListener)activity);
+    }
+
+    @Override
+    protected String doInBackground(Object[] params) {
+        String result = null;
+
+        if (params.length == 2) {
+            Uri uri = (Uri) params[0];
+            String filePath = (String) params[1];
+
+            String fullTempPath = FileStorageUtils.getTemporalPath(mAccountName) + filePath;
+            InputStream inputStream = null;
+            FileOutputStream outputStream = null;
+
+            try {
+                inputStream = mContentResolver.openInputStream(uri);
+                File cacheFile = new File(fullTempPath);
+                File tempDir = cacheFile.getParentFile();
+                if (!tempDir.exists()) {
+                    tempDir.mkdirs();
+                }
+                cacheFile.createNewFile();
+                outputStream = new FileOutputStream(fullTempPath);
+                byte[] buffer = new byte[4096];
+
+                int count = 0;
+
+                while ((count = inputStream.read(buffer)) > 0) {
+                    outputStream.write(buffer, 0, count);
+                }
+
+                outputStream.close();
+                inputStream.close();
+
+                result = fullTempPath;
+            } catch (Exception e) {
+                 Log_OC.e(TAG, "Exception ", e);
+                if (inputStream != null) {
+                    try {
+                        inputStream.close();
+                    } catch (Exception e1) {
+                        Log_OC.e(TAG, "Input Stream Exception ", e1);
+                    }
+                }
+
+                if (outputStream != null) {
+                    try {
+                        outputStream.close();
+                    } catch (Exception e1) {
+                        Log_OC.e(TAG, "Output Stream Exception ", e1);
+                    }
+                }
+
+                if (fullTempPath != null) {
+                    File f = new File(fullTempPath);
+                    f.delete();
+                }
+                result =  null;
+            }
+        } else {
+            Log_OC.e(TAG, "Error in parameters number");
+        }
+
+        return result;
+    }
+
+    @Override
+    protected void onPostExecute(String result) {
+
+        OnCopyTmpFileTaskListener listener = mListener.get();
+        if (listener!= null)
+        {
+            listener.OnCopyTmpFileTaskListener(result);
+        }
+    }
+
+    /*
+     * Interface to retrieve data from recognition task
+     */
+    public interface OnCopyTmpFileTaskListener{
+
+        void OnCopyTmpFileTaskListener(String result);
+    }
+}