summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--assets/dependency.json21
-rw-r--r--src/com/android/camera/SettingsManager.java309
2 files changed, 143 insertions, 187 deletions
diff --git a/assets/dependency.json b/assets/dependency.json
new file mode 100644
index 0000000..6fe735b
--- /dev/null
+++ b/assets/dependency.json
@@ -0,0 +1,21 @@
+{
+ "pref_camera2_scenemode_key":
+ {
+ "_comment":"Copyright (c) 2016, The Linux Foundation. All rights reserved.\nNot a Contribution.\nCopyright (C) 2013 The Android Open Source Project\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\nhttp://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and\nlimitations under the License.",
+ "default":
+ {"pref_camera2_coloreffect_key":"0",
+ "pref_camera2_flashmode_key":"2",
+ "pref_camera2_whitebalance_key":"1",
+ "pref_camera2_exposure_key":"0",
+ "pref_camera2_clearsight_key":"off",
+ "pref_camera2_mono_preview_key":"off"}
+ ,
+ "100":
+ {"pref_camera2_longshot_key":"off",
+ "pref_camera2_mono_only_key":"off"}
+ ,
+ "0":
+ {"pref_camera2_clearsight_key":"off",
+ "pref_camera2_mono_preview_key":"off"}
+ }
+} \ No newline at end of file
diff --git a/src/com/android/camera/SettingsManager.java b/src/com/android/camera/SettingsManager.java
index dbffb84..7909541 100644
--- a/src/com/android/camera/SettingsManager.java
+++ b/src/com/android/camera/SettingsManager.java
@@ -49,12 +49,16 @@ import com.android.camera.ui.ListMenu;
import com.android.camera.util.SettingTranslation;
import org.codeaurora.snapcam.R;
+import org.json.JSONException;
+import org.json.JSONObject;
+import java.io.IOException;
+import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -111,6 +115,7 @@ public class SettingsManager implements ListMenu.SettingsListener {
private Map<String, Set<String>> mDependendsOnMap;
private boolean mIsMonoCameraPresent = false;
private boolean mIsFrontCameraPresent = false;
+ private JSONObject mDependency;
private SettingsManager(Context context) {
mListeners = new ArrayList<>();
@@ -148,6 +153,8 @@ public class SettingsManager implements ListMenu.SettingsListener {
} catch (CameraAccessException e) {
e.printStackTrace();
}
+
+ mDependency = parseJson("dependency.json");
}
public static SettingsManager createInstance(Context context) {
@@ -216,22 +223,13 @@ public class SettingsManager implements ListMenu.SettingsListener {
for (int i = 0; i < mPreferenceGroup.size(); i++) {
ListPreference pref = (ListPreference) mPreferenceGroup.get(i);
String baseKey = pref.getKey();
- CharSequence[] dependencyList = null;
- if (!specialDepedency(baseKey)) dependencyList = pref.getDependencyList();
- else {
- List<KeyValue> keyValue = getSpecialDependencyList(pref);
- if (keyValue.size() > 0) {
- dependencyList = new CharSequence[keyValue.size()];
- int k = 0;
- for (KeyValue kv: keyValue) {
- dependencyList[k++] = kv.key;
- }
- }
- pref.setDependencyList(dependencyList);
- }
- if (dependencyList != null) {
- for (int j = 0; j < dependencyList.length; j++) {
- String key = dependencyList[j].toString();
+ String value = pref.getValue();
+
+ JSONObject dependency = getDependencyList(baseKey, value);
+ if (dependency != null) {
+ Iterator<String> keys = dependency.keys();
+ while (keys.hasNext()) {
+ String key = keys.next();
pref = mPreferenceGroup.findPreference(key);
if (pref == null) continue; //filtered?
Set set = mDependendsOnMap.get(key);
@@ -250,37 +248,27 @@ public class SettingsManager implements ListMenu.SettingsListener {
for (int i = 0; i < mPreferenceGroup.size(); i++) {
ListPreference pref = (ListPreference) mPreferenceGroup.get(i);
String key = pref.getKey();
- if (mDependendsOnMap.get(key) != null && mDependendsOnMap.get(key).size() != 0) {
+ Set<String> set = mDependendsOnMap.get(key);
+ if (set != null && set.size() != 0) {
processLater.add(key);
- continue;
}
Values values = new Values(pref.getValue(), null);
mValuesMap.put(pref.getKey(), values);
}
for (String keyToProcess : processLater) {
Set<String> dependsOnSet = mDependendsOnMap.get(keyToProcess);
- boolean active = true;
- List<KeyValue> keyValue = null;
- for (String s : dependsOnSet) {
- if (specialDepedency(s) || isOptionOn(s)) {
- active = false;
- if (specialDepedency(s)) {
- keyValue = getSpecialDependencyList(s);
- }
- }
- break;
- }
- ListPreference pref = mPreferenceGroup.findPreference(keyToProcess);
- Values values = new Values(pref.getValue(), null);
- if (!active) {
- String offValue = pref.getOffValue();
- if (keyValue != null) {
- String matchValue = getMatchingValue(keyToProcess, keyValue);
- if (matchValue != null)
- offValue = matchValue;
- }
- values.overriddenValue = offValue;
+ String dependentKey = dependsOnSet.iterator().next();
+ String value = getValue(dependentKey);
+ JSONObject dependencyList = getDependencyList(dependentKey, value);
+
+ String newValue = null;
+ try {
+ newValue = dependencyList.getString(keyToProcess);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ continue;
}
+ Values values = new Values(getValue(keyToProcess), newValue);
mValuesMap.put(keyToProcess, values);
}
}
@@ -289,114 +277,77 @@ public class SettingsManager implements ListMenu.SettingsListener {
ListPreference changedPref = mPreferenceGroup.findPreference(changedPrefKey);
if (changedPref == null) return null;
- String key = changedPref.getKey();
String value = changedPref.getValue();
- boolean special = specialDepedency(changedPrefKey);
String prevValue = getValue(changedPrefKey);
if (value.equals(prevValue)) return null;
- boolean turnedOff = value.equals(changedPref.getOffValue());
- boolean updateBackDependency = false;
List<SettingState> changed = new ArrayList();
Values values = new Values(value, null);
- mValuesMap.put(key, values);
- changed.add(new SettingState(key, values));
-
- Set<CharSequence> turnOn = new HashSet<>();
- Set<CharSequence> turnOff = new HashSet<>();
-
- CharSequence[] originalDependencyList = changedPref.getDependencyList();
- CharSequence[] dependencyList = null;
- List<KeyValue> keyValue = null;
- if (special) {
- keyValue = getSpecialDependencyList(changedPref);
- if (keyValue.size() > 0) {
- dependencyList = new CharSequence[keyValue.size()];
- int k = 0;
- for (KeyValue kv : keyValue) {
- dependencyList[k++] = kv.key;
- }
- }
- }
+ mValuesMap.put(changedPrefKey, values);
+ changed.add(new SettingState(changedPrefKey, values));
- if (special) {
- boolean same = Arrays.equals(originalDependencyList, dependencyList);
- if (!same) {
- changedPref.setDependencyList(dependencyList);
- if (originalDependencyList != null)
- for (CharSequence c : originalDependencyList) {
- turnOn.add(c);
- }
- if (dependencyList != null)
- for (CharSequence c : dependencyList) {
- turnOff.add(c);
- }
+ JSONObject map = getDependencyMapForKey(changedPrefKey);
+ if (map == null || getDependencyKey(map, value).equals(getDependencyKey(map,prevValue)))
+ return changed;
- if (originalDependencyList != null)
- for (CharSequence c : originalDependencyList) {
- turnOff.remove(c);
- }
- if (dependencyList != null)
- for (CharSequence c : dependencyList) {
- turnOn.remove(c);
- }
- updateBackDependency = true;
- }
- } else {
- if (originalDependencyList != null) {
- for (CharSequence c : originalDependencyList) {
- if (turnedOff) turnOn.add(c);
- else turnOff.add(c);
- }
- }
+ Set<String> turnOn = new HashSet<>();
+ Set<String> turnOff = new HashSet<>();
+
+ JSONObject dependencyList = getDependencyList(changedPrefKey, value);
+ JSONObject originalDependencyList = getDependencyList(changedPrefKey, prevValue);
+
+ Iterator<String> it = originalDependencyList.keys();
+ while (it.hasNext()) {
+ turnOn.add(it.next());
+ }
+ it = dependencyList.keys();
+ while (it.hasNext()) {
+ turnOff.add(it.next());
+ }
+ it = originalDependencyList.keys();
+ while (it.hasNext()) {
+ turnOff.remove(it.next());
+ }
+ it = dependencyList.keys();
+ while (it.hasNext()) {
+ turnOn.remove(it.next());
}
- for (CharSequence c : turnOn) {// turn back on
- key = c.toString();
- Set<String> dependsOnSet = mDependendsOnMap.get(key);
- if (dependsOnSet == null) continue;
- boolean active = true;
- for (String s : dependsOnSet) {
- if (s.equals(changedPrefKey)) continue;
- if (isOptionOn(s)) active = false;
- break;
- }
- if (active) {
- values = mValuesMap.get(key);
+ for (String keyToTurnOn: turnOn) {
+ Set<String> dependsOnSet = mDependendsOnMap.get(keyToTurnOn);
+ if (dependsOnSet == null || dependsOnSet.size() == 0) continue;
+
+ values = mValuesMap.get(keyToTurnOn);
if (values == null) continue;
values.overriddenValue = null;
- mValuesMap.put(key, values);
- changed.add(new SettingState(key, values));
- }
+ mValuesMap.put(keyToTurnOn, values);
+ changed.add(new SettingState(keyToTurnOn, values));
}
- for (CharSequence c : turnOff) {// turn off logic
- key = c.toString();
- ListPreference pref = mPreferenceGroup.findPreference(key);
+ for (String keyToTurnOff: turnOff) {
+ ListPreference pref = mPreferenceGroup.findPreference(keyToTurnOff);
if (pref == null) continue;
- values = mValuesMap.get(key);
+ values = mValuesMap.get(keyToTurnOff);
if (values == null) continue;
if (values != null && values.overriddenValue != null) continue;
- String offValue = pref.getOffValue();
- if (keyValue != null) {
- String matchValue = getMatchingValue(key, keyValue);
- if (matchValue != null)
- offValue = matchValue;
+ String newValue = null;
+ try {
+ newValue = dependencyList.getString(keyToTurnOff);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ continue;
}
- Values newValue = new Values(pref.getValue(), offValue);
- mValuesMap.put(key, newValue);
- changed.add(new SettingState(key, newValue));
- }
+ if (newValue == null) continue;
- if (updateBackDependency) {
- updateBackDependency(changedPrefKey, turnOn, turnOff);
+ Values newValues = new Values(pref.getValue(), newValue);
+ mValuesMap.put(keyToTurnOff, newValues);
+ changed.add(new SettingState(keyToTurnOff, newValues));
}
-
+ updateBackDependency(changedPrefKey, turnOn, turnOff);
return changed;
}
- private void updateBackDependency(String key, Set<CharSequence> remove, Set<CharSequence>
- add) {
+ private void updateBackDependency(String key, Set<String> remove, Set<String> add) {
for (CharSequence c : remove) {
String currentKey = c.toString();
Set<String> dependsOnSet = mDependendsOnMap.get(currentKey);
@@ -465,12 +416,6 @@ public class SettingsManager implements ListMenu.SettingsListener {
notifyListeners(changed);
}
- private boolean isOptionOn(String key) {
- ListPreference pref = mPreferenceGroup.findPreference(key);
- Values values = mValuesMap.get(key);
- return (values.overriddenValue == null && !pref.getValue().equals(pref.getOffValue()));
- }
-
public PreferenceGroup getPreferenceGroup() {
return mPreferenceGroup;
}
@@ -888,66 +833,10 @@ public class SettingsManager implements ListMenu.SettingsListener {
return modes;
}
- private boolean specialDepedency(String key) {
- return key.equals(KEY_SCENE_MODE);
- }
-
- private List<KeyValue> getSpecialDependencyList(String key) {
- ListPreference pref = mPreferenceGroup.findPreference(key);
- return getSpecialDependencyList(pref);
- }
-
- private List<KeyValue> getSpecialDependencyList(ListPreference pref) {
- String key = pref.getKey();
- List<KeyValue> dependency = new ArrayList<>();
- switch (key) {
- case KEY_SCENE_MODE:
- String value = pref.getValue();
- switch (value) {
- case "0":
- dependency.add(new KeyValue(KEY_CLEARSIGHT, "off"));
- dependency.add(new KeyValue(KEY_MONO_PREVIEW, "off"));
- break;
- case SCENE_MODE_DUAL_STRING:
- dependency.add(new KeyValue(KEY_LONGSHOT, "off"));
- dependency.add(new KeyValue(KEY_MONO_ONLY, "off"));
- break;
- default:
- dependency.add(new KeyValue(KEY_COLOR_EFFECT, "0"));
- dependency.add(new KeyValue(KEY_FLASH_MODE, "2"));
- dependency.add(new KeyValue(KEY_WHITE_BALANCE, "1"));
- dependency.add(new KeyValue(KEY_EXPOSURE, "0"));
- dependency.add(new KeyValue(KEY_CLEARSIGHT, "off"));
- dependency.add(new KeyValue(KEY_MONO_PREVIEW, "off"));
- break;
- }
- break;
- }
- return dependency;
- }
-
public interface Listener {
void onSettingsChanged(List<SettingState> settings);
}
- private String getMatchingValue(String key, List<KeyValue> keyValue) {
- for (KeyValue kv: keyValue) {
- if (key.equals(kv.key)) {
- return kv.value;
- }
- }
- return null;
- }
-
- static class KeyValue {
- String key;
- String value;
-
- KeyValue(String key, String value) {
- this.key = key;
- this.value = value;
- }
- }
static class Values {
String value;
String overriddenValue;
@@ -968,4 +857,50 @@ public class SettingsManager implements ListMenu.SettingsListener {
}
}
+ private JSONObject parseJson(String fileName) {
+ String json;
+ try {
+ InputStream is = mContext.getAssets().open(fileName);
+ int size = is.available();
+ byte[] buffer = new byte[size];
+ is.read(buffer);
+ is.close();
+ json = new String(buffer, "UTF-8");
+ return new JSONObject(json);
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ return null;
+ } catch (JSONException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ private JSONObject getDependencyMapForKey(String key) {
+ if (mDependency == null) return null;
+ try {
+ return mDependency.getJSONObject(key);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ private JSONObject getDependencyList(String key, String value) {
+ JSONObject dependencyMap = getDependencyMapForKey(key);
+ if (dependencyMap == null) return null;
+ if (!dependencyMap.has(value)) value = "default";
+ value = getDependencyKey(dependencyMap, value);
+ try {
+ return dependencyMap.getJSONObject(value);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ private String getDependencyKey(JSONObject dependencyMap, String value) {
+ if (!dependencyMap.has(value)) value = "default";
+ return value;
+ }
}