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

Commit 295336f6 authored by Winson's avatar Winson
Browse files

Call MediaStore#scanFile directly in RingtoneManager

Reproduced the failing case, even without thread pool changes.
Since RingtoneManager was blocking on the call anyways, changing it
to use the synchronous scanFile method directly should be fine.

Bug: 131882699

Test: manual add MP3 with RingtonePickerActivity

Change-Id: I53b49ed17403cda93ecfefb6985133b161a96983
parent d7314d7a
Loading
Loading
Loading
Loading
+1 −55
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.Cursor;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.net.Uri;
import android.os.Environment;
import android.os.Environment;
import android.os.FileUtils;
import android.os.FileUtils;
@@ -50,7 +49,6 @@ import android.util.Log;


import com.android.internal.database.SortCursor;
import com.android.internal.database.SortCursor;


import java.io.Closeable;
import java.io.File;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileOutputStream;
@@ -59,7 +57,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;


/**
/**
 * RingtoneManager provides access to ringtones, notification, and other types
 * RingtoneManager provides access to ringtones, notification, and other types
@@ -927,11 +924,7 @@ public class RingtoneManager {
        }
        }


        // Tell MediaScanner about the new file. Wait for it to assign a {@link Uri}.
        // Tell MediaScanner about the new file. Wait for it to assign a {@link Uri}.
        try (NewRingtoneScanner scanner =  new NewRingtoneScanner(outFile)) {
        return MediaStore.scanFile(mContext, outFile);
            return scanner.take();
        } catch (InterruptedException e) {
            throw new IOException("Audio file failed to scan as a ringtone", e);
        }
    }
    }


    private static final String getExternalDirectoryForType(final int type) {
    private static final String getExternalDirectoryForType(final int type) {
@@ -1108,53 +1101,6 @@ public class RingtoneManager {
        return AudioManager.hasHapticChannels(ringtoneUri);
        return AudioManager.hasHapticChannels(ringtoneUri);
    }
    }


    /**
     * Creates a {@link android.media.MediaScannerConnection} to scan a ringtone file and add its
     * information to the internal database.
     *
     * It uses a {@link java.util.concurrent.LinkedBlockingQueue} so that the caller can block until
     * the scan is completed.
     */
    private class NewRingtoneScanner implements Closeable, MediaScannerConnectionClient {
        private MediaScannerConnection mMediaScannerConnection;
        private File mFile;
        private LinkedBlockingQueue<Uri> mQueue = new LinkedBlockingQueue<>(1);

        public NewRingtoneScanner(File file) {
            mFile = file;
            mMediaScannerConnection = new MediaScannerConnection(mContext, this);
            mMediaScannerConnection.connect();
        }

        @Override
        public void close() {
            mMediaScannerConnection.disconnect();
        }

        @Override
        public void onMediaScannerConnected() {
            mMediaScannerConnection.scanFile(mFile.getAbsolutePath(), null);
        }

        @Override
        public void onScanCompleted(String path, Uri uri) {
            if (uri == null) {
                // There was some issue with scanning. Delete the copied file so it is not oprhaned.
                mFile.delete();
                return;
            }
            try {
                mQueue.put(uri);
            } catch (InterruptedException e) {
                Log.e(TAG, "Unable to put new ringtone Uri in queue", e);
            }
        }

        public Uri take() throws InterruptedException {
            return mQueue.take();
        }
    }

    /**
    /**
     * Attempts to create a context for the given user.
     * Attempts to create a context for the given user.
     *
     *