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.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 // If we don't use android.support.v7.app.AlertDialog instead of android.app.AlertDialog then the dialog will be covered by the keyboard.
30 import android.support.v7.app.AlertDialog;
31 import android.view.KeyEvent;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.EditText;
37 public class CreateHomeScreenShortcut extends DialogFragment {
38 // The public interface is used to send information back to the parent activity.
39 public interface CreateHomeScreenSchortcutListener {
40 void onCreateHomeScreenShortcut(DialogFragment dialogFragment);
43 //`createHomeScreenShortcutListener` is used in `onAttach()` and `onCreateDialog()`.
44 private CreateHomeScreenSchortcutListener createHomeScreenShortcutListener;
46 // Check to make sure that the parent activity implements the listener.
47 public void onAttach(Activity parentActivity) {
48 super.onAttach(parentActivity);
50 createHomeScreenShortcutListener = (CreateHomeScreenSchortcutListener) parentActivity;
51 } catch(ClassCastException exception) {
52 throw new ClassCastException(parentActivity.toString() + " must implement CreateHomeScreenShortcutListener.");
57 public Dialog onCreateDialog(Bundle savedInstanceState) {
58 // Get the activity's layout inflater.
59 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
61 // Create a drawable version of the favorite icon.
62 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
64 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
65 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
66 dialogBuilder.setTitle(R.string.create_shortcut);
67 dialogBuilder.setIcon(favoriteIconDrawable);
68 // The parent view is `null` because it will be assigned by `AlertDialog`.
69 dialogBuilder.setView(layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
71 // Set an `onClick` listener on the negative button.
72 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
74 public void onClick(DialogInterface dialog, int which) {
75 // Do nothing if `Cancel` is clicked.
79 // Set an `onClick` listener on the positive button.
80 dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
82 public void onClick(DialogInterface dialog, int which) {
83 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
88 // Create an `AlertDialog` from the `AlertDialog.Builder`.
89 final AlertDialog alertDialog = dialogBuilder.create();
91 // Show the keyboard when the Dialog is displayed on the screen.
92 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
94 // We need to show `alertDialog` before we can call `setOnKeyListener()` below.
97 // Allow the "enter" key on the keyboard to create the shortcut.
98 EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcut_name_edittext);
99 assert shortcutNameEditText != null; // Remove the warning below that shortcutNameEditText might be null.
100 shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
101 public boolean onKey(View v, int keyCode, KeyEvent event) {
102 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
103 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
104 // Trigger the create listener.
105 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
107 // Manually dismiss `alertDialog`.
108 alertDialog.dismiss();
110 // Consume the event.
112 } else { // If any other key was pressed, do not consume the event.
118 // `onCreateDialog` requires the return of an `AlertDialog`.