Show Waiting dialog, while copy temporal file async task is working
[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 private int mIndex;
45
46 public int getIndex(){
47 return mIndex;
48 }
49
50 public CopyTmpFileAsyncTask(Activity activity) {
51 mContentResolver = ((FileActivity) activity).getContentResolver();
52 mAccountName = ((FileActivity) activity).getAccount().name;
53 mListener = new WeakReference<OnCopyTmpFileTaskListener>((OnCopyTmpFileTaskListener)activity);
54 }
55
56 @Override
57 protected String doInBackground(Object[] params) {
58 String result = null;
59
60 if (params.length == 3) {
61 Uri uri = (Uri) params[0];
62 String filePath = (String) params[1];
63 mIndex = (int) params[2];
64
65 String fullTempPath = FileStorageUtils.getTemporalPath(mAccountName) + filePath;
66 InputStream inputStream = null;
67 FileOutputStream outputStream = null;
68
69 try {
70 inputStream = mContentResolver.openInputStream(uri);
71 File cacheFile = new File(fullTempPath);
72 File tempDir = cacheFile.getParentFile();
73 if (!tempDir.exists()) {
74 tempDir.mkdirs();
75 }
76 cacheFile.createNewFile();
77 outputStream = new FileOutputStream(fullTempPath);
78 byte[] buffer = new byte[4096];
79
80 int count = 0;
81
82 while ((count = inputStream.read(buffer)) > 0) {
83 outputStream.write(buffer, 0, count);
84 }
85
86 outputStream.close();
87 inputStream.close();
88
89 result = fullTempPath;
90 } catch (Exception e) {
91 Log_OC.e(TAG, "Exception ", e);
92 if (inputStream != null) {
93 try {
94 inputStream.close();
95 } catch (Exception e1) {
96 Log_OC.e(TAG, "Input Stream Exception ", e1);
97 }
98 }
99
100 if (outputStream != null) {
101 try {
102 outputStream.close();
103 } catch (Exception e1) {
104 Log_OC.e(TAG, "Output Stream Exception ", e1);
105 }
106 }
107
108 if (fullTempPath != null) {
109 File f = new File(fullTempPath);
110 f.delete();
111 }
112 result = null;
113 }
114 } else {
115 Log_OC.e(TAG, "Error in parameters number");
116 }
117
118 return result;
119 }
120
121 @Override
122 protected void onPostExecute(String result) {
123
124 OnCopyTmpFileTaskListener listener = mListener.get();
125 if (listener!= null)
126 {
127 listener.OnCopyTmpFileTaskListener(result, mIndex);
128 }
129 }
130
131 /*
132 * Interface to retrieve data from recognition task
133 */
134 public interface OnCopyTmpFileTaskListener{
135
136 void OnCopyTmpFileTaskListener(String result, int index);
137 }
138 }