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.ColorMatrix;
36 import android.graphics.ColorMatrixColorFilter;
37 import android.graphics.Matrix;
38 import android.graphics.Paint;
39 import android.graphics.PixelFormat;
40 import android.graphics.drawable.BitmapDrawable;
41 import android.graphics.drawable.Drawable;
42 import android.net.Uri;
43 import android.os.Environment;
44 import android.os.ParcelFileDescriptor;
45 import android.os.RemoteException;
46 import android.provider.MediaStore;
47 import android.provider.Settings;
48 import android.text.TextUtils;
49 import android.util.Log;
50 import android.view.Menu;
51 import android.view.MenuItem;
52 import android.view.SubMenu;
53 import android.view.View;
54 import android.view.ViewGroup;
55 import android.view.Window;
56 import android.widget.TabWidget;
57 import android.widget.TextView;
58 import android.widget.Toast;
61 import java.io.FileDescriptor;
62 import java.io.FileNotFoundException;
63 import java.io.IOException;
64 import java.io.InputStream;
65 import java.util.Arrays;
66 import java.util.Formatter;
67 import java.util.HashMap;
68 import java.util.Locale;
70 public class MusicUtils {
72 private static final String TAG = "MusicUtils";
74 public interface Defs {
75 public final static int OPEN_URL = 0;
76 public final static int ADD_TO_PLAYLIST = 1;
77 public final static int USE_AS_RINGTONE = 2;
78 public final static int PLAYLIST_SELECTED = 3;
79 public final static int NEW_PLAYLIST = 4;
80 public final static int PLAY_SELECTION = 5;
81 public final static int GOTO_START = 6;
82 public final static int GOTO_PLAYBACK = 7;
83 public final static int PARTY_SHUFFLE = 8;
84 public final static int SHUFFLE_ALL = 9;
85 public final static int DELETE_ITEM = 10;
86 public final static int SCAN_DONE = 11;
87 public final static int QUEUE = 12;
88 public final static int CHILD_MENU_BASE = 13; // this should be the last item
91 public static String makeAlbumsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
92 // There are two formats for the albums/songs information:
93 // "N Song(s)" - used for unknown artist/album
94 // "N Album(s)" - used for known albums
96 StringBuilder songs_albums = new StringBuilder();
98 Resources r = context.getResources();
101 songs_albums.append(context.getString(R.string.onesong));
103 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
104 sFormatBuilder.setLength(0);
105 sFormatter.format(f, Integer.valueOf(numsongs));
106 songs_albums.append(sFormatBuilder);
109 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
110 sFormatBuilder.setLength(0);
111 sFormatter.format(f, Integer.valueOf(numalbums));
112 songs_albums.append(sFormatBuilder);
113 songs_albums.append(context.getString(R.string.albumsongseparator));
115 return songs_albums.toString();
119 * This is now only used for the query screen
121 public static String makeAlbumsSongsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
122 // There are several formats for the albums/songs information:
123 // "1 Song" - used if there is only 1 song
124 // "N Songs" - used for the "unknown artist" item
125 // "1 Album"/"N Songs"
126 // "N Album"/"M Songs"
127 // Depending on locale, these may need to be further subdivided
129 StringBuilder songs_albums = new StringBuilder();
132 songs_albums.append(context.getString(R.string.onesong));
134 Resources r = context.getResources();
136 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
137 sFormatBuilder.setLength(0);
138 sFormatter.format(f, Integer.valueOf(numalbums));
139 songs_albums.append(sFormatBuilder);
140 songs_albums.append(context.getString(R.string.albumsongseparator));
142 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
143 sFormatBuilder.setLength(0);
144 sFormatter.format(f, Integer.valueOf(numsongs));
145 songs_albums.append(sFormatBuilder);
147 return songs_albums.toString();
150 public static IMediaPlaybackService sService = null;
151 private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
153 public static boolean bindToService(Context context) {
154 return bindToService(context, null);
157 public static boolean bindToService(Context context, ServiceConnection callback) {
158 context.startService(new Intent(context, MediaPlaybackService.class));
159 ServiceBinder sb = new ServiceBinder(callback);
160 sConnectionMap.put(context, sb);
161 return context.bindService((new Intent()).setClass(context,
162 MediaPlaybackService.class), sb, 0);
165 public static void unbindFromService(Context context) {
166 ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
168 Log.e("MusicUtils", "Trying to unbind for unknown Context");
171 context.unbindService(sb);
172 if (sConnectionMap.isEmpty()) {
173 // presumably there is nobody interested in the service at this point,
174 // so don't hang on to the ServiceConnection
179 private static class ServiceBinder implements ServiceConnection {
180 ServiceConnection mCallback;
181 ServiceBinder(ServiceConnection callback) {
182 mCallback = callback;
185 public void onServiceConnected(ComponentName className, android.os.IBinder service) {
186 sService = IMediaPlaybackService.Stub.asInterface(service);
188 if (mCallback != null) {
189 mCallback.onServiceConnected(className, service);
193 public void onServiceDisconnected(ComponentName className) {
194 if (mCallback != null) {
195 mCallback.onServiceDisconnected(className);
201 public static long getCurrentAlbumId() {
202 if (sService != null) {
204 return sService.getAlbumId();
205 } catch (RemoteException ex) {
211 public static long getCurrentArtistId() {
212 if (MusicUtils.sService != null) {
214 return sService.getArtistId();
215 } catch (RemoteException ex) {
221 public static long getCurrentAudioId() {
222 if (MusicUtils.sService != null) {
224 return sService.getAudioId();
225 } catch (RemoteException ex) {
231 public static int getCurrentShuffleMode() {
232 int mode = MediaPlaybackService.SHUFFLE_NONE;
233 if (sService != null) {
235 mode = sService.getShuffleMode();
236 } catch (RemoteException ex) {
242 public static void togglePartyShuffle() {
243 if (sService != null) {
244 int shuffle = getCurrentShuffleMode();
246 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
247 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
249 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
251 } catch (RemoteException ex) {
256 public static void setPartyShuffleMenuIcon(Menu menu) {
257 MenuItem item = menu.findItem(Defs.PARTY_SHUFFLE);
259 int shuffle = MusicUtils.getCurrentShuffleMode();
260 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
261 item.setIcon(R.drawable.ic_menu_party_shuffle);
262 item.setTitle(R.string.party_shuffle_off);
264 item.setIcon(R.drawable.ic_menu_party_shuffle);
265 item.setTitle(R.string.party_shuffle);
271 * Returns true if a file is currently opened for playback (regardless
272 * of whether it's playing or paused).
274 public static boolean isMusicLoaded() {
275 if (MusicUtils.sService != null) {
277 return sService.getPath() != null;
278 } catch (RemoteException ex) {
284 private final static long [] sEmptyList = new long[0];
286 public static long [] getSongListForCursor(Cursor cursor) {
287 if (cursor == null) {
290 int len = cursor.getCount();
291 long [] list = new long[len];
292 cursor.moveToFirst();
295 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
296 } catch (IllegalArgumentException ex) {
297 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
299 for (int i = 0; i < len; i++) {
300 list[i] = cursor.getLong(colidx);
306 public static long [] getSongListForArtist(Context context, long id) {
307 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
308 String where = MediaStore.Audio.Media.ARTIST_ID + "=" + id + " AND " +
309 MediaStore.Audio.Media.IS_MUSIC + "=1";
310 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
312 MediaStore.Audio.Media.ALBUM_KEY + "," + MediaStore.Audio.Media.TRACK);
314 if (cursor != null) {
315 long [] list = getSongListForCursor(cursor);
322 public static long [] getSongListForAlbum(Context context, long id) {
323 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
324 String where = MediaStore.Audio.Media.ALBUM_ID + "=" + id + " AND " +
325 MediaStore.Audio.Media.IS_MUSIC + "=1";
326 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
327 ccols, where, null, MediaStore.Audio.Media.TRACK);
329 if (cursor != null) {
330 long [] list = getSongListForCursor(cursor);
337 public static long [] getSongListForPlaylist(Context context, long plid) {
338 final String[] ccols = new String[] { MediaStore.Audio.Playlists.Members.AUDIO_ID };
339 Cursor cursor = query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
340 ccols, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
342 if (cursor != null) {
343 long [] list = getSongListForCursor(cursor);
350 public static void playPlaylist(Context context, long plid) {
351 long [] list = getSongListForPlaylist(context, plid);
353 playAll(context, list, -1, false);
357 public static long [] getAllSongs(Context context) {
358 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
359 new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC + "=1",
362 if (c == null || c.getCount() == 0) {
365 int len = c.getCount();
366 long [] list = new long[len];
367 for (int i = 0; i < len; i++) {
369 list[i] = c.getLong(0);
381 * Fills out the given submenu with items for "new playlist" and
382 * any existing playlists. When the user selects an item, the
383 * application will receive PLAYLIST_SELECTED with the Uri of
384 * the selected playlist, NEW_PLAYLIST if a new playlist
385 * should be created, and QUEUE if the "current playlist" was
387 * @param context The context to use for creating the menu items
388 * @param sub The submenu to add the items to.
390 public static void makePlaylistMenu(Context context, SubMenu sub) {
391 String[] cols = new String[] {
392 MediaStore.Audio.Playlists._ID,
393 MediaStore.Audio.Playlists.NAME
395 ContentResolver resolver = context.getContentResolver();
396 if (resolver == null) {
397 System.out.println("resolver = null");
399 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''";
400 Cursor cur = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
401 cols, whereclause, null,
402 MediaStore.Audio.Playlists.NAME);
404 sub.add(1, Defs.QUEUE, 0, R.string.queue);
405 sub.add(1, Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
406 if (cur != null && cur.getCount() > 0) {
407 //sub.addSeparator(1, 0);
409 while (! cur.isAfterLast()) {
410 Intent intent = new Intent();
411 intent.putExtra("playlist", cur.getLong(0));
412 // if (cur.getInt(0) == mLastPlaylistSelected) {
413 // sub.add(0, MusicBaseActivity.PLAYLIST_SELECTED, cur.getString(1)).setIntent(intent);
415 sub.add(1, Defs.PLAYLIST_SELECTED, 0, cur.getString(1)).setIntent(intent);
426 public static void clearPlaylist(Context context, int plid) {
428 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plid);
429 context.getContentResolver().delete(uri, null, null);
433 public static void deleteTracks(Context context, long [] list) {
435 String [] cols = new String [] { MediaStore.Audio.Media._ID,
436 MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID };
437 StringBuilder where = new StringBuilder();
438 where.append(MediaStore.Audio.Media._ID + " IN (");
439 for (int i = 0; i < list.length; i++) {
440 where.append(list[i]);
441 if (i < list.length - 1) {
446 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols,
447 where.toString(), null, null);
451 // step 1: remove selected tracks from the current playlist, as well
452 // as from the album art cache
455 while (! c.isAfterLast()) {
456 // remove from current playlist
457 long id = c.getLong(0);
458 sService.removeTrack(id);
459 // remove from album art cache
460 long artIndex = c.getLong(2);
461 synchronized(sArtCache) {
462 sArtCache.remove(artIndex);
466 } catch (RemoteException ex) {
469 // step 2: remove selected tracks from the database
470 context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where.toString(), null);
472 // step 3: remove files from card
474 while (! c.isAfterLast()) {
475 String name = c.getString(1);
476 File f = new File(name);
477 try { // File.delete can throw a security exception
479 // I'm not sure if we'd ever get here (deletion would
480 // have to fail, but no exception thrown)
481 Log.e("MusicUtils", "Failed to delete file " + name);
484 } catch (SecurityException ex) {
491 String message = context.getResources().getQuantityString(
492 R.plurals.NNNtracksdeleted, list.length, Integer.valueOf(list.length));
494 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
495 // We deleted a number of tracks, which could affect any number of things
496 // in the media content domain, so update everything.
497 context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
500 public static void addToCurrentPlaylist(Context context, long [] list) {
501 if (sService == null) {
505 sService.enqueue(list, MediaPlaybackService.LAST);
506 String message = context.getResources().getQuantityString(
507 R.plurals.NNNtrackstoplaylist, list.length, Integer.valueOf(list.length));
508 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
509 } catch (RemoteException ex) {
513 public static void addToPlaylist(Context context, long [] ids, long playlistid) {
515 // this shouldn't happen (the menuitems shouldn't be visible
516 // unless the selected item represents something playable
517 Log.e("MusicBase", "ListSelection null");
519 int size = ids.length;
520 ContentValues values [] = new ContentValues[size];
521 ContentResolver resolver = context.getContentResolver();
522 // need to determine the number of items currently in the playlist,
523 // so the play_order field can be maintained.
524 String[] cols = new String[] {
527 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistid);
528 Cursor cur = resolver.query(uri, cols, null, null, null);
530 int base = cur.getInt(0);
533 for (int i = 0; i < size; i++) {
534 values[i] = new ContentValues();
535 values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + i));
536 values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, ids[i]);
538 resolver.bulkInsert(uri, values);
539 String message = context.getResources().getQuantityString(
540 R.plurals.NNNtrackstoplaylist, size, Integer.valueOf(size));
541 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
542 //mLastPlaylistSelected = playlistid;
546 public static Cursor query(Context context, Uri uri, String[] projection,
547 String selection, String[] selectionArgs, String sortOrder, int limit) {
549 ContentResolver resolver = context.getContentResolver();
550 if (resolver == null) {
554 uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
556 return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
557 } catch (UnsupportedOperationException ex) {
562 public static Cursor query(Context context, Uri uri, String[] projection,
563 String selection, String[] selectionArgs, String sortOrder) {
564 return query(context, uri, projection, selection, selectionArgs, sortOrder, 0);
567 public static boolean isMediaScannerScanning(Context context) {
568 boolean result = false;
569 Cursor cursor = query(context, MediaStore.getMediaScannerUri(),
570 new String [] { MediaStore.MEDIA_SCANNER_VOLUME }, null, null, null);
571 if (cursor != null) {
572 if (cursor.getCount() == 1) {
573 cursor.moveToFirst();
574 result = "external".equals(cursor.getString(0));
582 public static void setSpinnerState(Activity a) {
583 if (isMediaScannerScanning(a)) {
584 // start the progress spinner
585 a.getWindow().setFeatureInt(
586 Window.FEATURE_INDETERMINATE_PROGRESS,
587 Window.PROGRESS_INDETERMINATE_ON);
589 a.getWindow().setFeatureInt(
590 Window.FEATURE_INDETERMINATE_PROGRESS,
591 Window.PROGRESS_VISIBILITY_ON);
593 // stop the progress spinner
594 a.getWindow().setFeatureInt(
595 Window.FEATURE_INDETERMINATE_PROGRESS,
596 Window.PROGRESS_VISIBILITY_OFF);
600 private static String mLastSdStatus;
602 public static void displayDatabaseError(Activity a) {
603 String status = Environment.getExternalStorageState();
604 int title = R.string.sdcard_error_title;
605 int message = R.string.sdcard_error_message;
607 if (status.equals(Environment.MEDIA_SHARED) ||
608 status.equals(Environment.MEDIA_UNMOUNTED)) {
609 title = R.string.sdcard_busy_title;
610 message = R.string.sdcard_busy_message;
611 } else if (status.equals(Environment.MEDIA_REMOVED)) {
612 title = R.string.sdcard_missing_title;
613 message = R.string.sdcard_missing_message;
614 } else if (status.equals(Environment.MEDIA_MOUNTED)){
615 // The card is mounted, but we didn't get a valid cursor.
616 // This probably means the mediascanner hasn't started scanning the
617 // card yet (there is a small window of time during boot where this
620 Intent intent = new Intent();
621 intent.setClass(a, ScanningProgress.class);
622 a.startActivityForResult(intent, Defs.SCAN_DONE);
623 } else if (!TextUtils.equals(mLastSdStatus, status)) {
624 mLastSdStatus = status;
625 Log.d(TAG, "sd card: " + status);
629 View v = a.findViewById(R.id.sd_message);
631 v.setVisibility(View.VISIBLE);
633 v = a.findViewById(R.id.sd_icon);
635 v.setVisibility(View.VISIBLE);
637 v = a.findViewById(android.R.id.list);
639 v.setVisibility(View.GONE);
641 v = a.findViewById(R.id.buttonbar);
643 v.setVisibility(View.GONE);
645 TextView tv = (TextView) a.findViewById(R.id.sd_message);
649 public static void hideDatabaseError(Activity a) {
650 View v = a.findViewById(R.id.sd_message);
652 v.setVisibility(View.GONE);
654 v = a.findViewById(R.id.sd_icon);
656 v.setVisibility(View.GONE);
658 v = a.findViewById(android.R.id.list);
660 v.setVisibility(View.VISIBLE);
664 static protected Uri getContentURIForPath(String path) {
665 return Uri.fromFile(new File(path));
669 /* Try to use String.format() as little as possible, because it creates a
670 * new Formatter every time you call it, which is very inefficient.
671 * Reusing an existing Formatter more than tripled the speed of
673 * This Formatter/StringBuilder are also used by makeAlbumSongsLabel()
675 private static StringBuilder sFormatBuilder = new StringBuilder();
676 private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
677 private static final Object[] sTimeArgs = new Object[5];
679 public static String makeTimeString(Context context, long secs) {
680 String durationformat = context.getString(
681 secs < 3600 ? R.string.durationformatshort : R.string.durationformatlong);
683 /* Provide multiple arguments so the format can be changed easily
684 * by modifying the xml.
686 sFormatBuilder.setLength(0);
688 final Object[] timeArgs = sTimeArgs;
689 timeArgs[0] = secs / 3600;
690 timeArgs[1] = secs / 60;
691 timeArgs[2] = (secs / 60) % 60;
693 timeArgs[4] = secs % 60;
695 return sFormatter.format(durationformat, timeArgs).toString();
698 public static void shuffleAll(Context context, Cursor cursor) {
699 playAll(context, cursor, 0, true);
702 public static void playAll(Context context, Cursor cursor) {
703 playAll(context, cursor, 0, false);
706 public static void playAll(Context context, Cursor cursor, int position) {
707 playAll(context, cursor, position, false);
710 public static void playAll(Context context, long [] list, int position) {
711 playAll(context, list, position, false);
714 private static void playAll(Context context, Cursor cursor, int position, boolean force_shuffle) {
716 long [] list = getSongListForCursor(cursor);
717 playAll(context, list, position, force_shuffle);
720 private static void playAll(Context context, long [] list, int position, boolean force_shuffle) {
721 if (list.length == 0 || sService == null) {
722 Log.d("MusicUtils", "attempt to play empty song list");
723 // Don't try to play empty playlists. Nothing good will come of it.
724 String message = context.getString(R.string.emptyplaylist, list.length);
725 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
730 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NORMAL);
732 long curid = sService.getAudioId();
733 int curpos = sService.getQueuePosition();
734 if (position != -1 && curpos == position && curid == list[position]) {
735 // The selected file is the file that's currently playing;
736 // figure out if we need to restart with a new playlist,
737 // or just launch the playback activity.
738 long [] playlist = sService.getQueue();
739 if (Arrays.equals(list, playlist)) {
740 // we don't need to set a new list, but we should resume playback if needed
742 return; // the 'finally' block will still run
748 sService.open(list, force_shuffle ? -1 : position);
750 } catch (RemoteException ex) {
752 Intent intent = new Intent(context, MediaPlaybackActivity.class)
753 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
754 context.startActivity(intent);
758 public static void clearQueue() {
760 sService.removeTracks(0, Integer.MAX_VALUE);
761 } catch (RemoteException ex) {
765 // A really simple BitmapDrawable-like class, that doesn't do
766 // scaling, dithering or filtering.
767 private static class FastBitmapDrawable extends Drawable {
768 private Bitmap mBitmap;
769 public FastBitmapDrawable(Bitmap b) {
773 public void draw(Canvas canvas) {
774 canvas.drawBitmap(mBitmap, 0, 0, null);
777 public int getOpacity() {
778 return PixelFormat.OPAQUE;
781 public void setAlpha(int alpha) {
784 public void setColorFilter(ColorFilter cf) {
788 private static int sArtId = -2;
789 private static Bitmap mCachedBit = null;
790 private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
791 private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
792 private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
793 private static final HashMap<Long, Drawable> sArtCache = new HashMap<Long, Drawable>();
794 private static int sArtCacheId = -1;
798 // 565 is faster to decode and display
799 // and we don't want to dither here because the image will be scaled down later
800 sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.RGB_565;
801 sBitmapOptionsCache.inDither = false;
803 sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
804 sBitmapOptions.inDither = false;
807 public static void initAlbumArtCache() {
809 int id = sService.getMediaMountedCount();
810 if (id != sArtCacheId) {
811 clearAlbumArtCache();
814 } catch (RemoteException e) {
819 public static void clearAlbumArtCache() {
820 synchronized(sArtCache) {
825 public static Drawable getCachedArtwork(Context context, long artIndex, BitmapDrawable defaultArtwork) {
827 synchronized(sArtCache) {
828 d = sArtCache.get(artIndex);
832 final Bitmap icon = defaultArtwork.getBitmap();
833 int w = icon.getWidth();
834 int h = icon.getHeight();
835 Bitmap b = MusicUtils.getArtworkQuick(context, artIndex, w, h);
837 d = new FastBitmapDrawable(b);
838 synchronized(sArtCache) {
839 // the cache may have changed since we checked
840 Drawable value = sArtCache.get(artIndex);
842 sArtCache.put(artIndex, d);
852 // Get album art for specified album. This method will not try to
853 // fall back to getting artwork directly from the file, nor will
854 // it attempt to repair the database.
855 private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
856 // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
857 // used to display this drawable. Take it into account now, so we don't have to
860 ContentResolver res = context.getContentResolver();
861 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
863 ParcelFileDescriptor fd = null;
865 fd = res.openFileDescriptor(uri, "r");
868 // Compute the closest power-of-two scale factor
869 // and pass that to sBitmapOptionsCache.inSampleSize, which will
870 // result in faster decoding and better quality
871 sBitmapOptionsCache.inJustDecodeBounds = true;
872 BitmapFactory.decodeFileDescriptor(
873 fd.getFileDescriptor(), null, sBitmapOptionsCache);
874 int nextWidth = sBitmapOptionsCache.outWidth >> 1;
875 int nextHeight = sBitmapOptionsCache.outHeight >> 1;
876 while (nextWidth>w && nextHeight>h) {
882 sBitmapOptionsCache.inSampleSize = sampleSize;
883 sBitmapOptionsCache.inJustDecodeBounds = false;
884 Bitmap b = BitmapFactory.decodeFileDescriptor(
885 fd.getFileDescriptor(), null, sBitmapOptionsCache);
888 // finally rescale to exactly the size we need
889 if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
890 Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
891 // Bitmap.createScaledBitmap() can return the same bitmap
892 if (tmp != b) b.recycle();
898 } catch (FileNotFoundException e) {
903 } catch (IOException e) {
910 /** Get album art for specified album. You should not pass in the album id
911 * for the "unknown" album here (use -1 instead)
912 * This method always returns the default album art icon when no album art is found.
914 public static Bitmap getArtwork(Context context, long song_id, long album_id) {
915 return getArtwork(context, song_id, album_id, true);
918 /** Get album art for specified album. You should not pass in the album id
919 * for the "unknown" album here (use -1 instead)
921 public static Bitmap getArtwork(Context context, long song_id, long album_id,
922 boolean allowdefault) {
925 // This is something that is not in the database, so get the album art directly
928 Bitmap bm = getArtworkFromFile(context, song_id, -1);
934 return getDefaultArtwork(context);
939 ContentResolver res = context.getContentResolver();
940 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
942 InputStream in = null;
944 in = res.openInputStream(uri);
945 return BitmapFactory.decodeStream(in, null, sBitmapOptions);
946 } catch (FileNotFoundException ex) {
947 // The album art thumbnail does not actually exist. Maybe the user deleted it, or
948 // maybe it never existed to begin with.
949 Bitmap bm = getArtworkFromFile(context, song_id, album_id);
951 if (bm.getConfig() == null) {
952 bm = bm.copy(Bitmap.Config.RGB_565, false);
953 if (bm == null && allowdefault) {
954 return getDefaultArtwork(context);
957 } else if (allowdefault) {
958 bm = getDefaultArtwork(context);
966 } catch (IOException ex) {
974 // get album art for specified file
975 private static final String sExternalMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
976 private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
981 if (albumid < 0 && songid < 0) {
982 throw new IllegalArgumentException("Must specify an album or a song id");
987 Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
988 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
990 FileDescriptor fd = pfd.getFileDescriptor();
991 bm = BitmapFactory.decodeFileDescriptor(fd);
994 Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
995 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
997 FileDescriptor fd = pfd.getFileDescriptor();
998 bm = BitmapFactory.decodeFileDescriptor(fd);
1001 } catch (FileNotFoundException ex) {
1010 private static Bitmap getDefaultArtwork(Context context) {
1011 BitmapFactory.Options opts = new BitmapFactory.Options();
1012 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
1013 return BitmapFactory.decodeStream(
1014 context.getResources().openRawResource(R.drawable.albumart_mp_unknown), null, opts);
1017 static int getIntPref(Context context, String name, int def) {
1018 SharedPreferences prefs =
1019 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
1020 return prefs.getInt(name, def);
1023 static void setIntPref(Context context, String name, int value) {
1024 SharedPreferences prefs =
1025 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
1026 Editor ed = prefs.edit();
1027 ed.putInt(name, value);
1031 static void setRingtone(Context context, long id) {
1032 ContentResolver resolver = context.getContentResolver();
1033 // Set the flag in the database to mark this as a ringtone
1034 Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
1036 ContentValues values = new ContentValues(2);
1037 values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
1038 values.put(MediaStore.Audio.Media.IS_ALARM, "1");
1039 resolver.update(ringUri, values, null, null);
1040 } catch (UnsupportedOperationException ex) {
1041 // most likely the card just got unmounted
1042 Log.e(TAG, "couldn't set ringtone flag for id " + id);
1046 String[] cols = new String[] {
1047 MediaStore.Audio.Media._ID,
1048 MediaStore.Audio.Media.DATA,
1049 MediaStore.Audio.Media.TITLE
1052 String where = MediaStore.Audio.Media._ID + "=" + id;
1053 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
1054 cols, where , null, null);
1056 if (cursor != null && cursor.getCount() == 1) {
1057 // Set the system setting to make this the current ringtone
1058 cursor.moveToFirst();
1059 Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
1060 String message = context.getString(R.string.ringtone_set, cursor.getString(2));
1061 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
1064 if (cursor != null) {
1070 static int sActiveTabIndex = -1;
1072 static boolean updateButtonBar(Activity a, int highlight) {
1073 final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);
1074 boolean withtabs = false;
1075 Intent intent = a.getIntent();
1076 if (intent != null) {
1077 withtabs = intent.getBooleanExtra("withtabs", false);
1080 if (highlight == 0 || !withtabs) {
1081 ll.setVisibility(View.GONE);
1083 } else if (withtabs) {
1084 ll.setVisibility(View.VISIBLE);
1086 for (int i = ll.getChildCount() - 1; i >= 0; i--) {
1088 View v = ll.getChildAt(i);
1089 boolean isActive = (v.getId() == highlight);
1091 ll.setCurrentTab(i);
1092 sActiveTabIndex = i;
1095 v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
1097 public void onFocusChange(View v, boolean hasFocus) {
1099 for (int i = 0; i < ll.getTabCount(); i++) {
1100 if (ll.getChildTabViewAt(i) == v) {
1101 ll.setCurrentTab(i);
1102 processTabClick((Activity)ll.getContext(), v, ll.getChildAt(sActiveTabIndex).getId());
1109 v.setOnClickListener(new View.OnClickListener() {
1111 public void onClick(View v) {
1112 processTabClick((Activity)ll.getContext(), v, ll.getChildAt(sActiveTabIndex).getId());
1118 static void processTabClick(Activity a, View v, int current) {
1120 if (id == current) {
1124 final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);
1125 ll.setCurrentTab((Integer) v.getTag());
1128 if (id != R.id.nowplayingtab) {
1129 setIntPref(a, "activetab", id);
1133 static void activateTab(Activity a, int id) {
1134 Intent intent = new Intent(Intent.ACTION_PICK);
1136 case R.id.artisttab:
1137 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum");
1140 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
1143 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
1145 case R.id.playlisttab:
1146 intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE);
1148 case R.id.nowplayingtab:
1149 intent = new Intent(a, MediaPlaybackActivity.class);
1150 a.startActivity(intent);
1151 // fall through and return
1155 intent.putExtra("withtabs", true);
1156 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1157 a.startActivity(intent);
1159 a.overridePendingTransition(0, 0);
1162 static void updateNowPlaying(Activity a) {
1163 View nowPlayingView = a.findViewById(R.id.nowplaying);
1164 if (nowPlayingView == null) {
1168 boolean withtabs = false;
1169 Intent intent = a.getIntent();
1170 if (intent != null) {
1171 withtabs = intent.getBooleanExtra("withtabs", false);
1173 if (true && MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
1174 TextView title = (TextView) nowPlayingView.findViewById(R.id.title);
1175 TextView artist = (TextView) nowPlayingView.findViewById(R.id.artist);
1176 title.setText(MusicUtils.sService.getTrackName());
1177 String artistName = MusicUtils.sService.getArtistName();
1178 if (MediaStore.UNKNOWN_STRING.equals(artistName)) {
1179 artistName = a.getString(R.string.unknown_artist_name);
1181 artist.setText(artistName);
1182 //mNowPlayingView.setOnFocusChangeListener(mFocuser);
1183 //mNowPlayingView.setOnClickListener(this);
1184 nowPlayingView.setVisibility(View.VISIBLE);
1185 nowPlayingView.setOnClickListener(new View.OnClickListener() {
1187 public void onClick(View v) {
1188 Context c = v.getContext();
1189 c.startActivity(new Intent(c, MediaPlaybackActivity.class));
1193 } catch (RemoteException ex) {
1195 nowPlayingView.setVisibility(View.GONE);
1198 static void setBackground(View v, Bitmap bm) {
1201 v.setBackgroundResource(0);
1205 int vwidth = v.getWidth();
1206 int vheight = v.getHeight();
1207 int bwidth = bm.getWidth();
1208 int bheight = bm.getHeight();
1209 float scalex = (float) vwidth / bwidth;
1210 float scaley = (float) vheight / bheight;
1211 float scale = Math.max(scalex, scaley) * 1.3f;
1213 Bitmap.Config config = Bitmap.Config.ARGB_8888;
1214 Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config);
1215 Canvas c = new Canvas(bg);
1216 Paint paint = new Paint();
1217 paint.setAntiAlias(true);
1218 paint.setFilterBitmap(true);
1219 ColorMatrix greymatrix = new ColorMatrix();
1220 greymatrix.setSaturation(0);
1221 ColorMatrix darkmatrix = new ColorMatrix();
1222 darkmatrix.setScale(.3f, .3f, .3f, 1.0f);
1223 greymatrix.postConcat(darkmatrix);
1224 ColorFilter filter = new ColorMatrixColorFilter(greymatrix);
1225 paint.setColorFilter(filter);
1226 Matrix matrix = new Matrix();
1227 matrix.setTranslate(-bwidth/2, -bheight/2); // move bitmap center to origin
1228 matrix.postRotate(10);
1229 matrix.postScale(scale, scale);
1230 matrix.postTranslate(vwidth/2, vheight/2); // Move bitmap center to view center
1231 c.drawBitmap(bm, matrix, paint);
1232 v.setBackgroundDrawable(new BitmapDrawable(bg));
1235 static int getCardId(Context context) {
1236 ContentResolver res = context.getContentResolver();
1237 Cursor c = res.query(Uri.parse("content://media/external/fs_id"), null, null, null, null);