Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2bb45aa8 authored by Dongwon Kang's avatar Dongwon Kang
Browse files

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
parent 16858d6a
Loading
Loading
Loading
Loading
+39 −31
Original line number Diff line number Diff line
@@ -738,43 +738,42 @@ 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 {
    private boolean attemptDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd,
            long startPos, long endPos) throws IOException {
        try {
            if (afd.getDeclaredLength() < 0) {
                handleDataSource(isCurrent,
                        srcId,
@@ -792,6 +791,15 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                        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();
            }
        }
    }

    private void handleDataSource(