2 * Copyright © 2016-2019 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.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.graphics.Bitmap;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.os.Bundle;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.EditText;
36 import androidx.annotation.NonNull;
37 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
40 import com.stoutner.privacybrowser.R;
42 public class CreateBookmarkDialog extends DialogFragment {
43 // The public interface is used to send information back to the parent activity.
44 public interface CreateBookmarkListener {
45 void onCreateBookmark(DialogFragment dialogFragment);
48 // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
49 private CreateBookmarkListener createBookmarkListener;
52 public void onAttach(Context context) {
53 super.onAttach(context);
55 // Get a handle for `CreateBookmarkListener` from the launching context.
56 createBookmarkListener = (CreateBookmarkListener) context;
59 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
60 @SuppressLint("InflateParams")
63 public Dialog onCreateDialog(Bundle savedInstanceState) {
64 // Get a copy of the favorite icon bitmap.
65 Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
67 // Scale the favorite icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
68 if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
69 favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
72 // Create a drawable version of the favorite icon.
73 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), favoriteIconBitmap);
75 // Use an alert dialog builder to create the alert dialog.
76 AlertDialog.Builder dialogBuilder;
78 // Set the style according to the theme.
79 if (MainWebViewActivity.darkTheme) {
80 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
82 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
85 // Set the title and icon.
86 dialogBuilder.setTitle(R.string.create_bookmark);
87 dialogBuilder.setIcon(favoriteIconDrawable);
89 // Remove the warning below that `getLayoutInflater()` might be null.
90 assert getActivity() != null;
92 // Set the view. The parent view is `null` because it will be assigned by the `AlertDialog`.
93 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
95 // Set an `onClick()` listener for the negative button.
96 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
97 // Do nothing. The `AlertDialog` will close automatically.
100 // Set an `onClick()` listener for the positive button.
101 dialogBuilder.setPositiveButton(R.string.create, (DialogInterface dialog, int which) -> {
102 // Return the `DialogFragment` to the parent activity.
103 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
106 // Create an `AlertDialog` from the `AlertDialog.Builder`.
107 final AlertDialog alertDialog = dialogBuilder.create();
109 // Remove the warning below that `getWindow()` might be null.
110 assert alertDialog.getWindow() != null;
112 // Disable screenshots if not allowed.
113 if (!MainWebViewActivity.allowScreenshots) {
114 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
117 // Show the keyboard when the `AlertDialog` is displayed on the screen.
118 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
120 // The alert dialog needs to be shown before `setOnKeyListener()` can be called.
123 // Get a handle for `create_bookmark_name_edittext`.
124 EditText createBookmarkNameEditText = alertDialog.findViewById(R.id.create_bookmark_name_edittext);
126 // Set the current `WebView` title as the text for `create_bookmark_name_edittext`.
127 createBookmarkNameEditText.setText(MainWebViewActivity.webViewTitle);
129 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
130 createBookmarkNameEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
131 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
132 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
133 // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
134 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
136 // Manually dismiss the `AlertDialog`.
137 alertDialog.dismiss();
139 // Consume the event.
141 } else { // If any other key was pressed, do not consume the event.
146 // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
147 EditText createBookmarkUrlEditText = alertDialog.findViewById(R.id.create_bookmark_url_edittext);
148 createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
150 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
151 createBookmarkUrlEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
152 // If the event is a key-down on the "enter" key, select the PositiveButton "Create".
153 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
154 // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
155 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
157 // Manually dismiss the `AlertDialog`.
158 alertDialog.dismiss();
160 // Consume the event.
162 } else { // If any other key was pressed, do not consume the event.
167 // Return the alert dialog.