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.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http")) { // Load the URL in Privacy Browser.
// Apply the domain settings for the new URL.
- applyDomainSettings(url, true);
+ applyDomainSettings(url, true, false);
// Returning false causes the current `WebView` to handle the URL and prevents it from adding redirects to the history list.
return false;
// Apply any custom domain settings if the URL was loaded by navigating history.
if (navigatingHistory) {
- applyDomainSettings(url, true);
+ applyDomainSettings(url, true, false);
}
// Set `urlIsLoading` to `true`, so that redirects while loading do not trigger changes in the user agent, which forces another reload of the existing page.
inputMethodManager.showSoftInput(urlTextBox, 0);
// Apply the domain settings. This clears any settings from the previous domain.
- applyDomainSettings(formattedUrlString, true);
+ applyDomainSettings(formattedUrlString, true, false);
} else { // `WebView` has loaded a webpage.
// Set `formattedUrlString`.
formattedUrlString = url;
// Apply the domain settings if returning from the Domains activity.
if (reapplyDomainSettingsOnRestart) {
// Reapply the domain settings.
- applyDomainSettings(formattedUrlString, false);
+ applyDomainSettings(formattedUrlString, false, true);
// Reset `reapplyDomainSettingsOnRestart`.
reapplyDomainSettingsOnRestart = false;
}
}
+ // 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();
private void loadUrl(String url) {
// Apply any custom domain settings.
- applyDomainSettings(url, true);
+ applyDomainSettings(url, true, false);
// Load the URL.
mainWebView.loadUrl(url, customHeaders);
}
}
- //
+ // `reloadWebsite` is used if returnig from the Domains activity. Otherwise JavaScript might not function correctly if it is newly enabled.
// The deprecated `.getDrawable()` must be used until the minimum API >= 21.
@SuppressWarnings("deprecation")
- private void applyDomainSettings(String url, boolean resetFavoriteIcon) {
+ private void applyDomainSettings(String url, boolean resetFavoriteIcon, boolean reloadWebsite) {
// Reset `navigatingHistory`.
navigatingHistory = false;
if (mainMenu != null) {
updatePrivacyIcons(true);
}
+
+ // Reload the website if returning from the Domains activity.
+ if (reloadWebsite) {
+ mainWebView.reload();
+ }
}
}