Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / SettingsActivity.java
1 package de.dhbwloe.campusapp;
2
3
4 import android.annotation.TargetApi;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.res.Configuration;
8 import android.media.Ringtone;
9 import android.media.RingtoneManager;
10 import android.net.Uri;
11 import android.os.Build;
12 import android.os.Bundle;
13 import android.preference.ListPreference;
14 import android.preference.Preference;
15 import android.preference.PreferenceActivity;
16 import android.support.v7.app.ActionBar;
17 import android.preference.PreferenceFragment;
18 import android.preference.PreferenceManager;
19 import android.preference.RingtonePreference;
20 import android.text.TextUtils;
21 import android.view.MenuItem;
22 import android.support.v4.app.NavUtils;
23
24 import java.util.List;
25
26 /**
27  * A {@link PreferenceActivity} that presents a set of application settings. On
28  * handset devices, settings are presented as a single list. On tablets,
29  * settings are split by category, with category headers shown to the left of
30  * the list of settings.
31  * <p/>
32  * See <a href="http://developer.android.com/design/patterns/settings.html">
33  * Android Design: Settings</a> for design guidelines and the <a
34  * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
35  * API Guide</a> for more information on developing a Settings UI.
36  */
37 public class SettingsActivity extends AppCompatPreferenceActivity {
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 = new Preference.OnPreferenceChangeListener() {
43         @Override
44         public boolean onPreferenceChange(Preference preference, Object value) {
45             String stringValue = value.toString();
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
81             } else {
82                 // For all other preferences, set the summary to the value's
83                 // simple string representation.
84                 preference.setSummary(stringValue);
85             }
86             return true;
87         }
88     };
89
90     /**
91      * Helper method to determine if the device has an extra-large screen. For
92      * example, 10" tablets are extra-large.
93      */
94     private static boolean isXLargeTablet(Context context) {
95         return (context.getResources().getConfiguration().screenLayout
96                 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
97     }
98
99     /**
100      * Binds a preference's summary to its value. More specifically, when the
101      * preference's value is changed, its summary (line of text below the
102      * preference title) is updated to reflect the value. The summary is also
103      * immediately updated upon calling this method. The exact display format is
104      * dependent on the type of preference.
105      *
106      * @see #sBindPreferenceSummaryToValueListener
107      */
108     private static void bindPreferenceSummaryToValue(Preference preference) {
109         // Set the listener to watch for value changes.
110         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
111
112         // Trigger the listener immediately with the preference's
113         // current value.
114         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
115                 PreferenceManager
116                         .getDefaultSharedPreferences(preference.getContext())
117                         .getString(preference.getKey(), ""));
118     }
119
120     @Override
121     protected void onCreate(Bundle savedInstanceState) {
122         super.onCreate(savedInstanceState);
123         setupActionBar();
124     }
125
126     /**
127      * Set up the {@link android.app.ActionBar}, if the API is available.
128      */
129     private void setupActionBar() {
130         ActionBar actionBar = getSupportActionBar();
131         if (actionBar != null) {
132             // Show the Up button in the action bar.
133             actionBar.setDisplayHomeAsUpEnabled(true);
134         }
135     }
136
137     @Override
138     public boolean onMenuItemSelected(int featureId, MenuItem item) {
139         int id = item.getItemId();
140         if (id == android.R.id.home) {
141             if (!super.onMenuItemSelected(featureId, item)) {
142                 NavUtils.navigateUpFromSameTask(this);
143             }
144             return true;
145         }
146         return super.onMenuItemSelected(featureId, item);
147     }
148
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public boolean onIsMultiPane() {
154         return isXLargeTablet(this);
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
162     public void onBuildHeaders(List<Header> target) {
163         loadHeadersFromResource(R.xml.pref_headers, target);
164     }
165
166     /**
167      * This method stops fragment injection in malicious applications.
168      * Make sure to deny any unknown fragments here.
169      */
170     protected boolean isValidFragment(String fragmentName) {
171         return PreferenceFragment.class.getName().equals(fragmentName)
172                 || GeneralPreferenceFragment.class.getName().equals(fragmentName)
173                 || DataSyncPreferenceFragment.class.getName().equals(fragmentName)
174                 || NotificationPreferenceFragment.class.getName().equals(fragmentName);
175     }
176
177     /**
178      * This fragment shows general preferences only. It is used when the
179      * activity is showing a two-pane settings UI.
180      */
181     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
182     public static class GeneralPreferenceFragment extends PreferenceFragment {
183         @Override
184         public void onCreate(Bundle savedInstanceState) {
185             super.onCreate(savedInstanceState);
186             addPreferencesFromResource(R.xml.pref_general);
187             setHasOptionsMenu(true);
188
189             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
190             // to their values. When their values change, their summaries are
191             // updated to reflect the new value, per the Android Design
192             // guidelines.
193             bindPreferenceSummaryToValue(findPreference("example_text"));
194             bindPreferenceSummaryToValue(findPreference("example_list"));
195         }
196
197         @Override
198         public boolean onOptionsItemSelected(MenuItem item) {
199             int id = item.getItemId();
200             if (id == android.R.id.home) {
201                 startActivity(new Intent(getActivity(), SettingsActivity.class));
202                 return true;
203             }
204             return super.onOptionsItemSelected(item);
205         }
206     }
207
208     /**
209      * This fragment shows notification preferences only. It is used when the
210      * activity is showing a two-pane settings UI.
211      */
212     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
213     public static class NotificationPreferenceFragment extends PreferenceFragment {
214         @Override
215         public void onCreate(Bundle savedInstanceState) {
216             super.onCreate(savedInstanceState);
217             addPreferencesFromResource(R.xml.pref_notification);
218             setHasOptionsMenu(true);
219
220             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
221             // to their values. When their values change, their summaries are
222             // updated to reflect the new value, per the Android Design
223             // guidelines.
224             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
225         }
226
227         @Override
228         public boolean onOptionsItemSelected(MenuItem item) {
229             int id = item.getItemId();
230             if (id == android.R.id.home) {
231                 startActivity(new Intent(getActivity(), SettingsActivity.class));
232                 return true;
233             }
234             return super.onOptionsItemSelected(item);
235         }
236     }
237
238     /**
239      * This fragment shows data and sync preferences only. It is used when the
240      * activity is showing a two-pane settings UI.
241      */
242     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
243     public static class DataSyncPreferenceFragment extends PreferenceFragment {
244         @Override
245         public void onCreate(Bundle savedInstanceState) {
246             super.onCreate(savedInstanceState);
247             addPreferencesFromResource(R.xml.pref_data_sync);
248             setHasOptionsMenu(true);
249
250             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
251             // to their values. When their values change, their summaries are
252             // updated to reflect the new value, per the Android Design
253             // guidelines.
254             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
255         }
256
257         @Override
258         public boolean onOptionsItemSelected(MenuItem item) {
259             int id = item.getItemId();
260             if (id == android.R.id.home) {
261                 startActivity(new Intent(getActivity(), SettingsActivity.class));
262                 return true;
263             }
264             return super.onOptionsItemSelected(item);
265         }
266     }
267 }