]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java
Fix crashing when a dialog displays while Privacy Browser is not in the foreground...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / PrepareSaveDialog.java
index 9e8b1fd83b39d4e366afdbdc10e1582a3fe6eba6..93bd110e7d5ad5edb4d8a1a23f9adf9f0b58cef1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2020-2021 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -30,6 +30,8 @@ import androidx.fragment.app.DialogFragment;
 import androidx.fragment.app.FragmentManager;
 
 import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import com.stoutner.privacybrowser.definitions.PendingDialog;
 import com.stoutner.privacybrowser.dialogs.SaveWebpageDialog;
 import com.stoutner.privacybrowser.helpers.ProxyHelper;
 
@@ -41,14 +43,14 @@ import java.text.NumberFormat;
 
 public class PrepareSaveDialog extends AsyncTask<String, Void, String[]> {
     // Define weak references.
-    private WeakReference<Activity> activityWeakReference;
-    private WeakReference<Context> contextWeakReference;
-    private WeakReference<FragmentManager> fragmentManagerWeakReference;
+    private final WeakReference<Activity> activityWeakReference;
+    private final WeakReference<Context> contextWeakReference;
+    private final WeakReference<FragmentManager> fragmentManagerWeakReference;
 
     // Define the class variables.
-    private int saveType;
-    private String userAgent;
-    private boolean cookiesEnabled;
+    private final int saveType;
+    private final String userAgent;
+    private final boolean cookiesEnabled;
     private String urlString;
 
     // The public constructor.
@@ -201,8 +203,14 @@ public class PrepareSaveDialog extends AsyncTask<String, Void, String[]> {
         // Instantiate the save dialog.
         DialogFragment saveDialogFragment = SaveWebpageDialog.saveWebpage(saveType, urlString, fileStringArray[0], fileStringArray[1], userAgent, cookiesEnabled);
 
-        // Show the save dialog.  It must be named `save_dialog` so that the file picker can update the file name.
-        saveDialogFragment.show(fragmentManager, activity.getString(R.string.save_dialog));
+        // Try to show the dialog.  Sometimes the window is not active.
+        try {
+            // Show the save dialog.  It must be named `save_dialog` so that the file picker can update the file name.
+            saveDialogFragment.show(fragmentManager, activity.getString(R.string.save_dialog));
+        } catch (Exception exception) {
+            // Add the dialog to the pending dialog array list.  It will be displayed in `onStart()`.
+            MainWebViewActivity.pendingDialogsArrayList.add(new PendingDialog(saveDialogFragment, activity.getString(R.string.save_dialog)));
+        }
     }
 
     // Content dispositions can contain other text besides the file name, and they can be in any order.