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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.activity;
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;
40 import net.ktnx.mobileledger.R;
42 import java.util.List;
45 * A {@link PreferenceActivity} that presents a set of application settings. On
46 * handset devices, settings are presented as a single list. On tablets,
47 * settings are split by category, with category headers shown to the left of
48 * the list of settings.
50 * See <a href="http://developer.android.com/design/patterns/settings.html">
51 * Android Design: Settings</a> for design guidelines and the <a
52 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
53 * API Guide</a> for more information on developing a Settings UI.
55 public class SettingsActivity extends AppCompatPreferenceActivity {
56 public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts";
59 * A preference value change listener that updates the preference's summary
60 * to reflect its new value.
62 private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
63 String stringValue = value.toString();
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);
71 // Set the summary to reflect the new value.
72 preference.setSummary(
74 ? listPreference.getEntries()[index]
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);
85 Ringtone ringtone = RingtoneManager.getRingtone(
86 preference.getContext(), Uri.parse(stringValue));
88 if (ringtone == null) {
89 // Clear the summary if there was a lookup error.
90 preference.setSummary(null);
92 // Set the summary to reflect the new ringtone display
94 String name = ringtone.getTitle(preference.getContext());
95 preference.setSummary(name);
99 // For all other preferences, set the summary to the value's
100 // simple string representation.
101 preference.setSummary(stringValue);
108 * Helper method to determine if the device has an extra-large screen. For
109 * example, 10" tablets are extra-large.
111 private static boolean isXLargeTablet(Context context) {
112 return (context.getResources().getConfiguration().screenLayout
113 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
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.
123 * @see #sBindPreferenceSummaryToValueListener
125 private static void bindPreferenceSummaryToValue(Preference preference) {
126 // Set the listener to watch for value changes.
127 preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
129 // Trigger the listener immediately with the preference's
131 sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
133 .getDefaultSharedPreferences(preference.getContext())
134 .getString(preference.getKey(), ""));
138 protected void onCreate(Bundle savedInstanceState) {
139 super.onCreate(savedInstanceState);
144 * Set up the {@link android.app.ActionBar}, if the API is available.
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);
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);
163 return super.onMenuItemSelected(featureId, item);
170 public boolean onIsMultiPane() {
171 return isXLargeTablet(this);
178 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
179 public void onBuildHeaders(List<Header> target) {
180 loadHeadersFromResource(R.xml.pref_headers, target);
184 * This method stops fragment injection in malicious applications.
185 * Make sure to deny any unknown fragments here.
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);
196 * This fragment shows general preferences only. It is used when the
197 * activity is showing a two-pane settings UI.
199 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
200 public static class BackendPreferenceFragment extends PreferenceFragment {
202 public void onCreate(Bundle savedInstanceState) {
203 super.onCreate(savedInstanceState);
204 addPreferencesFromResource(R.xml.pref_backend);
205 setHasOptionsMenu(true);
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
211 bindPreferenceSummaryToValue(findPreference("backend_url"));
212 bindPreferenceSummaryToValue(findPreference("backend_auth_user"));
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));
223 return super.onOptionsItemSelected(item);
228 * This fragment shows general preferences only. It is used when the
229 * activity is showing a two-pane settings UI.
231 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
233 class InterfacePreferenceFragment extends PreferenceFragment {
236 void onCreate(Bundle savedInstanceState) {
237 super.onCreate(savedInstanceState);
238 addPreferencesFromResource(R.xml.pref_interface);
239 setHasOptionsMenu(true);
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
250 boolean onOptionsItemSelected(MenuItem item) {
251 int id = item.getItemId();
252 if (id == android.R.id.home) {
253 startActivity(new Intent(getActivity(), SettingsActivity.class));
256 return super.onOptionsItemSelected(item);
261 * This fragment shows notification preferences only. It is used when the
262 * activity is showing a two-pane settings UI.
264 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
265 public static class NotificationPreferenceFragment extends PreferenceFragment {
267 public void onCreate(Bundle savedInstanceState) {
268 super.onCreate(savedInstanceState);
269 addPreferencesFromResource(R.xml.pref_notification);
270 setHasOptionsMenu(true);
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
276 bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
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));
286 return super.onOptionsItemSelected(item);
291 * This fragment shows data and sync preferences only. It is used when the
292 * activity is showing a two-pane settings UI.
294 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
295 public static class DataSyncPreferenceFragment extends PreferenceFragment {
297 public void onCreate(Bundle savedInstanceState) {
298 super.onCreate(savedInstanceState);
299 addPreferencesFromResource(R.xml.pref_data_sync);
300 setHasOptionsMenu(true);
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
306 bindPreferenceSummaryToValue(findPreference("sync_frequency"));
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));
316 return super.onOptionsItemSelected(item);