List of usage examples for android.preference RingtonePreference setOnPreferenceChangeListener
public void setOnPreferenceChangeListener(OnPreferenceChangeListener onPreferenceChangeListener)
From source file:com.googlecode.mindbell.MindBellPreferences.java
@SuppressWarnings("deprecation") // deprecation is because MindBell is not fragment-based @Override/* ww w.ja v a 2s .c om*/ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // check settings, delete any settings that are not valid final PrefsAccessor prefs = ContextAccessor.getInstance(this).getPrefs(); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences_1); addPreferencesFromResource(R.xml.preferences_2); // notifications depend on SDK addPreferencesFromResource(R.xml.preferences_3); final CheckBoxPreference preferenceUseAudioStreamVolumeSetting = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyUseAudioStreamVolumeSetting)); final CheckBoxPreference preferenceStatus = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyStatus)); final CheckBoxPreference preferenceShow = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyShow)); final CheckBoxPreference preferenceSound = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keySound)); final Preference preferenceReminderSoundLength = (Preference) getPreferenceScreen() .findPreference(getText(R.string.keyReminderSoundLength)); final ListPreferenceWithSummaryFix preferenceReminderBell = (ListPreferenceWithSummaryFix) getPreferenceScreen() .findPreference(getText(R.string.keyReminderBell)); final MediaVolumePreference preferenceVolume = (MediaVolumePreference) getPreferenceScreen() .findPreference(getText(R.string.keyVolume)); final RingtonePreference preferenceRingtone = (RingtonePreference) getPreferenceScreen() .findPreference(getText(R.string.keyRingtone)); final CheckBoxPreference preferenceVibrate = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyVibrate)); final ListPreferenceWithSummaryFix preferencePattern = (ListPreferenceWithSummaryFix) getPreferenceScreen() .findPreference(getText(R.string.keyPattern)); final CheckBoxPreference preferenceMuteOffHook = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyMuteOffHook)); final MinutesIntervalPickerPreference preferenceFrequency = (MinutesIntervalPickerPreference) getPreferenceScreen() .findPreference(getText(R.string.keyFrequency)); final CheckBoxPreference preferenceRandomize = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyRandomize)); final ListPreferenceWithSummaryFix preferenceNormalize = (ListPreferenceWithSummaryFix) getPreferenceScreen() .findPreference(getText(R.string.keyNormalize)); final MultiSelectListPreferenceWithSummary preferenceActiveOnDaysOfWeek = (MultiSelectListPreferenceWithSummary) getPreferenceScreen() .findPreference(getText(R.string.keyActiveOnDaysOfWeek)); final MediaVolumePreference preferenceMeditationVolume = (MediaVolumePreference) getPreferenceScreen() .findPreference(getText(R.string.keyMeditationVolume)); final CheckBoxPreference preferenceUseWorkaroundBell = (CheckBoxPreference) getPreferenceScreen() .findPreference(getText(R.string.keyUseWorkaroundBell)); final Preference preferenceFAQ = (Preference) getPreferenceScreen() .findPreference(getText(R.string.keyFAQ)); final Preference preferenceBatterySettings = (Preference) getPreferenceScreen() .findPreference(getText(R.string.keyBatterySettings)); final Preference preferenceSendMail = (Preference) getPreferenceScreen() .findPreference(getText(R.string.keySendMail)); preferenceUseAudioStreamVolumeSetting.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { boolean isChecked = (Boolean) newValue; if (!isChecked && prefs.mustUseAudioStreamVolumeSetting()) { Toast.makeText(MindBellPreferences.this, R.string.mustUseAudioStreamSetting, Toast.LENGTH_SHORT) .show(); return false; } else { preferenceVolume.setEnabled(preferenceSound.isChecked() && !isChecked); preferenceMeditationVolume.setEnabled(!isChecked); return true; } } }); preferenceStatus.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { return mediateMuteOffHookAndStatus(preferenceMuteOffHook, newValue, REQUEST_CODE_STATUS); } }); preferenceShow.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { return mediateShowAndSoundAndVibrate(preferenceSound, preferenceVibrate, newValue); } }); preferenceSound.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if (mediateShowAndSoundAndVibrate(preferenceShow, preferenceVibrate, newValue) && mediateSoundDurationRelatedSettings(preferenceFrequency, preferenceUseWorkaroundBell, preferenceReminderBell, preferenceRingtoneValue, newValue)) { boolean isChecked = (Boolean) newValue; preferenceReminderBell.setEnabled(isChecked); preferenceRingtone.setEnabled( isChecked && !PrefsAccessor.isUseStandardBell(preferenceReminderBell.getValue())); preferenceVolume.setEnabled(!preferenceUseAudioStreamVolumeSetting.isChecked() && isChecked); return true; } else { return false; } } }); preferenceReminderSoundLength.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { onPreferenceReminderSoundLength(preferenceUseWorkaroundBell.isChecked(), preferenceReminderBell.getValue(), preferenceRingtoneValue); return true; } }); preferenceReminderBell.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { String reminderBell = (String) newValue; boolean isChecked = PrefsAccessor.isUseStandardBell(reminderBell); if (PrefsAccessor.isUseStandardBell(reminderBell) || ContextCompat.checkSelfPermission(MindBellPreferences.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { if (mediateSoundDurationRelatedSettings(preferenceFrequency, preferenceUseWorkaroundBell, reminderBell, preferenceRingtoneValue, preferenceSound)) { // Allow setting this option to "off" if permission is granted preferenceRingtone.setEnabled(preferenceSound.isChecked() && !isChecked); // Weird, but ringtone cannot be retrieved from RingtonePreference, only from SharedPreference setPreferenceVolumeSoundUri(preferenceVolume, reminderBell, preferenceUseWorkaroundBell.isChecked(), preferenceRingtoneValue); return true; } else { return false; } } else { // Ask for permission if this option shall be set to "off" but permission is missing ActivityCompat.requestPermissions(MindBellPreferences.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, REQUEST_CODE_RINGTONE); // As the permission request is asynchronous we have to deny setting this option (to "off") return false; } } }); preferenceRingtone.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { String newRingtoneValue = (String) newValue; if (validatePreferenceRingtone(newRingtoneValue) && mediateSoundDurationRelatedSettings(preferenceFrequency, preferenceUseWorkaroundBell, preferenceReminderBell, newRingtoneValue, preferenceSound)) { setPreferenceRingtoneSummary(preferenceRingtone, newRingtoneValue); setPreferenceVolumeSoundUri(preferenceVolume, preferenceReminderBell.getValue(), preferenceUseWorkaroundBell.isChecked(), newRingtoneValue); preferenceRingtoneValue = newRingtoneValue; return true; } else { return false; } } }); preferenceVibrate.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { return mediateShowAndSoundAndVibrate(preferenceShow, preferenceSound, newValue); } }); preferencePattern.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { Vibrator vibrator = (Vibrator) MindBellPreferences.this.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(PrefsAccessor.getVibrationPattern((String) newValue), -1); return true; } }); preferenceMuteOffHook.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { return mediateMuteOffHookAndStatus(preferenceStatus, newValue, REQUEST_CODE_MUTE_OFF_HOOK); } }); preferenceRandomize.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if ((Boolean) newValue) { // if interval deviation is selected, normalize is disabled on screen but it must be disabled in preferences, // too. Otherwise the following scenario could happen: set interval 1 h, de-select randomize, set normalize to // hh:00, select randomize, set interval 2 h, de-select randomize again ... hh:00 would be left in normalize // erroneously. preferenceNormalize.setValue(PrefsAccessor.NORMALIZE_NONE); } return true; } }); preferenceFrequency.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if (!mediateSoundDurationRelatedSettings(newValue, preferenceUseWorkaroundBell, preferenceReminderBell, preferenceRingtoneValue, preferenceSound)) { return false; } else if (preferenceRandomize.isChecked()) { // if interval varies randomly, ringing on the minute is disabled and set to "no" anyway return true; } else if (isFrequencyDividesAnHour(new TimeOfDay((String) newValue))) { // if frequency is factor of an hour, ringing on the minute may be requested preferenceNormalize.setEnabled(true); } else { // if frequency is NOT factor of an hour, ringing on the minute may NOT be set if (preferenceNormalize.isEnabled() && isNormalize(preferenceNormalize.getValue())) { Toast.makeText(MindBellPreferences.this, R.string.frequencyDoesNotFitIntoAnHour, Toast.LENGTH_SHORT).show(); return false; } else { preferenceNormalize.setEnabled(false); } } return true; } }); preferenceNormalize.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if (!isNormalize((String) newValue)) { // if normalize - ringing on the minute - is not wanted, it's fine, no more to check here return true; } else if (isFrequencyDividesAnHour(preferenceFrequency.getTime())) { // if frequency is factor of an hour, requesting ringing on the minute is allowed return true; } else { // if frequency is NOT factor of an hour, ringing on the minute may NOT be set Toast.makeText(MindBellPreferences.this, R.string.frequencyDoesNotFitIntoAnHour, Toast.LENGTH_SHORT).show(); return false; } } }); preferenceActiveOnDaysOfWeek.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValues) { if (((Set<?>) newValues).isEmpty()) { Toast.makeText(MindBellPreferences.this, R.string.atLeastOneActiveDayNeeded, Toast.LENGTH_SHORT) .show(); return false; } return true; } }); preferenceFAQ.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Uri faqUri = Uri.parse(getText(R.string.faq_url).toString()); Intent browserIntent = new Intent(Intent.ACTION_VIEW, faqUri); startActivity(browserIntent); return true; } }); preferenceBatterySettings.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { onPreferenceClickBatterySettings(); return true; } }); preferenceUseWorkaroundBell.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if (mediateSoundDurationRelatedSettings(preferenceFrequency, newValue, preferenceReminderBell, preferenceRingtoneValue, preferenceSound)) { boolean isChecked = (Boolean) newValue; setPreferenceVolumeSoundUri(preferenceVolume, preferenceReminderBell.getValue(), isChecked, preferenceRingtoneValue); return true; } else { return false; } } }); preferenceSendMail.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { new AlertDialog.Builder(MindBellPreferences.this) // .setTitle(R.string.prefsSendMail) // .setMessage(R.string.mailInfo1) // .setIcon(R.mipmap.ic_launcher) // .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { onClickReallySendInfo(); } }) // .setNegativeButton(android.R.string.cancel, null) // .show(); return true; } }); // As no PreferenceChangeListener is called without change *BY USER*, some settings have to be made explicitly preferenceVolume .setEnabled(preferenceSound.isChecked() && !preferenceUseAudioStreamVolumeSetting.isChecked()); preferenceMeditationVolume.setEnabled(!preferenceUseAudioStreamVolumeSetting.isChecked()); preferenceReminderBell.setEnabled(preferenceSound.isChecked()); preferenceRingtone.setEnabled( preferenceSound.isChecked() && !PrefsAccessor.isUseStandardBell(preferenceReminderBell.getValue())); preferenceRingtoneValue = prefs.getRingtone(); // cannot be retrieved from preference setPreferenceRingtoneSummary(preferenceRingtone, preferenceRingtoneValue); setPreferenceVolumeSoundUri(preferenceVolume, preferenceReminderBell.getValue(), preferenceUseWorkaroundBell.isChecked(), preferenceRingtoneValue); }