summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Dongwon Kang <dwkang@google.com> 2018-10-17 17:50:33 -0700
committer Dongwon Kang <dwkang@google.com> 2018-10-18 09:41:14 -0700
commit2bb45aa869ee18ffde8a67acb7eb9aadc18eb4dc (patch)
treee81b3cbd27c1443482bac1f68802862058626672
parent16858d6ae0345344bffd15c8c2de32225e6bb02a (diff)
MediaPlayer2: add fallback logic for ringtone default actual uri.
Also, private API, getAuthorityWithoutUserId(), usage is replaced with RingtoneManager.getDefaultType(). The code is using authority to check if the uri is ringtone default uri, but it can be also checked with getDefaultType() since it returns -1 for non ringtone default uri. Test: MediaPlayer2Test Bug: 112767549 Change-Id: I4c41314364e8bc398ffe75cb0ecc20a06a4502c8
-rw-r--r--media/java/android/media/MediaPlayer2Impl.java70
1 files changed, 39 insertions, 31 deletions
diff --git a/media/java/android/media/MediaPlayer2Impl.java b/media/java/android/media/MediaPlayer2Impl.java
index a0da33014b03..71df7dce5ea2 100644
--- a/media/java/android/media/MediaPlayer2Impl.java
+++ b/media/java/android/media/MediaPlayer2Impl.java
@@ -738,59 +738,67 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
@Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies,
long startPos, long endPos)
throws IOException {
- // The context and URI usually belong to the calling user. Get a resolver for that user
- // and strip out the userId from the URI if present.
+ // The context and URI usually belong to the calling user. Get a resolver for that user.
final ContentResolver resolver = context.getContentResolver();
final String scheme = uri.getScheme();
- final String authority = ContentProvider.getAuthorityWithoutUserId(uri.getAuthority());
if (ContentResolver.SCHEME_FILE.equals(scheme)) {
handleDataSource(isCurrent, srcId, uri.getPath(), null, null, startPos, endPos);
return;
}
- AssetFileDescriptor afd = null;
+ final int ringToneType = RingtoneManager.getDefaultType(uri);
try {
+ AssetFileDescriptor afd;
// Try requested Uri locally first
- if (ContentResolver.SCHEME_CONTENT.equals(scheme)
- && Settings.AUTHORITY.equals(authority)) {
+ if (ContentResolver.SCHEME_CONTENT.equals(scheme) && ringToneType != -1) {
afd = RingtoneManager.openDefaultRingtoneUri(context, uri);
+ if (attemptDataSource(isCurrent, srcId, afd, startPos, endPos)) {
+ return;
+ }
+ final Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(
+ context, ringToneType);
+ afd = resolver.openAssetFileDescriptor(actualUri, "r");
} else {
afd = resolver.openAssetFileDescriptor(uri, "r");
}
- if (afd != null) {
- handleDataSource(isCurrent, srcId, afd, startPos, endPos);
+ if (attemptDataSource(isCurrent, srcId, afd, startPos, endPos)) {
return;
}
} catch (NullPointerException | SecurityException | IOException ex) {
Log.w(TAG, "Couldn't open " + uri + ": " + ex);
// Fallback to media server
- } finally {
- if (afd != null) {
- afd.close();
- }
}
handleDataSource(isCurrent, srcId, uri.toString(), headers, cookies, startPos, endPos);
}
- private void handleDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd,
- long startPos, long endPos)
- throws IOException {
- if (afd.getDeclaredLength() < 0) {
- handleDataSource(isCurrent,
- srcId,
- afd.getFileDescriptor(),
- 0,
- DataSourceDesc.LONG_MAX,
- startPos,
- endPos);
- } else {
- handleDataSource(isCurrent,
- srcId,
- afd.getFileDescriptor(),
- afd.getStartOffset(),
- afd.getDeclaredLength(),
- startPos,
- endPos);
+ private boolean attemptDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd,
+ long startPos, long endPos) throws IOException {
+ try {
+ if (afd.getDeclaredLength() < 0) {
+ handleDataSource(isCurrent,
+ srcId,
+ afd.getFileDescriptor(),
+ 0,
+ DataSourceDesc.LONG_MAX,
+ startPos,
+ endPos);
+ } else {
+ handleDataSource(isCurrent,
+ srcId,
+ afd.getFileDescriptor(),
+ afd.getStartOffset(),
+ afd.getDeclaredLength(),
+ startPos,
+ endPos);
+ }
+ return true;
+ } catch (NullPointerException | SecurityException | IOException ex) {
+ Log.w(TAG, "Couldn't open srcId:" + srcId + ": " + ex);
+ return false;
+ } finally {
+ if (afd != null) {
+ afd.close();
+ }
}
}