2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.music;
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.ContentResolver;
22 import android.content.ContentUris;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.ServiceConnection;
27 import android.content.SharedPreferences;
28 import android.content.SharedPreferences.Editor;
29 import android.content.res.Resources;
30 import android.database.Cursor;
31 import android.graphics.Bitmap;
32 import android.graphics.BitmapFactory;
33 import android.graphics.Canvas;
34 import android.graphics.ColorFilter;
35 import android.graphics.PixelFormat;
36 import android.graphics.drawable.BitmapDrawable;
37 import android.graphics.drawable.Drawable;
38 import android.net.Uri;
39 import android.os.Environment;
40 import android.os.ParcelFileDescriptor;
41 import android.os.RemoteException;
42 import android.provider.MediaStore;
43 import android.provider.Settings;
44 import android.util.Log;
45 import android.view.SubMenu;
46 import android.view.View;
47 import android.view.Window;
48 import android.widget.TextView;
49 import android.widget.Toast;
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;
61 public class MusicUtils {
63 private static final String TAG = "MusicUtils";
65 public interface Defs {
66 public final static int OPEN_URL = 0;
67 public final static int ADD_TO_PLAYLIST = 1;
68 public final static int USE_AS_RINGTONE = 2;
69 public final static int PLAYLIST_SELECTED = 3;
70 public final static int NEW_PLAYLIST = 4;
71 public final static int PLAY_SELECTION = 5;
72 public final static int GOTO_START = 6;
73 public final static int GOTO_PLAYBACK = 7;
74 public final static int PARTY_SHUFFLE = 8;
75 public final static int SHUFFLE_ALL = 9;
76 public final static int DELETE_ITEM = 10;
77 public final static int SCAN_DONE = 11;
78 public final static int QUEUE = 12;
79 public final static int CHILD_MENU_BASE = 13; // this should be the last item
82 public static String makeAlbumsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
83 // There are two formats for the albums/songs information:
84 // "N Song(s)" - used for unknown artist/album
85 // "N Album(s)" - used for known albums
87 StringBuilder songs_albums = new StringBuilder();
89 Resources r = context.getResources();
92 songs_albums.append(context.getString(R.string.onesong));
94 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
95 sFormatBuilder.setLength(0);
96 sFormatter.format(f, Integer.valueOf(numsongs));
97 songs_albums.append(sFormatBuilder);
100 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
101 sFormatBuilder.setLength(0);
102 sFormatter.format(f, Integer.valueOf(numalbums));
103 songs_albums.append(sFormatBuilder);
104 songs_albums.append(context.getString(R.string.albumsongseparator));
106 return songs_albums.toString();
110 * This is now only used for the query screen
112 public static String makeAlbumsSongsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
113 // There are several formats for the albums/songs information:
114 // "1 Song" - used if there is only 1 song
115 // "N Songs" - used for the "unknown artist" item
116 // "1 Album"/"N Songs"
117 // "N Album"/"M Songs"
118 // Depending on locale, these may need to be further subdivided
120 StringBuilder songs_albums = new StringBuilder();
123 songs_albums.append(context.getString(R.string.onesong));
125 Resources r = context.getResources();
127 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
128 sFormatBuilder.setLength(0);
129 sFormatter.format(f, Integer.valueOf(numalbums));
130 songs_albums.append(sFormatBuilder);
131 songs_albums.append(context.getString(R.string.albumsongseparator));
133 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
134 sFormatBuilder.setLength(0);
135 sFormatter.format(f, Integer.valueOf(numsongs));
136 songs_albums.append(sFormatBuilder);
138 return songs_albums.toString();
141 public static IMediaPlaybackService sService = null;
142 private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
144 public static boolean bindToService(Context context) {
145 return bindToService(context, null);
148 public static boolean bindToService(Context context, ServiceConnection callback) {
149 context.startService(new Intent(context, MediaPlaybackService.class));
150 ServiceBinder sb = new ServiceBinder(callback);
151 sConnectionMap.put(context, sb);
152 return context.bindService((new Intent()).setClass(context,
153 MediaPlaybackService.class), sb, 0);
156 public static void unbindFromService(Context context) {
157 ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
159 Log.e("MusicUtils", "Trying to unbind for unknown Context");
162 context.unbindService(sb);
163 if (sConnectionMap.isEmpty()) {
164 // presumably there is nobody interested in the service at this point,
165 // so don't hang on to the ServiceConnection
170 private static class ServiceBinder implements ServiceConnection {
171 ServiceConnection mCallback;
172 ServiceBinder(ServiceConnection callback) {
173 mCallback = callback;
176 public void onServiceConnected(ComponentName className, android.os.IBinder service) {
177 sService = IMediaPlaybackService.Stub.asInterface(service);
179 if (mCallback != null) {
180 mCallback.onServiceConnected(className, service);
184 public void onServiceDisconnected(ComponentName className) {
185 if (mCallback != null) {
186 mCallback.onServiceDisconnected(className);
192 public static long getCurrentAlbumId() {
193 if (sService != null) {
195 return sService.getAlbumId();
196 } catch (RemoteException ex) {
202 public static long getCurrentArtistId() {
203 if (MusicUtils.sService != null) {
205 return sService.getArtistId();
206 } catch (RemoteException ex) {
212 public static long getCurrentAudioId() {
213 if (MusicUtils.sService != null) {
215 return sService.getAudioId();
216 } catch (RemoteException ex) {
222 public static int getCurrentShuffleMode() {
223 int mode = MediaPlaybackService.SHUFFLE_NONE;
224 if (sService != null) {
226 mode = sService.getShuffleMode();
227 } catch (RemoteException ex) {
234 * Returns true if a file is currently opened for playback (regardless
235 * of whether it's playing or paused).
237 public static boolean isMusicLoaded() {
238 if (MusicUtils.sService != null) {
240 return sService.getPath() != null;
241 } catch (RemoteException ex) {
247 private final static long [] sEmptyList = new long[0];
249 public static long [] getSongListForCursor(Cursor cursor) {
250 if (cursor == null) {
253 int len = cursor.getCount();
254 long [] list = new long[len];
255 cursor.moveToFirst();
258 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
259 } catch (IllegalArgumentException ex) {
260 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
262 for (int i = 0; i < len; i++) {
263 list[i] = cursor.getLong(colidx);
269 public static long [] getSongListForArtist(Context context, long id) {
270 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
271 String where = MediaStore.Audio.Media.ARTIST_ID + "=" + id + " AND " +
272 MediaStore.Audio.Media.IS_MUSIC + "=1";
273 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
275 MediaStore.Audio.Media.ALBUM_KEY + "," + MediaStore.Audio.Media.TRACK);
277 if (cursor != null) {
278 long [] list = getSongListForCursor(cursor);
285 public static long [] getSongListForAlbum(Context context, long id) {
286 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
287 String where = MediaStore.Audio.Media.ALBUM_ID + "=" + id + " AND " +
288 MediaStore.Audio.Media.IS_MUSIC + "=1";
289 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
290 ccols, where, null, MediaStore.Audio.Media.TRACK);
292 if (cursor != null) {
293 long [] list = getSongListForCursor(cursor);
300 public static long [] getSongListForPlaylist(Context context, long plid) {
301 final String[] ccols = new String[] { MediaStore.Audio.Playlists.Members.AUDIO_ID };
302 Cursor cursor = query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
303 ccols, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
305 if (cursor != null) {
306 long [] list = getSongListForCursor(cursor);
313 public static void playPlaylist(Context context, long plid) {
314 long [] list = getSongListForPlaylist(context, plid);
316 playAll(context, list, -1, false);
320 public static long [] getAllSongs(Context context) {
321 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
322 new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC + "=1",
325 if (c == null || c.getCount() == 0) {
328 int len = c.getCount();
329 long [] list = new long[len];
330 for (int i = 0; i < len; i++) {
332 list[i] = c.getLong(0);
344 * Fills out the given submenu with items for "new playlist" and
345 * any existing playlists. When the user selects an item, the
346 * application will receive PLAYLIST_SELECTED with the Uri of
347 * the selected playlist, NEW_PLAYLIST if a new playlist
348 * should be created, and QUEUE if the "current playlist" was
350 * @param context The context to use for creating the menu items
351 * @param sub The submenu to add the items to.
353 public static void makePlaylistMenu(Context context, SubMenu sub) {
354 String[] cols = new String[] {
355 MediaStore.Audio.Playlists._ID,
356 MediaStore.Audio.Playlists.NAME
358 ContentResolver resolver = context.getContentResolver();
359 if (resolver == null) {
360 System.out.println("resolver = null");
362 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''";
363 Cursor cur = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
364 cols, whereclause, null,
365 MediaStore.Audio.Playlists.NAME);
367 sub.add(1, Defs.QUEUE, 0, R.string.queue);
368 sub.add(1, Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
369 if (cur != null && cur.getCount() > 0) {
370 //sub.addSeparator(1, 0);
372 while (! cur.isAfterLast()) {
373 Intent intent = new Intent();
374 intent.putExtra("playlist", cur.getLong(0));
375 // if (cur.getInt(0) == mLastPlaylistSelected) {
376 // sub.add(0, MusicBaseActivity.PLAYLIST_SELECTED, cur.getString(1)).setIntent(intent);
378 sub.add(1, Defs.PLAYLIST_SELECTED, 0, cur.getString(1)).setIntent(intent);
389 public static void clearPlaylist(Context context, int plid) {
391 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plid);
392 context.getContentResolver().delete(uri, null, null);
396 public static void deleteTracks(Context context, long [] list) {
398 String [] cols = new String [] { MediaStore.Audio.Media._ID,
399 MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID };
400 StringBuilder where = new StringBuilder();
401 where.append(MediaStore.Audio.Media._ID + " IN (");
402 for (int i = 0; i < list.length; i++) {
403 where.append(list[i]);
404 if (i < list.length - 1) {
409 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols,
410 where.toString(), null, null);
414 // step 1: remove selected tracks from the current playlist, as well
415 // as from the album art cache
418 while (! c.isAfterLast()) {
419 // remove from current playlist
420 long id = c.getLong(0);
421 sService.removeTrack(id);
422 // remove from album art cache
423 long artIndex = c.getLong(2);
424 synchronized(sArtCache) {
425 sArtCache.remove(artIndex);
429 } catch (RemoteException ex) {
432 // step 2: remove selected tracks from the database
433 context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where.toString(), null);
435 // step 3: remove files from card
437 while (! c.isAfterLast()) {
438 String name = c.getString(1);
439 File f = new File(name);
440 try { // File.delete can throw a security exception
442 // I'm not sure if we'd ever get here (deletion would
443 // have to fail, but no exception thrown)
444 Log.e("MusicUtils", "Failed to delete file " + name);
447 } catch (SecurityException ex) {
454 String message = context.getResources().getQuantityString(
455 R.plurals.NNNtracksdeleted, list.length, Integer.valueOf(list.length));
457 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
458 // We deleted a number of tracks, which could affect any number of things
459 // in the media content domain, so update everything.
460 context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
463 public static void addToCurrentPlaylist(Context context, long [] list) {
464 if (sService == null) {
468 sService.enqueue(list, MediaPlaybackService.LAST);
469 String message = context.getResources().getQuantityString(
470 R.plurals.NNNtrackstoplaylist, list.length, Integer.valueOf(list.length));
471 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
472 } catch (RemoteException ex) {
476 public static void addToPlaylist(Context context, long [] ids, long playlistid) {
478 // this shouldn't happen (the menuitems shouldn't be visible
479 // unless the selected item represents something playable
480 Log.e("MusicBase", "ListSelection null");
482 int size = ids.length;
483 ContentValues values [] = new ContentValues[size];
484 ContentResolver resolver = context.getContentResolver();
485 // need to determine the number of items currently in the playlist,
486 // so the play_order field can be maintained.
487 String[] cols = new String[] {
490 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistid);
491 Cursor cur = resolver.query(uri, cols, null, null, null);
493 int base = cur.getInt(0);
496 for (int i = 0; i < size; i++) {
497 values[i] = new ContentValues();
498 values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + i));
499 values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, ids[i]);
501 resolver.bulkInsert(uri, values);
502 String message = context.getResources().getQuantityString(
503 R.plurals.NNNtrackstoplaylist, size, Integer.valueOf(size));
504 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
505 //mLastPlaylistSelected = playlistid;
509 public static Cursor query(Context context, Uri uri, String[] projection,
510 String selection, String[] selectionArgs, String sortOrder, int limit) {
512 ContentResolver resolver = context.getContentResolver();
513 if (resolver == null) {
517 uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
519 return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
520 } catch (UnsupportedOperationException ex) {
525 public static Cursor query(Context context, Uri uri, String[] projection,
526 String selection, String[] selectionArgs, String sortOrder) {
527 return query(context, uri, projection, selection, selectionArgs, sortOrder, 0);
530 public static boolean isMediaScannerScanning(Context context) {
531 boolean result = false;
532 Cursor cursor = query(context, MediaStore.getMediaScannerUri(),
533 new String [] { MediaStore.MEDIA_SCANNER_VOLUME }, null, null, null);
534 if (cursor != null) {
535 if (cursor.getCount() == 1) {
536 cursor.moveToFirst();
537 result = "external".equals(cursor.getString(0));
545 public static void setSpinnerState(Activity a) {
546 if (isMediaScannerScanning(a)) {
547 // start the progress spinner
548 a.getWindow().setFeatureInt(
549 Window.FEATURE_INDETERMINATE_PROGRESS,
550 Window.PROGRESS_INDETERMINATE_ON);
552 a.getWindow().setFeatureInt(
553 Window.FEATURE_INDETERMINATE_PROGRESS,
554 Window.PROGRESS_VISIBILITY_ON);
556 // stop the progress spinner
557 a.getWindow().setFeatureInt(
558 Window.FEATURE_INDETERMINATE_PROGRESS,
559 Window.PROGRESS_VISIBILITY_OFF);
563 public static void displayDatabaseError(Activity a) {
564 String status = Environment.getExternalStorageState();
565 int title = R.string.sdcard_error_title;
566 int message = R.string.sdcard_error_message;
568 if (status.equals(Environment.MEDIA_SHARED) ||
569 status.equals(Environment.MEDIA_UNMOUNTED)) {
570 title = R.string.sdcard_busy_title;
571 message = R.string.sdcard_busy_message;
572 } else if (status.equals(Environment.MEDIA_REMOVED)) {
573 title = R.string.sdcard_missing_title;
574 message = R.string.sdcard_missing_message;
575 } else if (status.equals(Environment.MEDIA_MOUNTED)){
576 // The card is mounted, but we didn't get a valid cursor.
577 // This probably means the mediascanner hasn't started scanning the
578 // card yet (there is a small window of time during boot where this
581 Intent intent = new Intent();
582 intent.setClass(a, ScanningProgress.class);
583 a.startActivityForResult(intent, Defs.SCAN_DONE);
585 Log.d(TAG, "sd card: " + status);
589 View v = a.findViewById(R.id.sd_message);
591 v.setVisibility(View.VISIBLE);
593 v = a.findViewById(R.id.sd_icon);
595 v.setVisibility(View.VISIBLE);
597 v = a.findViewById(android.R.id.list);
599 v.setVisibility(View.GONE);
601 TextView tv = (TextView) a.findViewById(R.id.sd_message);
605 public static void hideDatabaseError(Activity a) {
606 View v = a.findViewById(R.id.sd_message);
608 v.setVisibility(View.GONE);
610 v = a.findViewById(R.id.sd_icon);
612 v.setVisibility(View.GONE);
614 v = a.findViewById(android.R.id.list);
616 v.setVisibility(View.VISIBLE);
620 static protected Uri getContentURIForPath(String path) {
621 return Uri.fromFile(new File(path));
625 /* Try to use String.format() as little as possible, because it creates a
626 * new Formatter every time you call it, which is very inefficient.
627 * Reusing an existing Formatter more than tripled the speed of
629 * This Formatter/StringBuilder are also used by makeAlbumSongsLabel()
631 private static StringBuilder sFormatBuilder = new StringBuilder();
632 private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
633 private static final Object[] sTimeArgs = new Object[5];
635 public static String makeTimeString(Context context, long secs) {
636 String durationformat = context.getString(R.string.durationformat);
638 /* Provide multiple arguments so the format can be changed easily
639 * by modifying the xml.
641 sFormatBuilder.setLength(0);
643 final Object[] timeArgs = sTimeArgs;
644 timeArgs[0] = secs / 3600;
645 timeArgs[1] = secs / 60;
646 timeArgs[2] = (secs / 60) % 60;
648 timeArgs[4] = secs % 60;
650 return sFormatter.format(durationformat, timeArgs).toString();
653 public static void shuffleAll(Context context, Cursor cursor) {
654 playAll(context, cursor, 0, true);
657 public static void playAll(Context context, Cursor cursor) {
658 playAll(context, cursor, 0, false);
661 public static void playAll(Context context, Cursor cursor, int position) {
662 playAll(context, cursor, position, false);
665 public static void playAll(Context context, long [] list, int position) {
666 playAll(context, list, position, false);
669 private static void playAll(Context context, Cursor cursor, int position, boolean force_shuffle) {
671 long [] list = getSongListForCursor(cursor);
672 playAll(context, list, position, force_shuffle);
675 private static void playAll(Context context, long [] list, int position, boolean force_shuffle) {
676 if (list.length == 0 || sService == null) {
677 Log.d("MusicUtils", "attempt to play empty song list");
678 // Don't try to play empty playlists. Nothing good will come of it.
679 String message = context.getString(R.string.emptyplaylist, list.length);
680 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
685 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NORMAL);
687 long curid = sService.getAudioId();
688 int curpos = sService.getQueuePosition();
689 if (position != -1 && curpos == position && curid == list[position]) {
690 // The selected file is the file that's currently playing;
691 // figure out if we need to restart with a new playlist,
692 // or just launch the playback activity.
693 long [] playlist = sService.getQueue();
694 if (Arrays.equals(list, playlist)) {
695 // we don't need to set a new list, but we should resume playback if needed
697 return; // the 'finally' block will still run
703 sService.open(list, force_shuffle ? -1 : position);
705 } catch (RemoteException ex) {
707 Intent intent = new Intent("com.android.music.PLAYBACK_VIEWER")
708 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
709 context.startActivity(intent);
713 public static void clearQueue() {
715 sService.removeTracks(0, Integer.MAX_VALUE);
716 } catch (RemoteException ex) {
720 // A really simple BitmapDrawable-like class, that doesn't do
721 // scaling, dithering or filtering.
722 private static class FastBitmapDrawable extends Drawable {
723 private Bitmap mBitmap;
724 public FastBitmapDrawable(Bitmap b) {
728 public void draw(Canvas canvas) {
729 canvas.drawBitmap(mBitmap, 0, 0, null);
732 public int getOpacity() {
733 return PixelFormat.OPAQUE;
736 public void setAlpha(int alpha) {
739 public void setColorFilter(ColorFilter cf) {
743 private static int sArtId = -2;
744 private static Bitmap mCachedBit = null;
745 private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
746 private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
747 private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
748 private static final HashMap<Long, Drawable> sArtCache = new HashMap<Long, Drawable>();
749 private static int sArtCacheId = -1;
753 // 565 is faster to decode and display
754 // and we don't want to dither here because the image will be scaled down later
755 sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.RGB_565;
756 sBitmapOptionsCache.inDither = false;
758 sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
759 sBitmapOptions.inDither = false;
762 public static void initAlbumArtCache() {
764 int id = sService.getMediaMountedCount();
765 if (id != sArtCacheId) {
766 clearAlbumArtCache();
769 } catch (RemoteException e) {
774 public static void clearAlbumArtCache() {
775 synchronized(sArtCache) {
780 public static Drawable getCachedArtwork(Context context, long artIndex, BitmapDrawable defaultArtwork) {
782 synchronized(sArtCache) {
783 d = sArtCache.get(artIndex);
787 final Bitmap icon = defaultArtwork.getBitmap();
788 int w = icon.getWidth();
789 int h = icon.getHeight();
790 Bitmap b = MusicUtils.getArtworkQuick(context, artIndex, w, h);
792 d = new FastBitmapDrawable(b);
793 synchronized(sArtCache) {
794 // the cache may have changed since we checked
795 Drawable value = sArtCache.get(artIndex);
797 sArtCache.put(artIndex, d);
807 // Get album art for specified album. This method will not try to
808 // fall back to getting artwork directly from the file, nor will
809 // it attempt to repair the database.
810 private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
811 // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
812 // used to display this drawable. Take it into account now, so we don't have to
815 ContentResolver res = context.getContentResolver();
816 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
818 ParcelFileDescriptor fd = null;
820 fd = res.openFileDescriptor(uri, "r");
823 // Compute the closest power-of-two scale factor
824 // and pass that to sBitmapOptionsCache.inSampleSize, which will
825 // result in faster decoding and better quality
826 sBitmapOptionsCache.inJustDecodeBounds = true;
827 BitmapFactory.decodeFileDescriptor(
828 fd.getFileDescriptor(), null, sBitmapOptionsCache);
829 int nextWidth = sBitmapOptionsCache.outWidth >> 1;
830 int nextHeight = sBitmapOptionsCache.outHeight >> 1;
831 while (nextWidth>w && nextHeight>h) {
837 sBitmapOptionsCache.inSampleSize = sampleSize;
838 sBitmapOptionsCache.inJustDecodeBounds = false;
839 Bitmap b = BitmapFactory.decodeFileDescriptor(
840 fd.getFileDescriptor(), null, sBitmapOptionsCache);
843 // finally rescale to exactly the size we need
844 if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
845 Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
846 // Bitmap.createScaledBitmap() can return the same bitmap
847 if (tmp != b) b.recycle();
853 } catch (FileNotFoundException e) {
858 } catch (IOException e) {
865 /** Get album art for specified album. You should not pass in the album id
866 * for the "unknown" album here (use -1 instead)
868 public static Bitmap getArtwork(Context context, long song_id, long album_id) {
871 // This is something that is not in the database, so get the album art directly
874 Bitmap bm = getArtworkFromFile(context, song_id, -1);
879 return getDefaultArtwork(context);
882 ContentResolver res = context.getContentResolver();
883 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
885 InputStream in = null;
887 in = res.openInputStream(uri);
888 return BitmapFactory.decodeStream(in, null, sBitmapOptions);
889 } catch (FileNotFoundException ex) {
890 // The album art thumbnail does not actually exist. Maybe the user deleted it, or
891 // maybe it never existed to begin with.
892 Bitmap bm = getArtworkFromFile(context, song_id, album_id);
894 if (bm.getConfig() == null) {
895 bm = bm.copy(Bitmap.Config.RGB_565, false);
897 return getDefaultArtwork(context);
901 bm = getDefaultArtwork(context);
909 } catch (IOException ex) {
917 // get album art for specified file
918 private static final String sExternalMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
919 private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
924 if (albumid < 0 && songid < 0) {
925 throw new IllegalArgumentException("Must specify an album or a song id");
930 Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
931 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
933 FileDescriptor fd = pfd.getFileDescriptor();
934 bm = BitmapFactory.decodeFileDescriptor(fd);
937 Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
938 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
940 FileDescriptor fd = pfd.getFileDescriptor();
941 bm = BitmapFactory.decodeFileDescriptor(fd);
944 } catch (FileNotFoundException ex) {
953 private static Bitmap getDefaultArtwork(Context context) {
954 BitmapFactory.Options opts = new BitmapFactory.Options();
955 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
956 return BitmapFactory.decodeStream(
957 context.getResources().openRawResource(R.drawable.albumart_mp_unknown), null, opts);
960 static int getIntPref(Context context, String name, int def) {
961 SharedPreferences prefs =
962 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
963 return prefs.getInt(name, def);
966 static void setIntPref(Context context, String name, int value) {
967 SharedPreferences prefs =
968 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
969 Editor ed = prefs.edit();
970 ed.putInt(name, value);
974 static void setRingtone(Context context, long id) {
975 ContentResolver resolver = context.getContentResolver();
976 // Set the flag in the database to mark this as a ringtone
977 Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
979 ContentValues values = new ContentValues(2);
980 values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
981 values.put(MediaStore.Audio.Media.IS_ALARM, "1");
982 resolver.update(ringUri, values, null, null);
983 } catch (UnsupportedOperationException ex) {
984 // most likely the card just got unmounted
985 Log.e(TAG, "couldn't set ringtone flag for id " + id);
989 String[] cols = new String[] {
990 MediaStore.Audio.Media._ID,
991 MediaStore.Audio.Media.DATA,
992 MediaStore.Audio.Media.TITLE
995 String where = MediaStore.Audio.Media._ID + "=" + id;
996 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
997 cols, where , null, null);
999 if (cursor != null && cursor.getCount() == 1) {
1000 // Set the system setting to make this the current ringtone
1001 cursor.moveToFirst();
1002 Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
1003 String message = context.getString(R.string.ringtone_set, cursor.getString(2));
1004 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
1007 if (cursor != null) {