Java tutorial
/* Respi 5/5 * * Copyright 2014-2015 Julien Viard de Galbert * * This file is part of Respi 5/5. * * Respi 5/5 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * Respi 5/5 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Respi 5/5. If not, see <http://www.gnu.org/licenses/>. */ package name.vdg.respi55; import android.annotation.TargetApi; import android.app.ActionBar; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.support.v4.app.NavUtils; import android.view.MenuItem; /** * Respi 5/5 Settings screen */ public class SettingsActivity extends PreferenceActivity { public static final String KEY_DISPLAY_DIGITS = "pref_key_display_digits"; public static final String KEY_SOUND_ENABLE = "pref_key_sound_enable"; public static final String KEY_ENABLE_TICKS = "pref_key_enable_ticks"; public static final String KEY_ENABLE_5S = "pref_key_enable_5s"; /** * The instance of the {@link RespiStateManager} for this activity. */ private RespiStateManager mRespiStateManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupActionBar(); } @Override protected void onResume() { super.onResume(); Intent intent = new Intent(this, RespiStateManager.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onPause() { super.onPause(); unbindService(mConnection); } private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { RespiStateManager.LocalBinder b = (RespiStateManager.LocalBinder) binder; mRespiStateManager = b.getService(); } public void onServiceDisconnected(ComponentName className) { mRespiStateManager = null; } }; @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && mRespiStateManager != null) mRespiStateManager.setAudioFocus(this); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Show the Up button in the action bar. ActionBar ab = getActionBar(); if (ab != null) ab.setDisplayHomeAsUpEnabled(true); } } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // // TODO: If Settings has multiple levels, Up should navigate up // that hierarchy. NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setupPreferenceScreen(); } @SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupPreferenceScreen() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { addPreferencesFromResource(R.xml.preferences); // Bind the summaries of EditText/List/Dialog preferences // to their values. When their values change, their summaries are // updated to reflect the new value, per the Android Design // guidelines. bindPreferenceSummaryToValue(findPreference(KEY_SOUND_ENABLE)); } else { // Display the fragment as the main content. getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit(); } } /** * A preference value change listener that updates the preference's summary * to reflect its new value. */ private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { String stringValue = value.toString(); if (preference instanceof ListPreference) { // For list preferences, look up the correct display value in // the preference's 'entries' list. ListPreference listPreference = (ListPreference) preference; int index = listPreference.findIndexOfValue(stringValue); // Set the summary to reflect the new value. preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null); } else { // For all other preferences, set the summary to the value's // simple string representation. preference.setSummary(stringValue); } return true; } }; /** * Binds a preference's summary to its value. More specifically, when the * preference's value is changed, its summary (line of text below the * preference title) is updated to reflect the value. The summary is also * immediately updated upon calling this method. The exact display format is * dependent on the type of preference. * * @see #sBindPreferenceSummaryToValueListener */ private static void bindPreferenceSummaryToValue(Preference preference) { // Set the listener to watch for value changes. preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); // Trigger the listener immediately with the preference's // current value. sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), "")); } /** * This fragment shows general preferences only. It is used when the * activity is showing a two-pane settings UI. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); // Bind the summaries of EditText/List/Dialog preferences // to their values. When their values change, their summaries are // updated to reflect the new value, per the Android Design // guidelines. bindPreferenceSummaryToValue(findPreference(KEY_SOUND_ENABLE)); } } @Override protected boolean isValidFragment(String fragmentName) { if (SettingsFragment.class.getName().equals(fragmentName)) return true; return false; } }