import android.webkit.CookieManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
+import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
// `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`.
private String oldFolderNameString;
+ // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`.
+ private ValueCallback<Uri[]> fileChooserCallback;
+
// The download strings are used in `onCreate()` and `onRequestPermissionResult()`.
private String downloadUrl;
private String downloadContentDisposition;
webViewTitle = title;
}
- // Enter full screen video
+ // Enter full screen video.
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// Pause the ad if this is the free flavor.
fullScreenVideoFrameLayout.setVisibility(View.VISIBLE);
}
- // Exit full screen video
+ // Exit full screen video.
+ @Override
public void onHideCustomView() {
// Hide `fullScreenVideoFrameLayout`.
fullScreenVideoFrameLayout.removeAllViews();
adView = findViewById(R.id.adview);
}
}
+
+ // Upload files.
+ @Override
+ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
+ // Show the file chooser if the device is running API >= 21.
+ if (Build.VERSION.SDK_INT >= 21) {
+ // Store the file path callback.
+ fileChooserCallback = filePathCallback;
+
+ // Create an intent to open a chooser based ont the file chooser parameters.
+ Intent fileChooserIntent = fileChooserParams.createIntent();
+
+ // Open the file chooser. Currently only one `startActivityForResult` exists in this activity, so the request code, used to differentiate them, is simply `0`.
+ startActivityForResult(fileChooserIntent, 0);
+ }
+ return true;
+ }
});
// Register `mainWebView` for a context menu. This is used to see link targets and download images.
}
}
+ // Process the results of an upload file chooser. Currently there is only one `startActivityForResult` in this activity, so the request code, used to differentiate them, is ignored.
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ // File uploads only work on API >= 21.
+ if (Build.VERSION.SDK_INT >= 21) {
+ // Pass the file to the WebView.
+ fileChooserCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
+ }
+ }
+
private void loadUrlFromTextBox() throws UnsupportedEncodingException {
// Get the text from urlTextBox and convert it to a string. trim() removes white spaces from the beginning and end of the string.
String unformattedUrlString = urlTextBox.getText().toString().trim();