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.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;
26 
27 public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
28 
29  private SettingsActivity getActivity() {return this;}
30 
31  @Override
32  public void onCreate(Bundle savedInstanceState) {
33  super.onCreate(savedInstanceState);
34  addPreferencesFromResource(R.layout.activity_settings);
35 
36  Preference buttonReset = findPreference(getString(R.string.pref_key_reset_button));
37  buttonReset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
38  @Override
39  public boolean onPreferenceClick(Preference preference) {
40  getPreferenceScreen().getSharedPreferences().edit().clear().commit();
41 
42  recreate();
43 
44  return true;
45  }
46  });
47 
48  Preference buttonOpen = findPreference(getString(R.string.pref_key_open_button));
49  buttonOpen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
50  @Override
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)); // rip off the ".xml"
60  }
61  final String[] files = newList.toArray(new String[filesArray.size()]);
62  if(files.length > 0)
63  {
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) {
68  //sp1 is the shared pref to copy to
69  SharedPreferences.Editor ed = getPreferenceScreen().getSharedPreferences().edit();
70  SharedPreferences sp = getActivity().getSharedPreferences(files[which], MODE_PRIVATE); //The shared preferences to copy from
71  //Cycle through all the entries in the sp
72  for(Entry<String,?> entry : sp.getAll().entrySet()){
73  Object v = entry.getValue();
74  String key = entry.getKey();
75  //Now we just figure out what type it is, so we can copy it.
76  // Note that i am using Boolean and Integer instead of boolean and int.
77  // That's because the Entry class can only hold objects and int and boolean are primatives.
78  if(v instanceof Boolean)
79  // Also note that i have to cast the object to a Boolean
80  // and then use .booleanValue to get the boolean
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));
90  }
91  ed.commit(); //save it.
92  recreate();
93  return;
94  }
95  });
96  builder.show();
97  }
98  }
99 
100  return true;
101  }
102  });
103 
104  Preference buttonSave = findPreference(getString(R.string.pref_key_save_button));
105  buttonSave.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
106  @Override
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() {
115  @Override
116  public void onClick(DialogInterface dialog, int which)
117  {
118  final String fileName = input.getText().toString();
119  dialog.dismiss();
120  if(!fileName.isEmpty())
121  {
122  File newFile = new File(getApplicationInfo().dataDir + "/shared_prefs/" + fileName + ".xml");
123  if(newFile.exists())
124  {
125  new AlertDialog.Builder(getActivity())
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) {
130  saveConfig(fileName);
131  }
132  })
133  .setNegativeButton("No", new DialogInterface.OnClickListener() {
134  public void onClick(DialogInterface dialog, int which) {
135  dialog.dismiss();
136  }
137  })
138  .show();
139  }
140  else
141  {
142  saveConfig(fileName);
143  }
144  }
145  }
146  });
147  AlertDialog alertToShow = builder.create();
148  alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
149  alertToShow.show();
150 
151  return true;
152  }
153  });
154 
155  Preference buttonRemove = findPreference(getString(R.string.pref_key_remove_button));
156  buttonRemove.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
157  @Override
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)); // rip off the ".xml"
167  }
168  final String[] files = newList.toArray(new String[filesArray.size()]);
169  if(files.length > 0)
170  {
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");
176  if(file.exists())
177  {
178  file.delete();
179  }
180  return;
181  }
182  });
183  builder.show();
184  }
185  }
186 
187  return true;
188  }
189  });
190 
191  Preference buttonGPS = findPreference(getString(R.string.pref_key_gps_saved));
192  buttonGPS.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
193  @Override
194  public boolean onPreferenceClick(Preference preference) {
195  if(((CustomSwitchPreference)preference).isChecked())
196  {
197  if (!PermissionHelper.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
198  ((CustomSwitchPreference)preference).setChecked(false);
199  PermissionHelper.requestPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
200  return false;
201  }
202  }
203  return true;
204  }
205  });
206  if(((CustomSwitchPreference)buttonGPS).isChecked())
207  {
208  if (!PermissionHelper.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
209  ((CustomSwitchPreference)buttonGPS).setChecked(false);
210  PermissionHelper.requestPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
211  }
212  }
213 
214 
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));
224 
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));
239 
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));
246 
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));
250 
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));
253  }
254 
255  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
256  Preference pref = findPreference(key);
257 
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)
262  {
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.0f && maxDepth <= minDepth)
267  {
268  ((ListPreference)findPreference(getString(R.string.pref_key_min_depth))).setValueIndex(0);
269  }
270  }
271  if(key.compareTo(getString(R.string.pref_key_min_depth))==0)
272  {
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)
277  {
278  ((ListPreference)findPreference(getString(R.string.pref_key_depth))).setValueIndex(0);
279  }
280  }
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));
286 
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));
301 
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));
308 
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));
312 
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));
315 
316  }
317  }
318 
319  @Override
320  protected void onResume() {
321  super.onResume();
322  // Set up a listener whenever a key changes
323  getPreferenceScreen().getSharedPreferences()
324  .registerOnSharedPreferenceChangeListener(this);
325  }
326 
327  @Override
328  protected void onPause() {
329  super.onPause();
330  // Unregister the listener whenever a key changes
331  getPreferenceScreen().getSharedPreferences()
332  .unregisterOnSharedPreferenceChangeListener(this);
333  }
334 
335  private void saveConfig(String fileName)
336  {
337  //sp1 is the shared pref to copy to
338  SharedPreferences.Editor ed = getActivity().getSharedPreferences(fileName, MODE_PRIVATE).edit();
339  SharedPreferences sp = getPreferenceScreen().getSharedPreferences(); //The shared preferences to copy from
340  ed.clear(); // This clears the one we are copying to, but you don't necessarily need to do that.
341  //Cycle through all the entries in the sp
342  for(Entry<String,?> entry : sp.getAll().entrySet()){
343  Object v = entry.getValue();
344  String key = entry.getKey();
345  //Now we just figure out what type it is, so we can copy it.
346  // Note that i am using Boolean and Integer instead of boolean and int.
347  // That's because the Entry class can only hold objects and int and boolean are primatives.
348  if(v instanceof Boolean)
349  // Also note that i have to cast the object to a Boolean
350  // and then use .booleanValue to get the boolean
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));
360  }
361  ed.commit(); //save it.
362  }
363 
364  @Override
365  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
366 
367  switch (requestCode) {
369  // If request is cancelled, the result arrays are empty.
370  if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
371  // permission was granted, yay! Do the
372  // contacts-related task you need to do.
373  Preference buttonGPS = findPreference(getString(R.string.pref_key_gps_saved));
374  ((CustomSwitchPreference)buttonGPS).setChecked(true);
375  } else {
376  // permission denied, boo! Disable the
377  // functionality that depends on this permission.
378  Toast.makeText(this, "Location permission is needed to use GPS functionality", Toast.LENGTH_LONG).show();
379  if (!PermissionHelper.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
380  PermissionHelper.launchPermissionSettings(this); // Permission denied with checking "Do not ask again".
381  }
382  }
383  return;
384  }
385  }
386  }
387 }
static boolean hasPermission(Activity activity, String permission)
f
static void requestPermission(Activity activity, String permission)
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results)
unsigned char Boolean
Definition: ConvertUTF.h:93
static boolean shouldShowRequestPermissionRationale(Activity activity, String permission)
void onCreate(Bundle savedInstanceState)
static void launchPermissionSettings(Activity activity)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:35:00