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.content.Context;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 import android.support.annotation.NonNull;
28 // If we don't use `android.support.v7.app.AlertDialog` instead of `android.app.AlertDialog` then the dialog will be covered by the keyboard.
29 import android.support.v7.app.AlertDialog;
30 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
31 import android.support.v7.app.AppCompatDialogFragment;
32 import android.view.KeyEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.EditText;
36 import android.widget.ImageView;
38 public class CreateBookmarkFolder extends AppCompatDialogFragment {
39 // The public interface is used to send information back to the parent activity.
40 public interface CreateBookmarkFolderListener {
41 void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment);
44 // `createBookmarkFolderListener` is used in `onAttach()` and `onCreateDialog`.
45 private CreateBookmarkFolderListener createBookmarkFolderListener;
47 public void onAttach(Context context) {
48 super.onAttach(context);
50 // Get a handle for `createBookmarkFolderListener` from `context`.
52 createBookmarkFolderListener = (CreateBookmarkFolderListener) context;
53 } catch(ClassCastException exception) {
54 throw new ClassCastException(context.toString() + " must implement CreateBookmarkFolderListener.");
58 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
59 @SuppressLint("InflateParams")
62 public Dialog onCreateDialog(Bundle savedInstanceState) {
63 // Use `AlertDialog.Builder` to create the `AlertDialog`. The style formats the color of the button text.
64 final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
65 dialogBuilder.setTitle(R.string.create_folder);
66 // The parent view is `null` because it will be assigned by the `AlertDialog`.
67 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.create_bookmark_folder_dialog, null));
69 // Set an `onClick()` listener for the negative button.
70 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
72 public void onClick(DialogInterface dialog, int which) {
73 // Do nothing. The `AlertDialog` will close automatically.
77 // Set an `onClick()` listener fo the positive button.
78 dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
80 public void onClick(DialogInterface dialog, int which) {
81 // Return the `DialogFragment` to the parent activity on create.
82 createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this);
87 // Create an `AlertDialog` from the `AlertDialog.Builder`.
88 final AlertDialog alertDialog = dialogBuilder.create();
90 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
91 assert alertDialog.getWindow() != null;
93 // Show the keyboard when the `Dialog` is displayed on the screen.
94 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
96 // We need to show the `AlertDialog` before we can call `setOnKeyListener()` below.
99 // Allow the `enter` key on the keyboard to create the folder from `create_folder_name_edittext`.
100 EditText createFolderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext);
101 assert createFolderNameEditText != null; // Remove the warning below that `createFolderNameEditText` might be `null`.
102 createFolderNameEditText.setOnKeyListener(new View.OnKeyListener() {
103 public boolean onKey(View v, int keyCode, KeyEvent event) {
104 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
105 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
106 // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
107 createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolder.this);
108 // Manually dismiss the `AlertDialog`.
109 alertDialog.dismiss();
110 // Consume the event.
112 } else { // If any other key was pressed do not consume the event.
118 // Display the current favorite icon.
119 ImageView webPageIconImageView = (ImageView) alertDialog.findViewById(R.id.create_folder_web_page_icon);
120 assert webPageIconImageView != null; // Remove the warning that `webPageIconImageView` may be null.
121 webPageIconImageView.setImageBitmap(MainWebViewActivity.favoriteIcon);
123 // `onCreateDialog()` requires the return of an `AlertDialog`.