Skip to content

Commit

Permalink
setDefaultRingtone
Browse files Browse the repository at this point in the history
fixes potential npe (RingtoneManager.getActualDefaultRingtoneUri may return null) (#634)
  • Loading branch information
forrestguice committed Oct 21, 2022
1 parent c9970f4 commit 17038cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.media.RingtoneManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

Expand Down Expand Up @@ -158,7 +159,16 @@ public Uri getActualDefaultRingtoneUri(Context context, int type) {
};

settings.setDefaultRingtone(context, AlarmClockItem.AlarmType.ALARM);
assertEquals(AlarmSettings.VALUE_RINGTONE_DEFAULT, prefs.getString(AlarmSettings.PREF_KEY_ALARM_RINGTONE_URI_ALARM, "UNSET"));
assertEquals(AlarmSettings.VALUE_RINGTONE_DEFAULT, AlarmSettings.getDefaultRingtoneUri(context, AlarmClockItem.AlarmType.ALARM, false).toString());
assertNull(prefs.getString(AlarmSettings.PREF_KEY_ALARM_RINGTONE_NAME_ALARM, null));
assertEquals(context.getString(R.string.configLabel_tagDefault), AlarmSettings.getDefaultRingtoneName(context, AlarmClockItem.AlarmType.ALARM));

settings.setDefaultRingtone(context, AlarmClockItem.AlarmType.NOTIFICATION);
assertEquals(AlarmSettings.VALUE_RINGTONE_DEFAULT, prefs.getString(AlarmSettings.PREF_KEY_ALARM_RINGTONE_URI_NOTIFICATION, "UNSET"));
assertEquals(AlarmSettings.VALUE_RINGTONE_DEFAULT, AlarmSettings.getDefaultRingtoneUri(context, AlarmClockItem.AlarmType.NOTIFICATION, false).toString());
assertNull(prefs.getString(AlarmSettings.PREF_KEY_ALARM_RINGTONE_NAME_NOTIFICATION, null));
assertEquals(context.getString(R.string.configLabel_tagDefault), AlarmSettings.getDefaultRingtoneName(context, AlarmClockItem.AlarmType.NOTIFICATION));
}

private void clearDefaultRingtone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,19 @@ public Uri setDefaultRingtone(Context context, AlarmClockItem.AlarmType type)
}

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
prefs.putString(key_uri, uri.toString());
prefs.putString(key_name, getRingtoneName(context, uri));
prefs.apply();
return uri;
if (uri != null)
{
prefs.putString(key_uri, uri.toString());
prefs.putString(key_name, getRingtoneName(context, uri));
prefs.apply();
return uri;

} else {
prefs.putString(key_uri, VALUE_RINGTONE_DEFAULT);
prefs.remove(key_name);
prefs.apply();
return Uri.parse(VALUE_RINGTONE_DEFAULT);
}
}

public static void setDefaultRingtoneUris(Context context)
Expand Down

0 comments on commit 17038cc

Please sign in to comment.