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

Commit 4d70bd7a authored by Ian Pedowitz's avatar Ian Pedowitz Committed by The Android Automerger
Browse files

Revert "Fix race condition when setting default ringtones"

This reverts commit f2cb9341.

Change-Id: I151972052aea47bf8c6823eb8bbf8161c5a7a4f0
parent df252d6e
Loading
Loading
Loading
Loading
+28 −61
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
@@ -42,7 +41,6 @@ import android.provider.MediaStore.Files.FileColumns;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.sax.Element;
import android.sax.ElementListener;
import android.sax.RootElement;
@@ -326,6 +324,8 @@ public class MediaScanner
    // used when scanning the image database so we know whether we have to prune
    // old thumbnail files
    private int mOriginalCount;
    /** Whether the database had any entries in it before the scan started */
    private boolean mWasEmptyPriorToScan = false;
    /** Whether the scanner has set a default sound for the ringer ringtone. */
    private boolean mDefaultRingtoneSet;
    /** Whether the scanner has set a default sound for the notification ringtone. */
@@ -535,18 +535,6 @@ public class MediaScanner
                if (mMtpObjectHandle != 0) {
                    entry.mRowId = 0;
                }

                if ((!mDefaultNotificationSet &&
                        doesPathHaveFilename(entry.mPath, mDefaultNotificationFilename))
                        || (!mDefaultRingtoneSet &&
                                doesPathHaveFilename(entry.mPath, mDefaultRingtoneFilename))
                        || (!mDefaultAlarmSet &&
                                doesPathHaveFilename(entry.mPath, mDefaultAlarmAlertFilename))) {
                    Log.w(TAG, "forcing rescan of " + entry.mPath +
                            "since ringtone setting didn't finish");
                    scanAlways = true;
                }

                // rescan for metadata if file was modified since last scan
                if (entry != null && (entry.mLastModifiedChanged || scanAlways)) {
                    if (noMedia) {
@@ -926,9 +914,21 @@ public class MediaScanner
            }
            Uri result = null;
            boolean needToSetSettings = false;
            if (rowId == 0) {
                if (mMtpObjectHandle != 0) {
                    values.put(MediaStore.MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, mMtpObjectHandle);
                }
                if (tableUri == mFilesUri) {
                    int format = entry.mFormat;
                    if (format == 0) {
                        format = MediaFile.getFormatCode(entry.mPath, mMimeType);
                    }
                    values.put(Files.FileColumns.FORMAT, format);
                }
                // Setting a flag in order not to use bulk insert for the file related with
                // notifications, ringtones, and alarms, because the rowId of the inserted file is
                // needed.
                if (mWasEmptyPriorToScan) {
                    if (notifications && !mDefaultNotificationSet) {
                        if (TextUtils.isEmpty(mDefaultNotificationFilename) ||
                                doesPathHaveFilename(entry.mPath, mDefaultNotificationFilename)) {
@@ -945,18 +945,8 @@ public class MediaScanner
                            needToSetSettings = true;
                        }
                    }

            if (rowId == 0) {
                if (mMtpObjectHandle != 0) {
                    values.put(MediaStore.MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, mMtpObjectHandle);
                }
                if (tableUri == mFilesUri) {
                    int format = entry.mFormat;
                    if (format == 0) {
                        format = MediaFile.getFormatCode(entry.mPath, mMimeType);
                    }
                    values.put(Files.FileColumns.FORMAT, format);
                }

                // New file, insert it.
                // Directories need to be inserted before the files they contain, so they
                // get priority when bulk inserting.
@@ -1026,20 +1016,13 @@ public class MediaScanner

        private void setSettingIfNotSet(String settingName, Uri uri, long rowId) {

            if(wasSettingAlreadySet(settingName)) {
                return;
            }

            String existingSettingValue = Settings.System.getString(mContext.getContentResolver(),
                    settingName);

            if (TextUtils.isEmpty(existingSettingValue)) {
                // Set the setting to the given URI

                ContentResolver cr = mContext.getContentResolver();
                Settings.System.putString(cr, settingName,
                Settings.System.putString(mContext.getContentResolver(), settingName,
                        ContentUris.withAppendedId(uri, rowId).toString());
                Settings.System.putInt(cr, settingSetIndicatorName(settingName), 1);
            }
        }

@@ -1067,20 +1050,6 @@ public class MediaScanner

    }; // end of anonymous MediaScannerClient instance

    private String settingSetIndicatorName(String base) {
        return base + "_set";
    }

    private boolean wasSettingAlreadySet(String name) {
        ContentResolver cr = mContext.getContentResolver();
        String indicatorName = settingSetIndicatorName(name);
        try {
            return Settings.System.getInt(cr, indicatorName) != 0;
        } catch (SettingNotFoundException e) {
            return false;
        }
    }

    private void prescan(String filePath, boolean prescanFiles) throws RemoteException {
        Cursor c = null;
        String where = null;
@@ -1102,10 +1071,6 @@ public class MediaScanner
            selectionArgs = new String[] { "" };
        }

        mDefaultRingtoneSet = wasSettingAlreadySet(Settings.System.RINGTONE);
        mDefaultNotificationSet = wasSettingAlreadySet(Settings.System.NOTIFICATION_SOUND);
        mDefaultAlarmSet = wasSettingAlreadySet(Settings.System.ALARM_ALERT);

        // Tell the provider to not delete the file.
        // If the file is truly gone the delete is unnecessary, and we want to avoid
        // accidentally deleting files that are really there (this may happen if the
@@ -1124,6 +1089,7 @@ public class MediaScanner
                // with CursorWindow positioning.
                long lastId = Long.MIN_VALUE;
                Uri limitUri = mFilesUri.buildUpon().appendQueryParameter("limit", "1000").build();
                mWasEmptyPriorToScan = true;

                while (true) {
                    selectionArgs[0] = "" + lastId;
@@ -1142,6 +1108,7 @@ public class MediaScanner
                    if (num == 0) {
                        break;
                    }
                    mWasEmptyPriorToScan = false;
                    while (c.moveToNext()) {
                        long rowId = c.getLong(FILES_PRESCAN_ID_COLUMN_INDEX);
                        String path = c.getString(FILES_PRESCAN_PATH_COLUMN_INDEX);
@@ -1293,7 +1260,7 @@ public class MediaScanner
        }
    }

    private void postscan(final String[] directories) throws RemoteException {
    private void postscan(String[] directories) throws RemoteException {

        // handle playlists last, after we know what media files are on the storage.
        if (mProcessPlaylists) {