]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.java
Allow saving of `data:` URLs. https://redmine.stoutner.com/issues/596
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SaveWebpageDialog.java
index c9e691f63200740d87a52726adf5a77d5557e349..57d9baa219777afa489a6991dc5df0a29bf26930 100644 (file)
@@ -22,19 +22,20 @@ package com.stoutner.privacybrowser.dialogs;
 import android.Manifest;
 import android.annotation.SuppressLint;
 import android.app.Activity;
-import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
-import android.net.Uri;
+import android.content.res.Configuration;
+import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
 import android.provider.DocumentsContract;
 import android.text.Editable;
+import android.text.InputType;
 import android.text.TextWatcher;
 import android.view.View;
 import android.view.WindowManager;
@@ -43,12 +44,16 @@ import android.widget.EditText;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
+import androidx.appcompat.app.AlertDialog;
 import androidx.core.content.ContextCompat;
 import androidx.fragment.app.DialogFragment;
 import androidx.preference.PreferenceManager;
 
+import com.google.android.material.textfield.TextInputLayout;
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import com.stoutner.privacybrowser.asynctasks.GetUrlSize;
+import com.stoutner.privacybrowser.helpers.DownloadLocationHelper;
 
 import java.io.File;
 
@@ -58,9 +63,12 @@ public class SaveWebpageDialog extends DialogFragment {
 
     // The public interface is used to send information back to the parent activity.
     public interface SaveWebpageListener {
-        void onSaveWebpage(int saveType, DialogFragment dialogFragment);
+        void onSaveWebpage(int saveType, String originalUrlString, DialogFragment dialogFragment);
     }
 
+    // Define the get URL size AsyncTask.  This allows previous instances of the task to be cancelled if a new one is run.
+    private AsyncTask getUrlSize;
+
     @Override
     public void onAttach(@NonNull Context context) {
         // Run the default commands.
@@ -70,13 +78,17 @@ public class SaveWebpageDialog extends DialogFragment {
         saveWebpageListener = (SaveWebpageListener) context;
     }
 
-    public static SaveWebpageDialog saveUrl(int saveType, String url) {
+    public static SaveWebpageDialog saveWebpage(int saveType, String urlString, String fileSizeString, String contentDispositionFileNameString, String userAgentString, boolean cookiesEnabled) {
         // Create an arguments bundle.
         Bundle argumentsBundle = new Bundle();
 
         // Store the arguments in the bundle.
         argumentsBundle.putInt("save_type", saveType);
-        argumentsBundle.putString("url", url);
+        argumentsBundle.putString("url_string", urlString);
+        argumentsBundle.putString("file_size_string", fileSizeString);
+        argumentsBundle.putString("content_disposition_file_name_string", contentDispositionFileNameString);
+        argumentsBundle.putString("user_agent_string", userAgentString);
+        argumentsBundle.putBoolean("cookies_enabled", cookiesEnabled);
 
         // Create a new instance of the save webpage dialog.
         SaveWebpageDialog saveWebpageDialog = new SaveWebpageDialog();
@@ -101,82 +113,72 @@ public class SaveWebpageDialog extends DialogFragment {
 
         // Get the arguments from the bundle.
         int saveType = arguments.getInt("save_type");
-        String url = arguments.getString("url");
+        String urlString = arguments.getString("url_string");
+        String fileSizeString = arguments.getString("file_size_string");
+        String contentDispositionFileNameString = arguments.getString("content_disposition_file_name_string");
+        String userAgentString = arguments.getString("user_agent_string");
+        boolean cookiesEnabled = arguments.getBoolean("cookies_enabled");
 
         // Get a handle for the activity and the context.
-        Activity activity = getActivity();
-        Context context = getContext();
-
-        // Remove the incorrect lint warnings below that the activity and context might be null.
-        assert activity != null;
-        assert context != null;
-
-        // Get a handle for the shared preferences.
-        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
-
-        // Get the screenshot and theme preferences.
-        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
-        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+        Activity activity = requireActivity();
+        Context context = requireContext();
 
         // Use an alert dialog builder to create the alert dialog.
-        AlertDialog.Builder dialogBuilder;
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog);
 
-        // Set the style and icon according to the theme.
-        if (darkTheme) {
-            // Set the style.
-            dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
+        // Get the current theme status.
+        int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
 
+        // Set the icon according to the theme.
+        if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {  // The night theme is enabled.
             // Set the icon according to the save type.
             switch (saveType) {
-                case StoragePermissionDialog.SAVE:
-                    dialogBuilder.setIcon(R.drawable.copy_enabled_dark);
+                case StoragePermissionDialog.SAVE_URL:
+                    dialogBuilder.setIcon(R.drawable.copy_enabled_night);
                     break;
 
-                case StoragePermissionDialog.SAVE_AS_ARCHIVE:
-                    dialogBuilder.setIcon(R.drawable.dom_storage_cleared_dark);
+                case StoragePermissionDialog.SAVE_ARCHIVE:
+                    dialogBuilder.setIcon(R.drawable.dom_storage_cleared_night);
                     break;
 
-                case StoragePermissionDialog.SAVE_AS_IMAGE:
-                    dialogBuilder.setIcon(R.drawable.images_enabled_dark);
+                case StoragePermissionDialog.SAVE_IMAGE:
+                    dialogBuilder.setIcon(R.drawable.images_enabled_night);
                     break;
             }
-        } else {
-            // Set the style.
-            dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
-
+        } else {  // The day theme is enabled.
             // Set the icon according to the save type.
             switch (saveType) {
-                case StoragePermissionDialog.SAVE:
-                    dialogBuilder.setIcon(R.drawable.copy_enabled_light);
+                case StoragePermissionDialog.SAVE_URL:
+                    dialogBuilder.setIcon(R.drawable.copy_enabled_day);
                     break;
 
-                case StoragePermissionDialog.SAVE_AS_ARCHIVE:
-                    dialogBuilder.setIcon(R.drawable.dom_storage_cleared_light);
+                case StoragePermissionDialog.SAVE_ARCHIVE:
+                    dialogBuilder.setIcon(R.drawable.dom_storage_cleared_day);
                     break;
 
-                case StoragePermissionDialog.SAVE_AS_IMAGE:
-                    dialogBuilder.setIcon(R.drawable.images_enabled_light);
+                case StoragePermissionDialog.SAVE_IMAGE:
+                    dialogBuilder.setIcon(R.drawable.images_enabled_day);
                     break;
             }
         }
 
         // Set the title according to the type.
         switch (saveType) {
-            case StoragePermissionDialog.SAVE:
+            case StoragePermissionDialog.SAVE_URL:
                 dialogBuilder.setTitle(R.string.save);
                 break;
 
-            case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+            case StoragePermissionDialog.SAVE_ARCHIVE:
                 dialogBuilder.setTitle(R.string.save_archive);
                 break;
 
-            case StoragePermissionDialog.SAVE_AS_IMAGE:
+            case StoragePermissionDialog.SAVE_IMAGE:
                 dialogBuilder.setTitle(R.string.save_image);
                 break;
         }
 
         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
-        dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.save_webpage_dialog, null));
+        dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.save_url_dialog, null));
 
         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
         dialogBuilder.setNegativeButton(R.string.cancel, null);
@@ -184,7 +186,7 @@ public class SaveWebpageDialog extends DialogFragment {
         // Set the save button listener.
         dialogBuilder.setPositiveButton(R.string.save, (DialogInterface dialog, int which) -> {
             // Return the dialog fragment to the parent activity.
-            saveWebpageListener.onSaveWebpage(saveType, this);
+            saveWebpageListener.onSaveWebpage(saveType, urlString, this);
         });
 
         // Create an alert dialog from the builder.
@@ -193,6 +195,12 @@ public class SaveWebpageDialog extends DialogFragment {
         // Remove the incorrect lint warning below that the window might be null.
         assert alertDialog.getWindow() != null;
 
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+        // Get the screenshot preference.
+        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
         // Disable screenshots if not allowed.
         if (!allowScreenshots) {
             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
@@ -202,31 +210,84 @@ public class SaveWebpageDialog extends DialogFragment {
         alertDialog.show();
 
         // Get handles for the layout items.
+        TextInputLayout urlTextInputLayout = alertDialog.findViewById(R.id.url_textinputlayout);
         EditText urlEditText = alertDialog.findViewById(R.id.url_edittext);
         EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext);
         Button browseButton = alertDialog.findViewById(R.id.browse_button);
+        TextView fileSizeTextView = alertDialog.findViewById(R.id.file_size_textview);
         TextView fileExistsWarningTextView = alertDialog.findViewById(R.id.file_exists_warning_textview);
         TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview);
         Button saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
 
-        // Update the status of the save button whe the URL changes.
-        urlEditText.addTextChangedListener(new TextWatcher() {
-            @Override
-            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
-                // Do nothing.
+        // Remove the incorrect warnings that the views might be null.
+        assert urlTextInputLayout != null;
+        assert urlEditText != null;
+        assert fileNameEditText != null;
+        assert browseButton != null;
+        assert fileSizeTextView != null;
+        assert fileExistsWarningTextView != null;
+        assert storagePermissionTextView != null;
+
+        // Set the file size text view.
+        fileSizeTextView.setText(fileSizeString);
+
+        // Modify the layout based on the save type.
+        if (saveType == StoragePermissionDialog.SAVE_URL) {  // A URL is being saved.
+            // Remove the incorrect lint error below that the URL string might be null.
+            assert urlString != null;
+
+            // Populate the URL edit text according to the type.  This must be done before the text change listener is created below so that the file size isn't requested again.
+            if (urlString.startsWith("data:")) {  // The URL contains the entire data of an image.
+                // Get a substring of the data URL with the first 100 characters.  Otherwise, the user interface will freeze while trying to layout the edit text.
+                String urlSubstring = urlString.substring(0, 100) + "…";
+
+                // Populate the URL edit text with the truncated URL.
+                urlEditText.setText(urlSubstring);
+
+                // Disable the editing of the URL edit text.
+                urlEditText.setInputType(InputType.TYPE_NULL);
+            } else {  // The URL contains a reference to the location of the data.
+                // Populate the URL edit text with the full URL.
+                urlEditText.setText(urlString);
             }
 
-            @Override
-            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
-                // Do nothing.
-            }
+            // Update the file size and the status of the save button when the URL changes.
+            urlEditText.addTextChangedListener(new TextWatcher() {
+                @Override
+                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
+                    // Do nothing.
+                }
 
-            @Override
-            public void afterTextChanged(Editable editable) {
-                // Enable the save button if the URL and file name are populated.
-                saveButton.setEnabled(!urlEditText.getText().toString().isEmpty() && !fileNameEditText.getText().toString().isEmpty());
-            }
-        });
+                @Override
+                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
+                    // Do nothing.
+                }
+
+                @Override
+                public void afterTextChanged(Editable editable) {
+                    // Cancel the get URL size AsyncTask if it is running.
+                    if ((getUrlSize != null)) {
+                        getUrlSize.cancel(true);
+                    }
+
+                    // Get the current URL to save.
+                    String urlToSave = urlEditText.getText().toString();
+
+                    // Wipe the file size text view.
+                    fileSizeTextView.setText("");
+
+                    // Get the file size for the current URL.
+                    getUrlSize = new GetUrlSize(context, alertDialog, userAgentString, cookiesEnabled).execute(urlToSave);
+
+                    // Enable the save button if the URL and file name are populated.
+                    saveButton.setEnabled(!urlToSave.isEmpty() && !fileNameEditText.getText().toString().isEmpty());
+                }
+            });
+        } else {  // An archive or an image is being saved.
+            // Hide the URL edit text and the file size text view.
+            urlTextInputLayout.setVisibility(View.GONE);
+            fileSizeTextView.setVisibility(View.GONE);
+        }
 
         // Update the status of the save button when the file name changes.
         fileNameEditText.addTextChangedListener(new TextWatcher() {
@@ -257,8 +318,14 @@ public class SaveWebpageDialog extends DialogFragment {
                     fileExistsWarningTextView.setVisibility(View.GONE);
                 }
 
-                // Enable the save button if the file name is populated.
-                saveButton.setEnabled(!fileNameString.isEmpty() && !urlEditText.getText().toString().isEmpty());
+                // Enable the save button based on the save type.
+                if (saveType == StoragePermissionDialog.SAVE_URL) {  // A URL is being saved.
+                    // Enable the save button if the file name and the URL is populated.
+                    saveButton.setEnabled(!fileNameString.isEmpty() && !urlEditText.getText().toString().isEmpty());
+                } else {  // An archive or an image is being saved.
+                    // Enable the save button if the file name is populated.
+                    saveButton.setEnabled(!fileNameString.isEmpty());
+                }
             }
         });
 
