Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: mobile/android/thirdparty/org/adblockplus/browser/InputValidatorDialogPreference.java

Issue 29543774: Issue 2801 - Create 'Whitelisted websites' screen and add link to 'Ad blocking' screen (Closed)
Patch Set: Created Sept. 13, 2017, 5:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.browser; 18 package org.adblockplus.browser;
19 19
20 import java.lang.ref.WeakReference; 20 import android.app.AlertDialog;
21
22 import android.content.Context; 21 import android.content.Context;
23 import android.preference.Preference; 22 import android.os.Bundle;
23 import android.preference.EditTextPreference;
24 import android.text.Editable;
25 import android.text.InputType;
26 import android.text.TextWatcher;
24 import android.util.AttributeSet; 27 import android.util.AttributeSet;
25 28 import android.util.Patterns;
26 public class UrlInputOpenerPreference extends Preference implements UrlInputDial og.UrlReadyCallback 29 import android.view.KeyEvent;
30 import android.view.View;
31 import android.view.inputmethod.EditorInfo;
32 import android.widget.EditText;
33 import android.widget.TextView;
34 import android.widget.Toast;
35
36 import org.apache.commons.validator.routines.DomainValidator;
37 import org.mozilla.gecko.R;
38
39 public class InputValidatorDialogPreference extends EditTextPreference implement s TextWatcher,
40 TextView.OnEditorActionListener
27 { 41 {
28 private static WeakReference<UrlInputDialog.UrlReadyCallback> redirectUrlReady Callback = null; 42
29 43 public enum ValidationType
30 public UrlInputOpenerPreference(Context context) 44 {
45 URL, DOMAIN
46 }
47
48 private ValidationType validationType;
49 private String errorMessage;
50 private OnInputReadyListener onInputReadyListener;
51 private AlertDialog alertDialog;
52
53 public InputValidatorDialogPreference(Context context)
31 { 54 {
32 super(context); 55 super(context);
33 } 56 this.init();
34 57 }
35 public UrlInputOpenerPreference(Context context, AttributeSet attrs) 58
59 public InputValidatorDialogPreference(Context context, AttributeSet attrs)
36 { 60 {
37 super(context, attrs); 61 super(context, attrs);
38 } 62 this.init();
39 63 }
40 public UrlInputOpenerPreference(Context context, AttributeSet attrs, int defSt yleAttr) 64
65 public InputValidatorDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)
41 { 66 {
42 super(context, attrs, defStyleAttr); 67 super(context, attrs, defStyleAttr);
43 } 68 this.init();
44 69 }
45 protected static synchronized void setRedirectUrlReadyCallback( 70
46 final UrlInputDialog.UrlReadyCallback callback) 71 private void init()
47 { 72 {
48 redirectUrlReadyCallback = new WeakReference<UrlInputDialog.UrlReadyCallback >(callback); 73 // Setting defaults
49 } 74 this.setIcon(android.R.drawable.ic_menu_add);
50 75 this.setPositiveButtonText(android.R.string.ok);
51 @Override 76 this.setNegativeButtonText(android.R.string.cancel);
52 protected void onAttachedToActivity() 77 this.setValidationType(ValidationType.URL);
53 { 78 this.setErrorMessage(R.string.abb_invalid_url);
54 this.setPersistent(false); 79 final EditText editText = this.getEditText();
55 } 80 editText.addTextChangedListener(this);
56 81 editText.setOnEditorActionListener(this);
57 @Override 82 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
58 protected void onClick() 83 }
59 { 84
60 new UrlInputDialog(this.getContext(), UrlInputDialog.Type.ADD_SUBSCRIPTION, this).show(); 85 @Override
61 } 86 protected void showDialog(Bundle state)
62 87 {
63 @Override 88 super.showDialog(state);
64 public void callback(String url) 89
65 { 90 this.alertDialog = (AlertDialog) this.getDialog();
66 try 91 // Positive button is disabled until a valid input is entered
67 { 92 this.setPositiveButtonEnabled(false);
68 if (redirectUrlReadyCallback != null) 93 }
94
95 @Override
96 protected void onDialogClosed(boolean positiveResult)
97 {
98 super.onDialogClosed(positiveResult);
99
100 this.alertDialog = null;
101 if (positiveResult && this.onInputReadyListener != null)
102 {
103 this.onInputReadyListener.onInputReady(this.getInput());
104 }
105 }
106
107 @Override
108 public void beforeTextChanged(CharSequence s, int start, int count, int after)
109 {
110 // Ignored
111 }
112
113 @Override
114 public void onTextChanged(CharSequence s, int start, int before, int count)
115 {
116 // Ignored
117 }
118
119 @Override
120 public void afterTextChanged(Editable s)
121 {
122 this.setPositiveButtonEnabled(this.isValidInput());
123 }
124
125 @Override
126 public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
127 {
128 if (actionId == EditorInfo.IME_ACTION_DONE)
129 {
130 if (this.isValidInput())
69 { 131 {
70 final UrlInputDialog.UrlReadyCallback callback = redirectUrlReadyCallbac k.get(); 132 this.alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
71 if (callback != null)
72 {
73 callback.callback(url);
74 }
75 } 133 }
76 } 134 else
77 catch (Throwable t) 135 {
78 { 136 Toast.makeText(this.getContext(), this.errorMessage, Toast.LENGTH_SHORT) .show();
79 // we do not care 137 }
80 } 138 return true;
139 }
140 return false;
141 }
142
143 @Override
144 protected void onBindView(View view)
145 {
146 super.onBindView(view);
147 final TextView title = (TextView) view.findViewById(android.R.id.title);
148 if (title != null)
149 {
150 title.setSingleLine(false);
151 title.setEllipsize(null);
152 }
153 }
154
155 public void setValidationType(ValidationType validationType)
156 {
157 this.validationType = validationType;
158 }
159
160 public void setErrorMessage(int msgResId)
161 {
162 this.setErrorMessage(this.getContext().getString(msgResId));
163 }
164
165 public void setErrorMessage(String message)
166 {
167 this.errorMessage = message;
168 }
169
170 public void setOnInputReadyListener(OnInputReadyListener listener)
171 {
172 this.onInputReadyListener = listener;
173 }
174
175 private boolean isValidInput()
176 {
177 switch (this.validationType)
178 {
179 case DOMAIN:
180 return this.isValidDomain();
181 default:
182 return this.isValidUrl();
183 }
184 }
185
186 private boolean isValidDomain()
187 {
188 return DomainValidator.getInstance().isValid(this.getInput());
189 }
190
191 private boolean isValidUrl()
192 {
193 return Patterns.WEB_URL.matcher(this.getInput()).matches();
194 }
195
196 private String getInput()
197 {
198 return this.getEditText().getText().toString();
199 }
200
201 private void setPositiveButtonEnabled(boolean enabled)
202 {
203 if (this.alertDialog != null)
204 {
205 this.alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled );
206 }
207 }
208
209 public interface OnInputReadyListener
210 {
211 void onInputReady(String input);
81 } 212 }
82 } 213 }
OLDNEW

Powered by Google App Engine
This is Rietveld