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.media.MediaFile;
39 import android.net.Uri;
40 import android.os.Environment;
41 import android.os.ParcelFileDescriptor;
42 import android.os.RemoteException;
43 import android.provider.MediaStore;
44 import android.provider.Settings;
45 import android.text.TextUtils;
46 import android.util.Log;
47 import android.view.Menu;
48 import android.view.MenuItem;
49 import android.view.SubMenu;
50 import android.view.View;
51 import android.view.ViewGroup;
52 import android.view.Window;
53 import android.widget.TabWidget;
54 import android.widget.TextView;
55 import android.widget.Toast;
58 import java.io.FileDescriptor;
59 import java.io.FileNotFoundException;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.util.Arrays;
63 import java.util.Formatter;
64 import java.util.HashMap;
65 import java.util.Locale;
67 public class MusicUtils {
69 private static final String TAG = "MusicUtils";
71 public interface Defs {
72 public final static int OPEN_URL = 0;
73 public final static int ADD_TO_PLAYLIST = 1;
74 public final static int USE_AS_RINGTONE = 2;
75 public final static int PLAYLIST_SELECTED = 3;
76 public final static int NEW_PLAYLIST = 4;
77 public final static int PLAY_SELECTION = 5;
78 public final static int GOTO_START = 6;
79 public final static int GOTO_PLAYBACK = 7;
80 public final static int PARTY_SHUFFLE = 8;
81 public final static int SHUFFLE_ALL = 9;
82 public final static int DELETE_ITEM = 10;
83 public final static int SCAN_DONE = 11;
84 public final static int QUEUE = 12;
85 public final static int CHILD_MENU_BASE = 13; // this should be the last item
88 public static String makeAlbumsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
89 // There are two formats for the albums/songs information:
90 // "N Song(s)" - used for unknown artist/album
91 // "N Album(s)" - used for known albums
93 StringBuilder songs_albums = new StringBuilder();
95 Resources r = context.getResources();
98 songs_albums.append(context.getString(R.string.onesong));
100 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
101 sFormatBuilder.setLength(0);
102 sFormatter.format(f, Integer.valueOf(numsongs));
103 songs_albums.append(sFormatBuilder);
106 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
107 sFormatBuilder.setLength(0);
108 sFormatter.format(f, Integer.valueOf(numalbums));
109 songs_albums.append(sFormatBuilder);
110 songs_albums.append(context.getString(R.string.albumsongseparator));
112 return songs_albums.toString();
116 * This is now only used for the query screen
118 public static String makeAlbumsSongsLabel(Context context, int numalbums, int numsongs, boolean isUnknown) {
119 // There are several formats for the albums/songs information:
120 // "1 Song" - used if there is only 1 song
121 // "N Songs" - used for the "unknown artist" item
122 // "1 Album"/"N Songs"
123 // "N Album"/"M Songs"
124 // Depending on locale, these may need to be further subdivided
126 StringBuilder songs_albums = new StringBuilder();
129 songs_albums.append(context.getString(R.string.onesong));
131 Resources r = context.getResources();
133 String f = r.getQuantityText(R.plurals.Nalbums, numalbums).toString();
134 sFormatBuilder.setLength(0);
135 sFormatter.format(f, Integer.valueOf(numalbums));
136 songs_albums.append(sFormatBuilder);
137 songs_albums.append(context.getString(R.string.albumsongseparator));
139 String f = r.getQuantityText(R.plurals.Nsongs, numsongs).toString();
140 sFormatBuilder.setLength(0);
141 sFormatter.format(f, Integer.valueOf(numsongs));
142 songs_albums.append(sFormatBuilder);
144 return songs_albums.toString();
147 public static IMediaPlaybackService sService = null;
148 private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
150 public static boolean bindToService(Context context) {
151 return bindToService(context, null);
154 public static boolean bindToService(Context context, ServiceConnection callback) {
155 context.startService(new Intent(context, MediaPlaybackService.class));
156 ServiceBinder sb = new ServiceBinder(callback);
157 sConnectionMap.put(context, sb);
158 return context.bindService((new Intent()).setClass(context,
159 MediaPlaybackService.class), sb, 0);
162 public static void unbindFromService(Context context) {
163 ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
165 Log.e("MusicUtils", "Trying to unbind for unknown Context");
168 context.unbindService(sb);
169 if (sConnectionMap.isEmpty()) {
170 // presumably there is nobody interested in the service at this point,
171 // so don't hang on to the ServiceConnection
176 private static class ServiceBinder implements ServiceConnection {
177 ServiceConnection mCallback;
178 ServiceBinder(ServiceConnection callback) {
179 mCallback = callback;
182 public void onServiceConnected(ComponentName className, android.os.IBinder service) {
183 sService = IMediaPlaybackService.Stub.asInterface(service);
185 if (mCallback != null) {
186 mCallback.onServiceConnected(className, service);
190 public void onServiceDisconnected(ComponentName className) {
191 if (mCallback != null) {
192 mCallback.onServiceDisconnected(className);
198 public static long getCurrentAlbumId() {
199 if (sService != null) {
201 return sService.getAlbumId();
202 } catch (RemoteException ex) {
208 public static long getCurrentArtistId() {
209 if (MusicUtils.sService != null) {
211 return sService.getArtistId();
212 } catch (RemoteException ex) {
218 public static long getCurrentAudioId() {
219 if (MusicUtils.sService != null) {
221 return sService.getAudioId();
222 } catch (RemoteException ex) {
228 public static int getCurrentShuffleMode() {
229 int mode = MediaPlaybackService.SHUFFLE_NONE;
230 if (sService != null) {
232 mode = sService.getShuffleMode();
233 } catch (RemoteException ex) {
239 public static void togglePartyShuffle() {
240 if (sService != null) {
241 int shuffle = getCurrentShuffleMode();
243 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
244 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
246 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
248 } catch (RemoteException ex) {
253 public static void setPartyShuffleMenuIcon(Menu menu) {
254 MenuItem item = menu.findItem(Defs.PARTY_SHUFFLE);
256 int shuffle = MusicUtils.getCurrentShuffleMode();
257 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
258 item.setIcon(R.drawable.ic_menu_party_shuffle);
259 item.setTitle(R.string.party_shuffle_off);
261 item.setIcon(R.drawable.ic_menu_party_shuffle);
262 item.setTitle(R.string.party_shuffle);
268 * Returns true if a file is currently opened for playback (regardless
269 * of whether it's playing or paused).
271 public static boolean isMusicLoaded() {
272 if (MusicUtils.sService != null) {
274 return sService.getPath() != null;
275 } catch (RemoteException ex) {
281 private final static long [] sEmptyList = new long[0];
283 public static long [] getSongListForCursor(Cursor cursor) {
284 if (cursor == null) {
287 int len = cursor.getCount();
288 long [] list = new long[len];
289 cursor.moveToFirst();
292 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
293 } catch (IllegalArgumentException ex) {
294 colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
296 for (int i = 0; i < len; i++) {
297 list[i] = cursor.getLong(colidx);
303 public static long [] getSongListForArtist(Context context, long id) {
304 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
305 String where = MediaStore.Audio.Media.ARTIST_ID + "=" + id + " AND " +
306 MediaStore.Audio.Media.IS_MUSIC + "=1";
307 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
309 MediaStore.Audio.Media.ALBUM_KEY + "," + MediaStore.Audio.Media.TRACK);
311 if (cursor != null) {
312 long [] list = getSongListForCursor(cursor);
319 public static long [] getSongListForAlbum(Context context, long id) {
320 final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
321 String where = MediaStore.Audio.Media.ALBUM_ID + "=" + id + " AND " +
322 MediaStore.Audio.Media.IS_MUSIC + "=1";
323 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
324 ccols, where, null, MediaStore.Audio.Media.TRACK);
326 if (cursor != null) {
327 long [] list = getSongListForCursor(cursor);
334 public static long [] getSongListForPlaylist(Context context, long plid) {
335 final String[] ccols = new String[] { MediaStore.Audio.Playlists.Members.AUDIO_ID };
336 Cursor cursor = query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
337 ccols, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
339 if (cursor != null) {
340 long [] list = getSongListForCursor(cursor);
347 public static void playPlaylist(Context context, long plid) {
348 long [] list = getSongListForPlaylist(context, plid);
350 playAll(context, list, -1, false);
354 public static long [] getAllSongs(Context context) {
355 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
356 new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC + "=1",
359 if (c == null || c.getCount() == 0) {
362 int len = c.getCount();
363 long [] list = new long[len];
364 for (int i = 0; i < len; i++) {
366 list[i] = c.getLong(0);
378 * Fills out the given submenu with items for "new playlist" and
379 * any existing playlists. When the user selects an item, the
380 * application will receive PLAYLIST_SELECTED with the Uri of
381 * the selected playlist, NEW_PLAYLIST if a new playlist
382 * should be created, and QUEUE if the "current playlist" was
384 * @param context The context to use for creating the menu items
385 * @param sub The submenu to add the items to.
387 public static void makePlaylistMenu(Context context, SubMenu sub) {
388 String[] cols = new String[] {
389 MediaStore.Audio.Playlists._ID,
390 MediaStore.Audio.Playlists.NAME
392 ContentResolver resolver = context.getContentResolver();
393 if (resolver == null) {
394 System.out.println("resolver = null");
396 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''";
397 Cursor cur = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
398 cols, whereclause, null,
399 MediaStore.Audio.Playlists.NAME);
401 sub.add(1, Defs.QUEUE, 0, R.string.queue);
402 sub.add(1, Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
403 if (cur != null && cur.getCount() > 0) {
404 //sub.addSeparator(1, 0);
406 while (! cur.isAfterLast()) {
407 Intent intent = new Intent();
408 intent.putExtra("playlist", cur.getLong(0));
409 // if (cur.getInt(0) == mLastPlaylistSelected) {
410 // sub.add(0, MusicBaseActivity.PLAYLIST_SELECTED, cur.getString(1)).setIntent(intent);
412 sub.add(1, Defs.PLAYLIST_SELECTED, 0, cur.getString(1)).setIntent(intent);
423 public static void clearPlaylist(Context context, int plid) {
425 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plid);
426 context.getContentResolver().delete(uri, null, null);
430 public static void deleteTracks(Context context, long [] list) {
432 String [] cols = new String [] { MediaStore.Audio.Media._ID,
433 MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID };
434 StringBuilder where = new StringBuilder();
435 where.append(MediaStore.Audio.Media._ID + " IN (");
436 for (int i = 0; i < list.length; i++) {
437 where.append(list[i]);
438 if (i < list.length - 1) {
443 Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols,
444 where.toString(), null, null);
448 // step 1: remove selected tracks from the current playlist, as well
449 // as from the album art cache
452 while (! c.isAfterLast()) {
453 // remove from current playlist
454 long id = c.getLong(0);
455 sService.removeTrack(id);
456 // remove from album art cache
457 long artIndex = c.getLong(2);
458 synchronized(sArtCache) {
459 sArtCache.remove(artIndex);
463 } catch (RemoteException ex) {
466 // step 2: remove selected tracks from the database
467 context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where.toString(), null);
469 // step 3: remove files from card
471 while (! c.isAfterLast()) {
472 String name = c.getString(1);
473 File f = new File(name);
474 try { // File.delete can throw a security exception
476 // I'm not sure if we'd ever get here (deletion would
477 // have to fail, but no exception thrown)
478 Log.e("MusicUtils", "Failed to delete file " + name);
481 } catch (SecurityException ex) {
488 String message = context.getResources().getQuantityString(
489 R.plurals.NNNtracksdeleted, list.length, Integer.valueOf(list.length));
491 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
492 // We deleted a number of tracks, which could affect any number of things
493 // in the media content domain, so update everything.
494 context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
497 public static void addToCurrentPlaylist(Context context, long [] list) {
498 if (sService == null) {
502 sService.enqueue(list, MediaPlaybackService.LAST);
503 String message = context.getResources().getQuantityString(
504 R.plurals.NNNtrackstoplaylist, list.length, Integer.valueOf(list.length));
505 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
506 } catch (RemoteException ex) {
510 public static void addToPlaylist(Context context, long [] ids, long playlistid) {
512 // this shouldn't happen (the menuitems shouldn't be visible
513 // unless the selected item represents something playable
514 Log.e("MusicBase", "ListSelection null");
516 int size = ids.length;
517 ContentValues values [] = new ContentValues[size];
518 ContentResolver resolver = context.getContentResolver();
519 // need to determine the number of items currently in the playlist,
520 // so the play_order field can be maintained.
521 String[] cols = new String[] {
524 Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistid);
525 Cursor cur = resolver.query(uri, cols, null, null, null);
527 int base = cur.getInt(0);
530 for (int i = 0; i < size; i++) {
531 values[i] = new ContentValues();
532 values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + i));
533 values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, ids[i]);
535 resolver.bulkInsert(uri, values);
536 String message = context.getResources().getQuantityString(
537 R.plurals.NNNtrackstoplaylist, size, Integer.valueOf(size));
538 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
539 //mLastPlaylistSelected = playlistid;
543 public static Cursor query(Context context, Uri uri, String[] projection,
544 String selection, String[] selectionArgs, String sortOrder, int limit) {
546 ContentResolver resolver = context.getContentResolver();
547 if (resolver == null) {
551 uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
553 return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
554 } catch (UnsupportedOperationException ex) {
559 public static Cursor query(Context context, Uri uri, String[] projection,
560 String selection, String[] selectionArgs, String sortOrder) {
561 return query(context, uri, projection, selection, selectionArgs, sortOrder, 0);
564 public static boolean isMediaScannerScanning(Context context) {
565 boolean result = false;
566 Cursor cursor = query(context, MediaStore.getMediaScannerUri(),
567 new String [] { MediaStore.MEDIA_SCANNER_VOLUME }, null, null, null);
568 if (cursor != null) {
569 if (cursor.getCount() == 1) {
570 cursor.moveToFirst();
571 result = "external".equals(cursor.getString(0));
579 public static void setSpinnerState(Activity a) {
580 if (isMediaScannerScanning(a)) {
581 // start the progress spinner
582 a.getWindow().setFeatureInt(
583 Window.FEATURE_INDETERMINATE_PROGRESS,
584 Window.PROGRESS_INDETERMINATE_ON);
586 a.getWindow().setFeatureInt(
587 Window.FEATURE_INDETERMINATE_PROGRESS,
588 Window.PROGRESS_VISIBILITY_ON);
590 // stop the progress spinner
591 a.getWindow().setFeatureInt(
592 Window.FEATURE_INDETERMINATE_PROGRESS,
593 Window.PROGRESS_VISIBILITY_OFF);
597 private static String mLastSdStatus;
599 public static void displayDatabaseError(Activity a) {
600 String status = Environment.getExternalStorageState();
601 int title = R.string.sdcard_error_title;
602 int message = R.string.sdcard_error_message;
604 if (status.equals(Environment.MEDIA_SHARED) ||
605 status.equals(Environment.MEDIA_UNMOUNTED)) {
606 title = R.string.sdcard_busy_title;
607 message = R.string.sdcard_busy_message;
608 } else if (status.equals(Environment.MEDIA_REMOVED)) {
609 title = R.string.sdcard_missing_title;
610 message = R.string.sdcard_missing_message;
611 } else if (status.equals(Environment.MEDIA_MOUNTED)){
612 // The card is mounted, but we didn't get a valid cursor.
613 // This probably means the mediascanner hasn't started scanning the
614 // card yet (there is a small window of time during boot where this
617 Intent intent = new Intent();
618 intent.setClass(a, ScanningProgress.class);
619 a.startActivityForResult(intent, Defs.SCAN_DONE);
620 } else if (!TextUtils.equals(mLastSdStatus, status)) {
621 mLastSdStatus = status;
622 Log.d(TAG, "sd card: " + status);
626 View v = a.findViewById(R.id.sd_message);
628 v.setVisibility(View.VISIBLE);
630 v = a.findViewById(R.id.sd_icon);
632 v.setVisibility(View.VISIBLE);
634 v = a.findViewById(android.R.id.list);
636 v.setVisibility(View.GONE);
638 v = a.findViewById(R.id.buttonbar);
640 v.setVisibility(View.GONE);
642 TextView tv = (TextView) a.findViewById(R.id.sd_message);
646 public static void hideDatabaseError(Activity a) {
647 View v = a.findViewById(R.id.sd_message);
649 v.setVisibility(View.GONE);
651 v = a.findViewById(R.id.sd_icon);
653 v.setVisibility(View.GONE);
655 v = a.findViewById(android.R.id.list);
657 v.setVisibility(View.VISIBLE);
661 static protected Uri getContentURIForPath(String path) {
662 return Uri.fromFile(new File(path));
666 /* Try to use String.format() as little as possible, because it creates a
667 * new Formatter every time you call it, which is very inefficient.
668 * Reusing an existing Formatter more than tripled the speed of
670 * This Formatter/StringBuilder are also used by makeAlbumSongsLabel()
672 private static StringBuilder sFormatBuilder = new StringBuilder();
673 private static Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
674 private static final Object[] sTimeArgs = new Object[5];
676 public static String makeTimeString(Context context, long secs) {
677 String durationformat = context.getString(
678 secs < 3600 ? R.string.durationformatshort : R.string.durationformatlong);
680 /* Provide multiple arguments so the format can be changed easily
681 * by modifying the xml.
683 sFormatBuilder.setLength(0);
685 final Object[] timeArgs = sTimeArgs;
686 timeArgs[0] = secs / 3600;
687 timeArgs[1] = secs / 60;
688 timeArgs[2] = (secs / 60) % 60;
690 timeArgs[4] = secs % 60;
692 return sFormatter.format(durationformat, timeArgs).toString();
695 public static void shuffleAll(Context context, Cursor cursor) {
696 playAll(context, cursor, 0, true);
699 public static void playAll(Context context, Cursor cursor) {
700 playAll(context, cursor, 0, false);
703 public static void playAll(Context context, Cursor cursor, int position) {
704 playAll(context, cursor, position, false);
707 public static void playAll(Context context, long [] list, int position) {
708 playAll(context, list, position, false);
711 private static void playAll(Context context, Cursor cursor, int position, boolean force_shuffle) {
713 long [] list = getSongListForCursor(cursor);
714 playAll(context, list, position, force_shuffle);
717 private static void playAll(Context context, long [] list, int position, boolean force_shuffle) {
718 if (list.length == 0 || sService == null) {
719 Log.d("MusicUtils", "attempt to play empty song list");
720 // Don't try to play empty playlists. Nothing good will come of it.
721 String message = context.getString(R.string.emptyplaylist, list.length);
722 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
727 sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NORMAL);
729 long curid = sService.getAudioId();
730 int curpos = sService.getQueuePosition();
731 if (position != -1 && curpos == position && curid == list[position]) {
732 // The selected file is the file that's currently playing;
733 // figure out if we need to restart with a new playlist,
734 // or just launch the playback activity.
735 long [] playlist = sService.getQueue();
736 if (Arrays.equals(list, playlist)) {
737 // we don't need to set a new list, but we should resume playback if needed
739 return; // the 'finally' block will still run
745 sService.open(list, force_shuffle ? -1 : position);
747 } catch (RemoteException ex) {
749 Intent intent = new Intent(context, MediaPlaybackActivity.class)
750 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
751 context.startActivity(intent);
755 public static void clearQueue() {
757 sService.removeTracks(0, Integer.MAX_VALUE);
758 } catch (RemoteException ex) {
762 // A really simple BitmapDrawable-like class, that doesn't do
763 // scaling, dithering or filtering.
764 private static class FastBitmapDrawable extends Drawable {
765 private Bitmap mBitmap;
766 public FastBitmapDrawable(Bitmap b) {
770 public void draw(Canvas canvas) {
771 canvas.drawBitmap(mBitmap, 0, 0, null);
774 public int getOpacity() {
775 return PixelFormat.OPAQUE;
778 public void setAlpha(int alpha) {
781 public void setColorFilter(ColorFilter cf) {
785 private static int sArtId = -2;
786 private static Bitmap mCachedBit = null;
787 private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
788 private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
789 private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
790 private static final HashMap<Long, Drawable> sArtCache = new HashMap<Long, Drawable>();
791 private static int sArtCacheId = -1;
795 // 565 is faster to decode and display
796 // and we don't want to dither here because the image will be scaled down later
797 sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.RGB_565;
798 sBitmapOptionsCache.inDither = false;
800 sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
801 sBitmapOptions.inDither = false;
804 public static void initAlbumArtCache() {
806 int id = sService.getMediaMountedCount();
807 if (id != sArtCacheId) {
808 clearAlbumArtCache();
811 } catch (RemoteException e) {
816 public static void clearAlbumArtCache() {
817 synchronized(sArtCache) {
822 public static Drawable getCachedArtwork(Context context, long artIndex, BitmapDrawable defaultArtwork) {
824 synchronized(sArtCache) {
825 d = sArtCache.get(artIndex);
829 final Bitmap icon = defaultArtwork.getBitmap();
830 int w = icon.getWidth();
831 int h = icon.getHeight();
832 Bitmap b = MusicUtils.getArtworkQuick(context, artIndex, w, h);
834 d = new FastBitmapDrawable(b);
835 synchronized(sArtCache) {
836 // the cache may have changed since we checked
837 Drawable value = sArtCache.get(artIndex);
839 sArtCache.put(artIndex, d);
849 // Get album art for specified album. This method will not try to
850 // fall back to getting artwork directly from the file, nor will
851 // it attempt to repair the database.
852 private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
853 // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
854 // used to display this drawable. Take it into account now, so we don't have to
857 ContentResolver res = context.getContentResolver();
858 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
860 ParcelFileDescriptor fd = null;
862 fd = res.openFileDescriptor(uri, "r");
865 // Compute the closest power-of-two scale factor
866 // and pass that to sBitmapOptionsCache.inSampleSize, which will
867 // result in faster decoding and better quality
868 sBitmapOptionsCache.inJustDecodeBounds = true;
869 BitmapFactory.decodeFileDescriptor(
870 fd.getFileDescriptor(), null, sBitmapOptionsCache);
871 int nextWidth = sBitmapOptionsCache.outWidth >> 1;
872 int nextHeight = sBitmapOptionsCache.outHeight >> 1;
873 while (nextWidth>w && nextHeight>h) {
879 sBitmapOptionsCache.inSampleSize = sampleSize;
880 sBitmapOptionsCache.inJustDecodeBounds = false;
881 Bitmap b = BitmapFactory.decodeFileDescriptor(
882 fd.getFileDescriptor(), null, sBitmapOptionsCache);
885 // finally rescale to exactly the size we need
886 if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
887 Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
888 // Bitmap.createScaledBitmap() can return the same bitmap
889 if (tmp != b) b.recycle();
895 } catch (FileNotFoundException e) {
900 } catch (IOException e) {
907 /** Get album art for specified album. You should not pass in the album id
908 * for the "unknown" album here (use -1 instead)
910 public static Bitmap getArtwork(Context context, long song_id, long album_id) {
913 // This is something that is not in the database, so get the album art directly
916 Bitmap bm = getArtworkFromFile(context, song_id, -1);
921 return getDefaultArtwork(context);
924 ContentResolver res = context.getContentResolver();
925 Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
927 InputStream in = null;
929 in = res.openInputStream(uri);
930 return BitmapFactory.decodeStream(in, null, sBitmapOptions);
931 } catch (FileNotFoundException ex) {
932 // The album art thumbnail does not actually exist. Maybe the user deleted it, or
933 // maybe it never existed to begin with.
934 Bitmap bm = getArtworkFromFile(context, song_id, album_id);
936 if (bm.getConfig() == null) {
937 bm = bm.copy(Bitmap.Config.RGB_565, false);
939 return getDefaultArtwork(context);
943 bm = getDefaultArtwork(context);
951 } catch (IOException ex) {
959 // get album art for specified file
960 private static final String sExternalMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
961 private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
966 if (albumid < 0 && songid < 0) {
967 throw new IllegalArgumentException("Must specify an album or a song id");
972 Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
973 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
975 FileDescriptor fd = pfd.getFileDescriptor();
976 bm = BitmapFactory.decodeFileDescriptor(fd);
979 Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
980 ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
982 FileDescriptor fd = pfd.getFileDescriptor();
983 bm = BitmapFactory.decodeFileDescriptor(fd);
986 } catch (FileNotFoundException ex) {
995 private static Bitmap getDefaultArtwork(Context context) {
996 BitmapFactory.Options opts = new BitmapFactory.Options();
997 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
998 return BitmapFactory.decodeStream(
999 context.getResources().openRawResource(R.drawable.albumart_mp_unknown), null, opts);
1002 static int getIntPref(Context context, String name, int def) {
1003 SharedPreferences prefs =
1004 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
1005 return prefs.getInt(name, def);
1008 static void setIntPref(Context context, String name, int value) {
1009 SharedPreferences prefs =
1010 context.getSharedPreferences("com.android.music", Context.MODE_PRIVATE);
1011 Editor ed = prefs.edit();
1012 ed.putInt(name, value);
1016 static void setRingtone(Context context, long id) {
1017 ContentResolver resolver = context.getContentResolver();
1018 // Set the flag in the database to mark this as a ringtone
1019 Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
1021 ContentValues values = new ContentValues(2);
1022 values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
1023 values.put(MediaStore.Audio.Media.IS_ALARM, "1");
1024 resolver.update(ringUri, values, null, null);
1025 } catch (UnsupportedOperationException ex) {
1026 // most likely the card just got unmounted
1027 Log.e(TAG, "couldn't set ringtone flag for id " + id);
1031 String[] cols = new String[] {
1032 MediaStore.Audio.Media._ID,
1033 MediaStore.Audio.Media.DATA,
1034 MediaStore.Audio.Media.TITLE
1037 String where = MediaStore.Audio.Media._ID + "=" + id;
1038 Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
1039 cols, where , null, null);
1041 if (cursor != null && cursor.getCount() == 1) {
1042 // Set the system setting to make this the current ringtone
1043 cursor.moveToFirst();
1044 Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
1045 String message = context.getString(R.string.ringtone_set, cursor.getString(2));
1046 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
1049 if (cursor != null) {
1055 static int sActiveTabIndex = -1;
1057 static boolean updateButtonBar(Activity a, int highlight) {
1058 final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);
1059 boolean withtabs = false;
1060 Intent intent = a.getIntent();
1061 if (intent != null) {
1062 withtabs = intent.getBooleanExtra("withtabs", false);
1065 if (highlight == 0 || !withtabs) {
1066 ll.setVisibility(View.GONE);
1068 } else if (withtabs) {
1069 ll.setVisibility(View.VISIBLE);
1071 for (int i = ll.getChildCount() - 1; i >= 0; i--) {
1073 View v = ll.getChildAt(i);
1074 boolean isActive = (v.getId() == highlight);
1076 ll.setCurrentTab(i);
1077 sActiveTabIndex = i;
1080 v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
1082 public void onFocusChange(View v, boolean hasFocus) {
1084 for (int i = 0; i < ll.getTabCount(); i++) {
1085 if (ll.getChildTabViewAt(i) == v) {
1086 ll.setCurrentTab(i);
1087 processTabClick((Activity)ll.getContext(), v, ll.getChildAt(sActiveTabIndex).getId());
1094 v.setOnClickListener(new View.OnClickListener() {
1096 public void onClick(View v) {
1097 processTabClick((Activity)ll.getContext(), v, ll.getChildAt(sActiveTabIndex).getId());
1103 static void processTabClick(Activity a, View v, int current) {
1105 if (id == current) {
1109 final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);
1110 ll.setCurrentTab((Integer) v.getTag());
1113 if (id != R.id.nowplayingtab) {
1114 setIntPref(a, "activetab", id);
1118 static void activateTab(Activity a, int id) {
1119 Intent intent = new Intent(Intent.ACTION_PICK);
1121 case R.id.artisttab:
1122 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum");
1125 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
1128 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
1130 case R.id.playlisttab:
1131 intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE);
1133 case R.id.nowplayingtab:
1134 intent = new Intent(a, MediaPlaybackActivity.class);
1135 a.startActivity(intent);
1136 // fall through and return
1140 intent.putExtra("withtabs", true);
1141 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1142 a.startActivity(intent);
1144 a.overridePendingTransition(0, 0);
1147 static void updateNowPlaying(Activity a) {
1148 View nowPlayingView = a.findViewById(R.id.nowplaying);
1149 if (nowPlayingView == null) {
1153 boolean withtabs = false;
1154 Intent intent = a.getIntent();
1155 if (intent != null) {
1156 withtabs = intent.getBooleanExtra("withtabs", false);
1158 if (true && MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
1159 TextView title = (TextView) nowPlayingView.findViewById(R.id.title);
1160 TextView artist = (TextView) nowPlayingView.findViewById(R.id.artist);
1161 title.setText(MusicUtils.sService.getTrackName());
1162 String artistName = MusicUtils.sService.getArtistName();
1163 if (MediaFile.UNKNOWN_STRING.equals(artistName)) {
1164 artistName = a.getString(R.string.unknown_artist_name);
1166 artist.setText(artistName);
1167 //mNowPlayingView.setOnFocusChangeListener(mFocuser);
1168 //mNowPlayingView.setOnClickListener(this);
1169 nowPlayingView.setVisibility(View.VISIBLE);
1170 nowPlayingView.setOnClickListener(new View.OnClickListener() {
1172 public void onClick(View v) {
1173 Context c = v.getContext();
1174 c.startActivity(new Intent(c, MediaPlaybackActivity.class));
1178 } catch (RemoteException ex) {
1180 nowPlayingView.setVisibility(View.GONE);