Skip to content

Commit

Permalink
Add capture intents
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasMerz committed Apr 8, 2017
1 parent a4103d8 commit 532e10b
Showing 1 changed file with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.cordova.engine;

import java.util.Arrays;
import java.io.File;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
Expand Down Expand Up @@ -262,7 +265,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
Intent intent = fileChooserParams.createIntent();
Intent[] intents = createCaptureIntent(fileChooserParams.getAcceptTypes());

Intent intent = null;
if (fileChooserParams.isCaptureEnabled() && intents.length == 1) {
intent = intents[0];
} else {
intent = fileChooserParams.createIntent();
}

try {
parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
@Override
Expand All @@ -275,6 +286,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
} catch (ActivityNotFoundException e) {
LOG.w("No activity found to handle file chooser intent.", e);
filePathsCallback.onReceiveValue(null);
} catch(Exception e){
LOG.w("Intent cancelled", e);
}
return true;
}
Expand All @@ -289,4 +302,42 @@ public void onPermissionRequest(final PermissionRequest request) {
public void destroyLastDialog(){
dialogsHelper.destroyLastDialog();
}

private Intent[] createCaptureIntent(String[] acceptTypes) {
String mimeType = "*/*";
if ( acceptTypes != null && acceptTypes.length > 0) {
mimeType = acceptTypes[0];
}
Intent[] intents;
if (mimeType.equals(IMAGE_MIME_TYPE)) {
intents = new Intent[1];
intents[0] = createCameraIntent();
} else if (mimeType.equals(VIDEO_MIME_TYPE)) {
intents = new Intent[1];
intents[0] = createCamcorderIntent();
} else if (mimeType.equals(AUDIO_MIME_TYPE)) {
intents = new Intent[1];
intents[0] = createSoundRecorderIntent();
} else {
intents = new Intent[3];
intents[0] = createCameraIntent();
intents[1] = createCamcorderIntent();
intents[2] = createSoundRecorderIntent();
}
return intents;
}

private Intent createCameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(parentEngine.cordova.getActivity().getApplicationContext().getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator + "capture.jpg");
Uri uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
return intent;
}
private Intent createCamcorderIntent() {
return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
}
private Intent createSoundRecorderIntent() {
return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
}
}

0 comments on commit 532e10b

Please sign in to comment.