Merge branch 'develop' into thumbnailOOM
[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.content.ContentResolver;
23 import android.net.Uri;
24 import android.os.AsyncTask;
25
26 import com.owncloud.android.lib.common.utils.Log_OC;
27
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.InputStream;
31 import java.lang.ref.WeakReference;
32
33 /**
34 * AsyncTask to copy a file from a uri in a temporal file
35 */
36 public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
37
38 private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName();
39 private final WeakReference<OnCopyTmpFileTaskListener> mListener;
40 private int mIndex;
41
42 public int getIndex(){
43 return mIndex;
44 }
45
46 public CopyTmpFileAsyncTask(OnCopyTmpFileTaskListener listener) {
47 mListener = new WeakReference<OnCopyTmpFileTaskListener>(listener);
48 }
49
50 /**
51 * Params for execute:
52 * - Uri: uri of file
53 * - String: path for saving the file into the app
54 * - int: index of upload
55 * - String: accountName
56 * - ContentResolver: content resolver
57 */
58 @Override
59 protected String doInBackground(Object[] params) {
60 String result = null;
61
62 if (params != null && params.length == 5) {
63 Uri uri = (Uri) params[0];
64 String filePath = (String) params[1];
65 mIndex = ((Integer) params[2]).intValue();
66 String accountName = (String) params[3];
67 ContentResolver contentResolver = (ContentResolver) params[4];
68
69 String fullTempPath = FileStorageUtils.getTemporalPath(accountName) + filePath;
70 InputStream inputStream = null;
71 FileOutputStream outputStream = null;
72
73 try {
74 inputStream = contentResolver.openInputStream(uri);
75 File cacheFile = new File(fullTempPath);
76 File tempDir = cacheFile.getParentFile();
77 if (!tempDir.exists()) {
78 tempDir.mkdirs();
79 }
80 cacheFile.createNewFile();
81 outputStream = new FileOutputStream(fullTempPath);
82 byte[] buffer = new byte[4096];
83
84 int count = 0;
85
86 while ((count = inputStream.read(buffer)) > 0) {
87 outputStream.write(buffer, 0, count);
88 }
89
90 outputStream.close();
91 inputStream.close();
92
93 result = fullTempPath;
94 } catch (Exception e) {
95 Log_OC.e(TAG, "Exception ", e);
96 if (inputStream != null) {
97 try {
98 inputStream.close();
99 } catch (Exception e1) {
100 Log_OC.e(TAG, "Input Stream Exception ", e1);
101 }
102 }
103
104 if (outputStream != null) {
105 try {
106 outputStream.close();
107 } catch (Exception e1) {
108 Log_OC.e(TAG, "Output Stream Exception ", e1);
109 }
110 }
111
112 if (fullTempPath != null) {
113 File f = new File(fullTempPath);
114 f.delete();
115 }
116 result = null;
117 }
118 } else {
119 throw new IllegalArgumentException("Error in parameters number");
120 }
121
122 return result;
123 }
124
125 @Override
126 protected void onPostExecute(String result) {
127
128 OnCopyTmpFileTaskListener listener = mListener.get();
129 if (listener!= null)
130 {
131 listener.onTmpFileCopied(result, mIndex);
132 }
133 }
134
135 /*
136 * Interface to retrieve data from recognition task
137 */
138 public interface OnCopyTmpFileTaskListener{
139
140 void onTmpFileCopied(String result, int index);
141 }
142 }