]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadLocationPermissionDialog.java
Use the public download directory. https://redmine.stoutner.com/issues/224
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadLocationPermissionDialog.java
1 /*
2  * Copyright © 2018 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.os.Bundle;
28
29 import com.stoutner.privacybrowser.R;
30 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
31
32 public class DownloadLocationPermissionDialog extends DialogFragment {
33     public static final int DOWNLOAD_FILE = 1;
34     public static final int DOWNLOAD_IMAGE = 2;
35
36     private int downloadType;
37
38     public static DownloadLocationPermissionDialog downloadType(int type) {
39         // Create an arguments bundle.
40         Bundle argumentsBundle = new Bundle();
41
42         // Store the download type in the bundle.
43         argumentsBundle.putInt("Download_Type", type);
44
45         // Add the arguments bundle to this instance of `DownloadLocationPermissionDialog`.
46         DownloadLocationPermissionDialog thisDownloadLocationPermissionDialog = new DownloadLocationPermissionDialog();
47         thisDownloadLocationPermissionDialog.setArguments(argumentsBundle);
48         return thisDownloadLocationPermissionDialog;
49     }
50
51     @Override
52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54
55         // Store the download type in the local class variable.
56         downloadType = getArguments().getInt("Download_Type");
57     }
58
59     // The public interface is used to send information back to the parent activity.
60     public interface DownloadLocationPermissionDialogListener {
61         void onCloseDownloadLocationPermissionDialog(int downloadType);
62     }
63
64     // `downloadLocationPermissionDialogListener` is used in `onAttach()` and `onCreateDialog()`.
65     private DownloadLocationPermissionDialogListener downloadLocationPermissionDialogListener;
66
67     @Override
68     public void onAttach(Context context) {
69         super.onAttach(context);
70
71         // Check to make sure the parent activity implements the listener.
72         try {
73             downloadLocationPermissionDialogListener = (DownloadLocationPermissionDialogListener) context;
74         } catch (ClassCastException exception) {
75             throw new ClassCastException(context.toString() + " must implement DownloadLocationPermissionDialogListener.");
76         }
77     }
78
79     @Override
80     public Dialog onCreateDialog(Bundle savedInstanceState) {
81         // Use a builder to create the alert dialog.
82         AlertDialog.Builder dialogBuilder;
83
84         // Set the style and the icon according to the theme.
85         if (MainWebViewActivity.darkTheme) {
86             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
87             dialogBuilder.setIcon(R.drawable.downloads_dark);
88         } else {
89             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
90             dialogBuilder.setIcon(R.drawable.downloads_light);
91         }
92
93         // Set an `onClick` listener on the negative button.  Using `null` as the listener closes the dialog without doing anything else.
94         dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
95             // Inform the parent activity that the dialog was closed.
96             downloadLocationPermissionDialogListener.onCloseDownloadLocationPermissionDialog(downloadType);
97         });
98
99         // Set the title.
100         dialogBuilder.setTitle(R.string.download_location);
101
102         // Set the text.
103         dialogBuilder.setMessage(R.string.download_location_message);
104
105         // `onCreateDialog` requires the return of an `AlertDialog`.
106         return dialogBuilder.create();
107     }
108 }