2 * Copyright 2016 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;
22 import android.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
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 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
31 import android.support.v7.app.AlertDialog;
32 import android.view.KeyEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.EditText;
37 public class CreateBookmark extends DialogFragment {
38 // The public interface is used to send information back to the parent activity.
39 public interface CreateBookmarkListener {
40 void onCreateBookmark(DialogFragment dialogFragment);
43 // `createBookmarkListener` is used in `onAttach()` and `onCreateDialog()`
44 private CreateBookmarkListener createBookmarkListener;
47 public void onAttach(Context context) {
48 super.onAttach(context);
50 // Get a handle for `CreateBookmarkListener` from `context`.
52 createBookmarkListener = (CreateBookmarkListener) context;
53 } catch(ClassCastException exception) {
54 throw new ClassCastException(context.toString() + " must implement CreateBookmarkListener.");
58 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
59 @SuppressLint("InflateParams")
61 public Dialog onCreateDialog(Bundle savedInstanceState) {
62 // Create a drawable version of the favorite icon.
63 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
65 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
66 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
67 dialogBuilder.setTitle(R.string.create_bookmark);
68 dialogBuilder.setIcon(favoriteIconDrawable);
69 // The parent view is `null` because it will be assigned by the `AlertDialog`.
70 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_dialog, null));
72 // Set an `onClick()` listener for the negative button.
73 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
75 public void onClick(DialogInterface dialog, int which) {
76 // Do nothing. The `AlertDialog` will close automatically.
80 // Set an `onClick()` listener for the positive button.
81 dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
83 public void onClick(DialogInterface dialog, int which) {
84 // Return the `DialogFragment` to the parent activity on create.
85 createBookmarkListener.onCreateBookmark(CreateBookmark.this);
90 // Create an `AlertDialog` from the `AlertDialog.Builder`.
91 final AlertDialog alertDialog = dialogBuilder.create();
93 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
94 assert alertDialog.getWindow() != null;
96 // Show the keyboard when the `Dialog` is displayed on the screen.
97 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
99 // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
102 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_name_edittext`.
103 EditText createBookmarkNameEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_name_edittext);
104 assert createBookmarkNameEditText != null; // Remove the warning below that `createBookmarkNameEditText` might be `null`.
105 createBookmarkNameEditText.setOnKeyListener(new View.OnKeyListener() {
106 public boolean onKey(View v, int keyCode, KeyEvent event) {
107 // If the event is a key-down on the `enter` button, select the `PositiveButton` `Create`.
108 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
109 // Trigger `createBookmarkListener` and return the `DialogFragment` to the parent activity.
110 createBookmarkListener.onCreateBookmark(CreateBookmark.this);
111 // Manually dismiss the `AlertDialog`.
112 alertDialog.dismiss();
113 // Consume the event.
115 } else { // If any other key was pressed, do not consume the event.
121 // Set the formattedUrlString as the initial text of `create_bookmark_url_edittext`.
122 EditText createBookmarkUrlEditText = (EditText) alertDialog.findViewById(R.id.create_bookmark_url_edittext);
123 assert createBookmarkUrlEditText != null;// Remove the warning below that `createBookmarkUrlEditText` might be `null`.
124 createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
126 // Allow the `enter` key on the keyboard to create the bookmark from `create_bookmark_url_edittext`.
127 createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
128 public boolean onKey(View v, int keyCode, KeyEvent event) {
129 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
130 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
131 // Trigger `createBookmarkListener` and return the DialogFragment to the parent activity.
132 createBookmarkListener.onCreateBookmark(CreateBookmark.this);
133 // Manually dismiss the `AlertDialog`.
134 alertDialog.dismiss();
135 // Consume the event.
137 } else { // If any other key was pressed, do not consume the event.
143 // `onCreateDialog()` requires the return of an `AlertDialog`.