WAI
Status Update
Comments
ni...@google.com <ni...@google.com> #2
Supporting RingtonePreference would have required porting over some awkward plumbing to route the activity result back to the preference object, and it made more sense to just leave it in the hands of the app developer to handle this by overriding onPreferenceTreeClick and then launching the ringtone activity themselves (see RingtoneManager.ACTION_RINGTONE_PICKER).
ca...@gmail.com <ca...@gmail.com> #3
That would be nice to point out in the API docs. Here's how I implemented the workaround described:
In you preferences XML resource, change RingtonePreference to Preference. Then, in your implementation of PreferenceFragment, add:
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey().equals(KEY_RINGTONE_PREFERENCE)) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
String existingValue = getRingtonePreferenceValue(); // TODO
if (existingValue != null) {
if (existingValue.length() == 0) {
// Select "Silent"
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue));
}
} else {
// No ringtone has been selected, set to the default
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
}
startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE);
return true;
} else {
return super.onPreferenceTreeClick(preference);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) {
Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (ringtone != null) {
setRingtonPreferenceValue(ringtone.toString()); // TODO
} else {
// "Silent" was selected
setRingtonPreferenceValue(""); // TODO
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
In you preferences XML resource, change RingtonePreference to Preference. Then, in your implementation of PreferenceFragment, add:
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey().equals(KEY_RINGTONE_PREFERENCE)) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
String existingValue = getRingtonePreferenceValue(); // TODO
if (existingValue != null) {
if (existingValue.length() == 0) {
// Select "Silent"
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue));
}
} else {
// No ringtone has been selected, set to the default
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
}
startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE);
return true;
} else {
return super.onPreferenceTreeClick(preference);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_ALERT_RINGTONE && data != null) {
Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (ringtone != null) {
setRingtonPreferenceValue(ringtone.toString()); // TODO
} else {
// "Silent" was selected
setRingtonPreferenceValue(""); // TODO
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Description
Version used: 23
Theme used: N/A
Devices/Android versions reproduced on: N/A
There's almost full Preference support, but it's missing RingtonePreference, which is commonly used for notification sound pickers.