/*
- * Copyright © 2015-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2015-2020 Soren Stoutner <soren@stoutner.com>.
*
* Download cookie code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
*
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
-import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.DialogFragment;
-import androidx.fragment.app.FragmentManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;
import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter;
import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses;
import com.stoutner.privacybrowser.asynctasks.PopulateBlocklists;
+import com.stoutner.privacybrowser.asynctasks.PrepareSaveDialog;
+import com.stoutner.privacybrowser.asynctasks.SaveUrl;
import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage;
import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
import com.stoutner.privacybrowser.dialogs.CreateHomeScreenShortcutDialog;
-import com.stoutner.privacybrowser.dialogs.DownloadFileDialog;
-import com.stoutner.privacybrowser.dialogs.DownloadImageDialog;
-import com.stoutner.privacybrowser.dialogs.DownloadLocationPermissionDialog;
import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
import com.stoutner.privacybrowser.dialogs.FontSizeDialog;
import com.stoutner.privacybrowser.dialogs.HttpAuthenticationDialog;
+import com.stoutner.privacybrowser.dialogs.OpenDialog;
import com.stoutner.privacybrowser.dialogs.ProxyNotInstalledDialog;
import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
-import com.stoutner.privacybrowser.dialogs.SaveWebpageImageDialog;
+import com.stoutner.privacybrowser.dialogs.SaveDialog;
import com.stoutner.privacybrowser.dialogs.SslCertificateErrorDialog;
import com.stoutner.privacybrowser.dialogs.StoragePermissionDialog;
import com.stoutner.privacybrowser.dialogs.UrlHistoryDialog;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
+import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
-// AppCompatActivity from android.support.v7.app.AppCompatActivity must be used to have access to the SupportActionBar until the minimum API is >= 21.
public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener,
- DownloadFileDialog.DownloadFileListener, DownloadImageDialog.DownloadImageListener, DownloadLocationPermissionDialog.DownloadLocationPermissionDialogListener, EditBookmarkDialog.EditBookmarkListener,
- EditBookmarkFolderDialog.EditBookmarkFolderListener, FontSizeDialog.UpdateFontSizeListener, NavigationView.OnNavigationItemSelectedListener, PinnedMismatchDialog.PinnedMismatchListener, PopulateBlocklists.PopulateBlocklistsListener, SaveWebpageImageDialog.SaveWebpageImageListener,
+ EditBookmarkDialog.EditBookmarkListener, EditBookmarkFolderDialog.EditBookmarkFolderListener, FontSizeDialog.UpdateFontSizeListener, NavigationView.OnNavigationItemSelectedListener,
+ OpenDialog.OpenListener, PinnedMismatchDialog.PinnedMismatchListener, PopulateBlocklists.PopulateBlocklistsListener, SaveDialog.SaveWebpageListener,
StoragePermissionDialog.StoragePermissionDialogListener, UrlHistoryDialog.NavigateHistoryListener, WebViewTabFragment.NewTabListener {
// `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`, `onResume()`, and `applyProxy()`.
public final static int DOMAINS_WEBVIEW_DEFAULT_USER_AGENT = 2;
public final static int DOMAINS_CUSTOM_USER_AGENT = 13;
- // Start activity for result request codes.
- private final int FILE_UPLOAD_REQUEST_CODE = 0;
- public final static int BROWSE_SAVE_WEBPAGE_IMAGE_REQUEST_CODE = 1;
+ // Start activity for result request codes. The public static entries are accessed from `OpenDialog()` and `SaveWebpageDialog()`.
+ public final static int BROWSE_OPEN_REQUEST_CODE = 0;
+ public final static int BROWSE_SAVE_WEBPAGE_REQUEST_CODE = 1;
+ private final int BROWSE_FILE_UPLOAD_REQUEST_CODE = 2;
+ // The proxy mode is public static so it can be accessed from `ProxyHelper()`.
+ // It is also used in `onRestart()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `applyAppSettings()`, and `applyProxy()`.
+ // It will be updated in `applyAppSettings()`, but it needs to be initialized here or the first run of `onPrepareOptionsMenu()` crashes.
+ public static String proxyMode = ProxyHelper.NONE;
+
+
+ // The permission result request codes are used in `onCreateContextMenu()`, `onRequestPermissionResult()`, `onSaveWebpage()`, `onCloseStoragePermissionDialog()`, and `initializeWebView()`.
+ private final int PERMISSION_OPEN_REQUEST_CODE = 0;
+ private final int PERMISSION_SAVE_URL_REQUEST_CODE = 1;
+ private final int PERMISSION_SAVE_AS_ARCHIVE_REQUEST_CODE = 2;
+ private final int PERMISSION_SAVE_AS_IMAGE_REQUEST_CODE = 3;
// The current WebView is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`,
// `findNextOnPage()`, `closeFindOnPage()`, `loadUrlFromTextBox()`, `onSslMismatchBack()`, `applyProxy()`, and `applyDomainSettings()`.
// `webViewDefaultUserAgent` is used in `onCreate()` and `onPrepareOptionsMenu()`.
private String webViewDefaultUserAgent;
- // The proxy mode is used in `onRestart()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `applyAppSettings()`, and `applyProxy()`.
- // It will be updated in `applyAppSettings()`, but it needs to be initialized here or the first run of `onPrepareOptionsMenu()` crashes.
- private String proxyMode = ProxyHelper.NONE;
-
// The incognito mode is set in `applyAppSettings()` and used in `initializeWebView()`.
private boolean incognitoModeEnabled;
private boolean sanitizeFacebookClickIds;
private boolean sanitizeTwitterAmpRedirects;
- // The download strings are used in `onCreate()`, `onRequestPermissionResult()` and `initializeWebView()`.
- private String downloadUrl;
- private String downloadContentDisposition;
- private long downloadContentLength;
-
- // `downloadImageUrl` is used in `onCreateContextMenu()` and `onRequestPermissionResult()`.
- private String downloadImageUrl;
-
- // The save website image file path string is used in `onSaveWebpageImage()` and `onRequestPermissionResult()`
- private String saveWebsiteImageFilePath;
-
- // The permission result request codes are used in `onCreateContextMenu()`, `onCloseDownloadLocationPermissionDialog()`, `onRequestPermissionResult()`, `onSaveWebpageImage()`,
- // `onCloseStoragePermissionDialog()`, and `initializeWebView()`.
- private final int DOWNLOAD_FILE_REQUEST_CODE = 1;
- private final int DOWNLOAD_IMAGE_REQUEST_CODE = 2;
- private final int SAVE_WEBPAGE_IMAGE_REQUEST_CODE = 3;
+ // The file path strings are used in `onSaveWebpage()` and `onRequestPermissionResult()`
+ private String openFilePath;
+ private String saveWebpageUrl;
+ private String saveWebpageFilePath;
@Override
// Remove the warning about needing to override `performClick()` when using an `OnTouchListener` with `WebView`.
@SuppressLint("ClickableViewAccessibility")
protected void onCreate(Bundle savedInstanceState) {
+ // Enable the drawing of the entire webpage. This makes it possible to save a website image. This must be done before anything else happens with the WebView.
if (Build.VERSION.SDK_INT >= 21) {
WebView.enableSlowWholeDocumentDraw();
}
// Select the current user agent menu item. A switch statement cannot be used because the user agents are not compile time constants.
if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[0])) { // Privacy Browser.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_privacy_browser));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_privacy_browser));
// Select the Privacy Browser radio box.
menu.findItem(R.id.user_agent_privacy_browser).setChecked(true);
} else if (currentUserAgent.equals(webViewDefaultUserAgent)) { // WebView Default.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_webview_default));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_webview_default));
// Select the WebView Default radio box.
menu.findItem(R.id.user_agent_webview_default).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[2])) { // Firefox on Android.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_firefox_on_android));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_android));
// Select the Firefox on Android radio box.
menu.findItem(R.id.user_agent_firefox_on_android).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[3])) { // Chrome on Android.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_chrome_on_android));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_android));
// Select the Chrome on Android radio box.
menu.findItem(R.id.user_agent_chrome_on_android).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[4])) { // Safari on iOS.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_safari_on_ios));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_ios));
// Select the Safari on iOS radio box.
menu.findItem(R.id.user_agent_safari_on_ios).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[5])) { // Firefox on Linux.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_firefox_on_linux));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_linux));
// Select the Firefox on Linux radio box.
menu.findItem(R.id.user_agent_firefox_on_linux).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[6])) { // Chromium on Linux.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_chromium_on_linux));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chromium_on_linux));
// Select the Chromium on Linux radio box.
menu.findItem(R.id.user_agent_chromium_on_linux).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[7])) { // Firefox on Windows.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_firefox_on_windows));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_firefox_on_windows));
// Select the Firefox on Windows radio box.
menu.findItem(R.id.user_agent_firefox_on_windows).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[8])) { // Chrome on Windows.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_chrome_on_windows));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_chrome_on_windows));
// Select the Chrome on Windows radio box.
menu.findItem(R.id.user_agent_chrome_on_windows).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[9])) { // Edge on Windows.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_edge_on_windows));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_edge_on_windows));
// Select the Edge on Windows radio box.
menu.findItem(R.id.user_agent_edge_on_windows).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[10])) { // Internet Explorer on Windows.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_internet_explorer_on_windows));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_internet_explorer_on_windows));
// Select the Internet on Windows radio box.
menu.findItem(R.id.user_agent_internet_explorer_on_windows).setChecked(true);
} else if (currentUserAgent.equals(getResources().getStringArray(R.array.user_agent_data)[11])) { // Safari on macOS.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_safari_on_macos));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_safari_on_macos));
// Select the Safari on macOS radio box.
menu.findItem(R.id.user_agent_safari_on_macos).setChecked(true);
} else { // Custom user agent.
// Update the user agent menu item title.
- userAgentMenuItem.setTitle(getString(R.string.user_agent) + " - " + getString(R.string.user_agent_custom));
+ userAgentMenuItem.setTitle(getString(R.string.options_user_agent) + " - " + getString(R.string.user_agent_custom));
// Select the Custom radio box.
menu.findItem(R.id.user_agent_custom).setChecked(true);
// Consume the event.
return true;
- case R.id.add_or_edit_domain:
- if (currentWebView.getDomainSettingsApplied()) { // Edit the current domain settings.
- // Reapply the domain settings on returning to `MainWebViewActivity`.
- reapplyDomainSettingsOnRestart = true;
-
- // Create an intent to launch the domains activity.
- Intent domainsIntent = new Intent(this, DomainsActivity.class);
-
- // Add the extra information to the intent.
- domainsIntent.putExtra("load_domain", currentWebView.getDomainSettingsDatabaseId());
- domainsIntent.putExtra("close_on_back", true);
- domainsIntent.putExtra("current_url", currentWebView.getUrl());
-
- // Get the current certificate.
- SslCertificate sslCertificate = currentWebView.getCertificate();
-
- // Check to see if the SSL certificate is populated.
- if (sslCertificate != null) {
- // Extract the certificate to strings.
- String issuedToCName = sslCertificate.getIssuedTo().getCName();
- String issuedToOName = sslCertificate.getIssuedTo().getOName();
- String issuedToUName = sslCertificate.getIssuedTo().getUName();
- String issuedByCName = sslCertificate.getIssuedBy().getCName();
- String issuedByOName = sslCertificate.getIssuedBy().getOName();
- String issuedByUName = sslCertificate.getIssuedBy().getUName();
- long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
- long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
-
- // Add the certificate to the intent.
- domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
- domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
- domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
- domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
- domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
- domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
- domainsIntent.putExtra("ssl_start_date", startDateLong);
- domainsIntent.putExtra("ssl_end_date", endDateLong);
- }
-
- // Check to see if the current IP addresses have been received.
- if (currentWebView.hasCurrentIpAddresses()) {
- // Add the current IP addresses to the intent.
- domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
- }
-
- // Make it so.
- startActivity(domainsIntent);
- } else { // Add a new domain.
- // Apply the new domain settings on returning to `MainWebViewActivity`.
- reapplyDomainSettingsOnRestart = true;
-
- // Get the current domain
- Uri currentUri = Uri.parse(currentWebView.getUrl());
- String currentDomain = currentUri.getHost();
-
- // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
- DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
-
- // Create the domain and store the database ID.
- int newDomainDatabaseId = domainsDatabaseHelper.addDomain(currentDomain);
-
- // Create an intent to launch the domains activity.
- Intent domainsIntent = new Intent(this, DomainsActivity.class);
-
- // Add the extra information to the intent.
- domainsIntent.putExtra("load_domain", newDomainDatabaseId);
- domainsIntent.putExtra("close_on_back", true);
- domainsIntent.putExtra("current_url", currentWebView.getUrl());
-
- // Get the current certificate.
- SslCertificate sslCertificate = currentWebView.getCertificate();
-
- // Check to see if the SSL certificate is populated.
- if (sslCertificate != null) {
- // Extract the certificate to strings.
- String issuedToCName = sslCertificate.getIssuedTo().getCName();
- String issuedToOName = sslCertificate.getIssuedTo().getOName();
- String issuedToUName = sslCertificate.getIssuedTo().getUName();
- String issuedByCName = sslCertificate.getIssuedBy().getCName();
- String issuedByOName = sslCertificate.getIssuedBy().getOName();
- String issuedByUName = sslCertificate.getIssuedBy().getUName();
- long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
- long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
+ case R.id.refresh:
+ if (menuItem.getTitle().equals(getString(R.string.refresh))) { // The refresh button was pushed.
+ // Reload the current WebView.
+ currentWebView.reload();
+ } else { // The stop button was pushed.
+ // Stop the loading of the WebView.
+ currentWebView.stopLoading();
+ }
- // Add the certificate to the intent.
- domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
- domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
- domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
- domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
- domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
- domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
- domainsIntent.putExtra("ssl_start_date", startDateLong);
- domainsIntent.putExtra("ssl_end_date", endDateLong);
- }
+ // Consume the event.
+ return true;
- // Check to see if the current IP addresses have been received.
- if (currentWebView.hasCurrentIpAddresses()) {
- // Add the current IP addresses to the intent.
- domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
- }
+ case R.id.bookmarks:
+ // Get a handle for the drawer layout.
+ DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
- // Make it so.
- startActivity(domainsIntent);
- }
+ // Open the bookmarks drawer.
+ drawerLayout.openDrawer(GravityCompat.END);
// Consume the event.
return true;
// Consume the event.
return true;
- case R.id.save_as_image:
- // Instantiate the save webpage image dialog.
- DialogFragment saveWebpageImageDialogFragment = new SaveWebpageImageDialog();
+ case R.id.save_url:
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(currentWebView.getCurrentUrl());
- // Show the save webpage image dialog.
- saveWebpageImageDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_as_image));
+ // Consume the event.
+ return true;
+
+ case R.id.save_as_archive:
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_AS_ARCHIVE, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(currentWebView.getCurrentUrl());
+
+ // Consume the event.
+ return true;
+
+ case R.id.save_as_image:
+ // Prepare the save dialog. The dialog will be displayed once the file size adn the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_AS_IMAGE, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(currentWebView.getCurrentUrl());
// Consume the event.
return true;
// Consume the event.
return true;
- case R.id.refresh:
- if (menuItem.getTitle().equals(getString(R.string.refresh))) { // The refresh button was pushed.
- // Reload the current WebView.
- currentWebView.reload();
- } else { // The stop button was pushed.
- // Stop the loading of the WebView.
- currentWebView.stopLoading();
+ case R.id.add_or_edit_domain:
+ if (currentWebView.getDomainSettingsApplied()) { // Edit the current domain settings.
+ // Reapply the domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
+
+ // Create an intent to launch the domains activity.
+ Intent domainsIntent = new Intent(this, DomainsActivity.class);
+
+ // Add the extra information to the intent.
+ domainsIntent.putExtra("load_domain", currentWebView.getDomainSettingsDatabaseId());
+ domainsIntent.putExtra("close_on_back", true);
+ domainsIntent.putExtra("current_url", currentWebView.getUrl());
+
+ // Get the current certificate.
+ SslCertificate sslCertificate = currentWebView.getCertificate();
+
+ // Check to see if the SSL certificate is populated.
+ if (sslCertificate != null) {
+ // Extract the certificate to strings.
+ String issuedToCName = sslCertificate.getIssuedTo().getCName();
+ String issuedToOName = sslCertificate.getIssuedTo().getOName();
+ String issuedToUName = sslCertificate.getIssuedTo().getUName();
+ String issuedByCName = sslCertificate.getIssuedBy().getCName();
+ String issuedByOName = sslCertificate.getIssuedBy().getOName();
+ String issuedByUName = sslCertificate.getIssuedBy().getUName();
+ long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
+ long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
+
+ // Add the certificate to the intent.
+ domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
+ domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
+ domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
+ domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
+ domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
+ domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
+ domainsIntent.putExtra("ssl_start_date", startDateLong);
+ domainsIntent.putExtra("ssl_end_date", endDateLong);
+ }
+
+ // Check to see if the current IP addresses have been received.
+ if (currentWebView.hasCurrentIpAddresses()) {
+ // Add the current IP addresses to the intent.
+ domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
+ }
+
+ // Make it so.
+ startActivity(domainsIntent);
+ } else { // Add a new domain.
+ // Apply the new domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
+
+ // Get the current domain
+ Uri currentUri = Uri.parse(currentWebView.getUrl());
+ String currentDomain = currentUri.getHost();
+
+ // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
+ DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
+
+ // Create the domain and store the database ID.
+ int newDomainDatabaseId = domainsDatabaseHelper.addDomain(currentDomain);
+
+ // Create an intent to launch the domains activity.
+ Intent domainsIntent = new Intent(this, DomainsActivity.class);
+
+ // Add the extra information to the intent.
+ domainsIntent.putExtra("load_domain", newDomainDatabaseId);
+ domainsIntent.putExtra("close_on_back", true);
+ domainsIntent.putExtra("current_url", currentWebView.getUrl());
+
+ // Get the current certificate.
+ SslCertificate sslCertificate = currentWebView.getCertificate();
+
+ // Check to see if the SSL certificate is populated.
+ if (sslCertificate != null) {
+ // Extract the certificate to strings.
+ String issuedToCName = sslCertificate.getIssuedTo().getCName();
+ String issuedToOName = sslCertificate.getIssuedTo().getOName();
+ String issuedToUName = sslCertificate.getIssuedTo().getUName();
+ String issuedByCName = sslCertificate.getIssuedBy().getCName();
+ String issuedByOName = sslCertificate.getIssuedBy().getOName();
+ String issuedByUName = sslCertificate.getIssuedBy().getUName();
+ long startDateLong = sslCertificate.getValidNotBeforeDate().getTime();
+ long endDateLong = sslCertificate.getValidNotAfterDate().getTime();
+
+ // Add the certificate to the intent.
+ domainsIntent.putExtra("ssl_issued_to_cname", issuedToCName);
+ domainsIntent.putExtra("ssl_issued_to_oname", issuedToOName);
+ domainsIntent.putExtra("ssl_issued_to_uname", issuedToUName);
+ domainsIntent.putExtra("ssl_issued_by_cname", issuedByCName);
+ domainsIntent.putExtra("ssl_issued_by_oname", issuedByOName);
+ domainsIntent.putExtra("ssl_issued_by_uname", issuedByUName);
+ domainsIntent.putExtra("ssl_start_date", startDateLong);
+ domainsIntent.putExtra("ssl_end_date", endDateLong);
+ }
+
+ // Check to see if the current IP addresses have been received.
+ if (currentWebView.hasCurrentIpAddresses()) {
+ // Add the current IP addresses to the intent.
+ domainsIntent.putExtra("current_ip_addresses", currentWebView.getCurrentIpAddresses());
+ }
+
+ // Make it so.
+ startActivity(domainsIntent);
}
// Consume the event.
urlHistoryDialogFragment.show(getSupportFragmentManager(), getString(R.string.history));
break;
+ case R.id.open:
+ // Instantiate the open file dialog.
+ DialogFragment openDialogFragment = new OpenDialog();
+
+ // Show the open file dialog.
+ openDialogFragment.show(getSupportFragmentManager(), getString(R.string.open));
+ break;
+
case R.id.requests:
// Populate the resource requests.
RequestsActivity.resourceRequests = currentWebView.getResourceRequests();
// Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
downloadManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ // Make it so.
startActivity(downloadManagerIntent);
break;
// Get handles for the system managers.
final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
- FragmentManager fragmentManager = getSupportFragmentManager();
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// Remove the lint errors below that the clipboard manager might be null.
assert clipboardManager != null;
return true;
});
- // Add a Download URL entry.
- menu.add(R.string.download_url).setOnMenuItemClickListener((MenuItem item) -> {
- // Check if the download should be processed by an external app.
- if (sharedPreferences.getBoolean("download_with_external_app", false)) { // Download with an external app.
- openUrlWithExternalApp(linkUrl);
- } else { // Download with Android's download manager.
- // Check to see if the storage permission has already been granted.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { // The 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.
- // Instantiate 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(fragmentManager, 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 storage permission has already been granted.
- // Get a handle for the download file alert dialog.
- DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(linkUrl, "none", -1);
-
- // Show the download file alert dialog.
- downloadFileDialogFragment.show(fragmentManager, getString(R.string.download));
- }
- }
-
- // Consume the event.
- return true;
- });
-
- // Add a Cancel entry, which by default closes the context menu.
- menu.add(R.string.cancel);
- break;
-
- case WebView.HitTestResult.EMAIL_TYPE:
- // Get the target URL.
- linkUrl = hitTestResult.getExtra();
-
- // Set the target URL as the title of the `ContextMenu`.
- menu.setHeaderTitle(linkUrl);
-
- // Add a Write Email entry.
- menu.add(R.string.write_email).setOnMenuItemClickListener(item -> {
- // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
- Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
-
- // Parse the url and set it as the data for the `Intent`.
- emailIntent.setData(Uri.parse("mailto:" + linkUrl));
-
- // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
- emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- // Make it so.
- startActivity(emailIntent);
-
- // Consume the event.
- return true;
- });
-
- // Add a Copy Email Address entry.
- menu.add(R.string.copy_email_address).setOnMenuItemClickListener(item -> {
- // Save the email address in a `ClipData`.
- ClipData srcEmailTypeClipData = ClipData.newPlainText(getString(R.string.email_address), linkUrl);
-
- // Set the `ClipData` as the clipboard's primary clip.
- clipboardManager.setPrimaryClip(srcEmailTypeClipData);
+ // Add a Save URL entry.
+ menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(linkUrl);
// Consume the event.
return true;
});
- // Add a `Cancel` entry, which by default closes the `ContextMenu`.
+ // Add an empty Cancel entry, which by default closes the context menu.
menu.add(R.string.cancel);
break;
return true;
});
- // Add a View Image entry.
- menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
- // Load the image in the current tab.
- loadUrl(currentWebView, imageUrl);
+ // Add an Open with App entry.
+ menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
+ // Open the image URL with an external app.
+ openWithApp(imageUrl);
// Consume the event.
return true;
});
- // Add a Download Image entry.
- menu.add(R.string.download_image).setOnMenuItemClickListener((MenuItem item) -> {
- // Check if the download should be processed by an external app.
- if (sharedPreferences.getBoolean("download_with_external_app", false)) { // Download with an external app.
- openUrlWithExternalApp(imageUrl);
- } else { // Download with Android's download manager.
- // Check to see if the storage permission has already been granted.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { // The storage permission needs to be requested.
- // Store the image URL for use by `onRequestPermissionResult()`.
- downloadImageUrl = imageUrl;
-
- // 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.
- // Instantiate the download location permission alert dialog and set the download type to DOWNLOAD_IMAGE.
- DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_IMAGE);
-
- // Show the download location permission alert dialog. The permission will be requested when the dialog is closed.
- downloadLocationPermissionDialogFragment.show(fragmentManager, 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_IMAGE_REQUEST_CODE);
- }
- } else { // The storage permission has already been granted.
- // Get a handle for the download image alert dialog.
- DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(imageUrl);
-
- // Show the download image alert dialog.
- downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
- }
- }
+ // Add an Open with Browser entry.
+ menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
+ // Open the image URL with an external browser.
+ openWithBrowser(imageUrl);
// Consume the event.
return true;
});
- // Add a Copy URL entry.
- menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
- // Save the image URL in a clip data.
- ClipData imageTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
-
- // Set the clip data as the clipboard's primary clip.
- clipboardManager.setPrimaryClip(imageTypeClipData);
+ // Add a View Image entry.
+ menu.add(R.string.view_image).setOnMenuItemClickListener(item -> {
+ // Load the image in the current tab.
+ loadUrl(currentWebView, imageUrl);
// Consume the event.
return true;
});
- // Add an Open with App entry.
- menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
- // Open the image URL with an external app.
- openWithApp(imageUrl);
+ // Add a Save Image entry.
+ menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(imageUrl);
// Consume the event.
return true;
});
- // Add an Open with Browser entry.
- menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
- // Open the image URL with an external browser.
- openWithBrowser(imageUrl);
+ // Add a Copy URL entry.
+ menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> {
+ // Save the image URL in a clip data.
+ ClipData imageTypeClipData = ClipData.newPlainText(getString(R.string.url), imageUrl);
+
+ // Set the clip data as the clipboard's primary clip.
+ clipboardManager.setPrimaryClip(imageTypeClipData);
// Consume the event.
return true;
});
- // Add a Cancel entry, which by default closes the context menu.
+ // Add an empty Cancel entry, which by default closes the context menu.
menu.add(R.string.cancel);
break;
return true;
});
+ // Add an Open with App entry.
+ menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
+ // Open the link URL with an external app.
+ openWithApp(linkUrl);
+
+ // Consume the event.
+ return true;
+ });
+
+ // Add an Open with Browser entry.
+ menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
+ // Open the link URL with an external browser.
+ openWithBrowser(linkUrl);
+
+ // Consume the event.
+ return true;
+ });
+
// Add a View Image entry.
menu.add(R.string.view_image).setOnMenuItemClickListener((MenuItem item) -> {
// View the image in the current tab.
return true;
});
- // Add a Download Image entry.
- menu.add(R.string.download_image).setOnMenuItemClickListener((MenuItem item) -> {
- // Check if the download should be processed by an external app.
- if (sharedPreferences.getBoolean("download_with_external_app", false)) { // Download with an external app.
- openUrlWithExternalApp(imageUrl);
- } else { // Download with Android's download manager.
- // Check to see if the storage permission has already been granted.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { // The storage permission needs to be requested.
- // Store the image URL for use by `onRequestPermissionResult()`.
- downloadImageUrl = imageUrl;
-
- // 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.
- // Instantiate the download location permission alert dialog and set the download type to DOWNLOAD_IMAGE.
- DialogFragment downloadLocationPermissionDialogFragment = DownloadLocationPermissionDialog.downloadType(DownloadLocationPermissionDialog.DOWNLOAD_IMAGE);
-
- // Show the download location permission alert dialog. The permission will be requested when the dialog is closed.
- downloadLocationPermissionDialogFragment.show(fragmentManager, 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_IMAGE_REQUEST_CODE);
- }
- } else { // The storage permission has already been granted.
- // Get a handle for the download image alert dialog.
- DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(imageUrl);
-
- // Show the download image alert dialog.
- downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
- }
- }
+ // Add a Save Image entry.
+ menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> {
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(imageUrl);
// Consume the event.
return true;
return true;
});
- // Add an Open with App entry.
- menu.add(R.string.open_with_app).setOnMenuItemClickListener((MenuItem item) -> {
- // Open the link URL with an external app.
- openWithApp(linkUrl);
+ // Add a Save URL entry.
+ menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> {
+ // Prepare the save dialog. The dialog will be displayed once the file size and the content disposition have been acquired.
+ new PrepareSaveDialog(this, this, getSupportFragmentManager(), StoragePermissionDialog.SAVE_URL, currentWebView.getSettings().getUserAgentString(),
+ currentWebView.getAcceptFirstPartyCookies()).execute(linkUrl);
// Consume the event.
return true;
});
- // Add an Open with Browser entry.
- menu.add(R.string.open_with_browser).setOnMenuItemClickListener((MenuItem item) -> {
- // Open the link URL with an external browser.
- openWithBrowser(linkUrl);
+ // Add an empty Cancel entry, which by default closes the context menu.
+ menu.add(R.string.cancel);
+ break;
+
+ case WebView.HitTestResult.EMAIL_TYPE:
+ // Get the target URL.
+ linkUrl = hitTestResult.getExtra();
+
+ // Set the target URL as the title of the `ContextMenu`.
+ menu.setHeaderTitle(linkUrl);
+
+ // Add a Write Email entry.
+ menu.add(R.string.write_email).setOnMenuItemClickListener(item -> {
+ // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
+ Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
+
+ // Parse the url and set it as the data for the `Intent`.
+ emailIntent.setData(Uri.parse("mailto:" + linkUrl));
+
+ // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser.
+ emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ // Make it so.
+ startActivity(emailIntent);
+
+ // Consume the event.
+ return true;
+ });
+
+ // Add a Copy Email Address entry.
+ menu.add(R.string.copy_email_address).setOnMenuItemClickListener(item -> {
+ // Save the email address in a `ClipData`.
+ ClipData srcEmailTypeClipData = ClipData.newPlainText(getString(R.string.email_address), linkUrl);
+
+ // Set the `ClipData` as the clipboard's primary clip.
+ clipboardManager.setPrimaryClip(srcEmailTypeClipData);
// Consume the event.
return true;
});
- // Add a cancel entry, which by default closes the context menu.
+ // Add an empty Cancel entry, which by default closes the context menu.
menu.add(R.string.cancel);
break;
}
}
@Override
- public void onCreateBookmarkFolder(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
+ public void onCreateBookmarkFolder(DialogFragment dialogFragment, @NonNull Bitmap favoriteIconBitmap) {
// Get a handle for the bookmarks list view.
ListView bookmarksListView = findViewById(R.id.bookmarks_drawer_listview);
bookmarksCursorAdapter.changeCursor(bookmarksCursor);
}
- @Override
- public void onCloseDownloadLocationPermissionDialog(int downloadType) {
- switch (downloadType) {
- case DownloadLocationPermissionDialog.DOWNLOAD_FILE:
- // Request the WRITE_EXTERNAL_STORAGE permission with a file request code.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_FILE_REQUEST_CODE);
- break;
-
- case DownloadLocationPermissionDialog.DOWNLOAD_IMAGE:
- // Request the WRITE_EXTERNAL_STORAGE permission with an image request code.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_IMAGE_REQUEST_CODE);
- break;
- }
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- // Get a handle for the fragment manager.
- FragmentManager fragmentManager = getSupportFragmentManager();
-
- switch (requestCode) {
- case DOWNLOAD_FILE_REQUEST_CODE:
- // Show the download file alert dialog. When the dialog closes, the correct command will be used based on the permission status.
- DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(downloadUrl, downloadContentDisposition, downloadContentLength);
-
- // On API 23, displaying the fragment must be delayed or the app will crash.
- if (Build.VERSION.SDK_INT == 23) {
- new Handler().postDelayed(() -> downloadFileDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
- } else {
- downloadFileDialogFragment.show(fragmentManager, getString(R.string.download));
- }
-
- // Reset the download variables.
- downloadUrl = "";
- downloadContentDisposition = "";
- downloadContentLength = 0;
- break;
-
- case DOWNLOAD_IMAGE_REQUEST_CODE:
- // Show the download image alert dialog. When the dialog closes, the correct command will be used based on the permission status.
- DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(downloadImageUrl);
-
- // On API 23, displaying the fragment must be delayed or the app will crash.
- if (Build.VERSION.SDK_INT == 23) {
- new Handler().postDelayed(() -> downloadImageDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
- } else {
- downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
- }
-
- // Reset the image URL variable.
- downloadImageUrl = "";
- break;
-
- case SAVE_WEBPAGE_IMAGE_REQUEST_CODE:
- // Check to see if the storage permission was granted. If the dialog was canceled the grant result will be empty.
- if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // The storage permission was granted.
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebsiteImageFilePath);
- } else { // The storage permission was not granted.
- // Display an error snackbar.
- Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
- }
-
- // Reset the save website image file path.
- saveWebsiteImageFilePath = "";
- break;
- }
- }
-
- @Override
- public void onDownloadImage(DialogFragment dialogFragment, String imageUrl) {
- // Download the image if it has an HTTP or HTTPS URI.
- if (imageUrl.startsWith("http")) {
- // Get a handle for the system `DOWNLOAD_SERVICE`.
- DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
-
- // Parse `imageUrl`.
- DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(imageUrl));
-
- // Get a handle for the cookie manager.
- CookieManager cookieManager = CookieManager.getInstance();
-
- // Pass cookies to download manager if cookies are enabled. This is required to download images from websites that require a login.
- // Code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
- if (cookieManager.acceptCookie()) {
- // Get the cookies for `imageUrl`.
- String cookies = cookieManager.getCookie(imageUrl);
-
- // Add the cookies to `downloadRequest`. In the HTTP request header, cookies are named `Cookie`.
- downloadRequest.addRequestHeader("Cookie", cookies);
- }
-
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get the file name from the dialog fragment.
- EditText downloadImageNameEditText = dialog.findViewById(R.id.download_image_name);
- String imageName = downloadImageNameEditText.getText().toString();
-
- // Specify the download location.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // External write permission granted.
- // Download to the public download directory.
- downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, imageName);
- } else { // External write permission denied.
- // Download to the app's external download directory.
- downloadRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, imageName);
- }
-
- // Allow `MediaScanner` to index the download if it is a media file.
- downloadRequest.allowScanningByMediaScanner();
-
- // Add the URL as the description for the download.
- downloadRequest.setDescription(imageUrl);
-
- // Show the download notification after the download is completed.
- downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
-
- // Remove the lint warning below that `downloadManager` might be `null`.
- assert downloadManager != null;
-
- // Initiate the download.
- downloadManager.enqueue(downloadRequest);
- } else { // The image is not an HTTP or HTTPS URI.
- Snackbar.make(currentWebView, R.string.cannot_download_image, Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-
- @Override
- public void onDownloadFile(DialogFragment dialogFragment, String downloadUrl) {
- // Download the file if it has an HTTP or HTTPS URI.
- if (downloadUrl.startsWith("http")) {
- // Get a handle for the system `DOWNLOAD_SERVICE`.
- DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
-
- // Parse `downloadUrl`.
- DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
-
- // Get a handle for the cookie manager.
- CookieManager cookieManager = CookieManager.getInstance();
-
- // Pass cookies to download manager if cookies are enabled. This is required to download files from websites that require a login.
- // Code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
- if (cookieManager.acceptCookie()) {
- // Get the cookies for `downloadUrl`.
- String cookies = cookieManager.getCookie(downloadUrl);
-
- // Add the cookies to `downloadRequest`. In the HTTP request header, cookies are named `Cookie`.
- downloadRequest.addRequestHeader("Cookie", cookies);
- }
-
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get the file name from the dialog.
- EditText downloadFileNameEditText = dialog.findViewById(R.id.download_file_name);
- String fileName = downloadFileNameEditText.getText().toString();
-
- // Specify the download location.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // External write permission granted.
- // Download to the public download directory.
- downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
- } else { // External write permission denied.
- // Download to the app's external download directory.
- downloadRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
- }
-
- // Allow `MediaScanner` to index the download if it is a media file.
- downloadRequest.allowScanningByMediaScanner();
-
- // Add the URL as the description for the download.
- downloadRequest.setDescription(downloadUrl);
-
- // Show the download notification after the download is completed.
- downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
-
- // Remove the lint warning below that `downloadManager` might be `null`.
- assert downloadManager != null;
-
- // Initiate the download.
- downloadManager.enqueue(downloadRequest);
- } else { // The download is not an HTTP or HTTPS URI.
- Snackbar.make(currentWebView, R.string.cannot_download_file, Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-
// Override `onBackPressed` to handle the navigation drawer and and the WebViews.
@Override
public void onBackPressed() {
// Process the results of a file browse.
@Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
// Run the default commands.
- super.onActivityResult(requestCode, resultCode, data);
+ super.onActivityResult(requestCode, resultCode, returnedIntent);
// Run the commands that correlate to the specified request code.
switch (requestCode) {
- case FILE_UPLOAD_REQUEST_CODE:
+ case BROWSE_FILE_UPLOAD_REQUEST_CODE:
// 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));
+ fileChooserCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, returnedIntent));
}
break;
- case BROWSE_SAVE_WEBPAGE_IMAGE_REQUEST_CODE:
+ case BROWSE_SAVE_WEBPAGE_REQUEST_CODE:
// Don't do anything if the user pressed back from the file picker.
if (resultCode == Activity.RESULT_OK) {
// Get a handle for the save dialog fragment.
- DialogFragment saveWebpageImageDialogFragment= (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_as_image));
+ DialogFragment saveWebpageDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_dialog));
+
+ // Only update the file name if the dialog still exists.
+ if (saveWebpageDialogFragment != null) {
+ // Get a handle for the save webpage dialog.
+ Dialog saveWebpageDialog = saveWebpageDialogFragment.getDialog();
+
+ // Remove the incorrect lint warning below that the dialog might be null.
+ assert saveWebpageDialog != null;
+
+ // Get a handle for the file name edit text.
+ EditText fileNameEditText = saveWebpageDialog.findViewById(R.id.file_name_edittext);
+ TextView fileExistsWarningTextView = saveWebpageDialog.findViewById(R.id.file_exists_warning_textview);
+
+ // Instantiate the file name helper.
+ FileNameHelper fileNameHelper = new FileNameHelper();
+
+ // Get the file path if it isn't null.
+ if (returnedIntent.getData() != null) {
+ // Convert the file name URI to a file name path.
+ String fileNamePath = fileNameHelper.convertUriToFileNamePath(returnedIntent.getData());
+
+ // Set the file name path as the text of the file name edit text.
+ fileNameEditText.setText(fileNamePath);
+
+ // Move the cursor to the end of the file name edit text.
+ fileNameEditText.setSelection(fileNamePath.length());
+
+ // Hide the file exists warning.
+ fileExistsWarningTextView.setVisibility(View.GONE);
+ }
+ }
+ }
+ break;
+
+ case BROWSE_OPEN_REQUEST_CODE:
+ // Don't do anything if the user pressed back from the file picker.
+ if (resultCode == Activity.RESULT_OK) {
+ // Get a handle for the open dialog fragment.
+ DialogFragment openDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.open));
// Only update the file name if the dialog still exists.
- if (saveWebpageImageDialogFragment != null) {
- // Get a handle for the save webpage image dialog.
- Dialog saveWebpageImageDialog = saveWebpageImageDialogFragment.getDialog();
+ if (openDialogFragment != null) {
+ // Get a handle for the open dialog.
+ Dialog openDialog = openDialogFragment.getDialog();
// Remove the incorrect lint warning below that the dialog might be null.
- assert saveWebpageImageDialog != null;
+ assert openDialog != null;
// Get a handle for the file name edit text.
- EditText fileNameEditText = saveWebpageImageDialog.findViewById(R.id.file_name_edittext);
+ EditText fileNameEditText = openDialog.findViewById(R.id.file_name_edittext);
// Instantiate the file name helper.
FileNameHelper fileNameHelper = new FileNameHelper();
// Get the file path if it isn't null.
- if (data.getData() != null) {
+ if (returnedIntent.getData() != null) {
// Convert the file name URI to a file name path.
- String fileNamePath = fileNameHelper.convertUriToFileNamePath(data.getData());
+ String fileNamePath = fileNameHelper.convertUriToFileNamePath(returnedIntent.getData());
// Set the file name path as the text of the file name edit text.
fileNameEditText.setText(fileNamePath);
+
+ // Move the cursor to the end of the file name edit text.
+ fileNameEditText.setSelection(fileNamePath.length());
}
}
}
// Get the font size from the edit text.
try {
- newFontSize = Integer.valueOf(fontSizeEditText.getText().toString());
+ newFontSize = Integer.parseInt(fontSizeEditText.getText().toString());
} catch (Exception exception) {
// If the edit text does not contain a valid font size do nothing.
}
}
@Override
- public void onSaveWebpageImage(DialogFragment dialogFragment) {
+ public void onOpen(DialogFragment dialogFragment) {
// Get the dialog.
Dialog dialog = dialogFragment.getDialog();
EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
// Get the file path string.
- saveWebsiteImageFilePath = fileNameEditText.getText().toString();
+ openFilePath = fileNameEditText.getText().toString();
+
+ // Check to see if the storage permission is needed.
+ if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
+ // Open the file.
+ currentWebView.loadUrl("file://" + openFilePath);
+ } else { // The storage permission has not been granted.
+ // Get the external private directory file.
+ File externalPrivateDirectoryFile = getExternalFilesDir(null);
+
+ // Remove the incorrect lint error below that the file might be null.
+ assert externalPrivateDirectoryFile != null;
+
+ // Get the external private directory string.
+ String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
+
+ // Check to see if the file path is in the external private directory.
+ if (openFilePath.startsWith(externalPrivateDirectory)) { // the file path is in the external private directory.
+ // Open the file.
+ currentWebView.loadUrl("file://" + openFilePath);
+ } else { // The file path is in a public directory.
+ // Check if the user has previously denied the storage permission.
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
+ // Instantiate the storage permission alert dialog.
+ DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(StoragePermissionDialog.OPEN);
+
+ // Show the storage permission alert dialog. The permission will be requested the the dialog is closed.
+ storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
+ } else { // Show the permission request directly.
+ // Request the write external storage permission. The file will be opened when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_OPEN_REQUEST_CODE);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onSaveWebpage(int saveType, DialogFragment dialogFragment) {
+ // Get the dialog.
+ Dialog dialog = dialogFragment.getDialog();
+
+ // Remove the incorrect lint warning below that the dialog might be null.
+ assert dialog != null;
+
+ // Get a handle for the edit texts.
+ EditText urlEditText = dialog.findViewById(R.id.url_edittext);
+ EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
+
+ // Get the strings from the edit texts.
+ saveWebpageUrl = urlEditText.getText().toString();
+ saveWebpageFilePath = fileNameEditText.getText().toString();
// Check to see if the storage permission is needed.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebsiteImageFilePath);
+ //Save the webpage according to the save type.
+ switch (saveType) {
+ case StoragePermissionDialog.SAVE_URL:
+ // Save the URL.
+ new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+ // Save the webpage archive.
+ currentWebView.saveWebArchive(saveWebpageFilePath);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_IMAGE:
+ // Save the webpage image.
+ new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath);
+ break;
+ }
} else { // The storage permission has not been granted.
- // Get the external private directory `File`.
+ // Get the external private directory file.
File externalPrivateDirectoryFile = getExternalFilesDir(null);
// Remove the incorrect lint error below that the file might be null.
String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
// Check to see if the file path is in the external private directory.
- if (saveWebsiteImageFilePath.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebsiteImageFilePath);
+ if (saveWebpageFilePath.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
+ // Save the webpage according to the save type.
+ switch (saveType) {
+ case StoragePermissionDialog.SAVE_URL:
+ // Save the URL.
+ new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+ // Save the webpage archive.
+ currentWebView.saveWebArchive(saveWebpageFilePath);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_IMAGE:
+ // Save the webpage image.
+ new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath);
+ break;
+ }
} else { // The file path is in a public directory.
// Check if the user has previously denied the storage permission.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
// Instantiate the storage permission alert dialog.
- DialogFragment storagePermissionDialogFragment = new StoragePermissionDialog();
+ DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(saveType);
// Show the storage permission alert dialog. The permission will be requested when the dialog is closed.
storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
} else { // Show the permission request directly.
- // Request the write external storage permission. The webpage image will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_WEBPAGE_IMAGE_REQUEST_CODE);
+ switch (saveType) {
+ case StoragePermissionDialog.SAVE_URL:
+ // Request the write external storage permission. The URL will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_URL_REQUEST_CODE);
+
+ case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+ // Request the write external storage permission. The webpage archive will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_AS_ARCHIVE_REQUEST_CODE);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_IMAGE:
+ // Request the write external storage permission. The webpage image will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_AS_IMAGE_REQUEST_CODE);
+ break;
+ }
}
}
}
}
@Override
- public void onCloseStoragePermissionDialog() {
- // Request the write external storage permission. The webpage image will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_WEBPAGE_IMAGE_REQUEST_CODE);
+ public void onCloseStoragePermissionDialog(int requestType) {
+ switch (requestType) {
+ case StoragePermissionDialog.OPEN:
+ // Request the write external storage permission. The file will be opened when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_OPEN_REQUEST_CODE);
+ break;
+
+ case StoragePermissionDialog.SAVE_URL:
+ // Request the write external storage permission. The URL will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_URL_REQUEST_CODE);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+ // Request the write external storage permission. The webpage archive will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_AS_ARCHIVE_REQUEST_CODE);
+ break;
+
+ case StoragePermissionDialog.SAVE_AS_IMAGE:
+ // Request the write external storage permission. The webpage image will be saved when it finishes.
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_AS_IMAGE_REQUEST_CODE);
+ break;
+ }
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+ //Only process the results if they exist (this method is triggered when a dialog is presented the first time for an app, but no grant results are included).
+ if (grantResults.length > 0) {
+ switch (requestCode) {
+ case PERMISSION_OPEN_REQUEST_CODE:
+ // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
+ // Load the file.
+ currentWebView.loadUrl("file://" + openFilePath);
+ } else { // The storage permission was not granted.
+ // Display an error snackbar.
+ Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
+ }
+
+ // Reset the open file path.
+ openFilePath = "";
+ break;
+
+ case PERMISSION_SAVE_URL_REQUEST_CODE:
+ // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
+ // Save the raw URL.
+ new SaveUrl(this, this, saveWebpageFilePath, currentWebView.getSettings().getUserAgentString(), currentWebView.getAcceptFirstPartyCookies()).execute(saveWebpageUrl);
+ } else { // The storage permission was not granted.
+ // Display an error snackbar.
+ Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
+ }
+
+ // Reset the save strings.
+ saveWebpageUrl = "";
+ saveWebpageFilePath = "";
+ break;
+
+ case PERMISSION_SAVE_AS_ARCHIVE_REQUEST_CODE:
+ // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
+ // Save the webpage archive.
+ currentWebView.saveWebArchive(saveWebpageFilePath);
+ } else { // The storage permission was not granted.
+ // Display an error snackbar.
+ Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
+ }
+
+ // Reset the save webpage file path.
+ saveWebpageFilePath = "";
+ break;
+
+ case PERMISSION_SAVE_AS_IMAGE_REQUEST_CODE:
+ // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
+ // Save the webpage image.
+ new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath);
+ } else { // The storage permission was not granted.
+ // Display an error snackbar.
+ Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
+ }
+
+ // Reset the save webpage file path.
+ saveWebpageFilePath = "";
+ break;
+ }
+ }
}
private void applyAppSettings() {
waitingForProxyDialogFragment.dismiss();
}
- // Load any URLs that are waiting for the proxy.
+ // Reload existing URLs and load any URLs that are waiting for the proxy.
for (int i = 0; i < webViewPagerAdapter.getCount(); i++) {
// Get the WebView tab fragment.
WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i);
String waitingForProxyUrlString = nestedScrollWebView.getWaitingForProxyUrlString();
// Load the pending URL if it exists.
- if (!waitingForProxyUrlString.isEmpty()) {
+ if (!waitingForProxyUrlString.isEmpty()) { // A URL is waiting to be loaded.
// Load the URL.
loadUrl(nestedScrollWebView, waitingForProxyUrlString);
// Reset the waiting for proxy URL string.
nestedScrollWebView.resetWaitingForProxyUrlString();
+ } else { // No URL is waiting to be loaded.
+ // Reload the existing URL.
+ nestedScrollWebView.reload();
}
}
}
// Listen for touches on the navigation menu.
navigationView.setNavigationItemSelectedListener(this);
- // Get handles for the navigation menu and the back and forward menu items. The menu is zero-based.
+ // Get handles for the navigation menu and the back and forward menu items. The menu is 0 based.
Menu navigationMenu = navigationView.getMenu();
MenuItem navigationBackMenuItem = navigationMenu.getItem(2);
MenuItem navigationForwardMenuItem = navigationMenu.getItem(3);
MenuItem navigationHistoryMenuItem = navigationMenu.getItem(4);
- MenuItem navigationRequestsMenuItem = navigationMenu.getItem(5);
+ MenuItem navigationRequestsMenuItem = navigationMenu.getItem(6);
// Update the web view pager every time a tab is modified.
webViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
drawerHeaderPaddingTop = statusBarPixelSize + (int) (4 * screenDensity);
drawerHeaderPaddingBottom = (int) (8 * screenDensity);
- // The drawer listener is used to update the navigation menu.`
+ // The drawer listener is used to update the navigation menu.
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
try { // Try the specified font size to see if it is valid.
if (fontSize == 0) { // Apply the default font size.
// Try to set the font size from the value in the app settings.
- nestedScrollWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
+ nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
} else { // Apply the font size from domain settings.
nestedScrollWebView.getSettings().setTextZoom(fontSize);
}
// Apply the default font size setting.
try {
// Try to set the font size from the value in the app settings.
- nestedScrollWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
+ nestedScrollWebView.getSettings().setTextZoom(Integer.parseInt(defaultFontSizeString));
} catch (Exception exception) {
// If the app settings value is invalid, set the font size to 100%.
nestedScrollWebView.getSettings().setTextZoom(100);
}
}
- private void openUrlWithExternalApp(String url) {
- // Create a download intent. Not specifying the action type will display the maximum number of options.
- Intent downloadIntent = new Intent();
-
- // Set the URI and the MIME type. Specifying `text/html` displays a good number of options.
- downloadIntent.setDataAndType(Uri.parse(url), "text/html");
-
- // Flag the intent to open in a new task.
- downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- // Show the chooser.
- startActivity(Intent.createChooser(downloadIntent, getString(R.string.open_with)));
- }
-
private void highlightUrlText() {
// Get a handle for the URL edit text.
EditText urlEditText = findViewById(R.id.url_edittext);
String urlString = urlEditText.getText().toString();
// Highlight the URL according to the protocol.
- if (urlString.startsWith("file://")) { // This is a file URL.
- // De-emphasize only the protocol.
- urlEditText.getText().setSpan(initialGrayColorSpan, 0, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
- } else if (urlString.startsWith("content://")) {
- // De-emphasize only the protocol.
- urlEditText.getText().setSpan(initialGrayColorSpan, 0, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+ if (urlString.startsWith("file://") || urlString.startsWith("content://")) { // This is a file or content URL.
+ // De-emphasize everything before the file name.
+ urlEditText.getText().setSpan(initialGrayColorSpan, 0, urlString.lastIndexOf("/") + 1,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
} else { // This is a web URL.
// Get the index of the `/` immediately after the domain name.
int endOfDomainName = urlString.indexOf("/", (urlString.indexOf("//") + 2));
}
private void openWithApp(String url) {
- // Create the open with intent with `ACTION_VIEW`.
+ // Create an open with app intent with `ACTION_VIEW`.
Intent openWithAppIntent = new Intent(Intent.ACTION_VIEW);
// Set the URI but not the MIME type. This should open all available apps.
// Flag the intent to open in a new task.
openWithAppIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ // Try the intent.
try {
// Show the chooser.
startActivity(openWithAppIntent);
- } catch (ActivityNotFoundException exception) {
+ } catch (ActivityNotFoundException exception) { // There are no apps available to open the URL.
// Show a snackbar with the error.
Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
}
}
private void openWithBrowser(String url) {
- // Create the open with intent with `ACTION_VIEW`.
+ // Create an open with browser intent with `ACTION_VIEW`.
Intent openWithBrowserIntent = new Intent(Intent.ACTION_VIEW);
// Set the URI and the MIME type. `"text/html"` should load browser options.
// Flag the intent to open in a new task.
openWithBrowserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ // Try the intent.
try {
// Show the chooser.
startActivity(openWithBrowserIntent);
- } catch (ActivityNotFoundException exception) {
+ } catch (ActivityNotFoundException exception) { // There are no browsers available to open the URL.
// Show a snackbar with the error.
Snackbar.make(currentWebView, getString(R.string.error) + " " + exception, Snackbar.LENGTH_INDEFINITE).show();
}
// Allow the downloading of files.
nestedScrollWebView.setDownloadListener((String downloadUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) -> {
- // Check if the download should be processed by an external app.
- if (sharedPreferences.getBoolean("download_with_external_app", false)) { // Download with an external app.
- // Create a download intent. Not specifying the action type will display the maximum number of options.
- Intent downloadIntent = new Intent();
-
- // Set the URI and the MIME type. Specifying `text/html` displays a good number of options.
- downloadIntent.setDataAndType(Uri.parse(downloadUrl), "text/html");
-
- // Flag the intent to open in a new task.
- downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- // Show the chooser.
- startActivity(Intent.createChooser(downloadIntent, getString(R.string.open_with)));
- } else { // Download with Android's download manager.
- // 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 storage permission has not been granted.
- // The WRITE_EXTERNAL_STORAGE permission needs to be requested.
-
- // Store the variables for future use by `onRequestPermissionsResult()`.
- this.downloadUrl = downloadUrl;
- downloadContentDisposition = contentDisposition;
- downloadContentLength = contentLength;
-
- // 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.
- // Instantiate 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(getSupportFragmentManager(), 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 storage permission has already been granted.
- // Get a handle for the download file alert dialog.
- DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(downloadUrl, contentDisposition, contentLength);
-
- // Show the download file alert dialog.
- downloadFileDialogFragment.show(getSupportFragmentManager(), getString(R.string.download));
- }
+ // Define a formatted file size string.
+ String formattedFileSizeString;
+
+ // Process the content length if it contains data.
+ if (contentLength > 0) { // The content length is greater than 0.
+ // Format the content length as a string.
+ formattedFileSizeString = NumberFormat.getInstance().format(contentLength) + " " + getString(R.string.bytes);
+ } else { // The content length is not greater than 0.
+ // Set the formatted file size string to be `unknown size`.
+ formattedFileSizeString = getString(R.string.unknown_size);
}
+
+ // Get the file name from the content disposition.
+ String fileNameString = PrepareSaveDialog.getFileNameFromContentDisposition(this, contentDisposition, downloadUrl);
+
+ // Instantiate the save dialog.
+ DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, downloadUrl, formattedFileSizeString, fileNameString, userAgent,
+ nestedScrollWebView.getAcceptFirstPartyCookies());
+
+ // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
+ saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog));
});
// Update the find on page count.
// Store the file path callback.
fileChooserCallback = filePathCallback;
- // Create an intent to open a chooser based ont the file chooser parameters.
+ // Create an intent to open a chooser based on the file chooser parameters.
Intent fileChooserIntent = fileChooserParams.createIntent();
- // Open the file chooser.
- startActivityForResult(fileChooserIntent, FILE_UPLOAD_REQUEST_CODE);
+ // Get a handle for the package manager.
+ PackageManager packageManager = getPackageManager();
+
+ // Check to see if the file chooser intent resolves to an installed package.
+ if (fileChooserIntent.resolveActivity(packageManager) != null) { // The file chooser intent is fine.
+ // Start the file chooser intent.
+ startActivityForResult(fileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
+ } else { // The file chooser intent will cause a crash.
+ // Create a generic intent to open a chooser.
+ Intent genericFileChooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
+
+ // Request an openable file.
+ genericFileChooserIntent.addCategory(Intent.CATEGORY_OPENABLE);
+
+ // Set the file type to everything.
+ genericFileChooserIntent.setType("*/*");
+
+ // Start the generic file chooser intent.
+ startActivityForResult(genericFileChooserIntent, BROWSE_FILE_UPLOAD_REQUEST_CODE);
+ }
}
return true;
}
Menu navigationMenu = navigationView.getMenu();
// Get a handle for the navigation requests menu item. The menu is 0 based.
- MenuItem navigationRequestsMenuItem = navigationMenu.getItem(5);
+ MenuItem navigationRequestsMenuItem = navigationMenu.getItem(6);
// Create an empty web resource response to be used if the resource request is blocked.
WebResourceResponse emptyWebResourceResponse = new WebResourceResponse("text/plain", "utf8", new ByteArrayInputStream("".getBytes()));