@@ -0,0 +1,3016 @@
+package com.lenworthrose.music.sync;%0A%0Aimport android.content.Context;%0Aimport android.database.Cursor;%0Aimport android.media.MediaScannerConnection;%0Aimport android.os.AsyncTask;%0Aimport android.provider.MediaStore;%0Aimport android.text.TextUtils;%0Aimport android.util.Log;%0A%0Aimport java.io.File;%0Aimport java.io.FilenameFilter;%0Aimport java.util.Locale;%0A%0A/**%0A * %7B@link AsyncTask%7D that will search for cover art in the folder that contains the album.%0A */%0Apublic class CoverArtSearchTask extends AsyncTask%3CContext, Integer, Void%3E %7B%0A    @Override%0A    protected Void doInBackground(Context... params) %7B%0A        Context context = params%5B0%5D;%0A%0A        Cursor albumsMissingArt = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,%0A                new String%5B%5D %7B MediaStore.Audio.Albums._ID %7D,%0A                MediaStore.Audio.Albums.ALBUM_ART + %22 IS NULL%22,%0A                null,%0A                null);%0A%0A        if (albumsMissingArt.moveToFirst()) %7B%0A            int current = 0, total = albumsMissingArt.getCount();%0A%0A            do %7B%0A                publishProgress(current++, total);%0A%0A                Cursor songs = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,%0A                        new String%5B%5D %7B MediaStore.Audio.Media.DATA %7D,%0A                        MediaStore.Audio.Media.ALBUM_ID + %22=?%22,%0A                        new String%5B%5D %7B String.valueOf(albumsMissingArt.getLong(0)) %7D,%0A                        MediaStore.Audio.Media.TRACK);%0A%0A                if (!songs.moveToFirst()) continue;%0A%0A                Log.w(%22Testing%22, %22---------------%22 + songs.getString(0));%0A                File file = new File(songs.getString(0));%0A%0A                if (!file.exists()) %7B%0A                    Log.w(%22CoverArtSearchTask%22, %22Path for song in MediaStore does not exist? path=%22 + songs.getString(0));%0A                    continue;%0A                %7D%0A%0A                String parentStr = file.getParent();%0A                if (TextUtils.isEmpty(parentStr)) continue;%0A%0A                File parentDir = new File(parentStr);%0A                if (!parentDir.exists()) continue;%0A%0A                File%5B%5D images = parentDir.listFiles(new ImageFilenameFilter());%0A%0A                if (images != null && images.length %3E 0) %7B%0A                    File%5B%5D allFiles = parentDir.listFiles();%0A                    String%5B%5D filesToScan = new String%5BallFiles.length%5D;%0A%0A                    for (int i = 0; i %3C allFiles.length; i++)%0A                        filesToScan%5Bi%5D = allFiles%5Bi%5D.getAbsolutePath();%0A%0A                    MediaScannerConnection.scanFile(context, filesToScan, null, null);%0A                %7D%0A            %7D while (albumsMissingArt.moveToNext());%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    private static final class ImageFilenameFilter implements FilenameFilter %7B%0A        @Override%0A        public boolean accept(File dir, String filename) %7B%0A            filename = filename.toLowerCase(Locale.ENGLISH);%0A            return filename.endsWith(%22jpg%22) %7C%7C filename.endsWith(%22jpeg%22);%0A        %7D%0A    %7D%0A%7D%0A

Create CoverArtSearchTask