packageorg.marcinzelent.liberavem;importandroid.annotation.TargetApi;importandroid.content.Context;importandroid.content.Intent;importandroid.content.res.Configuration;importandroid.media.Ringtone;importandroid.media.RingtoneManager;importandroid.net.Uri;importandroid.os.Build;importandroid.os.Bundle;importandroid.preference.ListPreference;importandroid.preference.Preference;importandroid.preference.PreferenceActivity;importandroid.support.v7.app.ActionBar;importandroid.preference.PreferenceFragment;importandroid.preference.PreferenceManager;importandroid.preference.RingtonePreference;importandroid.text.TextUtils;importandroid.view.MenuItem;importjava.util.List;/** * A {@link PreferenceActivity} that presents a set of application settings. On * handset devices, settings are presented as a single list. On tablets, * settings are split by category, with category headers shown to the left of * the list of settings. * <p> * See <a href="http://developer.android.com/design/patterns/settings.html"> * Android Design: Settings</a> for design guidelines and the <a * href="http://developer.android.com/guide/topics/ui/settings.html">Settings * API Guide</a> for more information on developing a Settings UI. */publicclassSettingsActivityextendsAppCompatPreferenceActivity{/** * A preference value change listener that updates the preference's summary * to reflect its new value. */privatestaticPreference.OnPreferenceChangeListenersBindPreferenceSummaryToValueListener=newPreference.OnPreferenceChangeListener(){@OverridepublicbooleanonPreferenceChange(Preferencepreference,Objectvalue){StringstringValue=value.toString();if(preferenceinstanceofListPreference){// For list preferences, look up the correct display value in// the preference's 'entries' list.ListPreferencelistPreference=(ListPreference)preference;intindex=listPreference.findIndexOfValue(stringValue);// Set the summary to reflect the new value.preference.setSummary(index>=0?listPreference.getEntries()[index]:null);}elseif(preferenceinstanceofRingtonePreference){// For ringtone preferences, look up the correct display value// using RingtoneManager.if(TextUtils.isEmpty(stringValue)){// Empty values correspond to 'silent' (no ringtone).preference.setSummary(R.string.pref_ringtone_silent);}else{Ringtoneringtone=RingtoneManager.getRingtone(preference.getContext(),Uri.parse(stringValue));if(ringtone==null){// Clear the summary if there was a lookup error.preference.setSummary(null);}else{// Set the summary to reflect the new ringtone display// name.Stringname=ringtone.getTitle(preference.getContext());preference.setSummary(name);}}}else{// For all other preferences, set the summary to the value's// simple string representation.preference.setSummary(stringValue);}returntrue;}};/** * Helper method to determine if the device has an extra-large screen. For * example, 10" tablets are extra-large. */privatestaticbooleanisXLargeTablet(Contextcontext){return(context.getResources().getConfiguration().screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)>=Configuration.SCREENLAYOUT_SIZE_XLARGE;}/** * Binds a preference's summary to its value. More specifically, when the * preference's value is changed, its summary (line of text below the * preference title) is updated to reflect the value. The summary is also * immediately updated upon calling this method. The exact display format is * dependent on the type of preference. * * @see #sBindPreferenceSummaryToValueListener */privatestaticvoidbindPreferenceSummaryToValue(Preferencepreference){// Set the listener to watch for value changes.preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);// Trigger the listener immediately with the preference's// current value.sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(),""));}@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setupActionBar();}/** * Set up the {@link android.app.ActionBar}, if the API is available. */privatevoidsetupActionBar(){ActionBaractionBar=getSupportActionBar();if(actionBar!=null){// Show the Up button in the action bar.actionBar.setDisplayHomeAsUpEnabled(true);}}/** * {@inheritDoc} */@OverridepublicbooleanonIsMultiPane(){returnisXLargeTablet(this);}/** * {@inheritDoc} */@Override@TargetApi(Build.VERSION_CODES.HONEYCOMB)publicvoidonBuildHeaders(List<Header>target){loadHeadersFromResource(R.xml.pref_headers,target);}/** * This method stops fragment injection in malicious applications. * Make sure to deny any unknown fragments here. */protectedbooleanisValidFragment(StringfragmentName){returnPreferenceFragment.class.getName().equals(fragmentName)||GeneralPreferenceFragment.class.getName().equals(fragmentName)||DataSyncPreferenceFragment.class.getName().equals(fragmentName)||NotificationPreferenceFragment.class.getName().equals(fragmentName);}/** * This fragment shows general preferences only. It is used when the * activity is showing a two-pane settings UI. */@TargetApi(Build.VERSION_CODES.HONEYCOMB)publicstaticclassGeneralPreferenceFragmentextendsPreferenceFragment{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.pref_general);setHasOptionsMenu(true);// Bind the summaries of EditText/List/Dialog/Ringtone preferences// to their values. When their values change, their summaries are// updated to reflect the new value, per the Android Design// guidelines.bindPreferenceSummaryToValue(findPreference("example_text"));bindPreferenceSummaryToValue(findPreference("example_list"));}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==android.R.id.home){startActivity(newIntent(getActivity(),SettingsActivity.class));returntrue;}returnsuper.onOptionsItemSelected(item);}}/** * This fragment shows notification preferences only. It is used when the * activity is showing a two-pane settings UI. */@TargetApi(Build.VERSION_CODES.HONEYCOMB)publicstaticclassNotificationPreferenceFragmentextendsPreferenceFragment{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.pref_notification);setHasOptionsMenu(true);// Bind the summaries of EditText/List/Dialog/Ringtone preferences// to their values. When their values change, their summaries are// updated to reflect the new value, per the Android Design// guidelines.bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==android.R.id.home){startActivity(newIntent(getActivity(),SettingsActivity.class));returntrue;}returnsuper.onOptionsItemSelected(item);}}/** * This fragment shows data and sync preferences only. It is used when the * activity is showing a two-pane settings UI. */@TargetApi(Build.VERSION_CODES.HONEYCOMB)publicstaticclassDataSyncPreferenceFragmentextendsPreferenceFragment{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.pref_data_sync);setHasOptionsMenu(true);// Bind the summaries of EditText/List/Dialog/Ringtone preferences// to their values. When their values change, their summaries are// updated to reflect the new value, per the Android Design// guidelines.bindPreferenceSummaryToValue(findPreference("sync_frequency"));}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==android.R.id.home){startActivity(newIntent(getActivity(),SettingsActivity.class));returntrue;}returnsuper.onOptionsItemSelected(item);}}}