Skip to content

Commit

Permalink
Add support for input[type=file] for Android.
Browse files Browse the repository at this point in the history
Patch in changes from apache#205
  • Loading branch information
dpa99c committed Mar 22, 2017
1 parent 885881e commit 5040cf1
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion src/android/ThemeableBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand All @@ -66,9 +68,11 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginManager;
import org.apache.cordova.PluginResult;
import org.apache.cordova.Whitelist;
import org.apache.cordova.inappbrowser.InAppBrowser;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -109,6 +113,11 @@ public class ThemeableBrowser extends CordovaPlugin {
private EditText edittext;
private CallbackContext callbackContext;

private ValueCallback<Uri> mUploadCallback;
private ValueCallback<Uri[]> mUploadCallbackLollipop;
private final static int FILECHOOSER_REQUESTCODE = 1;
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;

/**
* Executes the request and returns PluginResult.
*
Expand Down Expand Up @@ -757,7 +766,51 @@ public void onNothingSelected(
((LinearLayout.LayoutParams) inAppWebViewParams).weight = 1;
}
inAppWebView.setLayoutParams(inAppWebViewParams);
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView));
// File Chooser Implemented ChromeClient
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
// For Android 5.0
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
LOG.d(LOG_TAG, "File Chooser 5.0 ");
// If callback exists, finish it.
if(mUploadCallbackLollipop != null) {
mUploadCallbackLollipop.onReceiveValue(null);
}
mUploadCallbackLollipop = filePathCallback;

// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");

// Run cordova startActivityForResult
cordova.startActivityForResult(ThemeableBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
return true;
}

// For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
LOG.d(LOG_TAG, "File Chooser 4.1 ");
// Call file chooser for Android 3.0
openFileChooser(uploadMsg, acceptType);
}

// For Android 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
{
LOG.d(LOG_TAG, "File Chooser 3.0 ");
mUploadCallback = uploadMsg;
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);

// run startActivityForResult
cordova.startActivityForResult(ThemeableBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
}

});


WebViewClient client = new ThemeableBrowserClient(thatWebView, new PageLoadListener() {
@Override
public void onPageFinished(String url, boolean canGoBack, boolean canGoForward) {
Expand Down Expand Up @@ -1184,6 +1237,42 @@ public void onPageFinished(String url, boolean canGoBack,
boolean canGoForward);
}

/**
* Receive File Data from File Chooser
*
* @param requestCode the requested code from chromeclient
* @param resultCode the result code returned from android system
* @param intent the data from android file chooser
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// For Android >= 5.0
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallbackLollipop = null;
}
// For Android < 5.0
else {
LOG.d(LOG_TAG, "onActivityResult (For Android < 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}

if (null == mUploadCallback) return;
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();

mUploadCallback.onReceiveValue(result);
mUploadCallback = null;
}
}

/**
* The webview client receives notifications about appView
*/
Expand Down

0 comments on commit 5040cf1

Please sign in to comment.