SettingsActivity.java
Go to the documentation of this file.
1 package com.introlab.rtabmap;
2 
3 import java.io.File;
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;
9 
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;
22 
23 public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
24 
25  private SettingsActivity getActivity() {return this;}
26 
27  @Override
28  public void onCreate(Bundle savedInstanceState) {
29  super.onCreate(savedInstanceState);
30  addPreferencesFromResource(R.layout.activity_settings);
31 
32  Preference buttonReset = findPreference(getString(R.string.pref_key_reset_button));
33  buttonReset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
34  @Override
35  public boolean onPreferenceClick(Preference preference) {
36  getPreferenceScreen().getSharedPreferences().edit().clear().commit();
37 
38  recreate();
39 
40  return true;
41  }
42  });
43 
44  Preference buttonOpen = findPreference(getString(R.string.pref_key_open_button));
45  buttonOpen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
46  @Override
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)); // rip off the ".xml"
56  }
57  final String[] files = newList.toArray(new String[filesArray.size()]);
58  if(files.length > 0)
59  {
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) {
64  //sp1 is the shared pref to copy to
65  SharedPreferences.Editor ed = getPreferenceScreen().getSharedPreferences().edit();
66  SharedPreferences sp = getActivity().getSharedPreferences(files[which], MODE_PRIVATE); //The shared preferences to copy from
67  //Cycle through all the entries in the sp
68  for(Entry<String,?> entry : sp.getAll().entrySet()){
69  Object v = entry.getValue();
70  String key = entry.getKey();
71  //Now we just figure out what type it is, so we can copy it.
72  // Note that i am using Boolean and Integer instead of boolean and int.
73  // That's because the Entry class can only hold objects and int and boolean are primatives.
74  if(v instanceof Boolean)
75  // Also note that i have to cast the object to a Boolean
76  // and then use .booleanValue to get the boolean
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));
86  }
87  ed.commit(); //save it.
88  recreate();
89  return;
90  }
91  });
92  builder.show();
93  }
94  }
95 
96  return true;
97  }
98  });
99 
100  Preference buttonSave = findPreference(getString(R.string.pref_key_save_button));
101  buttonSave.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
102  @Override
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() {
111  @Override
112  public void onClick(DialogInterface dialog, int which)
113  {
114  final String fileName = input.getText().toString();
115  dialog.dismiss();
116  if(!fileName.isEmpty())
117  {
118  File newFile = new File(getApplicationInfo().dataDir + "/shared_prefs/" + fileName + ".xml");
119  if(newFile.exists())
120  {
121  new AlertDialog.Builder(getActivity())
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) {
126  saveConfig(fileName);
127  }
128  })
129  .setNegativeButton("No", new DialogInterface.OnClickListener() {
130  public void onClick(DialogInterface dialog, int which) {
131  dialog.dismiss();
132  }
133  })
134  .show();
135  }
136  else
137  {
138  saveConfig(fileName);
139  }
140  }
141  }
142  });
143  AlertDialog alertToShow = builder.create();
144  alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
145  alertToShow.show();
146 
147  return true;
148  }
149  });
150 
151  Preference buttonRemove = findPreference(getString(R.string.pref_key_remove_button));
152  buttonRemove.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
153  @Override
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)); // rip off the ".xml"
163  }
164  final String[] files = newList.toArray(new String[filesArray.size()]);
165  if(files.length > 0)
166  {
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");
172  if(file.exists())
173  {
174  file.delete();
175  }
176  return;
177  }
178  });
179  builder.show();
180  }
181  }
182 
183  return true;
184  }
185  });
186 
187 
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));
196 
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));
209 
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));
216 
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));
220 
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));
223  }
224 
225  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
226  Preference pref = findPreference(key);
227 
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)
231  {
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.0f && maxDepth <= minDepth)
236  {
237  ((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).setValueIndex(0);
238  }
239  }
240  if(key.compareTo(getString(R.string.pref_key_min_depth))==0)
241  {
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)
246  {
247  ((ListPreference)findPreference(getString(R.string.pref_key_depth))).setValueIndex(0);
248  }
249  }
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));
255 
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));
268 
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));
275 
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));
279 
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));
282 
283  }
284  }
285 
286  @Override
287  protected void onResume() {
288  super.onResume();
289  // Set up a listener whenever a key changes
290  getPreferenceScreen().getSharedPreferences()
291  .registerOnSharedPreferenceChangeListener(this);
292  }
293 
294  @Override
295  protected void onPause() {
296  super.onPause();
297  // Unregister the listener whenever a key changes
298  getPreferenceScreen().getSharedPreferences()
299  .unregisterOnSharedPreferenceChangeListener(this);
300  }
301 
302  private void saveConfig(String fileName)
303  {
304  //sp1 is the shared pref to copy to
305  SharedPreferences.Editor ed = getActivity().getSharedPreferences(fileName, MODE_PRIVATE).edit();
306  SharedPreferences sp = getPreferenceScreen().getSharedPreferences(); //The shared preferences to copy from
307  ed.clear(); // This clears the one we are copying to, but you don't necessarily need to do that.
308  //Cycle through all the entries in the sp
309  for(Entry<String,?> entry : sp.getAll().entrySet()){
310  Object v = entry.getValue();
311  String key = entry.getKey();
312  //Now we just figure out what type it is, so we can copy it.
313  // Note that i am using Boolean and Integer instead of boolean and int.
314  // That's because the Entry class can only hold objects and int and boolean are primatives.
315  if(v instanceof Boolean)
316  // Also note that i have to cast the object to a Boolean
317  // and then use .booleanValue to get the boolean
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));
327  }
328  ed.commit(); //save it.
329  }
330 }
f
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
unsigned char Boolean
Definition: ConvertUTF.h:93
void onCreate(Bundle savedInstanceState)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32