package org.thoughtcrime.securesms.preferences; import android.content.Context; import android.content.SharedPreferences; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceManager; import android.preference.RingtonePreference; import android.text.TextUtils; import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.util.TextSecurePreferences; public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment { @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); addPreferencesFromResource(R.xml.preferences_notifications); this.findPreference(TextSecurePreferences.LED_COLOR_PREF) .setOnPreferenceChangeListener(new ListSummaryListener()); this.findPreference(TextSecurePreferences.LED_BLINK_PREF) .setOnPreferenceChangeListener(new ListSummaryListener()); this.findPreference(TextSecurePreferences.RINGTONE_PREF) .setOnPreferenceChangeListener(new RingtoneSummaryListener()); this.findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF) .setOnPreferenceChangeListener(new ListSummaryListener()); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_COLOR_PREF)); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_BLINK_PREF)); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF)); initializeRingtoneSummary((RingtonePreference) findPreference(TextSecurePreferences.RINGTONE_PREF)); } @Override public void onResume() { super.onResume(); ((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.preferences__notifications); } private class RingtoneSummaryListener implements Preference.OnPreferenceChangeListener { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String value = (String) newValue; if (TextUtils.isEmpty(value)) { preference.setSummary(R.string.preferences__default); } else { Ringtone tone = RingtoneManager.getRingtone(getActivity(), Uri.parse(value)); if (tone != null) { preference.setSummary(tone.getTitle(getActivity())); } } return true; } } private void initializeRingtoneSummary(RingtonePreference pref) { RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); listener.onPreferenceChange(pref, sharedPreferences.getString(pref.getKey(), "")); } public static CharSequence getSummary(Context context) { final int onCapsResId = R.string.ApplicationPreferencesActivity_On; final int offCapsResId = R.string.ApplicationPreferencesActivity_Off; return context.getString(TextSecurePreferences.isNotificationsEnabled(context) ? onCapsResId : offCapsResId); } }