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

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

Issue 29322610: Issue 2720 - [Adblocking settings] Add the other filter lists category (Closed)
Patch Set: Created July 17, 2015, 6:47 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
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 package org.adblockplus.browser;
19
20 import org.mozilla.gecko.R;
21
22 import android.app.Dialog;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.text.Editable;
26 import android.text.TextWatcher;
27 import android.util.Patterns;
28 import android.view.KeyEvent;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.inputmethod.EditorInfo;
32 import android.widget.Button;
33 import android.widget.EditText;
34 import android.widget.TextView;
35 import android.widget.TextView.OnEditorActionListener;
36
37 public class UrlInputDialog extends Dialog implements TextWatcher,
38 OnEditorActionListener, OnClickListener
39 {
40 private static final int BUTTON_ENABLED_COLOR = 0xff0099cc;
41 private static final int BUTTON_DISABLED_COLOR = 0xffc5dfe8;
42 private final Type type;
43 private final UrlReadyCallback callback;
44
45 private EditText editText = null;
46 private Button button = null;
47
48 private boolean submitEnabled = false;
49 private boolean triedSubmit = false;
50 private String currentEditTextValue = null;
51
52 public enum Type
53 {
54 ADD_BLOCKING_LIST,
55 ADD_WHITELIST
56 }
57
58 public UrlInputDialog(final Context context, final Type type, final UrlReadyCa llback callback)
59 {
60 super(context);
61 this.type = type;
62 this.callback = callback;
63 }
64
65 @Override
66 protected void onCreate(final Bundle savedInstanceState)
67 {
68 super.onCreate(savedInstanceState);
69 this.setContentView(R.layout.abb_url_input_dialog);
70
71 this.editText = (EditText) this.findViewById(R.id.abb_listadd_textinput);
72 this.button = (Button) this.findViewById(R.id.abb_listadd_button);
73
74 this.setCanceledOnTouchOutside(true);
75 this.setCancelable(true);
76
77 this.editText.addTextChangedListener(this);
78 this.editText.setOnEditorActionListener(this);
79 this.editText.requestFocus();
80
81 this.button.setOnClickListener(this);
82
83 if (this.currentEditTextValue != null)
84 {
85 this.editText.setText(this.currentEditTextValue);
86 }
87 this.updateButtonState();
88
89 switch (this.type)
90 {
91 case ADD_BLOCKING_LIST:
92 this.button.setText(R.string.abb_block_list_button);
93 this.editText.setHint(R.string.abb_block_list_url);
94 this.setTitle(R.string.abb_pref_category_add_other_list);
95 break;
96 default:
97 break;
98 }
99 }
100
101 private void updateButtonState()
102 {
103 this.button.setEnabled(this.submitEnabled);
104 this.button.setBackgroundColor(this.submitEnabled
105 ? BUTTON_ENABLED_COLOR
106 : BUTTON_DISABLED_COLOR);
107 this.button.invalidate();
108 }
109
110 @Override
111 public void afterTextChanged(final Editable s)
112 {
113 this.currentEditTextValue = s.toString();
114 this.submitEnabled = this.validateUrl(this.currentEditTextValue) != null;
115 if (this.submitEnabled)
116 {
117 this.editText.setError(null);
118 }
119 else if (this.triedSubmit)
120 {
121 this.editText.setError(this.getContext().getString(R.string.abb_invalid_ur l));
122 }
123 this.updateButtonState();
124 }
125
126 @Override
127 public void beforeTextChanged(final CharSequence s, final int start, final int count,
128 final int after)
129 {
130 // ignored
131 }
132
133 @Override
134 public void onTextChanged(final CharSequence s, final int start, final int bef ore, final int count)
135 {
136 // ignored
137 }
138
139 private String validateUrl(final String possibleUrl)
140 {
141 String url = possibleUrl.trim();
142 if (Patterns.WEB_URL.matcher(url).matches())
143 {
144 return url;
145 }
146 url = "http://" + url;
147 if (Patterns.WEB_URL.matcher(url).matches())
148 {
149 return url;
150 }
151 return null;
152 }
153
154 private void validateAndSubmit(final String inputText)
155 {
156 final String url = this.validateUrl(inputText);
157 this.triedSubmit = true;
158 if (url == null)
159 {
160 this.editText.setError(this.getContext().getString(R.string.abb_invalid_ur l));
161 }
162 else
163 {
164 this.editText.setError(null);
165 this.dismiss();
166 this.callback.callback(url);
167 }
168 }
169
170 @Override
171 public boolean onEditorAction(final TextView v, final int actionId, final KeyE vent event)
172 {
173 if (actionId == EditorInfo.IME_ACTION_DONE)
174 {
175 if (this.currentEditTextValue != null)
176 {
177 this.validateAndSubmit(this.currentEditTextValue);
178 }
179 return true;
180 }
181 return false;
182 }
183
184 public interface UrlReadyCallback
185 {
186 public void callback(String url);
187 }
188
189 @Override
190 public void onClick(final View v)
191 {
192 if (this.currentEditTextValue != null)
193 {
194 this.validateAndSubmit(this.currentEditTextValue);
195 }
196 }
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld