|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.focus.search; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import android.content.SharedPreferences; |
| 9 | +import android.preference.Preference; |
| 10 | +import android.support.design.widget.TextInputLayout; |
| 11 | +import android.text.Editable; |
| 12 | +import android.text.TextUtils; |
| 13 | +import android.text.TextWatcher; |
| 14 | +import android.util.AttributeSet; |
| 15 | +import android.view.View; |
| 16 | +import android.view.ViewGroup; |
| 17 | +import android.widget.EditText; |
| 18 | + |
| 19 | +import org.mozilla.focus.R; |
| 20 | +import org.mozilla.focus.utils.UrlUtils; |
| 21 | + |
| 22 | +public class ManualAddSearchEnginePreference extends Preference { |
| 23 | + private EditText engineNameEditText; |
| 24 | + private EditText searchQueryEditText; |
| 25 | + private TextInputLayout engineNameErrorLayout; |
| 26 | + private TextInputLayout searchQueryErrorLayout; |
| 27 | + |
| 28 | + public ManualAddSearchEnginePreference(Context context, AttributeSet attrs) { |
| 29 | + super(context, attrs); |
| 30 | + } |
| 31 | + |
| 32 | + public ManualAddSearchEnginePreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 33 | + super(context, attrs, defStyleAttr); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected View onCreateView(ViewGroup parent) { |
| 38 | + final View view = super.onCreateView(parent); |
| 39 | + |
| 40 | + engineNameErrorLayout = view.findViewById(R.id.edit_engine_name_layout); |
| 41 | + searchQueryErrorLayout = view.findViewById(R.id.edit_search_string_layout); |
| 42 | + |
| 43 | + engineNameEditText = view.findViewById(R.id.edit_engine_name); |
| 44 | + engineNameEditText.addTextChangedListener(buildTextWatcherForErrorLayout(engineNameErrorLayout)); |
| 45 | + searchQueryEditText = view.findViewById(R.id.edit_search_string); |
| 46 | + searchQueryEditText.addTextChangedListener(buildTextWatcherForErrorLayout(searchQueryErrorLayout)); |
| 47 | + return view; |
| 48 | + } |
| 49 | + |
| 50 | + private boolean engineNameIsUnique(String engineName) { |
| 51 | + final SharedPreferences sharedPreferences = getContext().getSharedPreferences(SearchEngineManager.PREF_FILE_SEARCH_ENGINES, Context.MODE_PRIVATE); |
| 52 | + return !sharedPreferences.contains(engineName); |
| 53 | + } |
| 54 | + |
| 55 | + public boolean validateEngineNameAndShowError(String engineName) { |
| 56 | + if (TextUtils.isEmpty(engineName) || !engineNameIsUnique(engineName)) { |
| 57 | + engineNameErrorLayout.setError(getContext().getString(R.string.search_add_error_empty_name)); |
| 58 | + return false; |
| 59 | + } else { |
| 60 | + engineNameErrorLayout.setError(null); |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public boolean validateSearchQueryAndShowError(String searchQuery) { |
| 66 | + if (TextUtils.isEmpty(searchQuery)) { |
| 67 | + searchQueryErrorLayout.setError(getContext().getString(R.string.search_add_error_empty_search)); |
| 68 | + return false; |
| 69 | + } else if (!UrlUtils.isValidSearchQueryUrl(searchQuery)) { |
| 70 | + searchQueryErrorLayout.setError(getContext().getString(R.string.search_add_error_format)); |
| 71 | + return false; |
| 72 | + } else { |
| 73 | + searchQueryErrorLayout.setError(null); |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private TextWatcher buildTextWatcherForErrorLayout(final TextInputLayout errorLayout) { |
| 79 | + return new TextWatcher() { |
| 80 | + @Override |
| 81 | + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { |
| 85 | + errorLayout.setError(null); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void afterTextChanged(Editable editable) {} |
| 90 | + }; |
| 91 | + } |
| 92 | +} |
0 commit comments