X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=5e4d3eeb6a5e1360978659fd027e716985c8edf8;hp=83bd942d518e6e1fc82bacfbc50c069998529bfc;hb=4d51aa9acb8daaec1326f14e5025fde6d1f0dcd8;hpb=b235ba52731f112015d9058ade4c5a66abf7ed5c 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 83bd942d..5e4d3eeb 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2015-2019 Soren Stoutner . + * Copyright © 2015-2020 Soren Stoutner . * * Download cookie code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner . * @@ -119,6 +119,7 @@ import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter; import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses; import com.stoutner.privacybrowser.asynctasks.PopulateBlocklists; +import com.stoutner.privacybrowser.asynctasks.SaveUrl; import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage; import com.stoutner.privacybrowser.dialogs.AdConsentDialog; import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog; @@ -201,10 +202,15 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook public final static int DOMAINS_CUSTOM_USER_AGENT = 13; // Start activity for result request codes. The public static entries are accessed from `OpenDialog()` and `SaveWebpageDialog()`. - public static final int BROWSE_OPEN_REQUEST_CODE = 0; - public static final int BROWSE_SAVE_WEBPAGE_REQUEST_CODE = 1; + 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()`, `onCloseDownloadLocationPermissionDialog()`, `onRequestPermissionResult()`, `onSaveWebpage()`, // `onCloseStoragePermissionDialog()`, and `initializeWebView()`. @@ -213,6 +219,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook private final int PERMISSION_OPEN_REQUEST_CODE = 2; private final int PERMISSION_SAVE_WEBPAGE_ARCHIVE_REQUEST_CODE = 3; private final int PERMISSION_SAVE_WEBPAGE_IMAGE_REQUEST_CODE = 4; + private final int PERMISSION_SAVE_WEBPAGE_RAW_REQUEST_CODE = 5; // The current WebView is used in `onCreate()`, `onPrepareOptionsMenu()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`, // `findNextOnPage()`, `closeFindOnPage()`, `loadUrlFromTextBox()`, `onSslMismatchBack()`, `applyProxy()`, and `applyDomainSettings()`. @@ -238,10 +245,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // `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; @@ -324,6 +327,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // The file path strings are used in `onSaveWebpageImage()` and `onRequestPermissionResult()` private String openFilePath; + private String saveWebpageUrl; private String saveWebpageFilePath; @Override @@ -880,79 +884,79 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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); @@ -1704,22 +1708,32 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Consume the event. return true; + case R.id.save_url: + // Instantiate the save dialog. + DialogFragment saveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE, currentWebView.getCurrentUrl()); + + // Show the save dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); + + // Consume the event. + return true; + case R.id.save_as_archive: // Instantiate the save webpage archive dialog. - DialogFragment saveWebpageArchiveDialogFragment = SaveWebpageDialog.saveWebpage(StoragePermissionDialog.SAVE_ARCHIVE); + DialogFragment saveWebpageArchiveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE_AS_ARCHIVE, currentWebView.getCurrentUrl()); - // Show the save webpage archive dialog. - saveWebpageArchiveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_webpage)); + // Show the save webpage archive dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveWebpageArchiveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); // Consume the event. return true; case R.id.save_as_image: - // Instantiate the save webpage image dialog. - DialogFragment saveWebpageImageDialogFragment = SaveWebpageDialog.saveWebpage(StoragePermissionDialog.SAVE_IMAGE); + // Instantiate the save webpage image dialog. It must be named `save_webpage` so that the file picked can update the file name. + DialogFragment saveWebpageImageDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE_AS_IMAGE, currentWebView.getCurrentUrl()); - // Show the save webpage image dialog. - saveWebpageImageDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_webpage)); + // Show the save webpage image dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveWebpageImageDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); // Consume the event. return true; @@ -2047,8 +2061,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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; @@ -2109,38 +2121,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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}, PERMISSION_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); + // Add a Save URL entry. + menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> { + // Instantiate the save dialog. + DialogFragment saveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE, linkUrl); - // Show the download file alert dialog. - downloadFileDialogFragment.show(fragmentManager, getString(R.string.download)); - } - } + // Show the save dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); // Consume the event. return true; @@ -2217,36 +2204,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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}, PERMISSION_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); + // Add a Save Image entry. + menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> { + // Instantiate the save dialog. + DialogFragment saveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE, imageUrl); - // Show the download image alert dialog. - downloadImageDialogFragment.show(fragmentManager, getString(R.string.download)); - } - } + // Show the save dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); // Consume the event. return true; @@ -2342,41 +2306,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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}, PERMISSION_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)); - } - } - - // Consume the event. - return true; - }); - // Add a Copy URL entry. menu.add(R.string.copy_url).setOnMenuItemClickListener((MenuItem item) -> { // Save the link URL in a clip data. @@ -2389,6 +2318,28 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook return true; }); + menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> { + // Instantiate the save dialog. + DialogFragment saveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE, imageUrl); + + // Show the save raw dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); + + // Consume the event. + return true; + }); + + menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> { + // Instantiate the save dialog. + DialogFragment saveDialogFragment = SaveWebpageDialog.saveUrl(StoragePermissionDialog.SAVE, linkUrl); + + // Show the save raw dialog. It must be named `save_dialog` so that the file picked can update the file name. + saveDialogFragment.show(getSupportFragmentManager(), getString(R.string.save_dialog)); + + // Consume the event. + 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. @@ -2928,7 +2879,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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 saveWebpageDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_webpage)); + DialogFragment saveWebpageDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_dialog)); // Only update the file name if the dialog still exists. if (saveWebpageDialogFragment != null) { @@ -2940,6 +2891,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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(); @@ -2954,6 +2906,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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); } } } @@ -2970,7 +2925,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get a handle for the open dialog. Dialog openDialog = openDialogFragment.getDialog(); - // Remove the incorrect lint warning below tha tth edialog might be null. + // Remove the incorrect lint warning below that the dialog might be null. assert openDialog != null; // Get a handle for the file name edit text. @@ -3198,22 +3153,29 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Remove the incorrect lint warning below that the dialog might be null. assert dialog != null; - // Get a handle for the file name edit text. + // 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 file path string. + // 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 according to the save type. switch (saveType) { - case StoragePermissionDialog.SAVE_ARCHIVE: + case StoragePermissionDialog.SAVE: + // 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_IMAGE: + case StoragePermissionDialog.SAVE_AS_IMAGE: // Save the webpage image. new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath); break; @@ -3232,12 +3194,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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_ARCHIVE: + case StoragePermissionDialog.SAVE: + // 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_IMAGE: + case StoragePermissionDialog.SAVE_AS_IMAGE: // Save the webpage image. new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath); break; @@ -3252,12 +3219,16 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission)); } else { // Show the permission request directly. switch (saveType) { - case StoragePermissionDialog.SAVE_ARCHIVE: + case StoragePermissionDialog.SAVE: + // 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_WEBPAGE_RAW_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_WEBPAGE_ARCHIVE_REQUEST_CODE); break; - case StoragePermissionDialog.SAVE_IMAGE: + 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_WEBPAGE_IMAGE_REQUEST_CODE); break; @@ -3275,12 +3246,17 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_OPEN_REQUEST_CODE); break; - case StoragePermissionDialog.SAVE_ARCHIVE: + case StoragePermissionDialog.SAVE: + // 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_WEBPAGE_RAW_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_WEBPAGE_ARCHIVE_REQUEST_CODE); break; - case StoragePermissionDialog.SAVE_IMAGE: + 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_WEBPAGE_IMAGE_REQUEST_CODE); break; @@ -3366,6 +3342,21 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Reset the save webpage file path. saveWebpageFilePath = ""; break; + + case PERMISSION_SAVE_WEBPAGE_RAW_REQUEST_CODE: + // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty. + if ((grantResults.length > 0) && (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; } } @@ -3598,7 +3589,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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); @@ -3615,12 +3606,15 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook 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(); } } } @@ -4680,20 +4674,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } } - 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);