2 * Copyright (C) 2013 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package cz.cvut.fel.dce.barcodescanner;
19 import android.app.AlertDialog;
20 import android.content.SharedPreferences;
21 import android.os.Bundle;
22 import android.preference.CheckBoxPreference;
23 import android.preference.EditTextPreference;
24 import android.preference.Preference;
25 import android.preference.PreferenceFragment;
26 import android.preference.PreferenceScreen;
29 import java.net.URISyntaxException;
30 import java.util.ArrayList;
31 import java.util.Collection;
33 public final class PreferencesFragment
34 extends PreferenceFragment
35 implements SharedPreferences.OnSharedPreferenceChangeListener {
37 private CheckBoxPreference[] checkBoxPrefs;
40 public void onCreate(Bundle icicle) {
41 super.onCreate(icicle);
42 addPreferencesFromResource(R.xml.preferences);
44 PreferenceScreen preferences = getPreferenceScreen();
45 preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
46 checkBoxPrefs = findDecodePrefs(preferences,
47 PreferencesActivity.KEY_DECODE_1D_PRODUCT,
48 PreferencesActivity.KEY_DECODE_1D_INDUSTRIAL,
49 PreferencesActivity.KEY_DECODE_QR,
50 PreferencesActivity.KEY_DECODE_DATA_MATRIX,
51 PreferencesActivity.KEY_DECODE_AZTEC,
52 PreferencesActivity.KEY_DECODE_PDF417);
53 disableLastCheckedPref();
55 EditTextPreference customProductSearch = (EditTextPreference)
56 preferences.findPreference(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH);
57 customProductSearch.setOnPreferenceChangeListener(new CustomSearchURLValidator());
60 private static CheckBoxPreference[] findDecodePrefs(PreferenceScreen preferences, String... keys) {
61 CheckBoxPreference[] prefs = new CheckBoxPreference[keys.length];
62 for (int i = 0; i < keys.length; i++) {
63 prefs[i] = (CheckBoxPreference) preferences.findPreference(keys[i]);
69 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
70 disableLastCheckedPref();
73 private void disableLastCheckedPref() {
74 Collection<CheckBoxPreference> checked = new ArrayList<>(checkBoxPrefs.length);
75 for (CheckBoxPreference pref : checkBoxPrefs) {
76 if (pref.isChecked()) {
80 boolean disable = checked.size() <= 1;
81 for (CheckBoxPreference pref : checkBoxPrefs) {
82 pref.setEnabled(!(disable && checked.contains(pref)));
86 private class CustomSearchURLValidator implements Preference.OnPreferenceChangeListener {
88 public boolean onPreferenceChange(Preference preference, Object newValue) {
89 if (!isValid(newValue)) {
90 AlertDialog.Builder builder =
91 new AlertDialog.Builder(PreferencesFragment.this.getActivity());
92 builder.setTitle(R.string.msg_error);
93 builder.setMessage(R.string.msg_invalid_value);
94 builder.setCancelable(true);
101 private boolean isValid(Object newValue) {
102 // Allow empty/null value
103 if (newValue == null) {
106 String valueString = newValue.toString();
107 if (valueString.isEmpty()) {
110 // Before validating, remove custom placeholders, which will not
111 // be considered valid parts of the URL in some locations:
113 valueString = valueString.replaceAll("%[st]", "");
114 // Blank %f but not if followed by digit or a-f as it may be a hex sequence
115 valueString = valueString.replaceAll("%f(?![0-9a-f])", "");
116 // Require a scheme otherwise:
118 URI uri = new URI(valueString);
119 return uri.getScheme() != null;
120 } catch (URISyntaxException use) {