]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java
07e0c61bad8c4c3b5564714821482381f275f122
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / SettingsActivity.java
1 /*
2  * Copyright © 2018 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * Mobile-Ledger is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger;
19
20 import android.annotation.TargetApi;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.Configuration;
24 import android.media.Ringtone;
25 import android.media.RingtoneManager;
26 import android.net.Uri;
27 import android.os.Build;
28 import android.os.Bundle;
29 import android.preference.ListPreference;
30 import android.preference.Preference;
31 import android.preference.PreferenceActivity;
32 import android.preference.PreferenceFragment;
33 import android.preference.PreferenceManager;
34 import android.preference.RingtonePreference;
35 import android.support.v4.app.NavUtils;
36 import android.support.v7.app.ActionBar;
37 import android.text.TextUtils;
38 import android.view.MenuItem;
39
40 import java.util.List;
41
42 /**
43  * A {@link PreferenceActivity} that presents a set of application settings. On
44  * handset devices, settings are presented as a single list. On tablets,
45  * settings are split by category, with category headers shown to the left of
46  * the list of settings.
47  * <p>
48  * See <a href="http://developer.android.com/design/patterns/settings.html">
49  * Android Design: Settings</a> for design guidelines and the <a
50  * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
51  * API Guide</a> for more information on developing a Settings UI.
52  */
53 public class SettingsActivity extends AppCompatPreferenceActivity {
54     public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts";
55
56     /**
57      * A preference value change listener that updates the preference's summary
58      * to reflect its new value.
59      */
60     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
61         String stringValue = value.toString();
62
63         AccountSummary.preferences_changed();
64
65         if (preference instanceof ListPreference) {
66             // For list preferences, look up the correct display value in
67             // the preference's 'entries' list.
68             ListPreference listPreference = (ListPreference) preference;
69             int index = listPreference.findIndexOfValue(stringValue);
70
71             // Set the summary to reflect the new value.
72             preference.setSummary(
73                     index >= 0
74                             ? listPreference.getEntries()[index]
75                             : null);
76
77         } else if (preference instanceof RingtonePreference) {
78             // For ringtone preferences, look up the correct display value
79             // using RingtoneManager.
80             if (TextUtils.isEmpty(stringValue)) {
81                 // Empty values correspond to 'silent' (no ringtone).
82                 preference.setSummary(R.string.pref_ringtone_silent);
83
84             } else {
85                 Ringtone ringtone = RingtoneManager.getRingtone(
86                         preference.getContext(), Uri.parse(stringValue));
87
88                 if (ringtone == null) {
89                     // Clear the summary if there was a lookup error.
90                     preference.setSummary(null);
91                 } else {
92                     // Set the summary to reflect the new ringtone display
93                     // name.
94                     String name = ringtone.getTitle(preference.getContext());
95                     preference.setSummary(name);
96                 }
97             }
98         } else {
99             // For all other preferences, set the summary to the value's
100             // simple string representation.
101             preference.setSummary(stringValue);
102         }
103
104         return true;
105     };
106
107     /**
108      * Helper method to determine if the device has an extra-large screen. For
109      * example, 10" tablets are extra-large.
110      */
111     private static boolean isXLargeTablet(Context context) {
112         return (context.getResources().getConfiguration().screenLayout
113                 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
114     }
115
116     /**
117      * Binds a preference's summary to its value. More specifically, when the
118      * preference's value is changed, its summary (line of text below the
119      * preference title) is updated to reflect the value. The summary is also
120      * immediately updated upon calling this method. The exact display format is
121      * dependent on the type of preference.
122      *
123      * @see #sBindPreferenceSummaryToValueListener
124      */
125     private static void bindPreferenceSummaryToValue(Preference preference) {
126         // Set the listener to watch for value changes.
127         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
128
129         // Trigger the listener immediately with the preference's
130         // current value.
131         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
132                 PreferenceManager
133                         .getDefaultSharedPreferences(preference.getContext())
134                         .getString(preference.getKey(), ""));
135     }
136
137     @Override
138     protected void onCreate(Bundle savedInstanceState) {
139         super.onCreate(savedInstanceState);
140         setupActionBar();
141     }
142
143     /**
144      * Set up the {@link android.app.ActionBar}, if the API is available.
145      */
146     private void setupActionBar() {
147         ActionBar actionBar = getSupportActionBar();
148         if (actionBar != null) {
149             // Show the Up button in the action bar.
150             actionBar.setDisplayHomeAsUpEnabled(true);
151         }
152     }
153
154     @Override
155     public boolean onMenuItemSelected(int featureId, MenuItem item) {
156         int id = item.getItemId();
157         if (id == android.R.id.home) {
158             if (!super.onMenuItemSelected(featureId, item)) {
159                 NavUtils.navigateUpFromSameTask(this);
160             }
161             return true;
162         }
163         return super.onMenuItemSelected(featureId, item);
164     }
165
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public boolean onIsMultiPane() {
171         return isXLargeTablet(this);
172     }
173
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
179     public void onBuildHeaders(List<Header> target) {
180         loadHeadersFromResource(R.xml.pref_headers, target);
181     }
182
183     /**
184      * This method stops fragment injection in malicious applications.
185      * Make sure to deny any unknown fragments here.
186      */
187     protected boolean isValidFragment(String fragmentName) {
188         return PreferenceFragment.class.getName().equals(fragmentName)
189                 || BackendPreferenceFragment.class.getName().equals(fragmentName)
190                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
191                 || NotificationPreferenceFragment.class.getName().equals(fragmentName)
192                 || InterfacePreferenceFragment.class.getName().equals(fragmentName);
193     }
194
195     /**
196      * This fragment shows general preferences only. It is used when the
197      * activity is showing a two-pane settings UI.
198      */
199     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
200     public static class BackendPreferenceFragment extends PreferenceFragment {
201         @Override
202         public void onCreate(Bundle savedInstanceState) {
203             super.onCreate(savedInstanceState);
204             addPreferencesFromResource(R.xml.pref_backend);
205             setHasOptionsMenu(true);
206
207             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
208             // to their values. When their values change, their summaries are
209             // updated to reflect the new value, per the Android Design
210             // guidelines.
211             bindPreferenceSummaryToValue(findPreference("backend_url"));
212             bindPreferenceSummaryToValue(findPreference("backend_auth_user"));
213
214         }
215
216         @Override
217         public boolean onOptionsItemSelected(MenuItem item) {
218             int id = item.getItemId();
219             if (id == android.R.id.home) {
220                 startActivity(new Intent(getActivity(), SettingsActivity.class));
221                 return true;
222             }
223             return super.onOptionsItemSelected(item);
224         }
225     }
226
227     /**
228      * This fragment shows general preferences only. It is used when the
229      * activity is showing a two-pane settings UI.
230      */
231     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
232     public static
233     class InterfacePreferenceFragment extends PreferenceFragment {
234         @Override
235         public
236         void onCreate(Bundle savedInstanceState) {
237             super.onCreate(savedInstanceState);
238             addPreferencesFromResource(R.xml.pref_interface);
239             setHasOptionsMenu(true);
240
241             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
242             // to their values. When their values change, their summaries are
243             // updated to reflect the new value, per the Android Design
244             // guidelines.
245
246         }
247
248         @Override
249         public
250         boolean onOptionsItemSelected(MenuItem item) {
251             int id = item.getItemId();
252             if (id == android.R.id.home) {
253                 startActivity(new Intent(getActivity(), SettingsActivity.class));
254                 return true;
255             }
256             return super.onOptionsItemSelected(item);
257         }
258     }
259
260     /**
261      * This fragment shows notification preferences only. It is used when the
262      * activity is showing a two-pane settings UI.
263      */
264     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
265     public static class NotificationPreferenceFragment extends PreferenceFragment {
266         @Override
267         public void onCreate(Bundle savedInstanceState) {
268             super.onCreate(savedInstanceState);
269             addPreferencesFromResource(R.xml.pref_notification);
270             setHasOptionsMenu(true);
271
272             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
273             // to their values. When their values change, their summaries are
274             // updated to reflect the new value, per the Android Design
275             // guidelines.
276             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
277         }
278
279         @Override
280         public boolean onOptionsItemSelected(MenuItem item) {
281             int id = item.getItemId();
282             if (id == android.R.id.home) {
283                 startActivity(new Intent(getActivity(), SettingsActivity.class));
284                 return true;
285             }
286             return super.onOptionsItemSelected(item);
287         }
288     }
289
290     /**
291      * This fragment shows data and sync preferences only. It is used when the
292      * activity is showing a two-pane settings UI.
293      */
294     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
295     public static class DataSyncPreferenceFragment extends PreferenceFragment {
296         @Override
297         public void onCreate(Bundle savedInstanceState) {
298             super.onCreate(savedInstanceState);
299             addPreferencesFromResource(R.xml.pref_data_sync);
300             setHasOptionsMenu(true);
301
302             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
303             // to their values. When their values change, their summaries are
304             // updated to reflect the new value, per the Android Design
305             // guidelines.
306             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
307         }
308
309         @Override
310         public boolean onOptionsItemSelected(MenuItem item) {
311             int id = item.getItemId();
312             if (id == android.R.id.home) {
313                 startActivity(new Intent(getActivity(), SettingsActivity.class));
314                 return true;
315             }
316             return super.onOptionsItemSelected(item);
317         }
318     }
319 }