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.app.Activity;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.DialogInterface;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.os.Bundle;
29 import android.support.annotation.NonNull;
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.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
38 public class CreateBookmark extends DialogFragment {
39 // The public interface is used to send information back to the parent activity.
40 public interface CreateBookmarkListener {
41 void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment);
43 void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment);
46 // createBookmarkListener is used in onAttach() and onCreateDialog()
47 private CreateBookmarkListener createBookmarkListener;
49 // Check to make sure the parent activity implements the listener.
50 public void onAttach(Activity parentActivity) {
51 super.onAttach(parentActivity);
53 createBookmarkListener = (CreateBookmarkListener) parentActivity;
54 } catch(ClassCastException exception) {
55 throw new ClassCastException(parentActivity.toString() + " must implement CreateBookmarkListener.");
59 // onCreateDialog requires @NonNull.
62 public Dialog onCreateDialog(Bundle savedInstanceState) {
63 // Create a drawable version of the favorite icon.
64 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
66 // Get the activity's layout inflater.
67 LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
69 // Use AlertDialog.Builder to create the AlertDialog. The style formats the color of the button text.
70 AlertDialog.Builder createBookmarkDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowser_AlertDialog);
71 createBookmarkDialogBuilder.setTitle(R.string.create_bookmark);
72 createBookmarkDialogBuilder.setIcon(favoriteIconDrawable);
73 // The parent view is "null" because it will be assigned by AlertDialog.
74 createBookmarkDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_bookmark_dialog, null));
76 // Set an onClick listener on the negative button.
77 createBookmarkDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
79 public void onClick(DialogInterface dialog, int which) {
80 createBookmarkListener.onCreateBookmarkCancel(CreateBookmark.this);
84 // Set an onClick listener on the positive button.
85 createBookmarkDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
87 public void onClick(DialogInterface dialog, int which) {
88 createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
93 // Create an AlertDialog from the AlertDialog.Builder.
94 final AlertDialog createBookmarkDialog = createBookmarkDialogBuilder.create();
96 // Show the keyboard when the Dialog is displayed on the screen.
97 createBookmarkDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
99 // We need to show the AlertDialog before we can call setOnKeyListener() below.
100 createBookmarkDialog.show();
102 // Allow the "enter" key on the keyboard to create the bookmark from "create_bookmark_name_edittext".
103 EditText createBookmarkNameEditText = (EditText) createBookmarkDialog.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 the createBookmarkListener.
110 createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
112 // Manually dismiss the AlertDialog.
113 createBookmarkDialog.dismiss();
115 // Consume the event.
117 } else { // If any other key was pressed, do not consume the event.
123 // Set the formattedUrlString as the initial text of "create_bookmark_url_edittext".
124 EditText createBookmarkUrlEditText = (EditText) createBookmarkDialog.findViewById(R.id.create_bookmark_url_edittext);
125 assert createBookmarkUrlEditText != null;// Remove the warning below that createBookmarkUrlEditText might be null.
126 createBookmarkUrlEditText.setText(MainWebViewActivity.formattedUrlString);
128 // Allow the "enter" key on the keyboard to create the bookmark from "create_bookmark_url_edittext".
129 createBookmarkUrlEditText.setOnKeyListener(new View.OnKeyListener() {
130 public boolean onKey(View v, int keyCode, KeyEvent event) {
131 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
132 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
133 // Trigger the create listener.
134 createBookmarkListener.onCreateBookmarkCreate(CreateBookmark.this);
136 // Manually dismiss the AlertDialog.
137 createBookmarkDialog.dismiss();
139 // Consume the event.
141 } else { // If any other key was pressed, do not consume the event.
147 // onCreateDialog requires the return of an AlertDialog
148 return createBookmarkDialog;