2 * Copyright © 2019 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 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
190 || NotificationPreferenceFragment.class.getName().equals(fragmentName)
191 || InterfacePreferenceFragment.class.getName().equals(fragmentName);
195 * This fragment shows general preferences only. It is used when the
196 * activity is showing a two-pane settings UI.
198 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
200 class InterfacePreferenceFragment extends PreferenceFragment {
203 void onCreate(Bundle savedInstanceState) {
204 super.onCreate(savedInstanceState);
205 addPreferencesFromResource(R.xml.pref_interface);
206 setHasOptionsMenu(true);
208 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
209 // to their values. When their values change, their summaries are
210 // updated to reflect the new value, per the Android Design
217 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 notification preferences only. It is used when the
229 * activity is showing a two-pane settings UI.
231 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
232 public static class NotificationPreferenceFragment extends PreferenceFragment {
234 public void onCreate(Bundle savedInstanceState) {
235 super.onCreate(savedInstanceState);
236 addPreferencesFromResource(R.xml.pref_notification);
237 setHasOptionsMenu(true);
239 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
240 // to their values. When their values change, their summaries are
241 // updated to reflect the new value, per the Android Design
243 bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
247 public boolean onOptionsItemSelected(MenuItem item) {
248 int id = item.getItemId();
249 if (id == android.R.id.home) {
250 startActivity(new Intent(getActivity(), SettingsActivity.class));
253 return super.onOptionsItemSelected(item);
258 * This fragment shows data and sync preferences only. It is used when the
259 * activity is showing a two-pane settings UI.
261 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
262 public static class DataSyncPreferenceFragment extends PreferenceFragment {
264 public void onCreate(Bundle savedInstanceState) {
265 super.onCreate(savedInstanceState);
266 addPreferencesFromResource(R.xml.pref_data_sync);
267 setHasOptionsMenu(true);
269 // Bind the summaries of EditText/List/Dialog/Ringtone preferences
270 // to their values. When their values change, their summaries are
271 // updated to reflect the new value, per the Android Design
273 bindPreferenceSummaryToValue(findPreference("sync_frequency"));
277 public boolean onOptionsItemSelected(MenuItem item) {
278 int id = item.getItemId();
279 if (id == android.R.id.home) {
280 startActivity(new Intent(getActivity(), SettingsActivity.class));
283 return super.onOptionsItemSelected(item);