2 * Copyright 2015-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.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 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
33 import android.support.v7.app.AppCompatDialogFragment;
34 import android.view.KeyEvent;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.WindowManager;
38 import android.widget.EditText;
40 public class CreateHomeScreenShortcut extends AppCompatDialogFragment {
41 // The public interface is used to send information back to the parent activity.
42 public interface CreateHomeScreenSchortcutListener {
43 void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment);
46 //`createHomeScreenShortcutListener` is used in `onAttach()` and `onCreateDialog()`.
47 private CreateHomeScreenSchortcutListener createHomeScreenShortcutListener;
49 // Check to make sure that the parent activity implements the listener.
50 public void onAttach(Context context) {
51 super.onAttach(context);
53 createHomeScreenShortcutListener = (CreateHomeScreenSchortcutListener) context;
54 } catch(ClassCastException exception) {
55 throw new ClassCastException(context.toString() + " must implement CreateHomeScreenShortcutListener.");
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 the activity's layout inflater.
65 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
67 // Create a drawable version of the favorite icon.
68 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
70 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
71 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
72 dialogBuilder.setTitle(R.string.create_shortcut);
73 dialogBuilder.setIcon(favoriteIconDrawable);
74 // The parent view is `null` because it will be assigned by `AlertDialog`.
75 dialogBuilder.setView(layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
77 // Set an `onClick` listener on the negative button.
78 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
80 public void onClick(DialogInterface dialog, int which) {
81 // Do nothing if `Cancel` is clicked.
85 // Set an `onClick` listener on the positive button.
86 dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
88 public void onClick(DialogInterface dialog, int which) {
89 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
94 // Create an `AlertDialog` from the `AlertDialog.Builder`.
95 final AlertDialog alertDialog = dialogBuilder.create();
97 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
98 assert alertDialog.getWindow() != null;
100 // Show the keyboard when the Dialog is displayed on the screen.
101 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
103 // We need to show `alertDialog` before we can call `setOnKeyListener()` below.
106 // Allow the "enter" key on the keyboard to create the shortcut.
107 EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcut_name_edittext);
108 assert shortcutNameEditText != null; // Remove the warning below that shortcutNameEditText might be null.
109 shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
110 public boolean onKey(View v, int keyCode, KeyEvent event) {
111 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
112 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
113 // Trigger the create listener.
114 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
116 // Manually dismiss `alertDialog`.
117 alertDialog.dismiss();
119 // Consume the event.
121 } else { // If any other key was pressed, do not consume the event.
127 // `onCreateDialog` requires the return of an `AlertDialog`.