2 * Copyright 2016-2017 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.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.support.annotation.NonNull;
31 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.view.KeyEvent;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 import com.stoutner.privacybrowser.R;
41 public class CreateBookmarkDialog extends AppCompatDialogFragment {
42 // The public interface is used to send information back to the parent activity.
43 public interface CreateBookmarkListener {
44 void onCreateBookmark(AppCompatDialogFragment dialogFragment);
47 // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
48 private CreateBookmarkListener createBookmarkListener;
51 public void onAttach(Context context) {
52 super.onAttach(context);
54 // Get a handle for `CreateBookmarkListener` from `context`.
56 createBookmarkListener = (CreateBookmarkListener) context;
57 } catch(ClassCastException exception) {
58 throw new ClassCastException(context.toString() + " must implement `CreateBookmarkListener`.");
62 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
63 @SuppressLint("InflateParams")
66 public Dialog onCreateDialog(Bundle savedInstanceState) {
67 // Create a drawable version of the favorite icon.
68 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
70 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
71 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
72 dialogBuilder.setTitle(R.string.create_bookmark);
73 dialogBuilder.setIcon(favoriteIconDrawable);
74 // The parent view is `null` because it will be assigned by the `AlertDialog`.
75 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
77 // Set an `onClick()` listener for the negative button.
78 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
80 public void onClick(DialogInterface dialog, int which) {
81 // Do nothing. The `AlertDialog` will close automatically.
85 // Set an `onClick()` listener for the positive button.
86 dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
88 public void onClick(DialogInterface dialog, int which) {
89 // Return the `DialogFragment` to the parent activity on create.
90 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
95 // Create an `AlertDialog` from the `AlertDialog.Builder`.
96 final AlertDialog alertDialog = dialogBuilder.create();
98 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
99 assert alertDialog.getWindow() != null;
101 // Show the keyboard when the `AlertDialog` is displayed on the screen.
102 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
104 // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
107 // Get a handle for `create_bookmark_name_edittext`.
108 EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
110 // Set the current `WebView` title as the text for `create_bookmark_name_edittext`.
111 createBookmarkNameEditText.setText(MainWebViewActivity.webViewTitle);
113 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
114 createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
115 public boolean onKey(View view, int keyCode, KeyEvent event) {
116 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
117 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
118 // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
119 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
120 // Manually dismiss the `AlertDialog`.
121 alertDialog.dismiss();
122 // Consume the event.
124 } else { // If any other key was pressed, do not consume the event.
130 // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
131 EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext);
132 createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
134 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
135 createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
136 public boolean onKey(View v, int keyCode, KeyEvent event) {
137 // If the event is a key-down on the "enter" key, select the PositiveButton "Create".
138 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
139 // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
140 createBookmarkListener.onCreateBookmark(CreateBookmarkDialog.this);
141 // Manually dismiss the `AlertDialog`.
142 alertDialog.dismiss();
143 // Consume the event.
145 } else { // If any other key was pressed, do not consume the event.
151 // `onCreateDialog()` requires the return of an `AlertDialog`.