CB-8017 Add support for <input type=file> for Lollipop

Also refactors a bit to remove related special-case code from CordovaActivity
This commit is contained in:
Andrew Grieve 2015-01-19 16:15:25 -05:00
parent 56204c5748
commit 62c1c5f38b
2 changed files with 43 additions and 31 deletions

View File

@ -36,7 +36,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Display; import android.view.Display;
@ -48,7 +47,6 @@ import android.view.ViewGroup;
import android.view.ViewParent; import android.view.ViewParent;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -697,23 +695,12 @@ public class CordovaActivity extends Activity implements CordovaInterface {
* @param requestCode The request code originally supplied to startActivityForResult(), * @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from. * allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult(). * @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/ */
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
LOG.d(TAG, "Incoming Result"); LOG.d(TAG, "Incoming Result. Request code = " + requestCode);
super.onActivityResult(requestCode, resultCode, intent); super.onActivityResult(requestCode, resultCode, intent);
Log.d(TAG, "Request code = " + requestCode);
if (appView != null && requestCode == CordovaChromeClient.FILECHOOSER_RESULTCODE) {
ValueCallback<Uri> mUploadMessage = this.appView.getWebChromeClient().getValueCallback();
Log.d(TAG, "did we get here?");
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
Log.d(TAG, "result = " + result);
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
CordovaPlugin callback = this.activityResultCallback; CordovaPlugin callback = this.activityResultCallback;
if(callback == null && initCallbackClass != null) { if(callback == null && initCallbackClass != null) {
// The application was restarted, but had defined an initial callback // The application was restarted, but had defined an initial callback

View File

@ -22,10 +22,14 @@ import org.apache.cordova.CordovaInterface;
import org.apache.cordova.LOG; import org.apache.cordova.LOG;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
@ -68,9 +72,6 @@ public class CordovaChromeClient extends WebChromeClient {
//Keep track of last AlertDialog showed //Keep track of last AlertDialog showed
private AlertDialog lastHandledDialog; private AlertDialog lastHandledDialog;
// File Chooser
public ValueCallback<Uri> mUploadMessage;
@Deprecated @Deprecated
public CordovaChromeClient(CordovaInterface cordova) { public CordovaChromeClient(CordovaInterface cordova) {
this.cordova = cordova; this.cordova = cordova;
@ -310,6 +311,9 @@ public class CordovaChromeClient extends WebChromeClient {
return mVideoProgressView; return mVideoProgressView;
} }
// <input type=file> support:
// openFileChooser() is for pre KitKat and in KitKat mr1 (it's known broken in KitKat).
// For Lollipop, we use onShowFileChooser().
public void openFileChooser(ValueCallback<Uri> uploadMsg) { public void openFileChooser(ValueCallback<Uri> uploadMsg) {
this.openFileChooser(uploadMsg, "*/*"); this.openFileChooser(uploadMsg, "*/*");
} }
@ -318,18 +322,39 @@ public class CordovaChromeClient extends WebChromeClient {
this.openFileChooser(uploadMsg, acceptType, null); this.openFileChooser(uploadMsg, acceptType, null);
} }
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) public void openFileChooser(final ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{ {
mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Intent i = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE);
i.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*");
i.setType("*/*"); cordova.startActivityForResult(new CordovaPlugin() {
this.cordova.getActivity().startActivityForResult(Intent.createChooser(i, "File Browser"), @Override
FILECHOOSER_RESULTCODE); public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
Log.d(TAG, "Receive file chooser URL: " + result);
uploadMsg.onReceiveValue(result);
}
}, intent, FILECHOOSER_RESULTCODE);
} }
public ValueCallback<Uri> getValueCallback() { @TargetApi(Build.VERSION_CODES.LOLLIPOP)
return this.mUploadMessage; @Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
Intent intent = fileChooserParams.createIntent();
try {
cordova.startActivityForResult(new CordovaPlugin() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Uri[] result = WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
Log.d(TAG, "Receive file chooser URL: " + result);
filePathsCallback.onReceiveValue(result);
}
}, intent, FILECHOOSER_RESULTCODE);
} catch (ActivityNotFoundException e) {
Log.w("No activity found to handle file chooser intent.", e);
filePathsCallback.onReceiveValue(null);
}
return true;
} }
public void destroyLastDialog(){ public void destroyLastDialog(){