-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDocumentHandler.java
More file actions
186 lines (153 loc) · 4.95 KB
/
DocumentHandler.java
File metadata and controls
186 lines (153 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package ch.ti8m.phonegap.plugins;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.webkit.CookieManager;
import android.webkit.MimeTypeMap;
public class DocumentHandler extends CordovaPlugin {
public static final String HANDLE_DOCUMENT_ACTION = "HandleDocumentWithURL";
public static final int ERROR_NO_HANDLER_FOR_DATA_TYPE = 53;
public static final int ERROR_FILE_NOT_FOUND = 2;
public static final int ERROR_UNKNOWN_ERROR = 1;
@Override
public boolean execute(String action, JSONArray args,
final CallbackContext callbackContext) throws JSONException {
if (HANDLE_DOCUMENT_ACTION.equals(action)) {
// parse arguments
final JSONObject arg_object = args.getJSONObject(0);
final String url = arg_object.getString("url");
System.out.println("Found: " + url);
// start async download task
new FileDownloaderAsyncTask(callbackContext, url).execute();
return true;
}
return false;
}
// used for all downloaded files, so we can find and delete them again.
private final static String FILE_PREFIX = "DH_";
/**
* downloads a file from the given url to external storage.
*
* @param url
* @return
*/
private File downloadFile(String url, CallbackContext callbackContext) {
try {
// get an instance of a cookie manager since it has access to our
// auth cookie
CookieManager cookieManager = CookieManager.getInstance();
// get the cookie string for the site.
String auth = null;
if (cookieManager.getCookie(url) != null) {
auth = cookieManager.getCookie(url).toString();
}
URL url2 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
if (auth != null) {
conn.setRequestProperty("Cookie", auth);
}
InputStream reader = conn.getInputStream();
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
File f = File.createTempFile(FILE_PREFIX, "." + extension,
null);
// make sure the receiving app can read this file
f.setReadable(true, false);
FileOutputStream outStream = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int readBytes = reader.read(buffer);
while (readBytes > 0) {
outStream.write(buffer, 0, readBytes);
readBytes = reader.read(buffer);
}
reader.close();
outStream.close();
return f;
} catch (FileNotFoundException e) {
e.printStackTrace();
callbackContext.error(ERROR_FILE_NOT_FOUND);
return null;
} catch (IOException e) {
e.printStackTrace();
callbackContext.error(ERROR_UNKNOWN_ERROR);
return null;
}
}
/**
* Returns the MIME Type of the file by looking at file name extension in
* the URL.
*
* @param url
* @return
*/
private static String getMimeType(String url) {
String mimeType = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
mimeType = mime.getMimeTypeFromExtension(extension);
}
System.out.println("Mime Type: " + mimeType);
return mimeType;
}
private class FileDownloaderAsyncTask extends AsyncTask<Void, Void, File> {
private final CallbackContext callbackContext;
private final String url;
public FileDownloaderAsyncTask(CallbackContext callbackContext,
String url) {
super();
this.callbackContext = callbackContext;
this.url = url;
}
@Override
protected File doInBackground(Void... arg0) {
if(!url.startsWith("file://")){
return downloadFile(url, callbackContext);
}
else{
File file = new File(url.replaceFirst("file://", ""));
return file;
}
}
@Override
protected void onPostExecute(File result) {
if (result == null) {
// case has already been handled
return;
}
Context context = cordova.getActivity().getApplicationContext();
// get mime type of file data
String mimeType = getMimeType(url);
if (mimeType == null) {
callbackContext.error(ERROR_UNKNOWN_ERROR);
return;
}
// start an intent with the file
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result), mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
callbackContext.success(); // Thread-safe.
} catch (ActivityNotFoundException e) {
// happens when we start intent without something that can
// handle it
e.printStackTrace();
callbackContext.error(ERROR_NO_HANDLER_FOR_DATA_TYPE);
}
}
}
}