2012-07-13 11:06:24 -07: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 06:37:06 -08:00
|
|
|
import org.mozilla.gecko.mozglue.GeckoLoader;
|
2012-08-02 10:33:44 -07:00
|
|
|
import org.mozilla.gecko.util.ActivityResultHandler;
|
|
|
|
|
2012-07-13 11:06:24 -07:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2014-02-13 08:17:00 -08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.provider.MediaStore;
|
2012-07-13 11:06:24 -07:00
|
|
|
import android.provider.OpenableColumns;
|
2014-02-13 08:17:00 -08:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
|
|
|
import android.support.v4.content.CursorLoader;
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
import android.text.format.Time;
|
2012-07-13 11:06:24 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
2014-02-13 08:17:00 -08:00
|
|
|
import java.io.IOException;
|
2013-05-09 18:48:00 -07:00
|
|
|
import java.util.Queue;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2014-02-13 08:17:00 -08:00
|
|
|
class FilePickerResultHandler implements ActivityResultHandler {
|
2012-07-13 11:06:24 -07:00
|
|
|
private static final String LOGTAG = "GeckoFilePickerResultHandler";
|
|
|
|
|
2013-05-09 18:48:00 -07:00
|
|
|
protected final Queue<String> mFilePickerResult;
|
2014-02-14 12:02:05 -08:00
|
|
|
protected final FilePicker.ResultHandler mHandler;
|
2012-07-13 11:06:24 -07:00
|
|
|
|
2014-02-13 08:17:00 -08:00
|
|
|
// this code is really hacky and doesn't belong anywhere so I'm putting it here for now
|
|
|
|
// until I can come up with a better solution.
|
|
|
|
private String mImageName = "";
|
|
|
|
|
|
|
|
public FilePickerResultHandler(Queue<String> resultQueue) {
|
2012-07-13 11:06:24 -07:00
|
|
|
mFilePickerResult = resultQueue;
|
2014-02-13 08:17:00 -08:00
|
|
|
mHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use this constructor to asynchronously listen for results */
|
2014-02-14 12:02:05 -08:00
|
|
|
public FilePickerResultHandler(FilePicker.ResultHandler handler) {
|
2014-02-13 08:17:00 -08:00
|
|
|
mFilePickerResult = null;
|
2013-05-29 08:08:55 -07:00
|
|
|
mHandler = handler;
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-13 08:17:00 -08:00
|
|
|
private void sendResult(String res) {
|
|
|
|
if (mFilePickerResult != null)
|
|
|
|
mFilePickerResult.offer(res);
|
|
|
|
|
|
|
|
if (mHandler != null)
|
|
|
|
mHandler.gotFile(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int resultCode, Intent intent) {
|
|
|
|
if (resultCode != Activity.RESULT_OK) {
|
|
|
|
sendResult("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Camera results won't return an Intent. Use the file name we passed to the original intent.
|
|
|
|
if (intent == null) {
|
|
|
|
if (mImageName != null) {
|
|
|
|
File file = new File(Environment.getExternalStorageDirectory(), mImageName);
|
|
|
|
sendResult(file.getAbsolutePath());
|
|
|
|
} else {
|
|
|
|
sendResult("");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uri uri = intent.getData();
|
|
|
|
if (uri == null) {
|
|
|
|
sendResult("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some file pickers may return a file uri
|
2012-07-13 11:06:24 -07:00
|
|
|
if ("file".equals(uri.getScheme())) {
|
|
|
|
String path = uri.getPath();
|
2014-02-13 08:17:00 -08:00
|
|
|
sendResult(path == null ? "" : path);
|
|
|
|
return;
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
|
|
|
|
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
|
|
|
|
final LoaderManager lm = fa.getSupportLoaderManager();
|
|
|
|
// Finally, Video pickers and some file pickers may return a content provider.
|
2014-03-04 08:29:26 -08:00
|
|
|
Cursor cursor = null;
|
2012-07-13 11:06:24 -07:00
|
|
|
try {
|
2014-02-13 08:17:00 -08:00
|
|
|
// Try a query to make sure the expected columns exist
|
|
|
|
final ContentResolver cr = fa.getContentResolver();
|
2014-03-04 08:29:26 -08:00
|
|
|
cursor = cr.query(uri, new String[] { MediaStore.Video.Media.DATA }, null, null, null);
|
2014-02-13 08:17:00 -08:00
|
|
|
|
2014-03-04 08:29:26 -08:00
|
|
|
int index = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
|
|
|
|
if (index >= 0) {
|
|
|
|
lm.initLoader(intent.hashCode(), null, new VideoLoaderCallbacks(uri));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch(Exception ex) {
|
|
|
|
// We'll try a different loader below
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
|
|
|
|
lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String generateImageName() {
|
|
|
|
Time now = new Time();
|
|
|
|
now.setToNow();
|
|
|
|
mImageName = now.format("%Y-%m-%d %H.%M.%S") + ".jpg";
|
|
|
|
return mImageName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class VideoLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
|
|
final private Uri mUri;
|
|
|
|
public VideoLoaderCallbacks(Uri uri) {
|
|
|
|
mUri = uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
|
|
|
|
return new CursorLoader(fa,
|
|
|
|
mUri,
|
2014-03-04 08:29:26 -08:00
|
|
|
new String[] { MediaStore.Video.Media.DATA },
|
2014-02-13 08:17:00 -08:00
|
|
|
null, // selection
|
|
|
|
null, // selectionArgs
|
|
|
|
null); // sortOrder
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
String res = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
|
|
|
|
sendResult(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
private class FileLoaderCallbacks implements LoaderCallbacks<Cursor> {
|
|
|
|
final private Uri mUri;
|
|
|
|
public FileLoaderCallbacks(Uri uri) {
|
|
|
|
mUri = uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
|
|
|
|
return new CursorLoader(fa,
|
|
|
|
mUri,
|
|
|
|
new String[] { OpenableColumns.DISPLAY_NAME },
|
|
|
|
null, // selection
|
|
|
|
null, // selectionArgs
|
|
|
|
null); // sortOrder
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
String name = cursor.getString(0);
|
|
|
|
// tmp filenames must be at least 3 characters long. Add a prefix to make sure that happens
|
|
|
|
String fileName = "tmp_";
|
|
|
|
String fileExt = null;
|
|
|
|
int period;
|
|
|
|
|
|
|
|
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
|
|
|
|
final ContentResolver cr = fa.getContentResolver();
|
|
|
|
|
|
|
|
// Generate an extension if we don't already have one
|
|
|
|
if (name == null || (period = name.lastIndexOf('.')) == -1) {
|
|
|
|
String mimeType = cr.getType(mUri);
|
|
|
|
fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType);
|
|
|
|
} else {
|
|
|
|
fileExt = name.substring(period);
|
|
|
|
fileName += name.substring(0, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now write the data to the temp file
|
2012-07-13 11:06:24 -07:00
|
|
|
try {
|
2014-02-13 08:17:00 -08:00
|
|
|
File file = File.createTempFile(fileName, fileExt, GeckoLoader.getGREDir(GeckoAppShell.getContext()));
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
InputStream is = cr.openInputStream(mUri);
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int len = is.read(buf);
|
|
|
|
while (len != -1) {
|
|
|
|
fos.write(buf, 0, len);
|
|
|
|
len = is.read(buf);
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
fos.close();
|
2014-02-14 09:13:07 -08:00
|
|
|
|
2014-02-13 08:17:00 -08:00
|
|
|
String path = file.getAbsolutePath();
|
|
|
|
sendResult((path == null) ? "" : path);
|
|
|
|
} catch(IOException ex) {
|
|
|
|
Log.i(LOGTAG, "Error writing file", ex);
|
|
|
|
}
|
2012-07-13 11:06:24 -07:00
|
|
|
} else {
|
2014-02-13 08:17:00 -08:00
|
|
|
sendResult("");
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoaderReset(Loader<Cursor> loader) { }
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
|
2012-07-13 11:06:24 -07:00
|
|
|
}
|
2014-02-13 08:17:00 -08:00
|
|
|
|