2 * Copyright © 2019-2020 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.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.SharedPreferences;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.view.KeyEvent;
31 import android.view.View;
32 import android.view.Window;
33 import android.view.WindowManager;
34 import android.widget.EditText;
36 import androidx.annotation.NonNull;
37 import androidx.appcompat.app.AlertDialog;
38 import androidx.fragment.app.DialogFragment;
39 import androidx.preference.PreferenceManager;
41 import com.stoutner.privacybrowser.R;
43 public class FontSizeDialog extends DialogFragment {
44 // Define the update font size listener.
45 private UpdateFontSizeListener updateFontSizeListener;
47 // The public interface is used to send information back to the parent activity.
48 public interface UpdateFontSizeListener {
49 void onApplyNewFontSize(DialogFragment dialogFragment);
53 public void onAttach(@NonNull Context context) {
54 // Run the default commands.
55 super.onAttach(context);
57 // Get a handle for the update font size listener from the launching context.
58 updateFontSizeListener = (UpdateFontSizeListener) context;
61 public static FontSizeDialog displayDialog(int fontSize) {
62 // Create an arguments bundle.
63 Bundle argumentsBundle = new Bundle();
65 // Store the font size in the bundle.
66 argumentsBundle.putInt("font_size", fontSize);
68 // Create a new instance of the dialog.
69 FontSizeDialog fontSizeDialog = new FontSizeDialog();
71 // Add the bundle to the dialog.
72 fontSizeDialog.setArguments(argumentsBundle);
74 // Return the new dialog.
75 return fontSizeDialog;
78 // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
79 @SuppressLint("InflateParams")
82 public Dialog onCreateDialog(Bundle savedInstanceState) {
83 // Get a handle for the activity and the context.
84 Activity activity = getActivity();
85 Context context = getContext();
87 // Remove the incorrect lint warnings below that the activity and context might be null.
88 assert activity != null;
89 assert context != null;
92 Bundle arguments = getArguments();
94 // Remove the incorrect lint warning below that `getInt()` might be null.
95 assert arguments != null;
97 // Get the current font size.
98 int currentFontSize = arguments.getInt("font_size");
100 // Use a builder to create the alert dialog.
101 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog);
103 // Get the current theme status.
104 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
106 // Set the icon according to the theme.
107 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
108 dialogBuilder.setIcon(R.drawable.font_size_night);
110 dialogBuilder.setIcon(R.drawable.font_size_day);
114 dialogBuilder.setTitle(R.string.font_size);
116 // Set the view. The parent view is null because it will be assigned by the alert dialog.
117 dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.font_size_dialog, null));
119 // Set the close button listener. Using `null` as the listener closes the dialog without doing anything else.
120 dialogBuilder.setNegativeButton(R.string.close, null);
122 // Set the apply button listener.
123 dialogBuilder.setPositiveButton(R.string.apply, (DialogInterface dialog, int which) -> {
124 // Return the dialog fragment to the parent activity.
125 updateFontSizeListener.onApplyNewFontSize(this);
128 // Create an alert dialog from the builder.
129 AlertDialog alertDialog = dialogBuilder.create();
131 // Get the alert dialog window.
132 Window dialogWindow = alertDialog.getWindow();
134 // Remove the incorrect lint warning below that the dialog window might be null.
135 assert dialogWindow != null;
137 // Get a handle for the shared preferences.
138 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
140 // Get the screenshot preferences.
141 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
143 // Disable screenshots if not allowed.
144 if (!allowScreenshots) {
145 dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
148 // Display the keyboard.
149 dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
151 // The alert dialog must be shown before items in the layout can be modified.
154 // Get a handle for the font size edit text.
155 EditText fontSizeEditText = alertDialog.findViewById(R.id.font_size_edittext);
157 // Remove the incorrect lint warning below that the edit text might be null.
158 assert fontSizeEditText != null;
160 // Display the current font size.
161 fontSizeEditText.setText(String.valueOf(currentFontSize));
163 // Request focus on the font size edit text.
164 fontSizeEditText.requestFocus();
166 // Set the enter key on the keyboard to update the font size.
167 fontSizeEditText.setOnKeyListener((View view, int keyCode, KeyEvent keyEvent) -> {
168 // If the key event is a key-down on the `enter` key apply the new font size.
169 if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // The enter key was pressed.
170 // Trigger the update font size listener and return the dialog fragment to the parent activity.
171 updateFontSizeListener.onApplyNewFontSize((this));
173 // Manually dismiss the alert dialog.
174 alertDialog.dismiss();
178 } else { // If any other key was pressed do not consume the event.
183 // Return the alert dialog.