Skip to content

Commit

Permalink
getDefaultRingtone
Browse files Browse the repository at this point in the history
fixes NPE when default alarm ringtone is unset; RingtoneManager.getActualDefaultRingtoneUri may return null (#634)
  • Loading branch information
forrestguice committed Oct 21, 2022
1 parent 765ec6f commit 5d5cc54
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static Long loadUpcomingAlarmId(Context context)
///////////////////////////////////////////////////////////////////////////////////////////////

@NonNull
public static String getRingtoneName(Context context, Uri ringtoneUri)
public static String getRingtoneName(Context context, @Nullable Uri ringtoneUri)
{
String ringtoneName = "";
Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri); // TODO: getRingtone takes up to 100ms!
Expand Down Expand Up @@ -290,9 +290,14 @@ public static Uri getDefaultRingtoneUri(Context context, AlarmClockItem.AlarmTyp
public static String getDefaultRingtoneName(Context context, AlarmClockItem.AlarmType type)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString((type == AlarmClockItem.AlarmType.ALARM) ? PREF_KEY_ALARM_RINGTONE_NAME_ALARM : PREF_KEY_ALARM_RINGTONE_NAME_NOTIFICATION, context.getString(R.string.configLabel_tagDefault));
String name = prefs.getString((type == AlarmClockItem.AlarmType.ALARM) ? PREF_KEY_ALARM_RINGTONE_NAME_ALARM : PREF_KEY_ALARM_RINGTONE_NAME_NOTIFICATION, context.getString(R.string.configLabel_tagDefault));
return (name != null) ? name : context.getString(R.string.configLabel_tagDefault);
}

/**
* Caches the default ringtone uri.
* @return the default uri (or VALUE_RINGTONE_DEFAULT if not set)
*/
public static Uri setDefaultRingtone(Context context, AlarmClockItem.AlarmType type)
{
Uri uri;
Expand All @@ -313,13 +318,12 @@ public static Uri setDefaultRingtone(Context context, AlarmClockItem.AlarmType t
}

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
prefs.putString(key_uri, uri.toString());
prefs.putString(key_name, getRingtoneName(context, uri));
prefs.putString(key_uri, (uri != null) ? uri.toString() : VALUE_RINGTONE_DEFAULT);
prefs.putString(key_name, (uri != null) ? getRingtoneName(context, uri) : null);
prefs.apply();
return uri;
return (uri != null) ? uri : Uri.parse(VALUE_RINGTONE_DEFAULT);
}


public static void setDefaultRingtoneUris(Context context)
{
CacheDefaultRingtoneTask task = new CacheDefaultRingtoneTask(context);
Expand Down

0 comments on commit 5d5cc54

Please sign in to comment.