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.Manifest;
11 import android.app.AlertDialog;
12 import android.content.DialogInterface;
13 import android.content.SharedPreferences;
14 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
15 import android.content.pm.PackageManager;
16 import android.os.Bundle;
17 import android.preference.CheckBoxPreference;
18 import android.preference.ListPreference;
19 import android.preference.Preference;
20 import android.preference.PreferenceActivity;
21 import android.text.InputType;
22 import android.view.WindowManager;
23 import android.view.inputmethod.EditorInfo;
24 import android.widget.EditText;
25 import android.widget.Toast;
27 public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 addPreferencesFromResource(R.layout.activity_settings);
36 Preference buttonReset = findPreference(getString(R.string.pref_key_reset_button));
37 buttonReset.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
39 public boolean onPreferenceClick(Preference preference) {
40 getPreferenceScreen().getSharedPreferences().edit().clear().commit();
48 Preference buttonOpen = findPreference(getString(R.string.pref_key_open_button));
49 buttonOpen.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
51 public boolean onPreferenceClick(Preference preference) {
52 File prefsdir =
new File(getApplicationInfo().dataDir,
"shared_prefs");
53 if(prefsdir.exists() && prefsdir.isDirectory()){
54 ArrayList<String> filesArray =
new ArrayList<String>(Arrays.asList(prefsdir.list()));
55 ArrayList<String> newList =
new ArrayList<String>();
56 filesArray.remove(
"com.introlab.rtabmap_preferences.xml");
57 filesArray.remove(
"WebViewChromiumPrefs.xml");
58 for (String s : filesArray) {
59 newList.add(s.substring(0, s.length()-4));
61 final String[] files = newList.toArray(
new String[filesArray.size()]);
64 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
65 builder.setTitle(
"Choose Presets:");
66 builder.setItems(files,
new DialogInterface.OnClickListener() {
67 public void onClick(DialogInterface dialog,
final int which) {
69 SharedPreferences.Editor ed = getPreferenceScreen().getSharedPreferences().edit();
70 SharedPreferences sp =
getActivity().getSharedPreferences(files[which], MODE_PRIVATE);
72 for(Entry<String,?> entry : sp.getAll().entrySet()){
73 Object v = entry.getValue();
74 String key = entry.getKey();
81 ed.putBoolean(key, ((Boolean)v).booleanValue());
82 else if(v instanceof Float)
83 ed.putFloat(key, ((Float)v).floatValue());
84 else if(v instanceof Integer)
85 ed.putInt(key, ((Integer)v).intValue());
86 else if(v instanceof Long)
87 ed.putLong(key, ((Long)v).longValue());
88 else if(v instanceof String)
89 ed.putString(key, ((String)v));
104 Preference buttonSave = findPreference(getString(R.string.pref_key_save_button));
105 buttonSave.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
107 public boolean onPreferenceClick(Preference preference) {
108 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
109 builder.setTitle(
"Save Presets:");
110 final EditText input =
new EditText(
getActivity());
111 input.setInputType(InputType.TYPE_CLASS_TEXT);
112 input.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
113 builder.setView(input);
114 builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
116 public void onClick(DialogInterface dialog,
int which)
118 final String fileName = input.getText().toString();
120 if(!fileName.isEmpty())
122 File newFile =
new File(getApplicationInfo().dataDir +
"/shared_prefs/" + fileName +
".xml");
126 .setTitle(
"Presets Already Exist")
127 .setMessage(
"Do you want to overwrite the existing file?")
128 .setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
129 public void onClick(DialogInterface dialog,
int which) {
133 .setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
134 public void onClick(DialogInterface dialog,
int which) {
147 AlertDialog alertToShow = builder.create();
148 alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
155 Preference buttonRemove = findPreference(getString(R.string.pref_key_remove_button));
156 buttonRemove.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
158 public boolean onPreferenceClick(Preference preference) {
159 File prefsdir =
new File(getApplicationInfo().dataDir,
"shared_prefs");
160 if(prefsdir.exists() && prefsdir.isDirectory()){
161 ArrayList<String> filesArray =
new ArrayList<String>(Arrays.asList(prefsdir.list()));
162 ArrayList<String> newList =
new ArrayList<String>();
163 filesArray.remove(
"com.introlab.rtabmap_preferences.xml");
164 filesArray.remove(
"WebViewChromiumPrefs.xml");
165 for (String s : filesArray) {
166 newList.add(s.substring(0, s.length()-4));
168 final String[] files = newList.toArray(
new String[filesArray.size()]);
171 AlertDialog.Builder builder =
new AlertDialog.Builder(
getActivity());
172 builder.setTitle(
"Remove Presets:");
173 builder.setItems(files,
new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog,
final int which) {
175 File file =
new File(getApplicationInfo().dataDir +
"/shared_prefs/" + files[which] +
".xml");
191 Preference buttonGPS = findPreference(getString(R.string.pref_key_gps_saved));
192 buttonGPS.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
194 public boolean onPreferenceClick(Preference preference) {
215 ((Preference)findPreference(getString(R.string.pref_key_camera_driver))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_camera_driver))).getEntry() +
") "+getString(R.string.pref_summary_camera_driver));
216 ((Preference)findPreference(getString(R.string.pref_key_density))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_density))).getEntry() +
") "+getString(R.string.pref_summary_density));
217 ((Preference)findPreference(getString(R.string.pref_key_depth))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_depth))).getEntry() +
") "+getString(R.string.pref_summary_depth));
218 ((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));
219 ((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));
220 ((Preference)findPreference(getString(R.string.pref_key_angle))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_angle))).getEntry() +
") "+getString(R.string.pref_summary_angle));
221 ((Preference)findPreference(getString(R.string.pref_key_triangle))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_triangle))).getEntry() +
") "+getString(R.string.pref_summary_triangle));
222 ((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));
223 ((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));
225 ((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));
226 ((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));
227 ((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));
228 ((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));
229 ((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));
230 ((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));
231 ((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));
232 ((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));
233 ((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));
234 ((Preference)findPreference(getString(R.string.pref_key_features))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_features))).getEntry() +
") "+getString(R.string.pref_summary_features));
235 ((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));
236 ((Preference)findPreference(getString(R.string.pref_key_optimizer))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_optimizer))).getEntry() +
") "+getString(R.string.pref_summary_optimizer));
237 ((Preference)findPreference(getString(R.string.pref_key_marker_detection))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_marker_detection))).getEntry() +
") "+getString(R.string.pref_summary_marker_detection));
238 ((Preference)findPreference(getString(R.string.pref_key_marker_detection_depth_error))).setSummary(
"("+((ListPreference)findPreference(getString(R.string.pref_key_marker_detection_depth_error))).getEntry() +
") "+getString(R.string.pref_summary_marker_detection_depth_error));
240 ((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));
241 ((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));
242 ((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));
243 ((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));
244 ((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));
245 ((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));
247 ((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));
248 ((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));
249 ((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));
251 ((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));
252 ((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));
256 Preference pref = findPreference(key);
258 if (pref instanceof ListPreference) {
259 if(key.compareTo(getString(R.string.pref_key_camera_driver))==0) pref.setSummary(
"("+ ((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_camera_driver));
260 if(key.compareTo(getString(R.string.pref_key_density))==0) pref.setSummary(
"("+ ((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_density));
261 if(key.compareTo(getString(R.string.pref_key_depth))==0)
263 pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_depth));
264 float maxDepth = Float.parseFloat(((ListPreference)pref).getValue());
265 float minDepth = Float.parseFloat(((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).getValue());
266 if(maxDepth > 0.0
f && maxDepth <= minDepth)
268 ((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).setValueIndex(0);
271 if(key.compareTo(getString(R.string.pref_key_min_depth))==0)
273 pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_min_depth));
274 float maxDepth = Float.parseFloat(((ListPreference)findPreference(getString(R.string.pref_key_depth))).getValue());
275 float minDepth = Float.parseFloat(((ListPreference)pref).getValue());
276 if(minDepth >= maxDepth)
278 ((ListPreference)findPreference(getString(R.string.pref_key_depth))).setValueIndex(0);
281 if(key.compareTo(getString(R.string.pref_key_point_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_point_size));
282 if(key.compareTo(getString(R.string.pref_key_angle))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_angle));
283 if(key.compareTo(getString(R.string.pref_key_triangle))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_triangle));
284 if(key.compareTo(getString(R.string.pref_key_background_color))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_background_color));
285 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));
287 if(key.compareTo(getString(R.string.pref_key_update_rate))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_update_rate));
288 if(key.compareTo(getString(R.string.pref_key_max_speed))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_max_speed));
289 if(key.compareTo(getString(R.string.pref_key_time_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_time_thr));
290 if(key.compareTo(getString(R.string.pref_key_mem_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_mem_thr));
291 if(key.compareTo(getString(R.string.pref_key_loop_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_loop_thr));
292 if(key.compareTo(getString(R.string.pref_key_sim_thr))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_sim_thr));
293 if(key.compareTo(getString(R.string.pref_key_min_inliers))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_min_inliers));
294 if(key.compareTo(getString(R.string.pref_key_opt_error))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_error));
295 if(key.compareTo(getString(R.string.pref_key_features_voc))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features_voc));
296 if(key.compareTo(getString(R.string.pref_key_features))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features));
297 if(key.compareTo(getString(R.string.pref_key_features_type))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_features_type));
298 if(key.compareTo(getString(R.string.pref_key_optimizer))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_optimizer));
299 if(key.compareTo(getString(R.string.pref_key_marker_detection))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_marker_detection));
300 if(key.compareTo(getString(R.string.pref_key_marker_detection_depth_error))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_marker_detection_depth_error));
302 if(key.compareTo(getString(R.string.pref_key_cloud_voxel))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_cloud_voxel));
303 if(key.compareTo(getString(R.string.pref_key_texture_size))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_texture_size));
304 if(key.compareTo(getString(R.string.pref_key_texture_count))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_texture_count));
305 if(key.compareTo(getString(R.string.pref_key_normal_k))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_normal_k));
306 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));
307 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));
309 if(key.compareTo(getString(R.string.pref_key_opt_depth))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_opt_depth));
310 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));
311 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));
313 if(key.compareTo(getString(R.string.pref_key_cluster_ratio))==0) pref.setSummary(
"("+((ListPreference)pref).getEntry() +
") "+getString(R.string.pref_summary_cluster_ratio));
314 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));
323 getPreferenceScreen().getSharedPreferences()
324 .registerOnSharedPreferenceChangeListener(
this);
331 getPreferenceScreen().getSharedPreferences()
332 .unregisterOnSharedPreferenceChangeListener(
this);
338 SharedPreferences.Editor ed =
getActivity().getSharedPreferences(fileName, MODE_PRIVATE).edit();
339 SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
342 for(Entry<String,?> entry : sp.getAll().entrySet()){
343 Object v = entry.getValue();
344 String key = entry.getKey();
351 ed.putBoolean(key, ((Boolean)v).booleanValue());
352 else if(v instanceof Float)
353 ed.putFloat(key, ((Float)v).floatValue());
354 else if(v instanceof Integer)
355 ed.putInt(key, ((Integer)v).intValue());
356 else if(v instanceof Long)
357 ed.putLong(key, ((Long)v).longValue());
358 else if(v instanceof String)
359 ed.putString(key, ((String)v));
367 switch (requestCode) {
370 if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
373 Preference buttonGPS = findPreference(getString(R.string.pref_key_gps_saved));
378 Toast.makeText(
this,
"Location permission is needed to use GPS functionality", Toast.LENGTH_LONG).show();
static boolean hasPermission(Activity activity, String permission)
static void requestPermission(Activity activity, String permission)
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
static final int ACCESS_FINE_LOCATION_CODE
void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results)
SettingsActivity getActivity()
static boolean shouldShowRequestPermissionRationale(Activity activity, String permission)
void onCreate(Bundle savedInstanceState)
void saveConfig(String fileName)
static void launchPermissionSettings(Activity activity)