]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/FontSizeDialog.kt
75ef4e4517a88caa2bd9840e8097ae0db82d428d
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / FontSizeDialog.kt
1 /*
2  * Copyright © 2019-2020 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 package com.stoutner.privacybrowser.dialogs
20
21 import android.annotation.SuppressLint
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
25 import android.content.res.Configuration
26 import android.os.Bundle
27 import android.view.KeyEvent
28 import android.view.View
29 import android.view.WindowManager
30 import android.widget.EditText
31
32 import androidx.appcompat.app.AlertDialog
33 import androidx.fragment.app.DialogFragment
34 import androidx.preference.PreferenceManager
35
36 import com.stoutner.privacybrowser.R
37
38 // Declare the class constants.
39 private const val FONT_SIZE = "font_size"
40
41 class FontSizeDialog: DialogFragment() {
42     // The public interface is used to send information back to the parent activity.
43     interface UpdateFontSizeListener {
44         fun onApplyNewFontSize(dialogFragment: DialogFragment?)
45     }
46
47     // Declare the class variables.
48     private lateinit var updateFontSizeListener: UpdateFontSizeListener
49
50     override fun onAttach(context: Context) {
51         // Run the default commands.
52         super.onAttach(context)
53
54         // Get a handle for the update font size listener from the launching context.
55         updateFontSizeListener = context as UpdateFontSizeListener
56     }
57
58     companion object {
59         // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.  Also, the function can then be moved out of a companion object and just become a package-level function.
60         @JvmStatic
61         fun displayDialog(fontSize: Int): FontSizeDialog {
62             // Create an arguments bundle.
63             val argumentsBundle = Bundle()
64
65             // Store the font size in the bundle.
66             argumentsBundle.putInt(FONT_SIZE, fontSize)
67
68             // Create a new instance of the dialog.
69             val fontSizeDialog = FontSizeDialog()
70
71             // Add the bundle to the dialog.
72             fontSizeDialog.arguments = argumentsBundle
73
74             // Return the new dialog.
75             return fontSizeDialog
76         }
77     }
78
79     // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
80     @SuppressLint("InflateParams")
81     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
82         // Get the current font size from the arguments.
83         val currentFontSize = requireArguments().getInt(FONT_SIZE)
84
85         // Use a builder to create the alert dialog.
86         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
87
88         // Get the current theme status.
89         val currentThemeStatus = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
90
91         // Set the icon according to the theme.
92         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
93             dialogBuilder.setIcon(R.drawable.font_size_day)
94         } else {
95             dialogBuilder.setIcon(R.drawable.font_size_night)
96         }
97
98         // Set the title.
99         dialogBuilder.setTitle(R.string.font_size)
100
101         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
102         dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.font_size_dialog, null))
103
104         // Set the close button listener.  Using `null` as the listener closes the dialog without doing anything else.
105         dialogBuilder.setNegativeButton(R.string.close, null)
106
107         // Set the apply button listener.
108         dialogBuilder.setPositiveButton(R.string.apply) { _: DialogInterface?, _: Int ->
109             // Return the dialog fragment to the parent activity.
110             updateFontSizeListener.onApplyNewFontSize(this)
111         }
112
113         // Create an alert dialog from the builder.
114         val alertDialog = dialogBuilder.create()
115
116         // Get the alert dialog window.
117         val dialogWindow = alertDialog.window!!
118
119         // Get a handle for the shared preferences.
120         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
121
122         // Get the screenshot preferences.
123         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
124
125         // Disable screenshots if not allowed.
126         if (!allowScreenshots) {
127             dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
128         }
129
130         // Display the keyboard.
131         dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
132
133         // The alert dialog must be shown before items in the layout can be modified.
134         alertDialog.show()
135
136         // Get a handle for the font size edit text.
137         val fontSizeEditText = alertDialog.findViewById<EditText>(R.id.font_size_edittext)!!
138
139         // Display the current font size.
140         fontSizeEditText.setText(currentFontSize.toString())
141
142         // Request focus on the font size edit text.
143         fontSizeEditText.requestFocus()
144
145         // Set the enter key on the keyboard to update the font size.
146         fontSizeEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
147             // Check the key code, event, and button status.
148             if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {  // The enter key was pressed.
149                 // Trigger the update font size listener and return the dialog fragment to the parent activity.
150                 updateFontSizeListener.onApplyNewFontSize(this)
151
152                 // Manually dismiss the alert dialog.
153                 alertDialog.dismiss()
154
155                 //Consume the event.
156                 return@setOnKeyListener true
157             } else {  // If any other key was pressed do not consume the event.
158                 return@setOnKeyListener false
159             }
160         }
161
162         // Return the alert dialog.
163         return alertDialog
164     }
165 }