]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java
Create an "Add to Home Screen" AlertDialog.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / CreateHomeScreenShortcut.java
1 /**
2  * Copyright Soren Stoutner 2015.
3  */
4
5 package com.stoutner.privacybrowser;
6
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;
22
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);
27
28         void onCreateHomeScreenShortcutCreate(DialogFragment dialog);
29     }
30
31     CreateHomeScreenSchortcutListener buttonListener;
32
33     // Check to make sure that the activity that called CreateHomeScreenShortcut implements both listeners.
34     public void onAttach(Activity activity) {
35         super.onAttach(activity);
36         try {
37             buttonListener = (CreateHomeScreenSchortcutListener) activity;
38         } catch (ClassCastException e) {
39             throw new ClassCastException(activity.toString() + " must implement CreateHomeScreenShortcutListener.");
40         }
41     }
42
43     // onCreateDialog requires @NonNull.
44     @Override
45     @NonNull
46     public Dialog onCreateDialog(Bundle savedInstanceState) {
47         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
48         LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
49
50         // Create a drawable version of the favorite icon.
51         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), Webview.favoriteIcon);
52
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() {
58             @Override
59             public void onClick(DialogInterface dialog, int which) {
60                 buttonListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this);
61             }
62         });
63         alertDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
64             @Override
65             public void onClick(DialogInterface dialog, int which) {
66                 buttonListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
67             }
68         });
69
70         // Assign the resulting built dialog to an AlertDialog.
71         final AlertDialog alertDialog = alertDialogBuilder.create();
72
73         // Show the keyboard when the dialog is displayed on the screen.
74         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
75
76         // We need to show alertDialog before we can setOnKeyListener below.
77         alertDialog.show();
78
79         EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcutNameEditText);
80
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);
88
89                     // Manually dismiss alertDialog.
90                     alertDialog.dismiss();
91
92                     // Consume the event.
93                     return true;
94                 } else {
95                     // If any other key was pressed, do not consume the event.
96                     return false;
97                 }
98             }
99         });
100
101         // onCreateDialog requires the return of an AlertDialog.
102         return alertDialog;
103     }
104 }