]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/CreateHomeScreenShortcut.java
Initial bookmarks implementation. Fix for Custom User Agent not working. https...
[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.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 import android.support.annotation.NonNull;
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 onCreateHomeScreenShortcutCancel(DialogFragment dialogFragment);
42
43         void onCreateHomeScreenShortcutCreate(DialogFragment dialogFragment);
44     }
45
46     //createHomeScreenShortcutListener is used in onAttach and and onCreateDialog.
47     private CreateHomeScreenSchortcutListener createHomeScreenShortcutListener;
48
49     // Check to make sure that the parent activity implements the listener.
50     public void onAttach(Activity parentActivity) {
51         super.onAttach(parentActivity);
52         try {
53             createHomeScreenShortcutListener = (CreateHomeScreenSchortcutListener) parentActivity;
54         } catch(ClassCastException exception) {
55             throw new ClassCastException(parentActivity.toString() + " must implement CreateHomeScreenShortcutListener.");
56         }
57     }
58
59     // onCreateDialog requires @NonNull.
60     @Override
61     @NonNull
62     public Dialog onCreateDialog(Bundle savedInstanceState) {
63         // Create a drawable version of the favorite icon.
64         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIcon);
65
66         // Get the activity's layout inflater.
67         LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
68
69         // Use AlertDialog.Builder to create the AlertDialog.  The style formats the color of the button text.
70         AlertDialog.Builder createHomeScreenShorcutDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowser_AlertDialog);
71         createHomeScreenShorcutDialogBuilder.setTitle(R.string.create_shortcut);
72         createHomeScreenShorcutDialogBuilder.setIcon(favoriteIconDrawable);
73         // The parent view is "null" because it will be assigned by AlertDialog.
74         createHomeScreenShorcutDialogBuilder.setView(customDialogInflater.inflate(R.layout.create_home_screen_shortcut_dialog, null));
75
76         // Set an onClick listener on the negative button.
77         createHomeScreenShorcutDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
78             @Override
79             public void onClick(DialogInterface dialog, int which) {
80                 createHomeScreenShortcutListener.onCreateHomeScreenShortcutCancel(CreateHomeScreenShortcut.this);
81             }
82         });
83
84         // Set an onClick listener on the positive button.
85         createHomeScreenShorcutDialogBuilder.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
86             @Override
87             public void onClick(DialogInterface dialog, int which) {
88                 createHomeScreenShortcutListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
89             }
90         });
91
92
93         // Create an AlertDialog from the AlertDialogBuilder.
94         final AlertDialog createHomeScreenShortcutAlertDialog = createHomeScreenShorcutDialogBuilder.create();
95
96         // Show the keyboard when the Dialog is displayed on the screen.
97         createHomeScreenShortcutAlertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
98
99         // We need to show the AlertDialog before we can call setOnKeyListener() below.
100         createHomeScreenShortcutAlertDialog.show();
101
102         // Allow the "enter" key on the keyboard to create the shortcut.
103         EditText shortcutNameEditText = (EditText) createHomeScreenShortcutAlertDialog.findViewById(R.id.shortcut_name_edittext);
104         assert shortcutNameEditText != null;  // Remove the warning below that shortcutNameEditText might be null.
105         shortcutNameEditText.setOnKeyListener(new View.OnKeyListener() {
106             public boolean onKey(View v, int keyCode, KeyEvent event) {
107                 // If the event is a key-down on the "enter" button, select the PositiveButton "Create".
108                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
109                     // Trigger the create listener.
110                     createHomeScreenShortcutListener.onCreateHomeScreenShortcutCreate(CreateHomeScreenShortcut.this);
111
112                     // Manually dismiss the AlertDialog.
113                     createHomeScreenShortcutAlertDialog.dismiss();
114
115                     // Consume the event.
116                     return true;
117                 } else {  // If any other key was pressed, do not consume the event.
118                     return false;
119                 }
120             }
121         });
122
123         // onCreateDialog requires the return of an AlertDialog.
124         return createHomeScreenShortcutAlertDialog;
125     }
126 }