]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/FontSizeDialog.java
7a0e07ebf91647af0a27e1f8e68ccb096a36451c
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / FontSizeDialog.java
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
20 package com.stoutner.privacybrowser.dialogs;
21
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;
35
36 import androidx.annotation.NonNull;
37 import androidx.appcompat.app.AlertDialog;
38 import androidx.fragment.app.DialogFragment;
39 import androidx.preference.PreferenceManager;
40
41 import com.stoutner.privacybrowser.R;
42
43 public class FontSizeDialog extends DialogFragment {
44     // Define the update font size listener.
45     private UpdateFontSizeListener updateFontSizeListener;
46
47     // The public interface is used to send information back to the parent activity.
48     public interface UpdateFontSizeListener {
49         void onApplyNewFontSize(DialogFragment dialogFragment);
50     }
51
52     @Override
53     public void onAttach(@NonNull Context context) {
54         // Run the default commands.
55         super.onAttach(context);
56
57         // Get a handle for the update font size listener from the launching context.
58         updateFontSizeListener = (UpdateFontSizeListener) context;
59     }
60
61     public static FontSizeDialog displayDialog(int fontSize) {
62         // Create an arguments bundle.
63         Bundle argumentsBundle = new 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         FontSizeDialog fontSizeDialog = new FontSizeDialog();
70
71         // Add the bundle to the dialog.
72         fontSizeDialog.setArguments(argumentsBundle);
73
74         // Return the new dialog.
75         return fontSizeDialog;
76     }
77
78     // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
79     @SuppressLint("InflateParams")
80     @Override
81     @NonNull
82     public Dialog onCreateDialog(Bundle savedInstanceState) {
83         // Get a handle for the activity and the context.
84         Activity activity = getActivity();
85         Context context = getContext();
86
87         // Remove the incorrect lint warnings below that the activity and context might be null.
88         assert activity != null;
89         assert context != null;
90
91         // Get the arguments.
92         Bundle arguments = getArguments();
93
94         // Remove the incorrect lint warning below that `getInt()` might be null.
95         assert arguments != null;
96
97         // Get the current font size.
98         int currentFontSize = arguments.getInt("font_size");
99
100         // Use a builder to create the alert dialog.
101         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog);
102
103         // Get the current theme status.
104         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
105
106         // Set the icon according to the theme.
107         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
108             dialogBuilder.setIcon(R.drawable.font_size_night);
109         } else {
110             dialogBuilder.setIcon(R.drawable.font_size_day);
111         }
112
113         // Set the title.
114         dialogBuilder.setTitle(R.string.font_size);
115
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));
118
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);
121
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);
126         });
127
128         // Create an alert dialog from the builder.
129         AlertDialog alertDialog = dialogBuilder.create();
130
131         // Get the alert dialog window.
132         Window dialogWindow = alertDialog.getWindow();
133
134         // Remove the incorrect lint warning below that the dialog window might be null.
135         assert dialogWindow != null;
136
137         // Get a handle for the shared preferences.
138         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
139
140         // Get the screenshot preferences.
141         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
142
143         // Disable screenshots if not allowed.
144         if (!allowScreenshots) {
145             dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
146         }
147
148         // Display the keyboard.
149         dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
150
151         // The alert dialog must be shown before items in the layout can be modified.
152         alertDialog.show();
153
154         // Get a handle for the font size edit text.
155         EditText fontSizeEditText = alertDialog.findViewById(R.id.font_size_edittext);
156
157         // Remove the incorrect lint warning below that the edit text might be null.
158         assert fontSizeEditText != null;
159
160         // Display the current font size.
161         fontSizeEditText.setText(String.valueOf(currentFontSize));
162
163         // Request focus on the font size edit text.
164         fontSizeEditText.requestFocus();
165
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));
172
173                 // Manually dismiss the alert dialog.
174                 alertDialog.dismiss();
175
176                 //Consume the event.
177                 return true;
178             } else {  // If any other key was pressed do not consume the event.
179                 return false;
180             }
181         });
182
183         // Return the alert dialog.
184         return alertDialog;
185     }
186 }