Add option to remember duplicate scans
[zxing.git] / android / src / com / google / zxing / client / android / PreferencesActivity.java
1 /*
2  * Copyright (C) 2008 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 com.google.zxing.client.android;
18
19 import android.content.SharedPreferences;
20 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
21 import android.os.Bundle;
22 import android.preference.CheckBoxPreference;
23 import android.preference.PreferenceActivity;
24 import android.preference.PreferenceScreen;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28
29 /**
30  * The main settings activity.
31  *
32  * @author dswitkin@google.com (Daniel Switkin)
33  */
34 public final class PreferencesActivity extends PreferenceActivity
35     implements OnSharedPreferenceChangeListener {
36
37   public static final String KEY_DECODE_1D = "preferences_decode_1D";
38   public static final String KEY_DECODE_QR = "preferences_decode_QR";
39   public static final String KEY_DECODE_DATA_MATRIX = "preferences_decode_Data_Matrix";
40   public static final String KEY_CUSTOM_PRODUCT_SEARCH = "preferences_custom_product_search";
41
42   public static final String KEY_PLAY_BEEP = "preferences_play_beep";
43   public static final String KEY_VIBRATE = "preferences_vibrate";
44   public static final String KEY_COPY_TO_CLIPBOARD = "preferences_copy_to_clipboard";
45   public static final String KEY_FRONT_LIGHT = "preferences_front_light";
46   public static final String KEY_BULK_MODE = "preferences_bulk_mode";
47   public static final String KEY_REMEMBER_DUPLICATES = "preferences_remember_duplicates";
48
49   public static final String KEY_HELP_VERSION_SHOWN = "preferences_help_version_shown";
50   public static final String KEY_NOT_OUR_RESULTS_SHOWN = "preferences_not_out_results_shown";
51
52   private CheckBoxPreference decode1D;
53   private CheckBoxPreference decodeQR;
54   private CheckBoxPreference decodeDataMatrix;
55
56   @Override
57   protected void onCreate(Bundle icicle) {
58     super.onCreate(icicle);
59     addPreferencesFromResource(R.xml.preferences);
60
61     PreferenceScreen preferences = getPreferenceScreen();
62     preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
63     decode1D = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_1D);
64     decodeQR = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_QR);
65     decodeDataMatrix = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_DATA_MATRIX);
66     disableLastCheckedPref();
67   }
68
69   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
70     disableLastCheckedPref();
71   }
72
73   private void disableLastCheckedPref() {
74     Collection<CheckBoxPreference> checked = new ArrayList<CheckBoxPreference>(3);
75     if (decode1D.isChecked()) {
76       checked.add(decode1D);
77     }
78     if (decodeQR.isChecked()) {
79       checked.add(decodeQR);
80     }
81     if (decodeDataMatrix.isChecked()) {
82       checked.add(decodeDataMatrix);
83     }
84     boolean disable = checked.size() < 2;
85     for (CheckBoxPreference pref : new CheckBoxPreference[] {decode1D, decodeQR, decodeDataMatrix}) {
86       pref.setEnabled(!(disable && checked.contains(pref)));
87     }
88   }
89
90 }