2012-07-13 18:06:24 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-02-07 14:37:06 +00:00
|
|
|
import org.mozilla.gecko.mozglue.GeckoLoader;
|
2012-08-02 17:33:44 +00:00
|
|
|
import org.mozilla.gecko.util.ActivityResultHandler;
|
|
|
|
|
2012-07-13 18:06:24 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.OpenableColumns;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2012-07-28 00:53:54 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
2013-05-10 01:48:00 +00:00
|
|
|
import java.util.Queue;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2012-07-13 18:06:24 +00:00
|
|
|
abstract class FilePickerResultHandler implements ActivityResultHandler {
|
|
|
|
private static final String LOGTAG = "GeckoFilePickerResultHandler";
|
|
|
|
|
2013-05-10 01:48:00 +00:00
|
|
|
protected final Queue<String> mFilePickerResult;
|
2013-05-29 15:08:55 +00:00
|
|
|
protected final ActivityHandlerHelper.FileResultHandler mHandler;
|
2012-07-13 18:06:24 +00:00
|
|
|
|
2013-05-29 15:08:55 +00:00
|
|
|
protected FilePickerResultHandler(Queue<String> resultQueue, ActivityHandlerHelper.FileResultHandler handler) {
|
2012-07-13 18:06:24 +00:00
|
|
|
mFilePickerResult = resultQueue;
|
2013-05-29 15:08:55 +00:00
|
|
|
mHandler = handler;
|
2012-07-13 18:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected String handleActivityResult(int resultCode, Intent data) {
|
|
|
|
if (data == null || resultCode != Activity.RESULT_OK)
|
|
|
|
return "";
|
|
|
|
Uri uri = data.getData();
|
|
|
|
if (uri == null)
|
|
|
|
return "";
|
|
|
|
if ("file".equals(uri.getScheme())) {
|
|
|
|
String path = uri.getPath();
|
|
|
|
return path == null ? "" : path;
|
|
|
|
}
|
|
|
|
try {
|
2013-05-24 16:21:01 +00:00
|
|
|
ContentResolver cr = GeckoAppShell.getContext().getContentResolver();
|
2012-07-13 18:06:24 +00:00
|
|
|
Cursor cursor = cr.query(uri, new String[] { OpenableColumns.DISPLAY_NAME },
|
|
|
|
null, null, null);
|
|
|
|
String name = null;
|
|
|
|
if (cursor != null) {
|
|
|
|
try {
|
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
name = cursor.getString(0);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2013-05-29 15:08:55 +00:00
|
|
|
|
|
|
|
// tmp filenames must be at least 3 characters long. Add a prefix to make sure that happens
|
2012-07-13 18:06:24 +00:00
|
|
|
String fileName = "tmp_";
|
|
|
|
String fileExt = null;
|
|
|
|
int period;
|
|
|
|
if (name == null || (period = name.lastIndexOf('.')) == -1) {
|
|
|
|
String mimeType = cr.getType(uri);
|
|
|
|
fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType);
|
|
|
|
} else {
|
|
|
|
fileExt = name.substring(period);
|
2013-05-29 15:08:55 +00:00
|
|
|
fileName += name.substring(0, period);
|
2012-07-13 18:06:24 +00:00
|
|
|
}
|
2013-05-29 15:08:55 +00:00
|
|
|
Log.i(LOGTAG, "Filename: " + fileName + " . " + fileExt);
|
2013-05-24 16:21:01 +00:00
|
|
|
File file = File.createTempFile(fileName, fileExt, GeckoLoader.getGREDir(GeckoAppShell.getContext()));
|
2012-07-13 18:06:24 +00:00
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
InputStream is = cr.openInputStream(uri);
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int len = is.read(buf);
|
|
|
|
while (len != -1) {
|
|
|
|
fos.write(buf, 0, len);
|
|
|
|
len = is.read(buf);
|
|
|
|
}
|
|
|
|
fos.close();
|
|
|
|
String path = file.getAbsolutePath();
|
|
|
|
return path == null ? "" : path;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "showing file picker", e);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|