2 * Copyright Soren Stoutner 2015.
5 package com.stoutner.privacybrowser;
7 import android.app.Activity;
8 import android.app.Dialog;
9 import android.content.DialogInterface;
10 import android.graphics.drawable.BitmapDrawable;
11 import android.graphics.drawable.Drawable;
12 import android.os.Bundle;
13 import android.support.annotation.NonNull;
14 import android.support.v4.app.DialogFragment;
15 import android.support.v7.app.AlertDialog;
16 import android.support.v7.app.AppCompatDialogFragment;
17 import android.view.KeyEvent;
18 import android.view.LayoutInflater;
19 import android.view.View;
20 import android.view.WindowManager;
21 import android.widget.EditText;
23 public class CreateHomeScreenShortcut extends AppCompatDialogFragment {
24 // The public interface is used to send information back to the activity that called CreateHomeScreenShortcut.
25 public interface CreateHomeScreenSchortcutListener {
26 void onCreateHomeScreenShortcutCancel(DialogFragment dialog);
28 void onCreateHomeScreenShortcutCreate(DialogFragment dialog);
31 CreateHomeScreenSchortcutListener buttonListener;
33 // Check to make sure that the activity that called CreateHomeScreenShortcut implements both listeners.
34 public void onAttach(Activity activity) {
35 super.onAttach(activity);
37 buttonListener = (CreateHomeScreenSchortcutListener) activity;
38 } catch (ClassCastException e) {
39 throw new ClassCastException(activity.toString() + " must implement CreateHomeScreenShortcutListener.");
43 // onCreateDialog requires @NonNull.
46 public Dialog onCreateDialog(Bundle savedInstanceState) {
47 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
48 LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
50 // Create a drawable version of the favorite icon.
51 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), Webview.favoriteIcon);
53 // Setup dialogBuilder.
54 alertDialogBuilder.setTitle(R.string.shortcut_name);
55 alertDialogBuilder.setIcon(favoriteIconDrawable);
56 alertDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
57 alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
59 public void onClick(DialogInterface dialog, int which) {
60 buttonListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this);
63 alertDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
65 public void onClick(DialogInterface dialog, int which) {
66 buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
70 // Assign the resulting built dialog to an AlertDialog.
71 final AlertDialog alertDialog = alertDialogBuilder.create();
73 // Show the keyboard when the dialog is displayed on the screen.
74 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
76 // We need to show alertDialog before we can setOnKeyListener below.
79 EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcutNameEditText);
81 // Allow the "enter" key on the keyboard to create the shortcut.
82 shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
83 public boolean onKey(View v, int keyCode, KeyEvent event) {
84 // If the event is a key-down event on the "enter" button, select the PositiveButton "Create".
85 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
86 // Trigger the create listener.
87 buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
89 // Manually dismiss alertDialog.
90 alertDialog.dismiss();
95 // If any other key was pressed, do not consume the event.
101 // onCreateDialog requires the return of an AlertDialog.