]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java
Refactor the code to make all the Java files lint free.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / CreateHomeScreenShortcut.java
1 /**
2  * Copyright 2015-2016 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser;
21
22 import android.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.graphics.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
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 import android.view.KeyEvent;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
37
38 public class CreateHomeScreenShortcut extends DialogFragment {
39     // The public interface is used to send information back to the parent activity.
40     public interface CreateHomeScreenSchortcutListener {
41         void onCreateHomeScreenShortcut(DialogFragment dialogFragment);
42     }
43
44     //`createHomeScreenShortcutListener` is used in `onAttach()` and `onCreateDialog()`.
45     private CreateHomeScreenSchortcutListener createHomeScreenShortcutListener;
46
47     // Check to make sure that the parent activity implements the listener.
48     public void onAttach(Context context) {
49         super.onAttach(context);
50         try {
51             createHomeScreenShortcutListener = (CreateHomeScreenSchortcutListener) context;
52         } catch(ClassCastException exception) {
53             throw new ClassCastException(context.toString() + " must implement CreateHomeScreenShortcutListener.");
54         }
55     }
56
57     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
58     @SuppressLint("InflateParams")
59     @Override
60     public Dialog onCreateDialog(Bundle savedInstanceState) {
61         // Get the activity's layout inflater.
62         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
63
64         // Create a drawable version of the favorite icon.
65         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
66
67         // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
68         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
69         dialogBuilder.setTitle(R.string.create_shortcut);
70         dialogBuilder.setIcon(favoriteIconDrawable);
71         // The parent view is `null` because it will be assigned by `AlertDialog`.
72         dialogBuilder.setView(layoutInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
73
74         // Set an `onClick` listener on the negative button.
75         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
76             @Override
77             public void onClick(DialogInterface dialog, int which) {
78                 // Do nothing if `Cancel` is clicked.
79             }
80         });
81
82         // Set an `onClick` listener on the positive button.
83         dialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
84             @Override
85             public void onClick(DialogInterface dialog, int which) {
86                 createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
87             }
88         });
89
90
91         // Create an `AlertDialog` from the `AlertDialog.Builder`.
92         final AlertDialog alertDialog = dialogBuilder.create();
93
94         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
95         assert alertDialog.getWindow() != null;
96
97         // Show the keyboard when the Dialog is displayed on the screen.
98         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
99
100         // We need to show `alertDialog` before we can call `setOnKeyListener()` below.
101         alertDialog.show();
102
103         // Allow the "enter" key on the keyboard to create the shortcut.
104         EditText shortcutNameEditText = (EditText) alertDialog.findViewById(R.id.shortcut_name_edittext);
105         assert shortcutNameEditText != null;  // Remove the warning below that shortcutNameEditText might be null.
106         shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
107             public boolean onKey(View v, int keyCode, KeyEvent event) {
108                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
109                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
110                     // Trigger the create listener.
111                     createHomeScreenShortcutListener.onCreateHomeScreenShortcut(CreateHomeScreenShortcut.this);
112
113                     // Manually dismiss `alertDialog`.
114                     alertDialog.dismiss();
115
116                     // Consume the event.
117                     return true;
118                 } else {  // If any other key was pressed, do not consume the event.
119                     return false;
120                 }
121             }
122         });
123
124         // `onCreateDialog` requires the return of an `AlertDialog`.
125         return alertDialog;
126     }
127 }