2 * Copyright © 2019-2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
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
32 import androidx.appcompat.app.AlertDialog
33 import androidx.fragment.app.DialogFragment
34 import androidx.preference.PreferenceManager
36 import com.stoutner.privacybrowser.R
38 // Define the class constants.
39 private const val FONT_SIZE = "font_size"
41 class FontSizeDialog : DialogFragment() {
42 // Declare the class variables.
43 private lateinit var updateFontSizeListener: UpdateFontSizeListener
45 // The public interface is used to send information back to the parent activity.
46 interface UpdateFontSizeListener {
47 fun onApplyNewFontSize(dialogFragment: DialogFragment?)
50 override fun onAttach(context: Context) {
51 // Run the default commands.
52 super.onAttach(context)
54 // Get a handle for the update font size listener from the launching context.
55 updateFontSizeListener = context as UpdateFontSizeListener
59 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
61 fun displayDialog(fontSize: Int): FontSizeDialog {
62 // Create an arguments bundle.
63 val argumentsBundle = Bundle()
65 // Store the font size in the bundle.
66 argumentsBundle.putInt(FONT_SIZE, fontSize)
68 // Create a new instance of the dialog.
69 val fontSizeDialog = FontSizeDialog()
71 // Add the bundle to the dialog.
72 fontSizeDialog.arguments = argumentsBundle
74 // Return the new dialog.
79 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
80 // Get the current font size from the arguments.
81 val currentFontSize = requireArguments().getInt(FONT_SIZE)
83 // Use a builder to create the alert dialog.
84 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
86 // Set the icon according to the theme.
87 dialogBuilder.setIconAttribute(R.attr.fontSizeBlueIcon)
90 dialogBuilder.setTitle(R.string.font_size)
93 dialogBuilder.setView(R.layout.font_size_dialog)
95 // Set the close button listener. Using `null` as the listener closes the dialog without doing anything else.
96 dialogBuilder.setNegativeButton(R.string.close, null)
98 // Set the apply button listener.
99 dialogBuilder.setPositiveButton(R.string.apply) { _: DialogInterface?, _: Int ->
100 // Return the dialog fragment to the parent activity.
101 updateFontSizeListener.onApplyNewFontSize(this)
104 // Create an alert dialog from the builder.
105 val alertDialog = dialogBuilder.create()
107 // Get the alert dialog window.
108 val dialogWindow = alertDialog.window!!
110 // Get a handle for the shared preferences.
111 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
113 // Get the screenshot preferences.
114 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
116 // Disable screenshots if not allowed.
117 if (!allowScreenshots) {
118 dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
121 // Display the keyboard.
122 dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
124 // The alert dialog must be shown before items in the layout can be modified.
127 // Get a handle for the font size edit text.
128 val fontSizeEditText = alertDialog.findViewById<EditText>(R.id.font_size_edittext)!!
130 // Display the current font size.
131 fontSizeEditText.setText(currentFontSize.toString())
133 // Request focus on the font size edit text.
134 fontSizeEditText.requestFocus()
136 // Set the enter key on the keyboard to update the font size.
137 fontSizeEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
138 // Check the key code, event, and button status.
139 if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { // The enter key was pressed.
140 // Trigger the update font size listener and return the dialog fragment to the parent activity.
141 updateFontSizeListener.onApplyNewFontSize(this)
143 // Manually dismiss the alert dialog.
144 alertDialog.dismiss()
147 return@setOnKeyListener true
148 } else { // If any other key was pressed do not consume the event.
149 return@setOnKeyListener false
153 // Return the alert dialog.