SettingsActivity.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 Intermodalics All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package eu.intermodalics.tango_ros_streamer.activities;
00018 
00019 import android.annotation.TargetApi;
00020 import android.content.BroadcastReceiver;
00021 import android.content.Context;
00022 import android.content.Intent;
00023 import android.content.IntentFilter;
00024 import android.content.SharedPreferences;
00025 import android.os.Build;
00026 import android.os.Bundle;
00027 import android.preference.ListPreference;
00028 import android.preference.Preference;
00029 import android.preference.PreferenceActivity;
00030 import android.preference.PreferenceFragment;
00031 import android.preference.PreferenceManager;
00032 import android.preference.SwitchPreference;
00033 import android.support.design.widget.Snackbar;
00034 import android.support.v7.widget.Toolbar;
00035 import android.view.View;
00036 import android.widget.Toast;
00037 
00038 import java.util.HashMap;
00039 
00040 import eu.intermodalics.tango_ros_streamer.android.MapChooserPreference;
00041 import eu.intermodalics.tango_ros_streamer.R;
00042 
00054 public class SettingsActivity extends AppCompatPreferenceActivity implements
00055         SharedPreferences.OnSharedPreferenceChangeListener {
00056     private static final String TAG = SettingsActivity.class.getSimpleName();
00057     public static final String NEW_UUIDS_NAMES_MAP_ALERT = "new_uuids_names_map_alert";
00058 
00059     private SharedPreferences mSharedPref;
00060     private SettingsPreferenceFragment mSettingsPreferenceFragment;
00061     private HashMap<String, String> mUuidsNamesMap;
00062     private BroadcastReceiver mNewUuidsNamesMapAlertReceiver;
00063 
00068     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
00069         @Override
00070         public boolean onPreferenceChange(Preference preference, Object value) {
00071             String stringValue = value.toString();
00072 
00073             if (preference instanceof ListPreference) {
00074                 // For list preferences, look up the correct display value in
00075                 // the preference's 'entries' list.
00076                 ListPreference listPreference = (ListPreference) preference;
00077                 int index = listPreference.findIndexOfValue(stringValue);
00078 
00079                 // Set the summary to reflect the new value.
00080                 preference.setSummary(
00081                         index >= 0
00082                                 ? listPreference.getEntries()[index]
00083                                 : null);
00084             } else {
00085                 // For all other preferences, set the summary to the value's
00086                 // simple string representation.
00087                 preference.setSummary(stringValue);
00088             }
00089             return true;
00090         }
00091     };
00092 
00102     private static void bindPreferenceSummaryToValue(Preference preference) {
00103         // Set the listener to watch for value changes.
00104         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
00105 
00106         // Trigger the listener immediately with the preference's
00107         // current value.
00108         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
00109                 PreferenceManager
00110                         .getDefaultSharedPreferences(preference.getContext())
00111                         .getString(preference.getKey(), ""));
00112     }
00113 
00118     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {
00119         if (key == getString(R.string.pref_master_is_local_key) ||
00120                 key == getString(R.string.pref_master_uri_key) ||
00121                 key == getString(R.string.pref_create_new_map_key) ||
00122                 key == getString(R.string.pref_enable_depth_key) ||
00123                 key == getString(R.string.pref_enable_color_camera_key) ||
00124                 key == getString(R.string.pref_localization_mode_key) ||
00125                 key == getString(R.string.pref_localization_map_uuid_key)) {
00126             boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
00127             if (previouslyStarted && mSettingsPreferenceFragment.getView() != null) {
00128                 boolean isRosMasterConnected =  (mSharedPref.getInt(getString(R.string.ros_status),
00129                         RunningActivity.RosStatus.UNKNOWN.ordinal()) ==
00130                         RunningActivity.RosStatus.MASTER_CONNECTED.ordinal());
00131                 // These changes require to restart the app.
00132                 if (key == getString(R.string.pref_master_is_local_key) ||
00133                         (key == getString(R.string.pref_master_uri_key)) && isRosMasterConnected) {
00134                     Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(),
00135                             getString(R.string.snackbar_text_restart_app), Snackbar.LENGTH_INDEFINITE);
00136                     View snackBarView = snackbar.getView();
00137                     snackBarView.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
00138                     snackbar.show();
00139                 }
00140                 // These changes require to restart Tango only.
00141                 if (key == getString(R.string.pref_create_new_map_key) ||
00142                     key == getString(R.string.pref_enable_depth_key) ||
00143                     key == getString(R.string.pref_enable_color_camera_key) ||
00144                     key == getString(R.string.pref_localization_mode_key) ||
00145                     key == getString(R.string.pref_localization_map_uuid_key)) {
00146                     Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(),
00147                             getString(R.string.snackbar_text_restart_tango), Snackbar.LENGTH_INDEFINITE);
00148                     snackbar.setAction(getString(R.string.snackbar_action_text_restart_tango), new View.OnClickListener() {
00149                         @Override
00150                         public void onClick(View view) {
00151                             restartTango();
00152                         }
00153                     });
00154                     View snackBarView = snackbar.getView();
00155                     snackBarView.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
00156                     snackbar.show();
00157                 }
00158             }
00159         }
00160         updateMapChooserPreference();
00161     }
00162 
00163     @Override
00164     protected void onCreate(Bundle savedInstanceState) {
00165         super.onCreate(savedInstanceState);
00166         setContentView(R.layout.settings_activity);
00167         mSharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
00168         mSharedPref.registerOnSharedPreferenceChangeListener(this);
00169         mSettingsPreferenceFragment = new SettingsPreferenceFragment();
00170         getFragmentManager().beginTransaction()
00171                 .replace(R.id.fragment_container, mSettingsPreferenceFragment)
00172                 .commit();
00173         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
00174         setSupportActionBar(toolbar);
00175         mNewUuidsNamesMapAlertReceiver = new BroadcastReceiver() {
00176             @Override
00177             public void onReceive(Context context, Intent intent) {
00178                 mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
00179                 updateMapChooserPreference();
00180                 mSettingsPreferenceFragment.setPreferencesSummary();
00181             }
00182         };
00183         this.registerReceiver(this.mNewUuidsNamesMapAlertReceiver, new IntentFilter(NEW_UUIDS_NAMES_MAP_ALERT));
00184     }
00185 
00186     @Override
00187     protected void onStart() {
00188         super.onStart();
00189         boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
00190         if(!previouslyStarted) {
00191             runOnUiThread(new Runnable() {
00192                 @Override
00193                 public void run() {
00194                     Toast.makeText(getApplicationContext(), R.string.welcome_text_first_run, Toast.LENGTH_LONG).show();
00195                 }
00196             });
00197             Snackbar snackbar = Snackbar.make(mSettingsPreferenceFragment.getView(), getString(R.string.snackbar_text_first_run), Snackbar.LENGTH_INDEFINITE);
00198             snackbar.setAction(getString(R.string.snackbar_action_text_first_run), new View.OnClickListener() {
00199                 @Override
00200                 public void onClick(View view) {
00201                     setResult(RESULT_CANCELED, getIntent().putExtra(RunningActivity.RESTART_TANGO, false));
00202                     onBackPressed();
00203                 }
00204             });
00205             snackbar.show();
00206         }
00207         Preference enableColorCameraPref = mSettingsPreferenceFragment.findPreference(getString(R.string.pref_enable_color_camera_key));
00208         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
00209             enableColorCameraPref.setEnabled(true);
00210         }
00211         Preference aboutPref = mSettingsPreferenceFragment.findPreference(getString(R.string.pref_about_app_key));
00212         aboutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
00213             @Override
00214             public boolean onPreferenceClick(Preference preference) {
00215                 startAboutActivity();
00216                 return true;
00217             }
00218         });
00219 
00220         Intent intent = getIntent();
00221         mUuidsNamesMap = (HashMap<String, String>) intent.getSerializableExtra(getString(R.string.uuids_names_map));
00222         updateMapChooserPreference();
00223         mSettingsPreferenceFragment.setPreferencesSummary();
00224     }
00225 
00226     @Override
00227     protected void onDestroy() {
00228         super.onDestroy();
00229         this.unregisterReceiver(mNewUuidsNamesMapAlertReceiver);
00230     }
00231 
00232     private void updateMapChooserPreference() {
00233         SwitchPreference createNewMapPref = (SwitchPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_create_new_map_key));
00234         if (createNewMapPref == null) return;
00235         boolean createNewMap = createNewMapPref.isChecked();
00236         ListPreference localizationModePref = (ListPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_localization_mode_key));
00237         if (localizationModePref == null) return;
00238         String localizationMode = localizationModePref.getValue();
00239         MapChooserPreference mapChooserPreference =
00240                 (MapChooserPreference) mSettingsPreferenceFragment.findPreference(getString(R.string.pref_localization_map_uuid_key));
00241         if (mapChooserPreference == null) return;
00242         mapChooserPreference.setEnabled(!createNewMap && localizationMode.equals("3"));
00243 
00244         if (mUuidsNamesMap == null || mUuidsNamesMap.isEmpty()) {
00245             mapChooserPreference.setEnabled(false);
00246         } else {
00247             mapChooserPreference.setMapList(mUuidsNamesMap);
00248         }
00249     }
00250 
00255     protected boolean isValidFragment(String fragmentName) {
00256         return PreferenceFragment.class.getName().equals(fragmentName)
00257                 || SettingsPreferenceFragment.class.getName().equals(fragmentName);
00258     }
00259 
00263     @TargetApi(Build.VERSION_CODES.KITKAT)
00264     public static class SettingsPreferenceFragment extends PreferenceFragment {
00265         @Override
00266         public void onCreate(Bundle savedInstanceState) {
00267             super.onCreate(savedInstanceState);
00268             addPreferencesFromResource(R.xml.pref_settings);
00269             setHasOptionsMenu(true);
00270         }
00271 
00272         // Bind the summaries of EditText/List/Dialog/Ringtone preferences
00273         // to their values. When their values change, their summaries are
00274         // updated to reflect the new value, per the Android Design
00275         // guidelines.
00276         public void setPreferencesSummary() {
00277             bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_master_uri_key)));
00278             bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_log_file_key)));
00279             bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_localization_mode_key)));
00280             bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_localization_map_uuid_key)));
00281         }
00282     }
00283 
00284     @Override
00285     public void onBackPressed() {
00286         boolean previouslyStarted = mSharedPref.getBoolean(getString(R.string.pref_previously_started_key), false);
00287         if(!previouslyStarted) {
00288             SharedPreferences.Editor edit = mSharedPref.edit();
00289             edit.putBoolean(getString(R.string.pref_previously_started_key), Boolean.TRUE);
00290             edit.commit();
00291         }
00292         super.onBackPressed();
00293     }
00294 
00295     public void startAboutActivity() {
00296         Intent intent = new Intent(this, AboutActivity.class);
00297         startActivity(intent);
00298     }
00299 
00303     private void restartTango() {
00304         setResult(RESULT_CANCELED, getIntent().putExtra(RunningActivity.RESTART_TANGO, true));
00305         onBackPressed();
00306     }
00307 }


TangoRosStreamer
Author(s):
autogenerated on Thu Jun 6 2019 19:49:58