From 3b25df9ad3f44f30ebdbfd84143ddb78c90f8db1 Mon Sep 17 00:00:00 2001 From: "wan.li" Date: Thu, 6 Aug 2020 14:32:27 +0800 Subject: [PATCH] Ringtone uri should add type query When only DisplayName is used to query the default ringtone, the query may be incorrect, because other types of ringtones may contain resources with the same name. It should not happen that other types of ringtone uri are set as the default ringtone of the current type by mistake. Bug: 168431815 Change-Id: Ic37f879a658379179dc53715fa016631bb7309d9 --- media/java/android/media/RingtoneManager.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/media/java/android/media/RingtoneManager.java b/media/java/android/media/RingtoneManager.java index 8deb0c4451eac..20041a8cfc18b 100644 --- a/media/java/android/media/RingtoneManager.java +++ b/media/java/android/media/RingtoneManager.java @@ -1129,12 +1129,14 @@ public class RingtoneManager { } // Try finding the scanned ringtone + final String whichAudio = getQueryStringForType(type); final String filename = getDefaultRingtoneFilename(type); + final String where = MediaColumns.DISPLAY_NAME + "=?," + whichAudio + "=?"; final Uri baseUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; try (Cursor cursor = context.getContentResolver().query(baseUri, new String[] { MediaColumns._ID }, - MediaColumns.DISPLAY_NAME + "=?", - new String[] { filename }, null)) { + where, + new String[] { filename , "1"}, null)) { if (cursor.moveToFirst()) { final Uri ringtoneUri = context.getContentResolver().canonicalizeOrElse( ContentUris.withAppendedId(baseUri, cursor.getLong(0))); @@ -1162,4 +1164,13 @@ public class RingtoneManager { default: throw new IllegalArgumentException(); } } + + private static String getQueryStringForType(int type) { + switch (type) { + case TYPE_RINGTONE: return MediaStore.Audio.AudioColumns.IS_RINGTONE; + case TYPE_NOTIFICATION: return MediaStore.Audio.AudioColumns.IS_NOTIFICATION; + case TYPE_ALARM: return MediaStore.Audio.AudioColumns.IS_ALARM; + default: throw new IllegalArgumentException(); + } + } }