@@ -267,27 +334,18 @@ public class SaveWebpageDialog extends DialogFragment {
 
         // Set the file name according to the type.
         switch (saveType) {
-            case StoragePermissionDialog.SAVE:
-                // Convert the URL to a URI.
-                Uri uri = Uri.parse(url);
-
-                // Get the last path segment.
-                String lastPathSegment = uri.getLastPathSegment();
-
-                // Use a default file name if the last path segment is null.
-                if (lastPathSegment == null) {
-                    lastPathSegment = getString(R.string.file);
-                }
-
-                // Use the last path segment as the file name.
-                fileName = lastPathSegment;
+            case StoragePermissionDialog.SAVE_URL:
+                // Use the file name from the content disposition.
+                fileName = contentDispositionFileNameString;
                 break;
 
-            case StoragePermissionDialog.SAVE_AS_ARCHIVE:
+            case StoragePermissionDialog.SAVE_ARCHIVE:
+                // Use an archive name ending in `.mht`.
                 fileName = getString(R.string.webpage_mht);
                 break;
 
-            case StoragePermissionDialog.SAVE_AS_IMAGE:
+            case StoragePermissionDialog.SAVE_IMAGE:
+                // Use a file name ending in `.png`.
                 fileName = getString(R.string.webpage_png);
                 break;
         }
@@ -295,23 +353,13 @@ public class SaveWebpageDialog extends DialogFragment {
         // Save the file name as the default file name.  This must be final to be used in the lambda below.
         final String defaultFileName = fileName;
 
-        // Create a string for the default file path.
-        String defaultFilePath;
-
-        // Set the default file path according to the storage permission state.
-        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {  // The storage permission has been granted.
-            // Set the default file path to use the external public directory.
-            defaultFilePath = Environment.getExternalStorageDirectory() + "/" + defaultFileName;
+        // Instantiate the download location helper.
+        DownloadLocationHelper downloadLocationHelper = new DownloadLocationHelper();
 
-            // Hide the storage permission text view.
-            storagePermissionTextView.setVisibility(View.GONE);
-        } else {  // The storage permission has not been granted.
-            // Set the default file path to use the external private directory.
-            defaultFilePath = context.getExternalFilesDir(null) + "/" + defaultFileName;
-        }
+        // Get the default file path.
+        String defaultFilePath = downloadLocationHelper.getDownloadLocation(context) + "/" + defaultFileName;
 
-        // Populate the edit texts.
-        urlEditText.setText(url);
+        // Populate the file name edit text.
         fileNameEditText.setText(defaultFilePath);
 
         // Move the cursor to the end of the default file path.
@@ -340,6 +388,11 @@ public class SaveWebpageDialog extends DialogFragment {
             activity.startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_SAVE_WEBPAGE_REQUEST_CODE);
         });
 
+        // Hide the storage permission text view if the permission has already been granted.
+        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
+            storagePermissionTextView.setVisibility(View.GONE);
+        }
+
         // Return the alert dialog.
         return alertDialog;
     }