OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 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.sbrowser.contentblocker; |
| 19 |
| 20 import android.app.AlertDialog; |
| 21 import android.content.Context; |
| 22 import android.graphics.drawable.Drawable; |
| 23 import android.os.Build; |
| 24 import android.os.Bundle; |
| 25 import android.preference.EditTextPreference; |
| 26 import android.support.annotation.DrawableRes; |
| 27 import android.support.v4.content.ContextCompat; |
| 28 import android.text.Editable; |
| 29 import android.text.InputType; |
| 30 import android.text.TextWatcher; |
| 31 import android.util.AttributeSet; |
| 32 import android.util.Patterns; |
| 33 import android.view.KeyEvent; |
| 34 import android.view.inputmethod.EditorInfo; |
| 35 import android.widget.EditText; |
| 36 import android.widget.TextView; |
| 37 import android.widget.Toast; |
| 38 |
| 39 import org.adblockplus.adblockplussbrowser.R; |
| 40 |
| 41 public class UrlInputOpenerPreference extends EditTextPreference implements Text
Watcher, |
| 42 TextView.OnEditorActionListener |
| 43 { |
| 44 |
| 45 private OnUrlReadyListener onUrlReadyListener; |
| 46 private AlertDialog mAlertDialog; |
| 47 |
| 48 public UrlInputOpenerPreference(Context context) |
| 49 { |
| 50 this(context, null); |
| 51 } |
| 52 |
| 53 public UrlInputOpenerPreference(Context context, AttributeSet attrs) |
| 54 { |
| 55 super(context, attrs); |
| 56 |
| 57 // Setting defaults |
| 58 this.setIcon(android.R.drawable.ic_menu_add); |
| 59 this.setPositiveButtonText(android.R.string.ok); |
| 60 this.setNegativeButtonText(android.R.string.cancel); |
| 61 final EditText editText = getEditText(); |
| 62 editText.addTextChangedListener(this); |
| 63 editText.setOnEditorActionListener(this); |
| 64 editText.setInputType(InputType.TYPE_TEXT_VARIATION_URI); |
| 65 } |
| 66 |
| 67 @Override |
| 68 protected void showDialog(Bundle state) |
| 69 { |
| 70 super.showDialog(state); |
| 71 |
| 72 mAlertDialog = (AlertDialog) getDialog(); |
| 73 // Positive button is disabled until a valid URL is entered |
| 74 this.setPositiveButtonEnabled(false); |
| 75 } |
| 76 |
| 77 @Override |
| 78 protected void onDialogClosed(boolean positiveResult) |
| 79 { |
| 80 super.onDialogClosed(positiveResult); |
| 81 |
| 82 mAlertDialog = null; |
| 83 if (positiveResult && this.onUrlReadyListener != null) |
| 84 { |
| 85 this.onUrlReadyListener.onUrlReady(getUrl()); |
| 86 } |
| 87 } |
| 88 |
| 89 @Override |
| 90 public void beforeTextChanged(CharSequence s, int start, int count, int after) |
| 91 { |
| 92 // Ignored |
| 93 } |
| 94 |
| 95 @Override |
| 96 public void onTextChanged(CharSequence s, int start, int before, int count) |
| 97 { |
| 98 // Ignored |
| 99 } |
| 100 |
| 101 @Override |
| 102 public void afterTextChanged(Editable s) |
| 103 { |
| 104 setPositiveButtonEnabled(isValidUrl()); |
| 105 } |
| 106 |
| 107 @Override |
| 108 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
| 109 { |
| 110 if (actionId == EditorInfo.IME_ACTION_DONE) |
| 111 { |
| 112 if (this.isValidUrl()) |
| 113 { |
| 114 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); |
| 115 } |
| 116 else |
| 117 { |
| 118 Toast.makeText(getContext(), R.string.whitelist_website_invalid_url_erro
r, Toast.LENGTH_SHORT).show(); |
| 119 } |
| 120 return true; |
| 121 } |
| 122 return false; |
| 123 } |
| 124 |
| 125 public void setIcon(@DrawableRes int iconResId) |
| 126 { |
| 127 final Drawable drawable = ContextCompat.getDrawable(getContext(), iconResId)
; |
| 128 setIcon(drawable); |
| 129 } |
| 130 |
| 131 public void setIcon(Drawable icon) |
| 132 { |
| 133 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) |
| 134 { |
| 135 super.setIcon(android.R.drawable.ic_menu_add); |
| 136 } |
| 137 } |
| 138 |
| 139 public void setOnUrlReadyListener(OnUrlReadyListener listener) |
| 140 { |
| 141 this.onUrlReadyListener = listener; |
| 142 } |
| 143 |
| 144 private boolean isValidUrl() |
| 145 { |
| 146 return Patterns.WEB_URL.matcher(getUrl()).matches(); |
| 147 } |
| 148 |
| 149 private String getUrl() |
| 150 { |
| 151 return getEditText().getText().toString(); |
| 152 } |
| 153 |
| 154 private void setPositiveButtonEnabled(boolean enabled) |
| 155 { |
| 156 if (mAlertDialog != null) |
| 157 { |
| 158 mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
| 159 } |
| 160 } |
| 161 |
| 162 public interface OnUrlReadyListener |
| 163 { |
| 164 void onUrlReady(String url); |
| 165 } |
| 166 } |
OLD | NEW |