From 0def6d1b51e23d539728d4fa43032de8d18c69ad Mon Sep 17 00:00:00 2001 From: jiabin Date: Thu, 28 Oct 2021 00:38:33 +0000 Subject: [PATCH] Add RingtoneManager.hasHapticChannels(Context, Uri). Currently, RingtoneManager.hasHapticChannels(Uri) will use the context from AudioService to resolve the given Uri. However, that may not always work. In that case, add another API to ask apps to provide Context to resolve given Uri can help the API returning correct result. Test: atest RingtoneManagerTest Bug: 196195517 Change-Id: I484e157e0d03e5eeabdd68908b11db534ad3c3ab --- core/api/current.txt | 1 + media/java/android/media/RingtoneManager.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/api/current.txt b/core/api/current.txt index 0f0e928194fd0..1f2861a44c8f4 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -23844,6 +23844,7 @@ package android.media { method public static android.net.Uri getValidRingtoneUri(android.content.Context); method public boolean hasHapticChannels(int); method public static boolean hasHapticChannels(@NonNull android.net.Uri); + method public static boolean hasHapticChannels(@NonNull android.content.Context, @NonNull android.net.Uri); method public int inferStreamType(); method public static boolean isDefault(android.net.Uri); method @Nullable public static android.content.res.AssetFileDescriptor openDefaultRingtoneUri(@NonNull android.content.Context, @NonNull android.net.Uri) throws java.io.FileNotFoundException; diff --git a/media/java/android/media/RingtoneManager.java b/media/java/android/media/RingtoneManager.java index 3e7b8860c8181..ccbfb8ff84b92 100644 --- a/media/java/android/media/RingtoneManager.java +++ b/media/java/android/media/RingtoneManager.java @@ -1090,7 +1090,8 @@ public class RingtoneManager { * haptic channels or not. As this function doesn't has a context * to resolve the uri, the result may be wrong if the uri cannot be * resolved correctly. - * Use {@link #hasHapticChannels(int)} instead when possible. + * Use {@link #hasHapticChannels(int)} or {@link #hasHapticChannels(Context, Uri)} + * instead when possible. * * @param ringtoneUri The {@link Uri} of a sound or ringtone. * @return true if the ringtone contains haptic channels. @@ -1099,6 +1100,17 @@ public class RingtoneManager { return AudioManager.hasHapticChannels(null, ringtoneUri); } + /** + * Returns if the {@link Ringtone} from a given sound URI contains haptics channels or not. + * + * @param context the {@link android.content.Context} to use when resolving the Uri. + * @param ringtoneUri the {@link Uri} of a sound or ringtone. + * @return true if the ringtone contains haptic channels. + */ + public static boolean hasHapticChannels(@NonNull Context context, @NonNull Uri ringtoneUri) { + return AudioManager.hasHapticChannels(context, ringtoneUri); + } + /** * Attempts to create a context for the given user. *