d7a27a38ac0ae07921e29bc905a4bf499bbc112a
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / CopyTmpFileAsyncTask.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package com.owncloud.android.utils;
21
22 import android.app.Activity;
23 import android.content.ContentResolver;
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 int mIndex;
43
44 public int getIndex(){
45 return mIndex;
46 }
47
48 public CopyTmpFileAsyncTask(Activity activity) {
49 mListener = new WeakReference<OnCopyTmpFileTaskListener>((OnCopyTmpFileTaskListener)activity);
50 }
51
52 /**
53 * Params for execute:
54 * - Uri: uri of file
55 * - String: path for saving the file into the app
56 * - int: index of upload
57 * - String: accountName
58 * - ContentResolver: content resolver
59 */
60 @Override
61 protected String doInBackground(Object[] params) {
62 String result = null;
63
64 if (params != null && params.length == 5) {
65 Uri uri = (Uri) params[0];
66 String filePath = (String) params[1];
67 mIndex = ((Integer) params[2]).intValue();
68 String accountName = (String) params[3];
69 ContentResolver contentResolver = (ContentResolver) params[4];
70
71 String fullTempPath = FileStorageUtils.getTemporalPath(accountName) + filePath;
72 InputStream inputStream = null;
73 FileOutputStream outputStream = null;
74
75 try {
76 inputStream = contentResolver.openInputStream(uri);
77 File cacheFile = new File(fullTempPath);
78 File tempDir = cacheFile.getParentFile();
79 if (!tempDir.exists()) {
80 tempDir.mkdirs();
81 }
82 cacheFile.createNewFile();
83 outputStream = new FileOutputStream(fullTempPath);
84 byte[] buffer = new byte[4096];
85
86 int count = 0;
87
88 while ((count = inputStream.read(buffer)) > 0) {
89 outputStream.write(buffer, 0, count);
90 }
91
92 outputStream.close();
93 inputStream.close();
94
95 result = fullTempPath;
96 } catch (Exception e) {
97 Log_OC.e(TAG, "Exception ", e);
98 if (inputStream != null) {
99 try {
100 inputStream.close();
101 } catch (Exception e1) {
102 Log_OC.e(TAG, "Input Stream Exception ", e1);
103 }
104 }
105
106 if (outputStream != null) {
107 try {
108 outputStream.close();
109 } catch (Exception e1) {
110 Log_OC.e(TAG, "Output Stream Exception ", e1);
111 }
112 }
113
114 if (fullTempPath != null) {
115 File f = new File(fullTempPath);
116 f.delete();
117 }
118 result = null;
119 }
120 } else {
121 throw new IllegalArgumentException("Error in parameters number");
122 }
123
124 return result;
125 }
126
127 @Override
128 protected void onPostExecute(String result) {
129
130 OnCopyTmpFileTaskListener listener = mListener.get();
131 if (listener!= null)
132 {
133 listener.onTmpFileCopied(result, mIndex);
134 }
135 }
136
137 /*
138 * Interface to retrieve data from recognition task
139 */
140 public interface OnCopyTmpFileTaskListener{
141
142 void onTmpFileCopied(String result, int index);
143 }
144 }