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