1 package net.ktnx.mobileledger;
3 import android.annotation.TargetApi;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.res.Configuration;
7 import android.media.Ringtone;
8 import android.media.RingtoneManager;
9 import android.net.Uri;
10 import android.os.Build;
11 import android.os.Bundle;
12 import android.preference.ListPreference;
13 import android.preference.Preference;
14 import android.preference.PreferenceActivity;
15 import android.preference.PreferenceFragment;
16 import android.preference.PreferenceManager;
17 import android.preference.RingtonePreference;
18 import android.support.v4.app.NavUtils;
19 import android.support.v7.app.ActionBar;
20 import android.text.TextUtils;
21 import android.view.MenuItem;
23 import java.util.List;
26 * A {@link PreferenceActivity} that presents a set of application settings. On
27 * handset devices, settings are presented as a single list. On tablets,
28 * settings are split by category, with category headers shown to the left of
29 * the list of settings.
31 * See <a href="http://developer.android.com/design/patterns/settings.html">
32 * Android Design: Settings</a> for design guidelines and the <a
33 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
34 * API Guide</a> for more information on developing a Settings UI.
36 public class SettingsActivity extends AppCompatPreferenceActivity {
39 * A preference value change listener that updates the preference's summary
40 * to reflect its new value.
42 private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
44 public boolean onPreferenceChange(Preference preference, Object value) {
45 String stringValue = value.toString();
47 if (preference instanceof ListPreference) {
48 // For list preferences, look up the correct display value in
49 // the preference's 'entries' list.
50 ListPreference listPreference = (ListPreference) preference;
51 int index = listPreference.findIndexOfValue(stringValue);
53 // Set the summary to reflect the new value.
54 preference.setSummary(
56 ? listPreference.getEntries()[index]
59 } else if (preference instanceof RingtonePreference) {
60 // For ringtone preferences, look up the correct display value
61 // using RingtoneManager.
62 if (TextUtils.isEmpty(stringValue)) {
63 // Empty values correspond to 'silent' (no ringtone).
64 preference.setSummary(R.string.pref_ringtone_silent);
67 Ringtone ringtone = RingtoneManager.getRingtone(
68 preference.getContext(), Uri.parse(stringValue));
70 if (ringtone == null) {
71 // Clear the summary if there was a lookup error.
72 preference.setSummary(null);
74 // Set the summary to reflect the new ringtone display
76 String name = ringtone.getTitle(preference.getContext());
77 preference.setSummary(name);
81 // For all other preferences, set the summary to the value's
82 // simple string representation.
83 preference.setSummary(stringValue);
90 * Helper method to determine if the device has an extra-large screen. For
91 * example, 10" tablets are extra-large.
93 private static boolean isXLargeTablet(Context context) {
94 return (context.getResources().getConfiguration().screenLayout
95 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
99 * Binds a preference's summary to its value. More specifically, when the
100 * preference's value is changed, its summary (line of text below the
101 * preference title) is updated to reflect the value. The summary is also
102 * immediately updated upon calling this method. The exact display format is
103 * dependent on the type of preference.
105 * @see #sBindPreferenceSummaryToValueListener
107 private static void bindPreferenceSummaryToValue(Preference preference) {
108 // Set the listener to watch for value changes.
109 preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
111 // Trigger the listener immediately with the preference's
113 sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
115 .getDefaultSharedPreferences(preference.getContext())
116 .getString(preference.getKey(), ""));
120 protected void onCreate(Bundle savedInstanceState) {
121 super.onCreate(savedInstanceState);
126 * Set up the {@link android.app.ActionBar}, if the API is available.
128 private void setupActionBar() {
129 ActionBar actionBar = getSupportActionBar();
130 if (actionBar != null) {
131 // Show the Up button in the action bar.
132 actionBar.setDisplayHomeAsUpEnabled(true);
137 public boolean onMenuItemSelected(int featureId, MenuItem item) {
138 int id = item.getItemId();
139 if (id == android.R.id.home) {
140 if (!super.onMenuItemSelected(featureId, item)) {
141 NavUtils.navigateUpFromSameTask(this);
145 return super.onMenuItemSelected(featureId, item);
152 public boolean onIsMultiPane() {
153 return isXLargeTablet(this);
160 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
161 public void onBuildHeaders(List<Header> target) {
162 loadHeadersFromResource(R.xml.pref_headers, target);
166 * This method stops fragment injection in malicious applications.
167 * Make sure to deny any unknown fragments here.
169 protected boolean isValidFragment(String fragmentName) {
170 return PreferenceFragment.class.getName().equals(fragmentName)
171 || BackendPreferenceFragment.class.getName().equals(fragmentName)
172 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
173 || NotificationPreferenceFragment.class.getName().equals(fragmentName);
177 * This fragment shows general preferences only. It is used when the
178 * activity is showing a two-pane settings UI.
180 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
181 public static class BackendPreferenceFragment extends PreferenceFragment {
183 public void onCreate(Bundle savedInstanceState) {
184 super.onCreate(savedInstanceState);
185 addPreferencesFromResource(R.xml.pref_backend);
186 setHasOptionsMenu(true);
188 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
189 // to their values. When their values change, their summaries are
190 // updated to reflect the new value, per the Android Design
192 bindPreferenceSummaryToValue(findPreference("backend_url"));
193 bindPreferenceSummaryToValue(findPreference("backend_auth_user"));
198 public boolean onOptionsItemSelected(MenuItem item) {
199 int id = item.getItemId();
200 if (id == android.R.id.home) {
201 startActivity(new Intent(getActivity(), SettingsActivity.class));
204 return super.onOptionsItemSelected(item);
209 * This fragment shows notification preferences only. It is used when the
210 * activity is showing a two-pane settings UI.
212 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
213 public static class NotificationPreferenceFragment extends PreferenceFragment {
215 public void onCreate(Bundle savedInstanceState) {
216 super.onCreate(savedInstanceState);
217 addPreferencesFromResource(R.xml.pref_notification);
218 setHasOptionsMenu(true);
220 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
221 // to their values. When their values change, their summaries are
222 // updated to reflect the new value, per the Android Design
224 bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
228 public boolean onOptionsItemSelected(MenuItem item) {
229 int id = item.getItemId();
230 if (id == android.R.id.home) {
231 startActivity(new Intent(getActivity(), SettingsActivity.class));
234 return super.onOptionsItemSelected(item);
239 * This fragment shows data and sync preferences only. It is used when the
240 * activity is showing a two-pane settings UI.
242 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
243 public static class DataSyncPreferenceFragment extends PreferenceFragment {
245 public void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
247 addPreferencesFromResource(R.xml.pref_data_sync);
248 setHasOptionsMenu(true);
250 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
251 // to their values. When their values change, their summaries are
252 // updated to reflect the new value, per the Android Design
254 bindPreferenceSummaryToValue(findPreference("sync_frequency"));
258 public boolean onOptionsItemSelected(MenuItem item) {
259 int id = item.getItemId();
260 if (id == android.R.id.home) {
261 startActivity(new Intent(getActivity(), SettingsActivity.class));
264 return super.onOptionsItemSelected(item);