31 | * See
32 | * Android Design: Settings for design guidelines and the Settings
34 | * API Guide for more information on developing a Settings UI.
35 | */
36 | public class SettingsActivity extends AppCompatPreferenceActivity {
37 |
38 | /**
39 | * A preference value change listener that updates the preference's summary
40 | * to reflect its new value.
41 | */
42 | private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
43 | @Override
44 | public boolean onPreferenceChange(Preference preference, Object value) {
45 | String stringValue = value.toString();
46 |
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);
52 |
53 | // Set the summary to reflect the new value.
54 | preference.setSummary(
55 | index >= 0
56 | ? listPreference.getEntries()[index]
57 | : null);
58 |
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);
65 |
66 | } else {
67 | Ringtone ringtone = RingtoneManager.getRingtone(
68 | preference.getContext(), Uri.parse(stringValue));
69 |
70 | if (ringtone == null) {
71 | // Clear the summary if there was a lookup error.
72 | preference.setSummary(null);
73 | } else {
74 | // Set the summary to reflect the new ringtone display
75 | // name.
76 | String name = ringtone.getTitle(preference.getContext());
77 | preference.setSummary(name);
78 | }
79 | }
80 |
81 | } else {
82 | // For all other preferences, set the summary to the value's
83 | // simple string representation.
84 | preference.setSummary(stringValue);
85 | }
86 | return true;
87 | }
88 | };
89 |
90 | /**
91 | * Helper method to determine if the device has an extra-large screen. For
92 | * example, 10" tablets are extra-large.
93 | */
94 | private static boolean isXLargeTablet(Context context) {
95 | return (context.getResources().getConfiguration().screenLayout
96 | & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
97 | }
98 |
99 | /**
100 | * Binds a preference's summary to its value. More specifically, when the
101 | * preference's value is changed, its summary (line of text below the
102 | * preference title) is updated to reflect the value. The summary is also
103 | * immediately updated upon calling this method. The exact display format is
104 | * dependent on the type of preference.
105 | *
106 | * @see #sBindPreferenceSummaryToValueListener
107 | */
108 | private static void bindPreferenceSummaryToValue(Preference preference) {
109 | // Set the listener to watch for value changes.
110 | preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
111 |
112 | // Trigger the listener immediately with the preference's
113 | // current value.
114 | sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
115 | PreferenceManager
116 | .getDefaultSharedPreferences(preference.getContext())
117 | .getString(preference.getKey(), ""));
118 | }
119 |
120 | @Override
121 | protected void onCreate(Bundle savedInstanceState) {
122 | super.onCreate(savedInstanceState);
123 | setupActionBar();
124 | }
125 |
126 | /**
127 | * Set up the {@link android.app.ActionBar}, if the API is available.
128 | */
129 | private void setupActionBar() {
130 | ActionBar actionBar = getSupportActionBar();
131 | if (actionBar != null) {
132 | // Show the Up button in the action bar.
133 | actionBar.setDisplayHomeAsUpEnabled(true);
134 | }
135 | }
136 |
137 | /**
138 | * {@inheritDoc}
139 | */
140 | @Override
141 | public boolean onIsMultiPane() {
142 | return isXLargeTablet(this);
143 | }
144 |
145 | /**
146 | * {@inheritDoc}
147 | */
148 | @Override
149 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
150 | public void onBuildHeaders(List