SettingsActivity.java
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Intermodalics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package eu.intermodalics.tango_ros_streamer.activities;
18 
19 import android.annotation.TargetApi;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.SharedPreferences;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.preference.ListPreference;
28 import android.preference.Preference;
29 import android.preference.PreferenceActivity;
30 import android.preference.PreferenceFragment;
31 import android.preference.PreferenceManager;
32 import android.preference.SwitchPreference;
33 import android.support.design.widget.Snackbar;
34 import android.support.v7.widget.Toolbar;
35 import android.view.View;
36 import android.widget.Toast;
37 
38 import java.util.HashMap;
39 
42 
54 public class SettingsActivity extends AppCompatPreferenceActivity implements
55  SharedPreferences.OnSharedPreferenceChangeListener {
56  private static final String TAG = SettingsActivity.class.getSimpleName();
57  public static final String NEW_UUIDS_NAMES_MAP_ALERT = "new_uuids_names_map_alert";
58 
59  private SharedPreferences mSharedPref;
61  private HashMap<String, String> mUuidsNamesMap;
62  private BroadcastReceiver mNewUuidsNamesMapAlertReceiver;
63 
68  private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
69  @Override
70  public boolean onPreferenceChange(Preference preference, Object value) {
71  String stringValue = value.toString();
72 
73  if (preference instanceof ListPreference) {
74  // For list preferences, look up the correct display value in
75  // the preference's 'entries' list.
76  ListPreference listPreference = (ListPreference) preference;
77  int index = listPreference.findIndexOfValue(stringValue);
78 
79  // Set the summary to reflect the new value.
80  preference.setSummary(
81  index >= 0
82  ? listPreference.getEntries()[index]
83  : null);
84  } else {
85  // For all other preferences, set the summary to the value's
86  // simple string representation.
87  preference.setSummary(stringValue);
88  }
89  return true;
90  }
91  };
92 
102  private static void bindPreferenceSummaryToValue(Preference preference) {
103  // Set the listener to watch for value changes.
104  preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
105 
106  // Trigger the listener immediately with the preference's
107  // current value.
108  sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
109  PreferenceManager
110  .getDefaultSharedPreferences(preference.getContext())
111  .getString(preference.getKey(), ""));
112  }
113 
118  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {
119  if (key == getString(R.string.pref_master_is_local_key) ||
120  key == getString(R.string.pref_master_uri_key) ||
121  key == getString(R.string.pref_create_new_map_key) ||
122  key == getString(R.string.pref_enable_depth_key) ||
123  key == getString(R.string.pref_enable_color_camera_key) ||
124  key == getString(R.string.pref_localization_mode_key) ||
125  key == getString(R.string.pref_localization_map_uuid_key)) {
126  boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
127  if (previouslyStarted && mSettingsPreferenceFragment.getView() != null) {
128  boolean isRosMasterConnected = (mSharedPref.getInt(getString(R.string.ros_status),
129  RunningActivity.RosStatus.UNKNOWN.ordinal()) ==
130  RunningActivity.RosStatus.MASTER_CONNECTED.ordinal());
131  // These changes require to restart the app.
132  if (key == getString(R.string.pref_master_is_local_key) ||
133  (key == getString(R.string.pref_master_uri_key)) && isRosMasterConnected) {
134  Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(),
135  getString(R.string.snackbar_text_restart_app), Snackbar.LENGTH_INDEFINITE);
136  View snackBarView = snackbar.getView();
137  snackBarView.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
138  snackbar.show();
139  }
140  // These changes require to restart Tango only.
141  if (key == getString(R.string.pref_create_new_map_key) ||
142  key == getString(R.string.pref_enable_depth_key) ||
143  key == getString(R.string.pref_enable_color_camera_key) ||
144  key == getString(R.string.pref_localization_mode_key) ||
145  key == getString(R.string.pref_localization_map_uuid_key)) {
146  Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(),
147  getString(R.string.snackbar_text_restart_tango), Snackbar.LENGTH_INDEFINITE);
148  snackbar.setAction(getString(R.string.snackbar_action_text_restart_tango), new View.OnClickListener() {
149  @Override
150  public void onClick(View view) {
151  restartTango();
152  }
153  });
154  View snackBarView = snackbar.getView();
155  snackBarView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
156  snackbar.show();
157  }
158  }
159  }
161  }
162 
163  @Override
164  protected void onCreate(Bundle savedInstanceState) {
165  super.onCreate(savedInstanceState);
166  setContentView(R.layout.settings_activity);
167  mSharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
168  mSharedPref.registerOnSharedPreferenceChangeListener(this);
169  mSettingsPreferenceFragment = new SettingsPreferenceFragment();
170  getFragmentManager().beginTransaction()
171  .replace(R.id.fragment_container, mSettingsPreferenceFragment)
172  .commit();
173  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
174  setSupportActionBar(toolbar);
175  mNewUuidsNamesMapAlertReceiver = new BroadcastReceiver() {
176  @Override
177  public void onReceive(Context context, Intent intent) {
178  mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
180  mSettingsPreferenceFragment.setPreferencesSummary();
181  }
182  };
183  this.registerReceiver(this.mNewUuidsNamesMapAlertReceiver, new IntentFilter(NEW_UUIDS_NAMES_MAP_ALERT));
184  }
185 
186  @Override
187  protected void onStart() {
188  super.onStart();
189  boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
190  if(!previouslyStarted) {
191  runOnUiThread(new Runnable() {
192  @Override
193  public void run() {
194  Toast.makeText(getApplicationContext(), R.string.welcome_text_first_run, Toast.LENGTH_LONG).show();
195  }
196  });
197  Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(), getString(R.string.snackbar_text_first_run), Snackbar.LENGTH_INDEFINITE);
198  snackbar.setAction(getString(R.string.snackbar_action_text_first_run), new View.OnClickListener() {
199  @Override
200  public void onClick(View view) {
201  setResult(RESULT_CANCELED, getIntent().putExtra(RunningActivity.RESTART_TANGO, false));
202  onBackPressed();
203  }
204  });
205  snackbar.show();
206  }
207  Preference enableColorCameraPref = mSettingsPreferenceFragment.findPreference(getString(R.string.pref_enable_color_camera_key));
208  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
209  enableColorCameraPref.setEnabled(true);
210  }
211  Preference aboutPref = mSettingsPreferenceFragment.findPreference(getString(R.string.pref_about_app_key));
212  aboutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
213  @Override
214  public boolean onPreferenceClick(Preference preference) {
216  return true;
217  }
218  });
219 
220  Intent intent = getIntent();
221  mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
223  mSettingsPreferenceFragment.setPreferencesSummary();
224  }
225 
226  @Override
227  protected void onDestroy() {
228  super.onDestroy();
229  this.unregisterReceiver(mNewUuidsNamesMapAlertReceiver);
230  }
231 
232  private void updateMapChooserPreference() {
233  SwitchPreference createNewMapPref = (SwitchPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_create_new_map_key));
234  if (createNewMapPref == null) return;
235  boolean createNewMap = createNewMapPref.isChecked();
236  ListPreference localizationModePref = (ListPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_localization_mode_key));
237  if (localizationModePref == null) return;
238  String localizationMode = localizationModePref.getValue();
239  MapChooserPreference mapChooserPreference =
240  (MapChooserPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_localization_map_uuid_key));
241  if (mapChooserPreference == null) return;
242  mapChooserPreference.setEnabled(!createNewMap && localizationMode.equals("3"));
243 
244  if (mUuidsNamesMap == null || mUuidsNamesMap.isEmpty()) {
245  mapChooserPreference.setEnabled(false);
246  } else {
247  mapChooserPreference.setMapList(mUuidsNamesMap);
248  }
249  }
250 
255  protected boolean isValidFragment(String fragmentName) {
256  return PreferenceFragment.class.getName().equals(fragmentName)
257  || SettingsPreferenceFragment.class.getName().equals(fragmentName);
258  }
259 
263  @TargetApi(Build.VERSION_CODES.KITKAT)
264  public static class SettingsPreferenceFragment extends PreferenceFragment {
265  @Override
266  public void onCreate(Bundle savedInstanceState) {
267  super.onCreate(savedInstanceState);
268  addPreferencesFromResource(R.xml.pref_settings);
269  setHasOptionsMenu(true);
270  }
271 
272  // Bind the summaries of EditText/List/Dialog/Ringtone preferences
273  // to their values. When their values change, their summaries are
274  // updated to reflect the new value, per the Android Design
275  // guidelines.
276  public void setPreferencesSummary() {
277  bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_master_uri_key)));
278  bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_log_file_key)));
279  bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_localization_mode_key)));
280  bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_localization_map_uuid_key)));
281  }
282  }
283 
284  @Override
285  public void onBackPressed() {
286  boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
287  if(!previouslyStarted) {
288  SharedPreferences.Editor edit = mSharedPref.edit();
289  edit.putBoolean(getString(R.string.pref_previously_started_key), Boolean.TRUE);
290  edit.commit();
291  }
292  super.onBackPressed();
293  }
294 
295  public void startAboutActivity() {
296  Intent intent = new Intent(this, AboutActivity.class);
297  startActivity(intent);
298  }
299 
303  private void restartTango() {
304  setResult(RESULT_CANCELED, getIntent().putExtra(RunningActivity.RESTART_TANGO, true));
305  onBackPressed();
306  }
307 }
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key)
static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener
void run(ClassLoader *loader)


TangoRosStreamer
Author(s):
autogenerated on Mon Jun 10 2019 15:37:54