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