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

Commit edf4d227 authored by Dongwon Kang's avatar Dongwon Kang Committed by Android (Google) Code Review
Browse files

Merge "Add RingtoneManager.openDefaultRingtoneUri()"

parents a618cebc c049bd26
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25173,6 +25173,7 @@ package android.media {
    method public static android.net.Uri getValidRingtoneUri(android.content.Context);
    method public int inferStreamType();
    method public static boolean isDefault(android.net.Uri);
    method public static android.content.res.AssetFileDescriptor openDefaultRingtoneUri(android.content.Context, android.net.Uri) throws java.io.FileNotFoundException;
    method public static void setActualDefaultRingtoneUri(android.content.Context, int, android.net.Uri);
    method public deprecated void setIncludeDrm(boolean);
    method public void setStopPreviousRingtone(boolean);
+31 −37
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.SurfaceTexture;
import android.graphics.Rect;
import android.media.MediaPlayer2Proto;
import android.media.MediaPlayer2Proto.PlayerMessage;
import android.media.MediaPlayer2Proto.Value;
import android.net.Uri;
@@ -742,32 +741,32 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
            return;
        }

        AssetFileDescriptor afd = null;
        try {
            // Try requested Uri locally first
            if (ContentResolver.SCHEME_CONTENT.equals(scheme)
                    && Settings.AUTHORITY.equals(authority)) {
            // Try cached ringtone first since the actual provider may not be
            // encryption aware, or it may be stored on CE media storage
            final int type = RingtoneManager.getDefaultType(uri);
            final Uri cacheUri = RingtoneManager.getCacheForType(type, context.getUserId());
            final Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
            if (attemptDataSource(isCurrent, srcId, resolver, cacheUri)) {
                return;
                afd = RingtoneManager.openDefaultRingtoneUri(context, uri);
            } else {
                afd = resolver.openAssetFileDescriptor(uri, "r");
            }
            if (attemptDataSource(isCurrent, srcId, resolver, actualUri)) {
            if (afd != null) {
                handleDataSource(isCurrent, srcId, afd);
                return;
            }
            handleDataSource(isCurrent, srcId, uri.toString(), headers, cookies);
        } else {
            // Try requested Uri locally first, or fallback to media server
            if (attemptDataSource(isCurrent, srcId, resolver, uri)) {
                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);
        }
        handleDataSource(isCurrent, srcId, uri.toString(), headers, cookies);
    }

    private boolean attemptDataSource(
            boolean isCurrent, long srcId, ContentResolver resolver, Uri uri) {
        try (AssetFileDescriptor afd = resolver.openAssetFileDescriptor(uri, "r")) {
    private void handleDataSource(boolean isCurrent, long srcId, AssetFileDescriptor afd)
            throws IOException {
        if (afd.getDeclaredLength() < 0) {
            handleDataSource(isCurrent,
                    srcId,
@@ -781,11 +780,6 @@ public final class MediaPlayer2Impl extends MediaPlayer2 {
                    afd.getStartOffset(),
                    afd.getDeclaredLength());
        }
            return true;
        } catch (NullPointerException | SecurityException | IOException ex) {
            Log.w(TAG, "Couldn't open " + uri + ": " + ex);
            return false;
        }
    }

    private void handleDataSource(
+33 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
@@ -1127,6 +1128,38 @@ public class RingtoneManager {
        }
    }

    /**
     * Opens a raw file descriptor to read the data under the given default URI.
     *
     * @param context the Context to use when resolving the Uri.
     * @param uri The desired default URI to open.
     * @return a new AssetFileDescriptor pointing to the file. You own this descriptor
     * and are responsible for closing it when done. This value may be {@code null}.
     * @throws FileNotFoundException if the provided URI could not be opened.
     * @see #getDefaultUri
     */
    public static AssetFileDescriptor openDefaultRingtoneUri(
            @NonNull Context context, @NonNull Uri uri) throws FileNotFoundException {
        // Try cached ringtone first since the actual provider may not be
        // encryption aware, or it may be stored on CE media storage
        final int type = getDefaultType(uri);
        final Uri cacheUri = getCacheForType(type, context.getUserId());
        final Uri actualUri = getActualDefaultRingtoneUri(context, type);
        final ContentResolver resolver = context.getContentResolver();

        AssetFileDescriptor afd = null;
        if (cacheUri != null) {
            afd = resolver.openAssetFileDescriptor(cacheUri, "r");
            if (afd != null) {
                return afd;
            }
        }
        if (actualUri != null) {
            afd = resolver.openAssetFileDescriptor(actualUri, "r");
        }
        return afd;
    }

    /**
     * Creates a {@link android.media.MediaScannerConnection} to scan a ringtone file and add its
     * information to the internal database.