]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/FontSizeDialog.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / FontSizeDialog.kt
1 /*
2  * Copyright © 2019-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs
21
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
25
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 // Define the class constants.
39 private const val FONT_SIZE = "font_size"
40
41 class FontSizeDialog : DialogFragment() {
42     companion object {
43         fun displayDialog(fontSize: Int): FontSizeDialog {
44             // Create an arguments bundle.
45             val argumentsBundle = Bundle()
46
47             // Store the font size in the bundle.
48             argumentsBundle.putInt(FONT_SIZE, fontSize)
49
50             // Create a new instance of the dialog.
51             val fontSizeDialog = FontSizeDialog()
52
53             // Add the bundle to the dialog.
54             fontSizeDialog.arguments = argumentsBundle
55
56             // Return the new dialog.
57             return fontSizeDialog
58         }
59     }
60
61     // Declare the class variables.
62     private lateinit var updateFontSizeListener: UpdateFontSizeListener
63
64     // The public interface is used to send information back to the parent activity.
65     interface UpdateFontSizeListener {
66         fun updateFontSize(dialogFragment: DialogFragment)
67     }
68
69     override fun onAttach(context: Context) {
70         // Run the default commands.
71         super.onAttach(context)
72
73         // Get a handle for the update font size listener from the launching context.
74         updateFontSizeListener = context as UpdateFontSizeListener
75     }
76
77     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
78         // Get the current font size from the arguments.
79         val currentFontSize = requireArguments().getInt(FONT_SIZE)
80
81         // Use a builder to create the alert dialog.
82         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
83
84         // Set the icon according to the theme.
85         dialogBuilder.setIcon(R.drawable.font_size)
86
87         // Set the title.
88         dialogBuilder.setTitle(R.string.font_size)
89
90         // Set the view.
91         dialogBuilder.setView(R.layout.font_size_dialog)
92
93         // Set the close button listener.  Using `null` as the listener closes the dialog without doing anything else.
94         dialogBuilder.setNegativeButton(R.string.close, null)
95
96         // Set the apply button listener.
97         dialogBuilder.setPositiveButton(R.string.apply) { _: DialogInterface?, _: Int ->
98             // Return the dialog fragment to the parent activity.
99             updateFontSizeListener.updateFontSize(this)
100         }
101
102         // Create an alert dialog from the builder.
103         val alertDialog = dialogBuilder.create()
104
105         // Get the alert dialog window.
106         val dialogWindow = alertDialog.window!!
107
108         // Get a handle for the shared preferences.
109         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
110
111         // Get the screenshot preferences.
112         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
113
114         // Disable screenshots if not allowed.
115         if (!allowScreenshots) {
116             dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
117         }
118
119         // Display the keyboard.
120         dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
121
122         // The alert dialog must be shown before items in the layout can be modified.
123         alertDialog.show()
124
125         // Get a handle for the font size edit text.
126         val fontSizeEditText = alertDialog.findViewById<EditText>(R.id.font_size_edittext)!!
127
128         // Display the current font size.
129         fontSizeEditText.setText(currentFontSize.toString())
130
131         // Request focus on the font size edit text.
132         fontSizeEditText.requestFocus()
133
134         // Set the enter key on the keyboard to update the font size.
135         fontSizeEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
136             // Check the key code, event, and button status.
137             if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {  // The enter key was pressed.
138                 // Trigger the update font size listener and return the dialog fragment to the parent activity.
139                 updateFontSizeListener.updateFontSize(this)
140
141                 // Manually dismiss the alert dialog.
142                 alertDialog.dismiss()
143
144                 //Consume the event.
145                 return@setOnKeyListener true
146             } else {  // If any other key was pressed do not consume the event.
147                 return@setOnKeyListener false
148             }
149         }
150
151         // Return the alert dialog.
152         return alertDialog
153     }
154 }