]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java
better account starr-ing control
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / SettingsActivity.java
1 package net.ktnx.mobileledger;
2
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;
22
23 import java.util.List;
24
25 /**
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.
30  * <p>
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.
35  */
36 public class SettingsActivity extends AppCompatPreferenceActivity {
37     public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts";
38
39     /**
40      * A preference value change listener that updates the preference's summary
41      * to reflect its new value.
42      */
43     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
44         String stringValue = value.toString();
45
46         AccountSummary.preferences_changed();
47
48         if (preference instanceof ListPreference) {
49             // For list preferences, look up the correct display value in
50             // the preference's 'entries' list.
51             ListPreference listPreference = (ListPreference) preference;
52             int index = listPreference.findIndexOfValue(stringValue);
53
54             // Set the summary to reflect the new value.
55             preference.setSummary(
56                     index >= 0
57                             ? listPreference.getEntries()[index]
58                             : null);
59
60         } else if (preference instanceof RingtonePreference) {
61             // For ringtone preferences, look up the correct display value
62             // using RingtoneManager.
63             if (TextUtils.isEmpty(stringValue)) {
64                 // Empty values correspond to 'silent' (no ringtone).
65                 preference.setSummary(R.string.pref_ringtone_silent);
66
67             } else {
68                 Ringtone ringtone = RingtoneManager.getRingtone(
69                         preference.getContext(), Uri.parse(stringValue));
70
71                 if (ringtone == null) {
72                     // Clear the summary if there was a lookup error.
73                     preference.setSummary(null);
74                 } else {
75                     // Set the summary to reflect the new ringtone display
76                     // name.
77                     String name = ringtone.getTitle(preference.getContext());
78                     preference.setSummary(name);
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
87         return true;
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     @Override
138     public boolean onMenuItemSelected(int featureId, MenuItem item) {
139         int id = item.getItemId();
140         if (id == android.R.id.home) {
141             if (!super.onMenuItemSelected(featureId, item)) {
142                 NavUtils.navigateUpFromSameTask(this);
143             }
144             return true;
145         }
146         return super.onMenuItemSelected(featureId, item);
147     }
148
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public boolean onIsMultiPane() {
154         return isXLargeTablet(this);
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
162     public void onBuildHeaders(List<Header> target) {
163         loadHeadersFromResource(R.xml.pref_headers, target);
164     }
165
166     /**
167      * This method stops fragment injection in malicious applications.
168      * Make sure to deny any unknown fragments here.
169      */
170     protected boolean isValidFragment(String fragmentName) {
171         return PreferenceFragment.class.getName().equals(fragmentName)
172                 || BackendPreferenceFragment.class.getName().equals(fragmentName)
173                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
174                 || NotificationPreferenceFragment.class.getName().equals(fragmentName)
175                 || InterfacePreferenceFragment.class.getName().equals(fragmentName);
176     }
177
178     /**
179      * This fragment shows general preferences only. It is used when the
180      * activity is showing a two-pane settings UI.
181      */
182     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
183     public static class BackendPreferenceFragment extends PreferenceFragment {
184         @Override
185         public void onCreate(Bundle savedInstanceState) {
186             super.onCreate(savedInstanceState);
187             addPreferencesFromResource(R.xml.pref_backend);
188             setHasOptionsMenu(true);
189
190             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
191             // to their values. When their values change, their summaries are
192             // updated to reflect the new value, per the Android Design
193             // guidelines.
194             bindPreferenceSummaryToValue(findPreference("backend_url"));
195             bindPreferenceSummaryToValue(findPreference("backend_auth_user"));
196
197         }
198
199         @Override
200         public boolean onOptionsItemSelected(MenuItem item) {
201             int id = item.getItemId();
202             if (id == android.R.id.home) {
203                 startActivity(new Intent(getActivity(), SettingsActivity.class));
204                 return true;
205             }
206             return super.onOptionsItemSelected(item);
207         }
208     }
209
210     /**
211      * This fragment shows general preferences only. It is used when the
212      * activity is showing a two-pane settings UI.
213      */
214     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
215     public static
216     class InterfacePreferenceFragment extends PreferenceFragment {
217         @Override
218         public
219         void onCreate(Bundle savedInstanceState) {
220             super.onCreate(savedInstanceState);
221             addPreferencesFromResource(R.xml.pref_interface);
222             setHasOptionsMenu(true);
223
224             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
225             // to their values. When their values change, their summaries are
226             // updated to reflect the new value, per the Android Design
227             // guidelines.
228
229         }
230
231         @Override
232         public
233         boolean onOptionsItemSelected(MenuItem item) {
234             int id = item.getItemId();
235             if (id == android.R.id.home) {
236                 startActivity(new Intent(getActivity(), SettingsActivity.class));
237                 return true;
238             }
239             return super.onOptionsItemSelected(item);
240         }
241     }
242
243     /**
244      * This fragment shows notification preferences only. It is used when the
245      * activity is showing a two-pane settings UI.
246      */
247     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
248     public static class NotificationPreferenceFragment extends PreferenceFragment {
249         @Override
250         public void onCreate(Bundle savedInstanceState) {
251             super.onCreate(savedInstanceState);
252             addPreferencesFromResource(R.xml.pref_notification);
253             setHasOptionsMenu(true);
254
255             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
256             // to their values. When their values change, their summaries are
257             // updated to reflect the new value, per the Android Design
258             // guidelines.
259             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
260         }
261
262         @Override
263         public boolean onOptionsItemSelected(MenuItem item) {
264             int id = item.getItemId();
265             if (id == android.R.id.home) {
266                 startActivity(new Intent(getActivity(), SettingsActivity.class));
267                 return true;
268             }
269             return super.onOptionsItemSelected(item);
270         }
271     }
272
273     /**
274      * This fragment shows data and sync preferences only. It is used when the
275      * activity is showing a two-pane settings UI.
276      */
277     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
278     public static class DataSyncPreferenceFragment extends PreferenceFragment {
279         @Override
280         public void onCreate(Bundle savedInstanceState) {
281             super.onCreate(savedInstanceState);
282             addPreferencesFromResource(R.xml.pref_data_sync);
283             setHasOptionsMenu(true);
284
285             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
286             // to their values. When their values change, their summaries are
287             // updated to reflect the new value, per the Android Design
288             // guidelines.
289             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
290         }
291
292         @Override
293         public boolean onOptionsItemSelected(MenuItem item) {
294             int id = item.getItemId();
295             if (id == android.R.id.home) {
296                 startActivity(new Intent(getActivity(), SettingsActivity.class));
297                 return true;
298             }
299             return super.onOptionsItemSelected(item);
300         }
301     }
302 }