Guest User

SettingsActivity.class

a guest
Feb 23rd, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.69 KB | None | 0 0
  1. package com.shreeasishkumar.settings;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.content.res.Configuration;
  6. import android.media.Ringtone;
  7. import android.media.RingtoneManager;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.preference.ListPreference;
  12. import android.preference.Preference;
  13. import android.preference.PreferenceActivity;
  14. import android.preference.PreferenceCategory;
  15. import android.preference.PreferenceFragment;
  16. import android.preference.PreferenceManager;
  17. import android.preference.RingtonePreference;
  18. import android.text.TextUtils;
  19.  
  20.  
  21. import java.util.List;
  22.  
  23. /**
  24.  * A {@link PreferenceActivity} that presents a set of application settings. On
  25.  * handset devices, settings are presented as a single list. On tablets,
  26.  * settings are split by category, with category headers shown to the left of
  27.  * the list of settings.
  28.  * <p/>
  29.  * See <a href="http://developer.android.com/design/patterns/settings.html">
  30.  * Android Design: Settings</a> for design guidelines and the <a
  31.  * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
  32.  * API Guide</a> for more information on developing a Settings UI.
  33.  */
  34. public class SettingsActivity extends PreferenceActivity {
  35.     /**
  36.      * Determines whether to always show the simplified settings UI, where
  37.      * settings are presented in a single list. When false, settings are shown
  38.      * as a master/detail two-pane view on tablets. When true, a single pane is
  39.      * shown on tablets.
  40.      */
  41.     private static final boolean ALWAYS_SIMPLE_PREFS = false;
  42.  
  43.  
  44.     @Override
  45.     protected void onPostCreate(Bundle savedInstanceState) {
  46.         super.onPostCreate(savedInstanceState);
  47.  
  48.         setupSimplePreferencesScreen();
  49.     }
  50.  
  51.     /**
  52.      * Shows the simplified settings UI if the device configuration if the
  53.      * device configuration dictates that a simplified, single-pane UI should be
  54.      * shown.
  55.      */
  56.     private void setupSimplePreferencesScreen() {
  57.         if (!isSimplePreferences(this)) {
  58.             return;
  59.         }
  60.  
  61.         // In the simplified UI, fragments are not used at all and we instead
  62.         // use the older PreferenceActivity APIs.
  63.  
  64.         // Add 'general' preferences.
  65.         addPreferencesFromResource(R.xml.pref_general);
  66.  
  67.         // Add 'notifications' preferences, and a corresponding header.
  68.         PreferenceCategory fakeHeader = new PreferenceCategory(this);
  69.         fakeHeader.setTitle(R.string.pref_header_notifications);
  70.         getPreferenceScreen().addPreference(fakeHeader);
  71.         addPreferencesFromResource(R.xml.pref_notification);
  72.  
  73.         // Add 'data and sync' preferences, and a corresponding header.
  74.         fakeHeader = new PreferenceCategory(this);
  75.         fakeHeader.setTitle(R.string.pref_header_data_sync);
  76.         getPreferenceScreen().addPreference(fakeHeader);
  77.         addPreferencesFromResource(R.xml.pref_data_sync);
  78.  
  79.         // Bind the summaries of EditText/List/Dialog/Ringtone preferences to
  80.         // their values. When their values change, their summaries are updated
  81.         // to reflect the new value, per the Android Design guidelines.
  82.         bindPreferenceSummaryToValue(findPreference("example_text"));
  83.         bindPreferenceSummaryToValue(findPreference("example_list"));
  84.         bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
  85.         bindPreferenceSummaryToValue(findPreference("sync_frequency"));
  86.     }
  87.  
  88.     /**
  89.      * {@inheritDoc}
  90.      */
  91.     @Override
  92.     public boolean onIsMultiPane() {
  93.         return isXLargeTablet(this) && !isSimplePreferences(this);
  94.     }
  95.  
  96.     /**
  97.      * Helper method to determine if the device has an extra-large screen. For
  98.      * example, 10" tablets are extra-large.
  99.      */
  100.     private static boolean isXLargeTablet(Context context) {
  101.         return (context.getResources().getConfiguration().screenLayout
  102.                 & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
  103.     }
  104.  
  105.     /**
  106.      * Determines whether the simplified settings UI should be shown. This is
  107.      * true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
  108.      * doesn't have newer APIs like {@link PreferenceFragment}, or the device
  109.      * doesn't have an extra-large screen. In these cases, a single-pane
  110.      * "simplified" settings UI should be shown.
  111.      */
  112.     private static boolean isSimplePreferences(Context context) {
  113.         return ALWAYS_SIMPLE_PREFS
  114.                 || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
  115.                 || !isXLargeTablet(context);
  116.     }
  117.  
  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     @Override
  122.     public void onBuildHeaders(List<Header> target) {
  123.         if (!isSimplePreferences(this)) {
  124.             loadHeadersFromResource(R.xml.pref_headers, target);
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * A preference value change listener that updates the preference's summary
  130.      * to reflect its new value.
  131.      */
  132.     private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
  133.         @Override
  134.         public boolean onPreferenceChange(Preference preference, Object value) {
  135.             String stringValue = value.toString();
  136.  
  137.             if (preference instanceof ListPreference) {
  138.                 // For list preferences, look up the correct display value in
  139.                 // the preference's 'entries' list.
  140.                 ListPreference listPreference = (ListPreference) preference;
  141.                 int index = listPreference.findIndexOfValue(stringValue);
  142.  
  143.                 // Set the summary to reflect the new value.
  144.                 preference.setSummary(
  145.                         index >= 0
  146.                                 ? listPreference.getEntries()[index]
  147.                                 : null);
  148.  
  149.             } else if (preference instanceof RingtonePreference) {
  150.                 // For ringtone preferences, look up the correct display value
  151.                 // using RingtoneManager.
  152.                 if (TextUtils.isEmpty(stringValue)) {
  153.                     // Empty values correspond to 'silent' (no ringtone).
  154.                     preference.setSummary(R.string.pref_ringtone_silent);
  155.  
  156.                 } else {
  157.                     Ringtone ringtone = RingtoneManager.getRingtone(
  158.                             preference.getContext(), Uri.parse(stringValue));
  159.  
  160.                     if (ringtone == null) {
  161.                         // Clear the summary if there was a lookup error.
  162.                         preference.setSummary(null);
  163.                     } else {
  164.                         // Set the summary to reflect the new ringtone display
  165.                         // name.
  166.                         String name = ringtone.getTitle(preference.getContext());
  167.                         preference.setSummary(name);
  168.                     }
  169.                 }
  170.  
  171.             } else {
  172.                 // For all other preferences, set the summary to the value's
  173.                 // simple string representation.
  174.                 preference.setSummary(stringValue);
  175.             }
  176.             return true;
  177.         }
  178.     };
  179.  
  180.     /**
  181.      * Binds a preference's summary to its value. More specifically, when the
  182.      * preference's value is changed, its summary (line of text below the
  183.      * preference title) is updated to reflect the value. The summary is also
  184.      * immediately updated upon calling this method. The exact display format is
  185.      * dependent on the type of preference.
  186.      *
  187.      * @see #sBindPreferenceSummaryToValueListener
  188.      */
  189.     private static void bindPreferenceSummaryToValue(Preference preference) {
  190.         // Set the listener to watch for value changes.
  191.         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
  192.  
  193.         // Trigger the listener immediately with the preference's
  194.         // current value.
  195.         sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
  196.                 PreferenceManager
  197.                         .getDefaultSharedPreferences(preference.getContext())
  198.                         .getString(preference.getKey(), ""));
  199.     }
  200.  
  201.     /**
  202.      * This fragment shows general preferences only. It is used when the
  203.      * activity is showing a two-pane settings UI.
  204.      */
  205.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  206.     public static class GeneralPreferenceFragment extends PreferenceFragment {
  207.         @Override
  208.         public void onCreate(Bundle savedInstanceState) {
  209.             super.onCreate(savedInstanceState);
  210.             addPreferencesFromResource(R.xml.pref_general);
  211.  
  212.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  213.             // to their values. When their values change, their summaries are
  214.             // updated to reflect the new value, per the Android Design
  215.             // guidelines.
  216.             bindPreferenceSummaryToValue(findPreference("example_text"));
  217.             bindPreferenceSummaryToValue(findPreference("example_list"));
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * This fragment shows notification preferences only. It is used when the
  223.      * activity is showing a two-pane settings UI.
  224.      */
  225.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  226.     public static class NotificationPreferenceFragment extends PreferenceFragment {
  227.         @Override
  228.         public void onCreate(Bundle savedInstanceState) {
  229.             super.onCreate(savedInstanceState);
  230.             addPreferencesFromResource(R.xml.pref_notification);
  231.  
  232.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  233.             // to their values. When their values change, their summaries are
  234.             // updated to reflect the new value, per the Android Design
  235.             // guidelines.
  236.             bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
  237.         }
  238.     }
  239.  
  240.     /**
  241.      * This fragment shows data and sync preferences only. It is used when the
  242.      * activity is showing a two-pane settings UI.
  243.      */
  244.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  245.     public static class DataSyncPreferenceFragment extends PreferenceFragment {
  246.         @Override
  247.         public void onCreate(Bundle savedInstanceState) {
  248.             super.onCreate(savedInstanceState);
  249.             addPreferencesFromResource(R.xml.pref_data_sync);
  250.  
  251.             // Bind the summaries of EditText/List/Dialog/Ringtone preferences
  252.             // to their values. When their values change, their summaries are
  253.             // updated to reflect the new value, per the Android Design
  254.             // guidelines.
  255.             bindPreferenceSummaryToValue(findPreference("sync_frequency"));
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment