This source file includes following definitions.
- JNINamespace
- selectFile
- getFileForImageCapture
- resolveFileName
- onIntentCompleted
- onFileNotSelected
- noSpecificType
- shouldShowTypes
- shouldShowImageTypes
- shouldShowVideoTypes
- shouldShowAudioTypes
- acceptsSpecificType
- captureCamera
- captureCamcorder
- captureMicrophone
- acceptSpecificType
- create
- nativeOnFileSelected
- nativeOnFileNotSelected
package org.chromium.ui.base;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.ui.R;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@JNINamespace("ui")
class SelectFileDialog implements WindowAndroid.IntentCallback{
private static final String IMAGE_TYPE = "image/";
private static final String VIDEO_TYPE = "video/";
private static final String AUDIO_TYPE = "audio/";
private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*";
private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*";
private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*";
private static final String ANY_TYPES = "*/*";
private static final String CAPTURE_IMAGE_DIRECTORY = "browser-photos";
private final long mNativeSelectFileDialog;
private List<String> mFileTypes;
private boolean mCapture;
private Uri mCameraOutputUri;
private SelectFileDialog(long nativeSelectFileDialog) {
mNativeSelectFileDialog = nativeSelectFileDialog;
}
@CalledByNative
private void selectFile(String[] fileTypes, boolean capture, WindowAndroid window) {
mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes));
mCapture = capture;
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mCameraOutputUri = Uri.fromFile(getFileForImageCapture());
camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri);
Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent soundRecorder = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
if (captureCamera()) {
if (window.showIntent(camera, this, R.string.low_memory_error)) return;
} else if (captureCamcorder()) {
if (window.showIntent(camcorder, this, R.string.low_memory_error)) return;
} else if (captureMicrophone()) {
if (window.showIntent(soundRecorder, this, R.string.low_memory_error)) return;
}
Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT);
getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);
ArrayList<Intent> extraIntents = new ArrayList<Intent>();
if (!noSpecificType()) {
if (shouldShowImageTypes()) {
extraIntents.add(camera);
getContentIntent.setType(ALL_IMAGE_TYPES);
} else if (shouldShowVideoTypes()) {
extraIntents.add(camcorder);
getContentIntent.setType(ALL_VIDEO_TYPES);
} else if (shouldShowAudioTypes()) {
extraIntents.add(soundRecorder);
getContentIntent.setType(ALL_AUDIO_TYPES);
}
}
if (extraIntents.isEmpty()) {
getContentIntent.setType(ANY_TYPES);
extraIntents.add(camera);
extraIntents.add(camcorder);
extraIntents.add(soundRecorder);
}
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,
extraIntents.toArray(new Intent[] { }));
chooser.putExtra(Intent.EXTRA_INTENT, getContentIntent);
if (!window.showIntent(chooser, this, R.string.low_memory_error)) {
onFileNotSelected();
}
}
private File getFileForImageCapture() {
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + CAPTURE_IMAGE_DIRECTORY);
if (!cameraDataDir.exists() && !cameraDataDir.mkdirs()) {
cameraDataDir = externalDataDir;
}
File photoFile = new File(cameraDataDir.getAbsolutePath() +
File.separator + System.currentTimeMillis() + ".jpg");
return photoFile;
}
private String resolveFileName(Uri uri, ContentResolver contentResolver) {
if (contentResolver == null || uri == null) return "";
Cursor cursor = null;
try {
cursor = contentResolver.query(uri, null, null, null, null);
if (cursor != null && cursor.getCount() >= 1) {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (index > -1) return cursor.getString(index);
}
} catch (NullPointerException e) {
return "";
} finally {
if (cursor != null) cursor.close();
}
return "";
}
@Override
public void onIntentCompleted(WindowAndroid window, int resultCode,
ContentResolver contentResolver, Intent results) {
if (resultCode != Activity.RESULT_OK) {
onFileNotSelected();
return;
}
if (results == null) {
nativeOnFileSelected(mNativeSelectFileDialog, mCameraOutputUri.getPath(), "");
window.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraOutputUri));
return;
}
if (ContentResolver.SCHEME_FILE.equals(results.getData().getScheme())) {
nativeOnFileSelected(mNativeSelectFileDialog,
results.getData().getSchemeSpecificPart(), "");
return;
}
if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) {
nativeOnFileSelected(mNativeSelectFileDialog,
results.getData().toString(),
resolveFileName(results.getData(),
contentResolver));
return;
}
onFileNotSelected();
window.showError(R.string.opening_file_error);
}
private void onFileNotSelected() {
nativeOnFileNotSelected(mNativeSelectFileDialog);
}
private boolean noSpecificType() {
return mFileTypes.size() != 1 || mFileTypes.contains(ANY_TYPES);
}
private boolean shouldShowTypes(String allTypes, String specificType) {
if (noSpecificType() || mFileTypes.contains(allTypes)) return true;
return acceptSpecificType(specificType);
}
private boolean shouldShowImageTypes() {
return shouldShowTypes(ALL_IMAGE_TYPES, IMAGE_TYPE);
}
private boolean shouldShowVideoTypes() {
return shouldShowTypes(ALL_VIDEO_TYPES, VIDEO_TYPE);
}
private boolean shouldShowAudioTypes() {
return shouldShowTypes(ALL_AUDIO_TYPES, AUDIO_TYPE);
}
private boolean acceptsSpecificType(String type) {
return mFileTypes.size() == 1 && TextUtils.equals(mFileTypes.get(0), type);
}
private boolean captureCamera() {
return mCapture && acceptsSpecificType(ALL_IMAGE_TYPES);
}
private boolean captureCamcorder() {
return mCapture && acceptsSpecificType(ALL_VIDEO_TYPES);
}
private boolean captureMicrophone() {
return mCapture && acceptsSpecificType(ALL_AUDIO_TYPES);
}
private boolean acceptSpecificType(String accept) {
for (String type : mFileTypes) {
if (type.startsWith(accept)) {
return true;
}
}
return false;
}
@CalledByNative
private static SelectFileDialog create(long nativeSelectFileDialog) {
return new SelectFileDialog(nativeSelectFileDialog);
}
private native void nativeOnFileSelected(long nativeSelectFileDialogImpl,
String filePath, String displayName);
private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl);
}