]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java
7d84fe0c2e03ce972a45f7006dc05393b61b3bc7
[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
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 = (preference, value) -> {
43         String stringValue = value.toString();
44
45         AccountSummary.preferences_changed();
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         } else {
81             // For all other preferences, set the summary to the value's
82             // simple string representation.
83             preference.setSummary(stringValue);
84         }
85         return true;
86     };
87
88     /**
89      * Helper method to determine if the device has an extra-large screen. For
90      * example, 10" tablets are extra-large.
91      */
92     private static boolean isXLargeTablet(Context context) {
93         return (context.getResources().getConfiguration().screenLayout
94                 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
95     }
96
97     /**
98      * Binds a preference's summary to its value. More specifically, when the
99      * preference's value is changed, its summary (line of text below the
100      * preference title) is updated to reflect the value. The summary is also
101      * immediately updated upon calling this method. The exact display format is
102      * dependent on the type of preference.
103      *
104      * @see #sBindPreferenceSummaryToValueListener
105      */
106     private static void bindPreferenceSummaryToValue(Preference preference) {
107         // Set the listener to watch for value changes.
108         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
109
110         // Trigger the listener immediately with the preference's
111         // current value.
112         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
113                 PreferenceManager
114                         .getDefaultSharedPreferences(preference.getContext())
115                         .getString(preference.getKey(), ""));
116     }
117
118     @Override
119     protected void onCreate(Bundle savedInstanceState) {
120         super.onCreate(savedInstanceState);
121         setupActionBar();
122     }
123
124     /**
125      * Set up the {@link android.app.ActionBar}, if the API is available.
126      */
127     private void setupActionBar() {
128         ActionBar actionBar = getSupportActionBar();
129         if (actionBar != null) {
130             // Show the Up button in the action bar.
131             actionBar.setDisplayHomeAsUpEnabled(true);
132         }
133     }
134
135     @Override
136     public boolean onMenuItemSelected(int featureId, MenuItem item) {
137         int id = item.getItemId();
138         if (id == android.R.id.home) {
139             if (!super.onMenuItemSelected(featureId, item)) {
140                 NavUtils.navigateUpFromSameTask(this);
141             }
142             return true;
143         }
144         return super.onMenuItemSelected(featureId, item);
145     }
146
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public boolean onIsMultiPane() {
152         return isXLargeTablet(this);
153     }
154
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
160     public void onBuildHeaders(List<Header> target) {
161         loadHeadersFromResource(R.xml.pref_headers, target);
162     }
163
164     /**
165      * This method stops fragment injection in malicious applications.
166      * Make sure to deny any unknown fragments here.
167      */
168     protected boolean isValidFragment(String fragmentName) {
169         return PreferenceFragment.class.getName().equals(fragmentName)
170                 || BackendPreferenceFragment.class.getName().equals(fragmentName)
171                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
172                 || NotificationPreferenceFragment.class.getName().equals(fragmentName);
173     }
174
175     /**
176      * This fragment shows general preferences only. It is used when the
177      * activity is showing a two-pane settings UI.
178      */
179     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
180     public static class BackendPreferenceFragment extends PreferenceFragment {
181         @Override
182         public void onCreate(Bundle savedInstanceState) {
183             super.onCreate(savedInstanceState);
184             addPreferencesFromResource(R.xml.pref_backend);
185             setHasOptionsMenu(true);
186
187             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
188             // to their values. When their values change, their summaries are
189             // updated to reflect the new value, per the Android Design
190             // guidelines.
191             bindPreferenceSummaryToValue(findPreference("backend_url"));
192             bindPreferenceSummaryToValue(findPreference("backend_auth_user"));
193
194         }
195
196         @Override
197         public boolean onOptionsItemSelected(MenuItem item) {
198             int id = item.getItemId();
199             if (id == android.R.id.home) {
200                 startActivity(new Intent(getActivity(), SettingsActivity.class));
201                 return true;
202             }
203             return super.onOptionsItemSelected(item);
204         }
205     }
206
207     /**
208      * This fragment shows notification preferences only. It is used when the
209      * activity is showing a two-pane settings UI.
210      */
211     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
212     public static class NotificationPreferenceFragment extends PreferenceFragment {
213         @Override
214         public void onCreate(Bundle savedInstanceState) {
215             super.onCreate(savedInstanceState);
216             addPreferencesFromResource(R.xml.pref_notification);
217             setHasOptionsMenu(true);
218
219             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
220             // to their values. When their values change, their summaries are
221             // updated to reflect the new value, per the Android Design
222             // guidelines.
223             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
224         }
225
226         @Override
227         public boolean onOptionsItemSelected(MenuItem item) {
228             int id = item.getItemId();
229             if (id == android.R.id.home) {
230                 startActivity(new Intent(getActivity(), SettingsActivity.class));
231                 return true;
232             }
233             return super.onOptionsItemSelected(item);
234         }
235     }
236
237     /**
238      * This fragment shows data and sync preferences only. It is used when the
239      * activity is showing a two-pane settings UI.
240      */
241     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
242     public static class DataSyncPreferenceFragment extends PreferenceFragment {
243         @Override
244         public void onCreate(Bundle savedInstanceState) {
245             super.onCreate(savedInstanceState);
246             addPreferencesFromResource(R.xml.pref_data_sync);
247             setHasOptionsMenu(true);
248
249             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
250             // to their values. When their values change, their summaries are
251             // updated to reflect the new value, per the Android Design
252             // guidelines.
253             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
254         }
255
256         @Override
257         public boolean onOptionsItemSelected(MenuItem item) {
258             int id = item.getItemId();
259             if (id == android.R.id.home) {
260                 startActivity(new Intent(getActivity(), SettingsActivity.class));
261                 return true;
262             }
263             return super.onOptionsItemSelected(item);
264         }
265     }
266 }