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=267e59bf769564b92f2d851217e55435bbcf0b3a;hp=4fc10372eab493e85799eb831b7400a63aa2ecc4;hb=9df712df3780161d77d10c6f3a2444bf8f218c99;hpb=774c5c4de0adb1bcfd6474f18d203c197d594782 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 4fc10372..267e59bf 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -117,6 +117,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.PrepareSaveDialog; import com.stoutner.privacybrowser.asynctasks.SaveUrl; import com.stoutner.privacybrowser.asynctasks.SaveWebpageImage; import com.stoutner.privacybrowser.dialogs.AdConsentDialog; @@ -155,6 +156,7 @@ import java.net.MalformedURLException; 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; @@ -1606,34 +1608,25 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook return true; case R.id.save_url: - // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, currentWebView.getCurrentUrl(), currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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()); // Consume the event. return true; case R.id.save_as_archive: - // Instantiate the save webpage archive dialog. - DialogFragment saveWebpageArchiveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_AS_ARCHIVE, currentWebView.getCurrentUrl(), currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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: - // Instantiate the save webpage image dialog. It must be named `save_webpage` so that the file picked can update the file name. - DialogFragment saveWebpageImageDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_AS_IMAGE, currentWebView.getCurrentUrl(), currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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; @@ -2120,12 +2113,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add a Save URL entry. menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> { - // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, linkUrl, currentWebView.getSettings().getUserAgentString(), - currentWebView.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)); + // 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; @@ -2181,12 +2171,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add a Save Image entry. menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> { - // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, imageUrl, currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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; @@ -2284,12 +2271,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add a Save Image entry. menu.add(R.string.save_image).setOnMenuItemClickListener((MenuItem item) -> { - // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, imageUrl, currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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; @@ -2309,12 +2293,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Add a Save URL entry. menu.add(R.string.save_url).setOnMenuItemClickListener((MenuItem item) -> { - // Instantiate the save dialog. - DialogFragment saveDialogFragment = SaveDialog.saveUrl(StoragePermissionDialog.SAVE_URL, linkUrl, currentWebView.getSettings().getUserAgentString(), - currentWebView.getAcceptFirstPartyCookies()); - - // 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)); + // 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; @@ -2412,7 +2393,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } @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); @@ -5228,8 +5209,24 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Allow the downloading of files. nestedScrollWebView.setDownloadListener((String downloadUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) -> { + // 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, userAgent, nestedScrollWebView.getAcceptFirstPartyCookies()); + 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)); @@ -5516,11 +5513,29 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // 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, BROWSE_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; }