1 package com.introlab.rtabmap;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Iterator;
8 import java.util.Map.Entry;
10 import android.app.AlertDialog;
11 import android.content.DialogInterface;
12 import android.content.SharedPreferences;
13 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
14 import android.os.Bundle;
15 import android.preference.ListPreference;
16 import android.preference.Preference;
17 import android.preference.PreferenceActivity;
18 import android.text.InputType;
19 import android.view.WindowManager;
20 import android.view.inputmethod.EditorInfo;
21 import android.widget.EditText;
23 public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 addPreferencesFromResource(R.layout.activity_settings);
32 Preference buttonReset = findPreference(getString(R.string.pref_key_reset_button));
33 buttonReset.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
35 public boolean onPreferenceClick(Preference preference) {
36 getPreferenceScreen().getSharedPreferences().edit().clear().commit();
44 Preference buttonOpen = findPreference(getString(R.string.pref_key_open_button));
45 buttonOpen.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
47 public boolean onPreferenceClick(Preference preference) {
48 File prefsdir =
new File(getApplicationInfo().dataDir,
"shared_prefs");
49 if(prefsdir.exists() && prefsdir.isDirectory()){
50 ArrayList<String> filesArray =
new ArrayList<String>(Arrays.asList(prefsdir.list()));
51 ArrayList<String> newList =
new ArrayList<String>();
52 filesArray.remove(
"com.introlab.rtabmap_preferences.xml");
53 filesArray.remove(
"WebViewChromiumPrefs.xml");
54 for (String s : filesArray) {
55 newList.add(s.substring(0, s.length()-4));
57 final String[] files = newList.toArray(
new String[filesArray.size()]);
60 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
61 builder.setTitle(
"Choose Presets:");
62 builder.setItems(files,
new DialogInterface.OnClickListener() {
63 public void onClick(DialogInterface dialog,
final int which) {
65 SharedPreferences.Editor ed = getPreferenceScreen().getSharedPreferences().edit();
66 SharedPreferences sp =
getActivity().getSharedPreferences(files[which], MODE_PRIVATE);
68 for(Entry<String,?> entry : sp.getAll().entrySet()){
69 Object v = entry.getValue();
70 String key = entry.getKey();
77 ed.putBoolean(key, ((Boolean)v).booleanValue());
78 else if(v instanceof Float)
79 ed.putFloat(key, ((Float)v).floatValue());
80 else if(v instanceof Integer)
81 ed.putInt(key, ((Integer)v).intValue());
82 else if(v instanceof Long)
83 ed.putLong(key, ((Long)v).longValue());
84 else if(v instanceof String)
85 ed.putString(key, ((String)v));
100 Preference buttonSave = findPreference(getString(R.string.pref_key_save_button));
101 buttonSave.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
103 public boolean onPreferenceClick(Preference preference) {
104 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
105 builder.setTitle(
"Save Presets:");
106 final EditText input =
new EditText(
getActivity());
107 input.setInputType(InputType.TYPE_CLASS_TEXT);
108 input.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
109 builder.setView(input);
110 builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
112 public void onClick(DialogInterface dialog,
int which)
114 final String fileName = input.getText().toString();
116 if(!fileName.isEmpty())
118 File newFile =
new File(getApplicationInfo().dataDir +
"/shared_prefs/" + fileName +
".xml");
122 .setTitle(
"Presets Already Exist")
123 .setMessage(
"Do you want to overwrite the existing file?")
124 .setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
125 public void onClick(DialogInterface dialog,
int which) {
129 .setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
130 public void onClick(DialogInterface dialog,
int which) {
143 AlertDialog alertToShow = builder.create();
144 alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
151 Preference buttonRemove = findPreference(getString(R.string.pref_key_remove_button));
152 buttonRemove.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
154 public boolean onPreferenceClick(Preference preference) {
155 File prefsdir =
new File(getApplicationInfo().dataDir,
"shared_prefs");
156 if(prefsdir.exists() && prefsdir.isDirectory()){
157 ArrayList<String> filesArray =
new ArrayList<String>(Arrays.asList(prefsdir.list()));
158 ArrayList<String> newList =
new ArrayList<String>();
159 filesArray.remove(
"com.introlab.rtabmap_preferences.xml");
160 filesArray.remove(
"WebViewChromiumPrefs.xml");
161 for (String s : filesArray) {
162 newList.add(s.substring(0, s.length()-4));
164 final String[] files = newList.toArray(
new String[filesArray.size()]);
167 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
168 builder.setTitle(
"Remove Presets:");
169 builder.setItems(files,
new DialogInterface.OnClickListener() {
170 public void onClick(DialogInterface dialog,
final int which) {
171 File file =
new File(getApplicationInfo().dataDir +
"/shared_prefs/" + files[which] +
".xml");
188 ((Preference)findPreference(getString(R.string.pref_key_density))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_density))).getEntry() +
") "+getString(R.string.pref_summary_density));
189 ((Preference)findPreference(getString(R.string.pref_key_depth))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_depth))).getEntry() +
") "+getString(R.string.pref_summary_depth));
190 ((Preference)findPreference(getString(R.string.pref_key_min_depth))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).getEntry() +
") "+getString(R.string.pref_summary_min_depth));
191 ((Preference)findPreference(getString(R.string.pref_key_point_size))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_point_size))).getEntry() +
") "+getString(R.string.pref_summary_point_size));
192 ((Preference)findPreference(getString(R.string.pref_key_angle))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_angle))).getEntry() +
") "+getString(R.string.pref_summary_angle));
193 ((Preference)findPreference(getString(R.string.pref_key_triangle))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_triangle))).getEntry() +
") "+getString(R.string.pref_summary_triangle));
194 ((Preference)findPreference(getString(R.string.pref_key_background_color))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_background_color))).getEntry() +
") "+getString(R.string.pref_summary_background_color));
195 ((Preference)findPreference(getString(R.string.pref_key_rendering_texture_decimation))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_rendering_texture_decimation))).getEntry() +
") "+getString(R.string.pref_summary_rendering_texture_decimation));
197 ((Preference)findPreference(getString(R.string.pref_key_update_rate))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_update_rate))).getEntry() +
") "+getString(R.string.pref_summary_update_rate));
198 ((Preference)findPreference(getString(R.string.pref_key_max_speed))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_max_speed))).getEntry() +
") "+getString(R.string.pref_summary_max_speed));
199 ((Preference)findPreference(getString(R.string.pref_key_time_thr))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_time_thr))).getEntry() +
") "+getString(R.string.pref_summary_time_thr));
200 ((Preference)findPreference(getString(R.string.pref_key_mem_thr))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_mem_thr))).getEntry() +
") "+getString(R.string.pref_summary_mem_thr));
201 ((Preference)findPreference(getString(R.string.pref_key_loop_thr))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_loop_thr))).getEntry() +
") "+getString(R.string.pref_summary_loop_thr));
202 ((Preference)findPreference(getString(R.string.pref_key_sim_thr))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_sim_thr))).getEntry() +
") "+getString(R.string.pref_summary_sim_thr));
203 ((Preference)findPreference(getString(R.string.pref_key_min_inliers))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_min_inliers))).getEntry() +
") "+getString(R.string.pref_summary_min_inliers));
204 ((Preference)findPreference(getString(R.string.pref_key_opt_error))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_opt_error))).getEntry() +
") "+getString(R.string.pref_summary_opt_error));
205 ((Preference)findPreference(getString(R.string.pref_key_features_voc))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_features_voc))).getEntry() +
") "+getString(R.string.pref_summary_features_voc));
206 ((Preference)findPreference(getString(R.string.pref_key_features))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_features))).getEntry() +
") "+getString(R.string.pref_summary_features));
207 ((Preference)findPreference(getString(R.string.pref_key_features_type))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_features_type))).getEntry() +
") "+getString(R.string.pref_summary_features_type));
208 ((Preference)findPreference(getString(R.string.pref_key_optimizer))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_optimizer))).getEntry() +
") "+getString(R.string.pref_summary_optimizer));
210 ((Preference)findPreference(getString(R.string.pref_key_cloud_voxel))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_cloud_voxel))).getEntry() +
") "+getString(R.string.pref_summary_cloud_voxel));
211 ((Preference)findPreference(getString(R.string.pref_key_texture_size))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_texture_size))).getEntry() +
") "+getString(R.string.pref_summary_texture_size));
212 ((Preference)findPreference(getString(R.string.pref_key_texture_count))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_texture_count))).getEntry() +
") "+getString(R.string.pref_summary_texture_count));
213 ((Preference)findPreference(getString(R.string.pref_key_normal_k))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_normal_k))).getEntry() +
") "+getString(R.string.pref_summary_normal_k));
214 ((Preference)findPreference(getString(R.string.pref_key_max_texture_distance))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_max_texture_distance))).getEntry() +
") "+getString(R.string.pref_summary_max_texture_distance));
215 ((Preference)findPreference(getString(R.string.pref_key_min_texture_cluster_size))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_min_texture_cluster_size))).getEntry() +
") "+getString(R.string.pref_summary_min_texture_cluster_size));
217 ((Preference)findPreference(getString(R.string.pref_key_opt_depth))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_opt_depth))).getEntry() +
") "+getString(R.string.pref_summary_opt_depth));
218 ((Preference)findPreference(getString(R.string.pref_key_opt_color_radius))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_opt_color_radius))).getEntry() +
") "+getString(R.string.pref_summary_opt_color_radius));
219 ((Preference)findPreference(getString(R.string.pref_key_opt_min_cluster_size))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_opt_min_cluster_size))).getEntry() +
") "+getString(R.string.pref_summary_opt_min_cluster_size));
221 ((Preference)findPreference(getString(R.string.pref_key_cluster_ratio))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_cluster_ratio))).getEntry() +
") "+getString(R.string.pref_summary_cluster_ratio));
222 ((Preference)findPreference(getString(R.string.pref_key_gain_max_radius))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_gain_max_radius))).getEntry() +
") "+getString(R.string.pref_summary_gain_max_radius));
226 Preference pref = findPreference(key);
228 if (pref instanceof ListPreference) {
229 if(key.compareTo(getString(R.string.pref_key_density))==0) pref.setSummary(
"("+ ((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_density));
230 if(key.compareTo(getString(R.string.pref_key_depth))==0)
232 pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_depth));
233 float maxDepth = Float.parseFloat(((ListPreference)pref).getValue());
234 float minDepth = Float.parseFloat(((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).getValue());
235 if(maxDepth > 0.0
f && maxDepth <= minDepth)
237 ((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).setValueIndex(0);
240 if(key.compareTo(getString(R.string.pref_key_min_depth))==0)
242 pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_min_depth));
243 float maxDepth = Float.parseFloat(((ListPreference)findPreference(getString(R.string.pref_key_depth))).getValue());
244 float minDepth = Float.parseFloat(((ListPreference)pref).getValue());
245 if(minDepth >= maxDepth)
247 ((ListPreference)findPreference(getString(R.string.pref_key_depth))).setValueIndex(0);
250 if(key.compareTo(getString(R.string.pref_key_point_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_point_size));
251 if(key.compareTo(getString(R.string.pref_key_angle))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_angle));
252 if(key.compareTo(getString(R.string.pref_key_triangle))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_triangle));
253 if(key.compareTo(getString(R.string.pref_key_background_color))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_background_color));
254 if(key.compareTo(getString(R.string.pref_key_rendering_texture_decimation))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_rendering_texture_decimation));
256 if(key.compareTo(getString(R.string.pref_key_update_rate))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_update_rate));
257 if(key.compareTo(getString(R.string.pref_key_max_speed))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_max_speed));
258 if(key.compareTo(getString(R.string.pref_key_time_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_time_thr));
259 if(key.compareTo(getString(R.string.pref_key_mem_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_mem_thr));
260 if(key.compareTo(getString(R.string.pref_key_loop_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_loop_thr));
261 if(key.compareTo(getString(R.string.pref_key_sim_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_sim_thr));
262 if(key.compareTo(getString(R.string.pref_key_min_inliers))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_min_inliers));
263 if(key.compareTo(getString(R.string.pref_key_opt_error))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_error));
264 if(key.compareTo(getString(R.string.pref_key_features_voc))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features_voc));
265 if(key.compareTo(getString(R.string.pref_key_features))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features));
266 if(key.compareTo(getString(R.string.pref_key_features_type))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features_type));
267 if(key.compareTo(getString(R.string.pref_key_optimizer))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_optimizer));
269 if(key.compareTo(getString(R.string.pref_key_cloud_voxel))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_cloud_voxel));
270 if(key.compareTo(getString(R.string.pref_key_texture_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_texture_size));
271 if(key.compareTo(getString(R.string.pref_key_texture_count))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_texture_count));
272 if(key.compareTo(getString(R.string.pref_key_normal_k))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_normal_k));
273 if(key.compareTo(getString(R.string.pref_key_max_texture_distance))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_max_texture_distance));
274 if(key.compareTo(getString(R.string.pref_key_min_texture_cluster_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_min_texture_cluster_size));
276 if(key.compareTo(getString(R.string.pref_key_opt_depth))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_depth));
277 if(key.compareTo(getString(R.string.pref_key_opt_color_radius))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_color_radius));
278 if(key.compareTo(getString(R.string.pref_key_opt_min_cluster_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_min_cluster_size));
280 if(key.compareTo(getString(R.string.pref_key_cluster_ratio))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_cluster_ratio));
281 if(key.compareTo(getString(R.string.pref_key_gain_max_radius))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_gain_max_radius));
290 getPreferenceScreen().getSharedPreferences()
291 .registerOnSharedPreferenceChangeListener(
this);
298 getPreferenceScreen().getSharedPreferences()
299 .unregisterOnSharedPreferenceChangeListener(
this);
305 SharedPreferences.Editor ed =
getActivity().getSharedPreferences(fileName, MODE_PRIVATE).edit();
306 SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
309 for(Entry<String,?> entry : sp.getAll().entrySet()){
310 Object v = entry.getValue();
311 String key = entry.getKey();
318 ed.putBoolean(key, ((Boolean)v).booleanValue());
319 else if(v instanceof Float)
320 ed.putFloat(key, ((Float)v).floatValue());
321 else if(v instanceof Integer)
322 ed.putInt(key, ((Integer)v).intValue());
323 else if(v instanceof Long)
324 ed.putLong(key, ((Long)v).longValue());
325 else if(v instanceof String)
326 ed.putString(key, ((String)v));
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
SettingsActivity getActivity()
void onCreate(Bundle savedInstanceState)
void saveConfig(String fileName)