]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveWebpageDialog.java
Implement opening of local files. https://redmine.stoutner.com/issues/513
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SaveWebpageDialog.java
index 1c24e0bedc6ca95f4414c74ae3c92bd92001c205..72d0f20a6725cf7d65c7ca584243662f470fa4dc 100644 (file)
@@ -50,10 +50,6 @@ import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 
 public class SaveWebpageDialog extends DialogFragment {
-    // Define the save type constants.
-    public static final int ARCHIVE = 0;
-    public static final int IMAGE = 1;
-
     // Define the save webpage listener.
     private SaveWebpageListener saveWebpageListener;
 
@@ -88,7 +84,7 @@ public class SaveWebpageDialog extends DialogFragment {
         return saveWebpageDialog;
     }
 
-    // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
+    // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
     @SuppressLint("InflateParams")
     @Override
     @NonNull
@@ -127,11 +123,11 @@ public class SaveWebpageDialog extends DialogFragment {
 
             // Set the icon according to the save type.
             switch (saveType) {
-                case ARCHIVE:
+                case StoragePermissionDialog.SAVE_ARCHIVE:
                     dialogBuilder.setIcon(R.drawable.dom_storage_cleared_dark);
                     break;
 
-                case IMAGE:
+                case StoragePermissionDialog.SAVE_IMAGE:
                     dialogBuilder.setIcon(R.drawable.images_enabled_dark);
                     break;
             }
@@ -141,11 +137,11 @@ public class SaveWebpageDialog extends DialogFragment {
 
             // Set the icon according to the save type.
             switch (saveType) {
-                case ARCHIVE:
+                case StoragePermissionDialog.SAVE_ARCHIVE:
                     dialogBuilder.setIcon(R.drawable.dom_storage_cleared_light);
                     break;
 
-                case IMAGE:
+                case StoragePermissionDialog.SAVE_IMAGE:
                     dialogBuilder.setIcon(R.drawable.images_enabled_light);
                     break;
             }
@@ -153,11 +149,11 @@ public class SaveWebpageDialog extends DialogFragment {
 
         // Set the title according to the type.
         switch (saveType) {
-            case ARCHIVE:
+            case StoragePermissionDialog.SAVE_ARCHIVE:
                 dialogBuilder.setTitle(R.string.save_archive);
                 break;
 
-            case IMAGE:
+            case StoragePermissionDialog.SAVE_IMAGE:
                 dialogBuilder.setTitle(R.string.save_image);
                 break;
         }
@@ -177,7 +173,7 @@ public class SaveWebpageDialog extends DialogFragment {
         // Create an alert dialog from the builder.
         AlertDialog alertDialog = dialogBuilder.create();
 
-        // Remove the incorrect lint warning below that `getWindow()` might be null.
+        // Remove the incorrect lint warning below that the window might be null.
         assert alertDialog.getWindow() != null;
 
         // Disable screenshots if not allowed.
@@ -199,11 +195,11 @@ public class SaveWebpageDialog extends DialogFragment {
 
         // Set the default file name according to the type.
         switch (saveType) {
-            case ARCHIVE:
+            case StoragePermissionDialog.SAVE_ARCHIVE:
                 defaultFileName = getString(R.string.webpage_mht);
                 break;
 
-            case IMAGE:
+            case StoragePermissionDialog.SAVE_IMAGE:
                 defaultFileName = getString(R.string.webpage_png);
                 break;
         }
@@ -223,6 +219,9 @@ public class SaveWebpageDialog extends DialogFragment {
         // Display the default file path.
         fileNameEditText.setText(defaultFilePath);
 
+        // Move the cursor to the end of the default file path.
+        fileNameEditText.setSelection(defaultFilePath.length());
+
         // Update the status of the save button when the file name changes.
         fileNameEditText.addTextChangedListener(new TextWatcher() {
             @Override
@@ -237,7 +236,7 @@ public class SaveWebpageDialog extends DialogFragment {
 
             @Override
             public void afterTextChanged(Editable s) {
-                // // Enable the save button if a file name exists.
+                // Enable the save button if a file name exists.
                 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
             }
         });
@@ -252,11 +251,11 @@ public class SaveWebpageDialog extends DialogFragment {
 
             // Set the initial file name according to the type.
             switch (saveType) {
-                case ARCHIVE:
+                case StoragePermissionDialog.SAVE_ARCHIVE:
                     browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.webpage_mht));
                     break;
 
-                case IMAGE:
+                case StoragePermissionDialog.OPEN:
                     browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.webpage_png));
                     break;
             }
@@ -274,7 +273,7 @@ public class SaveWebpageDialog extends DialogFragment {
         });
 
         // Hide the storage permission text view on API < 23 as permissions on older devices are automatically granted.
-        if (Build.VERSION.SDK_INT < 23) {
+        if (Build.VERSION.SDK_INT < 23 || (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
             storagePermissionTextView.setVisibility(View.GONE);
         }