Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Remove code for getting and saving album art from the music app and

use the media provider instead.
  • Loading branch information...
commit 7a16cc726fd83eff0095f3f82364d1438773218f 1 parent 9fe0133
authored June 16, 2009
14  src/com/android/music/MediaPlaybackActivity.java
@@ -1220,13 +1220,16 @@ private void updateTrackInfo() {
1220 1220
                 return;
1221 1221
             }
1222 1222
             
1223  
-            if (mService.getAudioId() < 0 && path.toLowerCase().startsWith("http://")) {
  1223
+            int songid = mService.getAudioId(); 
  1224
+            if (songid < 0 && path.toLowerCase().startsWith("http://")) {
  1225
+                // Once we can get album art and meta data from MediaPlayer, we
  1226
+                // can show that info again when streaming.
1224 1227
                 ((View) mArtistName.getParent()).setVisibility(View.INVISIBLE);
1225 1228
                 ((View) mAlbumName.getParent()).setVisibility(View.INVISIBLE);
1226 1229
                 mAlbum.setVisibility(View.GONE);
1227 1230
                 mTrackName.setText(path);
1228 1231
                 mAlbumArtHandler.removeMessages(GET_ALBUM_ART);
1229  
-                mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, -1, 0).sendToTarget();
  1232
+                mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, -1, -1).sendToTarget();
1230 1233
             } else {
1231 1234
                 ((View) mArtistName.getParent()).setVisibility(View.VISIBLE);
1232 1235
                 ((View) mAlbumName.getParent()).setVisibility(View.VISIBLE);
@@ -1244,7 +1247,7 @@ private void updateTrackInfo() {
1244 1247
                 mAlbumName.setText(albumName);
1245 1248
                 mTrackName.setText(mService.getTrackName());
1246 1249
                 mAlbumArtHandler.removeMessages(GET_ALBUM_ART);
1247  
-                mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, albumid, 0).sendToTarget();
  1250
+                mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, albumid, songid).sendToTarget();
1248 1251
                 mAlbum.setVisibility(View.VISIBLE);
1249 1252
             }
1250 1253
             mDuration = mService.duration();
@@ -1263,14 +1266,15 @@ public AlbumArtHandler(Looper looper) {
1263 1266
         public void handleMessage(Message msg)
1264 1267
         {
1265 1268
             int albumid = msg.arg1;
  1269
+            int songid = msg.arg2;
1266 1270
             if (msg.what == GET_ALBUM_ART && (mAlbumId != albumid || albumid < 0)) {
1267 1271
                 // while decoding the new image, show the default album art
1268 1272
                 Message numsg = mHandler.obtainMessage(ALBUM_ART_DECODED, null);
1269 1273
                 mHandler.removeMessages(ALBUM_ART_DECODED);
1270 1274
                 mHandler.sendMessageDelayed(numsg, 300);
1271  
-                Bitmap bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, albumid);
  1275
+                Bitmap bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, songid, albumid);
1272 1276
                 if (bm == null) {
1273  
-                    bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, -1);
  1277
+                    bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, songid, -1);
1274 1278
                     albumid = -1;
1275 1279
                 }
