]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.kt
5bdf65acba0caa4ad4205f459fd667d727a735c1
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / OpenDialog.kt
1 /*
2  * Copyright © 2019-2021 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.annotation.SuppressLint
23 import android.app.Dialog
24 import android.content.Context
25 import android.content.DialogInterface
26 import android.content.Intent
27 import android.content.res.Configuration
28 import android.os.Bundle
29 import android.text.Editable
30 import android.text.TextWatcher
31 import android.view.WindowManager
32 import android.widget.Button
33 import android.widget.EditText
34
35 import androidx.appcompat.app.AlertDialog
36 import androidx.fragment.app.DialogFragment
37 import androidx.preference.PreferenceManager
38
39 import com.stoutner.privacybrowser.R
40 import com.stoutner.privacybrowser.activities.MainWebViewActivity
41
42 class OpenDialog : DialogFragment() {
43     // Define the open listener.
44     private lateinit var openListener: OpenListener
45
46     // The public interface is used to send information back to the parent activity.
47     interface OpenListener {
48         fun onOpen(dialogFragment: DialogFragment)
49     }
50
51     override fun onAttach(context: Context) {
52         // Run the default commands.
53         super.onAttach(context)
54
55         // Get a handle for the open listener from the launching context.
56         openListener = context as OpenListener
57     }
58
59     // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
60     @SuppressLint("InflateParams")
61     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
62         // Use an alert dialog builder to create the alert dialog.
63         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
64
65         // Get the current theme status.
66         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
67
68         // Set the icon according to the theme.
69         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
70             dialogBuilder.setIcon(R.drawable.proxy_enabled_day)
71         } else {
72             dialogBuilder.setIcon(R.drawable.proxy_enabled_night)
73         }
74
75         // Set the title.
76         dialogBuilder.setTitle(R.string.open)
77
78         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
79         dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.open_dialog, null))
80
81         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
82         dialogBuilder.setNegativeButton(R.string.cancel, null)
83
84         // Set the open button listener.
85         dialogBuilder.setPositiveButton(R.string.open) { _: DialogInterface?, _: Int ->
86             // Return the dialog fragment to the parent activity.
87             openListener.onOpen(this)
88         }
89
90         // Create an alert dialog from the builder.
91         val alertDialog = dialogBuilder.create()
92
93         // Get a handle for the shared preferences.
94         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
95
96         // Get the screenshot preference.
97         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
98
99         // Disable screenshots if not allowed.
100         if (!allowScreenshots) {
101             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
102         }
103
104         // The alert dialog must be shown before items in the layout can be modified.
105         alertDialog.show()
106
107         // Get handles for the layout items.
108         val fileNameEditText = alertDialog.findViewById<EditText>(R.id.file_name_edittext)!!
109         val browseButton = alertDialog.findViewById<Button>(R.id.browse_button)!!
110         val openButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
111
112         // Initially disable the open button.
113         openButton.isEnabled = false
114
115         // Update the status of the open button when the file name changes.
116         fileNameEditText.addTextChangedListener(object : TextWatcher {
117             override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
118                 // Do nothing.
119             }
120
121             override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
122                 // Do nothing.
123             }
124
125             override fun afterTextChanged(editable: Editable) {
126                 // Get the current file name.
127                 val fileNameString = fileNameEditText.text.toString()
128
129                 // Enable the open button if the file name is populated.
130                 openButton.isEnabled = fileNameString.isNotEmpty()
131             }
132         })
133
134         // Handle clicks on the browse button.
135         browseButton.setOnClickListener {
136             // Create the file picker intent.
137             val browseIntent = Intent(Intent.ACTION_OPEN_DOCUMENT)
138
139             // Only display files that can be opened.
140             browseIntent.addCategory(Intent.CATEGORY_OPENABLE)
141
142             // Set the intent MIME type to include all files so that everything is visible.
143             browseIntent.type = "*/*"
144
145             // Start the file picker.  This must be started under `activity` to that the request code is returned correctly.
146             requireActivity().startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_OPEN_REQUEST_CODE)
147         }
148
149         // Return the alert dialog.
150         return alertDialog
151     }
152 }