]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/PreferencesFragment.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / PreferencesFragment.java
1 /*
2  * Copyright (C) 2013 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner;
18
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;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32
33 public final class PreferencesFragment 
34     extends PreferenceFragment 
35     implements SharedPreferences.OnSharedPreferenceChangeListener {
36
37   private CheckBoxPreference[] checkBoxPrefs;
38   
39   @Override
40   public void onCreate(Bundle icicle) {
41     super.onCreate(icicle);
42     addPreferencesFromResource(R.xml.preferences);
43     
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();
54
55     EditTextPreference customProductSearch = (EditTextPreference)
56         preferences.findPreference(PreferencesActivity.KEY_CUSTOM_PRODUCT_SEARCH);
57     customProductSearch.setOnPreferenceChangeListener(new CustomSearchURLValidator());
58   }
59
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]);
64     }
65     return prefs;
66   }
67   
68   @Override
69   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
70     disableLastCheckedPref();
71   }
72
73   private void disableLastCheckedPref() {
74     Collection<CheckBoxPreference> checked = new ArrayList<>(checkBoxPrefs.length);
75     for (CheckBoxPreference pref : checkBoxPrefs) {
76       if (pref.isChecked()) {
77         checked.add(pref);
78       }
79     }
80     boolean disable = checked.size() <= 1;
81     for (CheckBoxPreference pref : checkBoxPrefs) {
82       pref.setEnabled(!(disable && checked.contains(pref)));
83     }
84   }
85
86   private class CustomSearchURLValidator implements Preference.OnPreferenceChangeListener {
87     @Override
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);
95         builder.show();
96         return false;
97       }
98       return true;
99     }
100
101     private boolean isValid(Object newValue) {
102       // Allow empty/null value
103       if (newValue == null) {
104         return true;
105       }
106       String valueString = newValue.toString();
107       if (valueString.isEmpty()) {
108         return true;
109       }
110       // Before validating, remove custom placeholders, which will not
111       // be considered valid parts of the URL in some locations:
112       // Blank %t and %s:
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:
117       try {
118         URI uri = new URI(valueString);
119         return uri.getScheme() != null;
120       } catch (URISyntaxException use) {
121         return false;
122       }
123     }
124   }
125
126 }