1276 1280
                 if (bm != null) {
274  src/com/android/music/MusicUtils.java
@@ -16,21 +16,7 @@
16 16
 
17 17
 package com.android.music;
18 18
 
19  
-import java.io.File;
20  
-import java.io.FileDescriptor;
21  
-import java.io.FileInputStream;
22  
-import java.io.FileNotFoundException;
23  
-import java.io.FileOutputStream;
24  
-import java.io.IOException;
25  
-import java.io.InputStream;
26  
-import java.io.OutputStream;
27  
-import java.util.Arrays;
28  
-import java.util.Formatter;
29  
-import java.util.HashMap;
30  
-import java.util.Locale;
31  
-
32 19
 import android.app.Activity;
33  
-import android.app.ExpandableListActivity;
34 20
 import android.content.ComponentName;
35 21
 import android.content.ContentResolver;
36 22
 import android.content.ContentUris;
@@ -49,12 +35,10 @@
49 35
 import android.graphics.PixelFormat;
50 36
 import android.graphics.drawable.BitmapDrawable;
51 37
 import android.graphics.drawable.Drawable;
52  
-import android.media.MediaFile;
53  
-import android.media.MediaScanner;
54 38
 import android.net.Uri;
55  
-import android.os.RemoteException;
56 39
 import android.os.Environment;
57 40
 import android.os.ParcelFileDescriptor;
  41
+import android.os.RemoteException;
58 42
 import android.provider.MediaStore;
59 43
 import android.provider.Settings;
60 44
 import android.util.Log;
@@ -64,6 +48,16 @@
64 48
 import android.widget.TextView;
65 49
 import android.widget.Toast;
66 50
 
  51
+import java.io.File;
  52
+import java.io.FileDescriptor;
  53
+import java.io.FileNotFoundException;
  54
+import java.io.IOException;
  55
+import java.io.InputStream;
  56
+import java.util.Arrays;
  57
+import java.util.Formatter;
  58
+import java.util.HashMap;
  59
+import java.util.Locale;
  60
+
67 61
 public class MusicUtils {
68 62
 
69 63
     private static final String TAG = "MusicUtils";
@@ -740,7 +734,6 @@ public void setColorFilter(ColorFilter cf) {
740 734
     }
741 735
     
742 736
     private static int sArtId = -2;
743  
-    private static byte [] mCachedArt;
744 737
     private static Bitmap mCachedBit = null;
745 738
     private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
746 739
     private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
@@ -865,27 +858,18 @@ private static Bitmap getArtworkQuick(Context context, int album_id, int w, int
865 858
     /** Get album art for specified album. You should not pass in the album id
866 859
      * for the "unknown" album here (use -1 instead)
867 860
      */
868  
-    public static Bitmap getArtwork(Context context, int album_id) {
869  
-        return getArtwork(context, album_id, true);
870  
-    }
871  
-    
872  
-    /** Get album art for specified album. You should not pass in the album id
873  
-     * for the "unknown" album here (use -1 instead)
874  
-     */
875  
-    public static Bitmap getArtwork(Context context, int album_id, boolean allowDefault) {
  861
+    public static Bitmap getArtwork(Context context, int song_id, int album_id) {
876 862
 
877 863
         if (album_id < 0) {
878 864
             // This is something that is not in the database, so get the album art directly
879 865
             // from the file.
880  
-            Bitmap bm = getArtworkFromFile(context, null, -1);
881  
-            if (bm != null) {
882  
-                return bm;
883  
-            }
884  
-            if (allowDefault) {
885  
-                return getDefaultArtwork(context);
886  
-            } else {
887  
-                return null;
  866
+            if (song_id >= 0) {
  867
+                Bitmap bm = getArtworkFromFile(context, song_id, -1);
  868
+                if (bm != null) {
  869
+                    return bm;
  870
+                }
888 871
             }
  872
+            return getDefaultArtwork(context);
889 873
         }
890 874
 
891 875
         ContentResolver res = context.getContentResolver();
@@ -898,61 +882,16 @@ public static Bitmap getArtwork(Context context, int album_id, boolean allowDefa
898 882
             } catch (FileNotFoundException ex) {
899 883
                 // The album art thumbnail does not actually exist. Maybe the user deleted it, or
900 884
                 // maybe it never existed to begin with.
901  
-                Bitmap bm = getArtworkFromFile(context, null, album_id);
  885
+                Bitmap bm = getArtworkFromFile(context, song_id, album_id);
902 886
                 if (bm != null) {
903 887
                     if (bm.getConfig() == null) {
904 888
                         bm = bm.copy(Bitmap.Config.RGB_565, false);
905 889
                         if (bm == null) {
906  
-                            if (allowDefault) {
907  
-                                return getDefaultArtwork(context);
908  
-                            } else {
909  
-                                return null;
910  
-                            }
911  
-                        }
912  
-                    }
913  
-                    // Put the newly found artwork in the database.
914  
-                    // Note that this shouldn't be done for the "unknown" album,
915  
-                    // but if this method is called correctly, that won't happen.
916  
-                    
917  
-                    // first write it somewhere
918  
-                    String file = Environment.getExternalStorageDirectory()
919  
-                        + "/albumthumbs/" + String.valueOf(System.currentTimeMillis());
920  
-                    if (ensureFileExists(file)) {
921  
-                        try {
922  
-                            OutputStream outstream = new FileOutputStream(file);
923  
-                            boolean success = bm.compress(Bitmap.CompressFormat.JPEG, 75, outstream);
924  
-                            outstream.close();
925  
-                            if (success) {
926  
-                                ContentValues values = new ContentValues();
927  
-                                values.put("album_id", album_id);
928  
-                                values.put("_data", file);
929  
-                                Uri newuri = res.insert(sArtworkUri, values);
930  
-                                if (newuri == null) {
931  
-                                    // Failed to insert in to the database. The most likely
932  
-                                    // cause of this is that the item already existed in the
933  
-                                    // database, and the most likely cause of that is that
934  
-                                    // the album was scanned before, but the user deleted the
935  
-                                    // album art from the sd card.
936  
-                                    // We can ignore that case here, since the media provider
937  
-                                    // will regenerate the album art for those entries when
938  
-                                    // it detects this.
939  
-                                    success = false;
940  
-                                }
941  
-                            }
942  
-                            if (!success) {
943  
-                                File f = new File(file);
944  
-                                f.delete();
945  
-                            }
946  
-                        } catch (FileNotFoundException e) {
947  
-                            Log.e(TAG, "error creating file", e);
948  
-                        } catch (IOException e) {
949  
-                            Log.e(TAG, "error creating file", e);
  890
+                            return getDefaultArtwork(context);
950 891
                         }
951 892
                     }
952  
-                } else if (allowDefault) {
953  
-                    bm = getDefaultArtwork(context);
954 893
                 } else {
955  
-                    bm = null;
  894
+                    bm = getDefaultArtwork(context);
956 895
                 }
957 896
                 return bm;
958 897
             } finally {
@@ -967,168 +906,39 @@ public static Bitmap getArtwork(Context context, int album_id, boolean allowDefa
967 906
         
968 907
         return null;
969 908
     }
970  
-
971  
-    // copied from MediaProvider
972  
-    private static boolean ensureFileExists(String path) {
973  
-        File file = new File(path);
974  
-        if (file.exists()) {
975  
-            return true;
976  
-        } else {
977  
-            // we will not attempt to create the first directory in the path
978  
-            // (for example, do not create /sdcard if the SD card is not mounted)
979  
-            int secondSlash = path.indexOf('/', 1);
980  
-            if (secondSlash < 1) return false;
981  
-            String directoryPath = path.substring(0, secondSlash);
982  
-            File directory = new File(directoryPath);
983  
-            if (!directory.exists())
984  
-                return false;
985  
-            file.getParentFile().mkdirs();
986  
-            try {
987  
-                return file.createNewFile();
988  
-            } catch(IOException ioe) {
989  
-                Log.d(TAG, "File creation failed for " + path);
990  
-            }
991  
-            return false;
992  
-        }
993  
-    }
994 909
     
995 910
     // get album art for specified file
996 911
     private static final String sExternalMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
997  
-    private static Bitmap getArtworkFromFile(Context context, Uri uri, int albumid) {
  912
+    private static Bitmap getArtworkFromFile(Context context, int songid, int albumid) {
998 913
         Bitmap bm = null;
999 914
         byte [] art = null;
1000 915
         String path = null;
1001 916
 
1002  
-        if (sArtId == albumid) {
1003  
-            //Log.i("@@@@@@ ", "reusing cached data", new Exception());
1004  
-            if (mCachedBit != null) {
1005  
-                return mCachedBit;
1006  
-            }
1007  
-            art = mCachedArt;
1008  
-        } else {
1009  
-            // try reading embedded artwork
1010  
-            if (uri == null) {
1011  
-                try {
1012  
-                    int curalbum = sService.getAlbumId();
1013  
-                    if (curalbum == albumid || albumid < 0) {
1014  
-                        path = sService.getPath();
1015  
-                        if (path != null) {
1016  
-                            uri = Uri.parse(path);
1017  
-                        }
1018  
-                    }
1019  
-                } catch (RemoteException ex) {
1020  
-                    return null;
1021  
-                } catch (NullPointerException ex) {
1022  
-                    return null;
1023  
-                }
1024  
-            }
1025  
-            if (uri == null) {
1026  
-                if (albumid >= 0) {
1027  
-                    Cursor c = query(context,MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
1028  
-                            new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM },
1029  
-                            MediaStore.Audio.Media.ALBUM_ID + "=?", new String [] {String.valueOf(albumid)},
1030  
-                            null);
1031  
-                    if (c != null) {
1032  
-                        if (c.moveToFirst()) {
1033  
-                            int trackid = c.getInt(0);
1034  
-                            uri = ContentUris.withAppendedId(
1035  
-                                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, trackid);
1036  
-                            if (MediaFile.UNKNOWN_STRING.equals(c.getString(1))) {
1037  
-                                albumid = -1;
1038  
-                            }
1039  
-                        }
1040  
-                        c.close();
1041  
-                    }
1042  
-                }
1043  
-            }
1044  
-            if (uri != null) {
1045  
-                MediaScanner scanner = new MediaScanner(context);
1046  
-                ParcelFileDescriptor pfd = null;
1047  
-                try {
1048  
-                    pfd = context.getContentResolver().openFileDescriptor(uri, "r");
1049  
-                    if (pfd != null) {
1050  
-                        FileDescriptor fd = pfd.getFileDescriptor();
1051  
-                        art = scanner.extractAlbumArt(fd);
1052  
-                    }
1053  
-                } catch (IOException ex) {
1054  
-                } catch (SecurityException ex) {
1055  
-                } finally {
1056  
-                    try {
1057  
-                        if (pfd != null) {
1058  
-                            pfd.close();
1059  
-                        }
1060  
-                    } catch (IOException ex) {
1061  
-                    }
1062  
-                }
1063  
-            }
  917
+        if (albumid < 0 && songid < 0) {
  918
+            throw new IllegalArgumentException("Must specify an album or a song id");
1064 919
         }
1065  
-        // if no embedded art exists, look for AlbumArt.jpg in same directory as the media file
1066  
-        if (art == null && path != null) {
1067  
-            if (path.startsWith(sExternalMediaUri)) {
1068  
-                // get the real path
1069  
-                Cursor c = query(context,Uri.parse(path),
1070  
-                        new String[] { MediaStore.Audio.Media.DATA},
1071  
-                        null, null, null);
1072  
-                if (c != null) {
1073  
-                    c.moveToFirst();
1074  
-                    if (!c.isAfterLast()) {
1075  
-                        path = c.getString(0);
1076  
-                    }
1077  
-                    c.close();
  920
+
  921
+        try {
  922
+            if (albumid < 0) {
  923
+                Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
  924
+                ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
  925
+                if (pfd != null) {
  926
+                    FileDescriptor fd = pfd.getFileDescriptor();
  927
+                    bm = BitmapFactory.decodeFileDescriptor(fd);
1078 928
                 }
1079  
-            }
1080  
-            int lastSlash = path.lastIndexOf('/');
1081  
-            if (lastSlash > 0) {
1082  
-                String artPath = path.substring(0, lastSlash + 1) + "AlbumArt.jpg";
1083  
-                File file = new File(artPath);
1084  
-                if (file.exists()) {
1085  
-                    art = new byte[(int)file.length()];
1086  
-                    FileInputStream stream = null;
1087  
-                    try {
1088  
-                        stream = new FileInputStream(file);
1089  
-                        stream.read(art);
1090  
-                    } catch (IOException ex) {
1091  
-                        art = null;
1092  
-                    } finally {
1093  
-                        try {
1094  
-                            if (stream != null) {
1095  
-                                stream.close();
1096  
-                            }
1097  
-                        } catch (IOException ex) {
1098  
-                        }
1099  
-                    }
1100  
-                } else {
1101  
-                    // TODO: try getting album art from the web
  929
+            } else {
  930
+                Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
  931
+                ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
  932
+                if (pfd != null) {
  933
+                    FileDescriptor fd = pfd.getFileDescriptor();
  934
+                    bm = BitmapFactory.decodeFileDescriptor(fd);
1102 935
                 }
1103 936
             }
  937
+        } catch (FileNotFoundException ex) {
  938
+            //
1104 939
         }
1105  
-        
1106  
-        if (art != null) {
1107  
-            try {
1108  
-                // get the size of the bitmap
1109  
-                BitmapFactory.Options opts = new BitmapFactory.Options();
1110  
-                opts.inJustDecodeBounds = true;
1111  
-                opts.inSampleSize = 1;
1112  
-                BitmapFactory.decodeByteArray(art, 0, art.length, opts);
1113  
-                
1114  
-                // request a reasonably sized output image
1115  
-                // TODO: don't hardcode the size
1116  
-                while (opts.outHeight > 320 || opts.outWidth > 320) {
1117  
-                    opts.outHeight /= 2;
1118  
-                    opts.outWidth /= 2;
1119  
-                    opts.inSampleSize *= 2;
1120  
-                }
1121  
-                
1122  
-                // get the image for real now
1123  
-                opts.inJustDecodeBounds = false;
1124  
-                bm = BitmapFactory.decodeByteArray(art, 0, art.length, opts);
1125  
-                if (albumid != -1) {
1126  
-                    sArtId = albumid;
1127  
-                }
1128  
-                mCachedArt = art;
1129  
-                mCachedBit = bm;
1130  
-            } catch (Exception e) {
1131  
-            }
  940
+        if (bm != null) {
  941
+            mCachedBit = bm;
1132 942
         }
1133 943
         return bm;
1134 944
     }

0 notes on commit 7a16cc7

Please sign in to comment.
Something went wrong with that request. Please try again.