Execute the file to upload copy in an AsyncTask
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / CopyTmpFileAsyncTask.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2015 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.utils;
20
21 import android.app.Activity;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.net.Uri;
25 import android.os.AsyncTask;
26
27 import com.owncloud.android.lib.common.utils.Log_OC;
28 import com.owncloud.android.ui.activity.FileActivity;
29
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.InputStream;
33 import java.lang.ref.WeakReference;
34
35 /**
36 * AsyncTask to copy a file from a uri in a temporal file
37 */
38 public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
39
40 private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName();
41 private final WeakReference<OnCopyTmpFileTaskListener> mListener;
42 private String mAccountName;
43 private ContentResolver mContentResolver;
44
45 public CopyTmpFileAsyncTask(Activity activity) {
46 mContentResolver = ((FileActivity) activity).getContentResolver();
47 mAccountName = ((FileActivity) activity).getAccount().name;
48 mListener = new WeakReference<OnCopyTmpFileTaskListener>((OnCopyTmpFileTaskListener)activity);
49 }
50
51 @Override
52 protected String doInBackground(Object[] params) {
53 String result = null;
54
55 if (params.length == 2) {
56 Uri uri = (Uri) params[0];
57 String filePath = (String) params[1];
58
59 String fullTempPath = FileStorageUtils.getTemporalPath(mAccountName) + filePath;
60 InputStream inputStream = null;
61 FileOutputStream outputStream = null;
62
63 try {
64 inputStream = mContentResolver.openInputStream(uri);
65 File cacheFile = new File(fullTempPath);
66 File tempDir = cacheFile.getParentFile();
67 if (!tempDir.exists()) {
68 tempDir.mkdirs();
69 }
70 cacheFile.createNewFile();
71 outputStream = new FileOutputStream(fullTempPath);
72 byte[] buffer = new byte[4096];
73
74 int count = 0;
75
76 while ((count = inputStream.read(buffer)) > 0) {
77 outputStream.write(buffer, 0, count);
78 }
79
80 outputStream.close();
81 inputStream.close();
82
83 result = fullTempPath;
84 } catch (Exception e) {
85 Log_OC.e(TAG, "Exception ", e);
86 if (inputStream != null) {
87 try {
88 inputStream.close();
89 } catch (Exception e1) {
90 Log_OC.e(TAG, "Input Stream Exception ", e1);
91 }
92 }
93
94 if (outputStream != null) {
95 try {
96 outputStream.close();
97 } catch (Exception e1) {
98 Log_OC.e(TAG, "Output Stream Exception ", e1);
99 }
100 }
101
102 if (fullTempPath != null) {
103 File f = new File(fullTempPath);
104 f.delete();
105 }
106 result = null;
107 }
108 } else {
109 Log_OC.e(TAG, "Error in parameters number");
110 }
111
112 return result;
113 }
114
115 @Override
116 protected void onPostExecute(String result) {
117
118 OnCopyTmpFileTaskListener listener = mListener.get();
119 if (listener!= null)
120 {
121 listener.OnCopyTmpFileTaskListener(result);
122 }
123 }
124
125 /*
126 * Interface to retrieve data from recognition task
127 */
128 public interface OnCopyTmpFileTaskListener{
129
130 void OnCopyTmpFileTaskListener(String result);
131 }
132 }