2 * Copyright 2015 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.content.DialogInterface;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.os.Bundle;
28 import android.support.annotation.NonNull;
29 import android.support.v4.app.DialogFragment;
30 import android.support.v7.app.AlertDialog;
31 import android.support.v7.app.AppCompatDialogFragment;
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 CreateHomeScreenShortcut extends AppCompatDialogFragment {
39 // The public interface is used to send information back to the activity that called CreateHomeScreenShortcut.
40 public interface CreateHomeScreenSchortcutListener {
41 void onCreateHomeScreenShortcutCancel(DialogFragment dialog);
43 void onCreateHomeScreenShortcutCreate(DialogFragment dialog);
46 CreateHomeScreenSchortcutListener buttonListener;
48 // Check to make sure that the activity that called CreateHomeScreenShortcut implements both listeners.
49 public void onAttach(Activity activity) {
50 super.onAttach(activity);
52 buttonListener = (CreateHomeScreenSchortcutListener) activity;
53 } catch (ClassCastException e) {
54 throw new ClassCastException(activity.toString() + " must implement CreateHomeScreenShortcutListener.");
58 // onCreateDialog requires @NonNull.
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
66 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
67 LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
68 alertDialogBuilder.setTitle(R.string.shortcut_name);
69 alertDialogBuilder.setIcon(favoriteIconDrawable);
70 alertDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
71 alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
73 public void onClick(DialogInterface dialog, int which) {
74 buttonListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this);
77 alertDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
79 public void onClick(DialogInterface dialog, int which) {
80 buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
84 // Assign the alertDialogBuilder to an AlertDialog.
85 final AlertDialog alertDialog = alertDialogBuilder.create();
87 // Show the keyboard when the dialog is displayed on the screen.
88 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
90 // We need to show alertDialog before we can setOnKeyListener below.
93 EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcutNameEditText);
95 // Allow the "enter" key on the keyboard to create the shortcut.
96 shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
97 public boolean onKey(View v, int keyCode, KeyEvent event) {
98 // If the event is a key-down event on the "enter" button, select the PositiveButton "Create".
99 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
100 // Trigger the create listener.
101 buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
103 // Manually dismiss alertDialog.
104 alertDialog.dismiss();
106 // Consume the event.
109 // If any other key was pressed, do not consume the event.
115 // onCreateDialog requires the return of an AlertDialog.