Skip to content

Commit fa70a64

Browse files
darron1217infil00p
authored andcommitted
Add Support for input[type=file] File Chooser
1 parent f32917d commit fa70a64

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

src/android/InAppBrowser.java

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Licensed to the Apache Software Foundation (ASF) under one
6868
import java.util.HashMap;
6969
import java.util.StringTokenizer;
7070

71+
import android.util.Log;
72+
import android.webkit.ValueCallback;
73+
import android.webkit.WebChromeClient;
74+
7175
@SuppressLint("SetJavaScriptEnabled")
7276
public class InAppBrowser extends CordovaPlugin {
7377

@@ -104,6 +108,11 @@ public class InAppBrowser extends CordovaPlugin {
104108
private boolean shouldPauseInAppBrowser = false;
105109
private boolean useWideViewPort = true;
106110

111+
private ValueCallback<Uri> mUploadCallback;
112+
private ValueCallback<Uri[]> mUploadCallbackLollipop;
113+
private final static int FILECHOOSER_REQUESTCODE = 1;
114+
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
115+
107116
/**
108117
* Executes the request and returns PluginResult.
109118
*
@@ -722,7 +731,59 @@ public void onClick(View v) {
722731
inAppWebView = new WebView(cordova.getActivity());
723732
inAppWebView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
724733
inAppWebView.setId(Integer.valueOf(6));
725-
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView));
734+
735+
// File Chooser Implemented ChromeClient
736+
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
737+
// For Android 5.0+
738+
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
739+
{
740+
Log.d(LOG_TAG, "File Chooser 5.0+");
741+
// If callback exists, finish it.
742+
if(mUploadCallbackLollipop != null) {
743+
mUploadCallbackLollipop.onReceiveValue(null);
744+
}
745+
mUploadCallbackLollipop = filePathCallback;
746+
747+
// Create File Chooser Intent
748+
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
749+
content.addCategory(Intent.CATEGORY_OPENABLE);
750+
content.setType("*/*");
751+
752+
// [important] Register ActvityResultCallback on Cordova
753+
cordova.setActivityResultCallback(InAppBrowser.this);
754+
cordova.getActivity().startActivityForResult(content, FILECHOOSER_REQUESTCODE_LOLLIPOP);
755+
return true;
756+
}
757+
758+
// For Android 4.1+
759+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
760+
{
761+
Log.d(LOG_TAG, "File Chooser 4.1+");
762+
// Call file chooser for Android 3.0+
763+
openFileChooser(uploadMsg, acceptType);
764+
}
765+
766+
// For Android 3.0+
767+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
768+
{
769+
Log.d(LOG_TAG, "File Chooser 3.0+");
770+
mUploadCallback = uploadMsg;
771+
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
772+
i.addCategory(Intent.CATEGORY_OPENABLE);
773+
774+
cordova.setActivityResultCallback(InAppBrowser.this);
775+
cordova.getActivity().startActivityForResult( Intent.createChooser(i, "File Chooser"), FILECHOOSER_REQUESTCODE);
776+
}
777+
778+
// For Android < 3.0
779+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
780+
Log.d(LOG_TAG, "File Chooser 3.0 <");
781+
// Call file chooser for Android 3.0+
782+
openFileChooser(uploadMsg, "");
783+
}
784+
785+
});
786+
726787
WebViewClient client = new InAppBrowserClient(thatWebView, edittext);
727788
inAppWebView.setWebViewClient(client);
728789
WebSettings settings = inAppWebView.getSettings();
@@ -1026,5 +1087,35 @@ public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, Str
10261087
// By default handle 401 like we'd normally do!
10271088
super.onReceivedHttpAuthRequest(view, handler, host, realm);
10281089
}
1090+
1091+
@Override
1092+
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
1093+
// For Android >= 5.0
1094+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1095+
Log.i("mytag", "onActivityResult (For Android >= 5.0)");
1096+
// If RequestCode or Callback is Invalid
1097+
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
1098+
super.onActivityResult(requestCode, resultCode, intent);
1099+
return;
1100+
}
1101+
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
1102+
mUploadCallbackLollipop = null;
1103+
}
1104+
// For Android < 5.0
1105+
else {
1106+
Log.i("mytag", "onActivityResult (For Android < 5.0)");
1107+
// If RequestCode or Callback is Invalid
1108+
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
1109+
super.onActivityResult(requestCode, resultCode, intent);
1110+
return;
1111+
}
1112+
1113+
if (null == mUploadCallback) return;
1114+
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();
1115+
1116+
mUploadCallback.onReceiveValue(result);
1117+
mUploadCallback = null;
1118+
}
1119+
}
10291120
}
10301121
}

0 commit comments

Comments
 (0)