17 package eu.intermodalics.tango_ros_streamer.activities;
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;
38 import java.util.HashMap;
55 SharedPreferences.OnSharedPreferenceChangeListener {
70 public boolean onPreferenceChange(Preference preference, Object value) {
71 String stringValue = value.toString();
73 if (preference instanceof ListPreference) {
76 ListPreference listPreference = (ListPreference) preference;
77 int index = listPreference.findIndexOfValue(stringValue);
80 preference.setSummary(
82 ? listPreference.getEntries()[index]
87 preference.setSummary(stringValue);
110 .getDefaultSharedPreferences(preference.getContext())
111 .getString(preference.getKey(),
""));
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),
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));
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() {
150 public void onClick(View view) {
154 View snackBarView = snackbar.getView();
155 snackBarView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
164 protected void onCreate(Bundle savedInstanceState) {
165 super.onCreate(savedInstanceState);
167 mSharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
168 mSharedPref.registerOnSharedPreferenceChangeListener(
this);
170 getFragmentManager().beginTransaction()
171 .replace(R.id.fragment_container, mSettingsPreferenceFragment)
173 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
175 mNewUuidsNamesMapAlertReceiver =
new BroadcastReceiver() {
177 public void onReceive(Context context, Intent intent) {
178 mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
183 this.registerReceiver(this.mNewUuidsNamesMapAlertReceiver,
new IntentFilter(NEW_UUIDS_NAMES_MAP_ALERT));
189 boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key),
false);
190 if(!previouslyStarted) {
191 runOnUiThread(
new Runnable() {
194 Toast.makeText(getApplicationContext(), R.string.welcome_text_first_run, Toast.LENGTH_LONG).show();
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() {
200 public void onClick(View view) {
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);
211 Preference aboutPref = mSettingsPreferenceFragment.findPreference(getString(R.string.pref_about_app_key));
212 aboutPref.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
214 public boolean onPreferenceClick(Preference preference) {
220 Intent intent = getIntent();
221 mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
229 this.unregisterReceiver(mNewUuidsNamesMapAlertReceiver);
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();
240 (
MapChooserPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_localization_map_uuid_key));
241 if (mapChooserPreference == null)
return;
242 mapChooserPreference.setEnabled(!createNewMap && localizationMode.equals(
"3"));
244 if (mUuidsNamesMap == null || mUuidsNamesMap.isEmpty()) {
245 mapChooserPreference.setEnabled(
false);
247 mapChooserPreference.
setMapList(mUuidsNamesMap);
256 return PreferenceFragment.class.getName().equals(fragmentName)
263 @TargetApi(Build.VERSION_CODES.KITKAT)
267 super.onCreate(savedInstanceState);
268 addPreferencesFromResource(R.xml.pref_settings);
269 setHasOptionsMenu(
true);
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);
292 super.onBackPressed();
297 startActivity(intent);
void updateMapChooserPreference()
void startAboutActivity()
void setMapList(Map< String, String > uuidNameMap)
void setSupportActionBar(@Nullable Toolbar toolbar)
void setPreferencesSummary()
BroadcastReceiver mNewUuidsNamesMapAlertReceiver
void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key)
static final String RESTART_TANGO
HashMap< String, String > mUuidsNamesMap
SharedPreferences mSharedPref
static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener
static final String NEW_UUIDS_NAMES_MAP_ALERT
SettingsPreferenceFragment mSettingsPreferenceFragment
static void bindPreferenceSummaryToValue(Preference preference)
void onCreate(Bundle savedInstanceState)
void setContentView(@LayoutRes int layoutResID)
void onCreate(Bundle savedInstanceState)
boolean isValidFragment(String fragmentName)
void run(ClassLoader *loader)