]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/SettingsActivity.java
3d50a606c8af2af2f7fbd8c310976a9b6486bafc
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / SettingsActivity.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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  * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
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 net.ktnx.mobileledger.R;
41
42 import java.util.List;
43
44 /**
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.
49  * <p>
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.
54  */
55 public class SettingsActivity extends AppCompatPreferenceActivity {
56     public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts";
57
58     /**
59      * A preference value change listener that updates the preference's summary
60      * to reflect its new value.
61      */
62     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
63         String stringValue = value.toString();
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                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
190                 || NotificationPreferenceFragment.class.getName().equals(fragmentName)
191                 || InterfacePreferenceFragment.class.getName().equals(fragmentName);
192     }
193
194     /**
195      * This fragment shows general preferences only. It is used when the
196      * activity is showing a two-pane settings UI.
197      */
198     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
199     public static
200     class InterfacePreferenceFragment extends PreferenceFragment {
201         @Override
202         public
203         void onCreate(Bundle savedInstanceState) {
204             super.onCreate(savedInstanceState);
205             addPreferencesFromResource(R.xml.pref_interface);
206             setHasOptionsMenu(true);
207
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
211             // guidelines.
212
213         }
214
215         @Override
216         public
217         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 notification 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 class NotificationPreferenceFragment extends PreferenceFragment {
233         @Override
234         public void onCreate(Bundle savedInstanceState) {
235             super.onCreate(savedInstanceState);
236             addPreferencesFromResource(R.xml.pref_notification);
237             setHasOptionsMenu(true);
238
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
242             // guidelines.
243             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
244         }
245
246         @Override
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));
251                 return true;
252             }
253             return super.onOptionsItemSelected(item);
254         }
255     }
256
257     /**
258      * This fragment shows data and sync preferences only. It is used when the
259      * activity is showing a two-pane settings UI.
260      */
261     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
262     public static class DataSyncPreferenceFragment extends PreferenceFragment {
263         @Override
264         public void onCreate(Bundle savedInstanceState) {
265             super.onCreate(savedInstanceState);
266             addPreferencesFromResource(R.xml.pref_data_sync);
267             setHasOptionsMenu(true);
268
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
272             // guidelines.
273             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
274         }
275
276         @Override
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));
281                 return true;
282             }
283             return super.onOptionsItemSelected(item);
284         }
285     }
286 }