X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=4d6d6b18444cc59f63999b3e71020010f83b1daa;hb=43b63d39e3c3663e09fc3afcbed993efcc4f5156;hp=a758fedebb87bc0b883af47524c6efb283404bb0;hpb=6fa2fbb5a767bbe036daae06b0da883c4bafb798;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index a758fede..4d6d6b18 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -88,6 +88,7 @@ import android.view.inputmethod.InputMethodManager; 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; @@ -398,6 +399,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // `oldFolderNameString` is used in `onCreate()` and `onSaveEditBookmarkFolder()`. private String oldFolderNameString; + // `fileChooserCallback` is used in `onCreate()` and `onActivityResult()`. + private ValueCallback fileChooserCallback; + // The download strings are used in `onCreate()` and `onRequestPermissionResult()`. private String downloadUrl; private String downloadContentDisposition; @@ -906,7 +910,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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. @@ -934,7 +938,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook fullScreenVideoFrameLayout.setVisibility(View.VISIBLE); } - // Exit full screen video + // Exit full screen video. + @Override public void onHideCustomView() { // Hide `fullScreenVideoFrameLayout`. fullScreenVideoFrameLayout.removeAllViews(); @@ -955,6 +960,23 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook adView = findViewById(R.id.adview); } } + + // Upload files. + @Override + public boolean onShowFileChooser(WebView webView, ValueCallback 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. @@ -1087,7 +1109,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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; @@ -1220,7 +1242,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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. @@ -1276,7 +1298,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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; @@ -1465,7 +1487,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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; @@ -1717,8 +1739,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Create an intent to launch the domains activity. Intent domainsIntent = new Intent(this, DomainsActivity.class); - // Put extra information instructing the domains activity to directly load the current domain. - domainsIntent.putExtra("LoadDomain", domainSettingsDatabaseId); + // Put extra information instructing the domains activity to directly load the current domain and close on back instread of returning to the domains list. + domainsIntent.putExtra("loadDomain", domainSettingsDatabaseId); + domainsIntent.putExtra("closeOnBack", true); // Make it so. startActivity(domainsIntent); @@ -1740,8 +1763,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Create an intent to launch the domains activity. Intent domainsIntent = new Intent(this, DomainsActivity.class); - // Put extra information instructing the domains activity to directly load the new domain. - domainsIntent.putExtra("LoadDomain", newDomainDatabaseId); + // Put extra information instructing the domains activity to directly load the new domain and close on back instead of returning to the domains list. + domainsIntent.putExtra("loadDomain", newDomainDatabaseId); + domainsIntent.putExtra("closeOnBack", true); // Make it so. startActivity(domainsIntent); @@ -2334,14 +2358,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Set the target URL as the title of the `ContextMenu`. menu.setHeaderTitle(linkUrl); - // Add a `Load URL` entry. - menu.add(R.string.load_url).setOnMenuItemClickListener(item -> { + // Add a Load URL entry. + menu.add(R.string.load_url).setOnMenuItemClickListener((MenuItem item) -> { loadUrl(linkUrl); return false; }); - // Add a `Copy URL` entry. - menu.add(R.string.copy_url).setOnMenuItemClickListener(item -> { + // Add a Copy URL entry. + menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> { // Save the link URL in a `ClipData`. ClipData srcAnchorTypeClipData = ClipData.newPlainText(getString(R.string.url), linkUrl); @@ -2350,6 +2374,38 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook return false; }); + // Add a Download URL entry. + menu.add(R.string.download_url).setOnMenuItemClickListener((MenuItem item) -> { + // Check to see if the WRITE_EXTERNAL_STORAGE permission has already been granted. + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { + // The WRITE_EXTERNAL_STORAGE permission needs to be requested. + + // Store the variables for future use by `onRequestPermissionsResult()`. + downloadUrl = linkUrl; + downloadContentDisposition = "none"; + downloadContentLength = -1; + + // Show a dialog if the user has previously denied the permission. + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first. + // Get a handle for the download location permission alert dialog and set the download type to DOWNLOAD_FILE. + DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_FILE); + + // Show the download location permission alert dialog. The permission will be requested when the the dialog is closed. + downloadLocationPermissionDialogFragment.show(getFragmentManager(), getString(R.string.download_location)); + } else { // Show the permission request directly. + // Request the permission. The download dialog will be launched by `onRequestPermissionResult()`. + ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_FILE_REQUEST_CODE); + } + } else { // The WRITE_EXTERNAL_STORAGE permission has already been granted. + // Get a handle for the download file alert dialog. + AppCompatDialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(linkUrl, "none", -1); + + // Show the download file alert dialog. + downloadFileDialogFragment.show(getSupportFragmentManager(), getString(R.string.download)); + } + return false; + }); + // Add a `Cancel` entry, which by default closes the `ContextMenu`. menu.add(R.string.cancel); break; @@ -2958,6 +3014,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } } + // 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(); @@ -3009,7 +3075,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook private void loadUrl(String url) { // Apply any custom domain settings. - applyDomainSettings(url, true); + applyDomainSettings(url, true, false); // Load the URL. mainWebView.loadUrl(url, customHeaders); @@ -3201,10 +3267,10 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } } - // + // `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; @@ -3498,6 +3564,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook if (mainMenu != null) { updatePrivacyIcons(true); } + + // Reload the website if returning from the Domains activity. + if (reloadWebsite) { + mainWebView.reload(); + } } }