2 * Copyright (C) 2007 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 com.android.music.QueryBrowserActivity.QueryListAdapter.QueryHandler;
21 import android.app.ExpandableListActivity;
22 import android.app.SearchManager;
23 import android.content.AsyncQueryHandler;
24 import android.content.BroadcastReceiver;
25 import android.content.ComponentName;
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.content.ServiceConnection;
31 import android.content.res.Resources;
32 import android.database.Cursor;
33 import android.database.CursorWrapper;
34 import android.graphics.drawable.BitmapDrawable;
35 import android.graphics.drawable.Drawable;
36 import android.media.AudioManager;
37 import android.net.Uri;
38 import android.os.Bundle;
39 import android.os.Handler;
40 import android.os.IBinder;
41 import android.os.Message;
42 import android.os.Parcel;
43 import android.os.Parcelable;
44 import android.provider.MediaStore;
45 import android.util.Log;
46 import android.util.SparseArray;
47 import android.view.ContextMenu;
48 import android.view.Menu;
49 import android.view.MenuItem;
50 import android.view.SubMenu;
51 import android.view.View;
52 import android.view.ViewGroup;
53 import android.view.Window;
54 import android.view.ContextMenu.ContextMenuInfo;
55 import android.widget.ExpandableListView;
56 import android.widget.ImageView;
57 import android.widget.SectionIndexer;
58 import android.widget.SimpleCursorTreeAdapter;
59 import android.widget.TextView;
60 import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
62 import java.text.Collator;
65 public class ArtistAlbumBrowserActivity extends ExpandableListActivity
66 implements View.OnCreateContextMenuListener, MusicUtils.Defs, ServiceConnection
68 private String mCurrentArtistId;
69 private String mCurrentArtistName;
70 private String mCurrentAlbumId;
71 private String mCurrentAlbumName;
72 private String mCurrentArtistNameForAlbum;
73 boolean mIsUnknownArtist;
74 boolean mIsUnknownAlbum;
75 private ArtistAlbumListAdapter mAdapter;
76 private boolean mAdapterSent;
77 private final static int SEARCH = CHILD_MENU_BASE;
78 private static int mLastListPosCourse = -1;
79 private static int mLastListPosFine = -1;
81 /** Called when the activity is first created. */
83 public void onCreate(Bundle icicle) {
84 super.onCreate(icicle);
85 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
86 requestWindowFeature(Window.FEATURE_NO_TITLE);
87 setVolumeControlStream(AudioManager.STREAM_MUSIC);
89 mCurrentAlbumId = icicle.getString("selectedalbum");
90 mCurrentAlbumName = icicle.getString("selectedalbumname");
91 mCurrentArtistId = icicle.getString("selectedartist");
92 mCurrentArtistName = icicle.getString("selectedartistname");
94 MusicUtils.bindToService(this, this);
96 IntentFilter f = new IntentFilter();
97 f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
98 f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
99 f.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
100 f.addDataScheme("file");
101 registerReceiver(mScanListener, f);
103 setContentView(R.layout.media_picker_activity_expanding);
104 MusicUtils.updateButtonBar(this, R.id.artisttab);
105 ExpandableListView lv = getExpandableListView();
106 lv.setOnCreateContextMenuListener(this);
107 lv.setTextFilterEnabled(true);
109 mAdapter = (ArtistAlbumListAdapter) getLastNonConfigurationInstance();
110 if (mAdapter == null) {
111 //Log.i("@@@", "starting query");
112 mAdapter = new ArtistAlbumListAdapter(
116 R.layout.track_list_item_group,
119 R.layout.track_list_item_child,
122 setListAdapter(mAdapter);
123 setTitle(R.string.working_artists);
124 getArtistCursor(mAdapter.getQueryHandler(), null);
126 mAdapter.setActivity(this);
127 setListAdapter(mAdapter);
128 mArtistCursor = mAdapter.getCursor();
129 if (mArtistCursor != null) {
132 getArtistCursor(mAdapter.getQueryHandler(), null);
138 public Object onRetainNonConfigurationInstance() {
144 public void onSaveInstanceState(Bundle outcicle) {
145 // need to store the selected item so we don't lose it in case
146 // of an orientation switch. Otherwise we could lose it while
147 // in the middle of specifying a playlist to add the item to.
148 outcicle.putString("selectedalbum", mCurrentAlbumId);
149 outcicle.putString("selectedalbumname", mCurrentAlbumName);
150 outcicle.putString("selectedartist", mCurrentArtistId);
151 outcicle.putString("selectedartistname", mCurrentArtistName);
152 super.onSaveInstanceState(outcicle);
156 public void onDestroy() {
157 ExpandableListView lv = getExpandableListView();
159 mLastListPosCourse = lv.getFirstVisiblePosition();
160 View cv = lv.getChildAt(0);
162 mLastListPosFine = cv.getTop();
166 MusicUtils.unbindFromService(this);
167 // If we have an adapter and didn't send it off to another activity yet, we should
168 // close its cursor, which we do by assigning a null cursor to it. Doing this
169 // instead of closing the cursor directly keeps the framework from accessing
170 // the closed cursor later.
171 if (!mAdapterSent && mAdapter != null) {
172 mAdapter.changeCursor(null);
174 // Because we pass the adapter to the next activity, we need to make
175 // sure it doesn't keep a reference to this activity. We can do this
176 // by clearing its DatasetObservers, which setListAdapter(null) does.
177 setListAdapter(null);
179 unregisterReceiver(mScanListener);
180 setListAdapter(null);
185 public void onResume() {
187 IntentFilter f = new IntentFilter();
188 f.addAction(MediaPlaybackService.META_CHANGED);
189 f.addAction(MediaPlaybackService.QUEUE_CHANGED);
190 registerReceiver(mTrackListListener, f);
191 mTrackListListener.onReceive(null, null);
193 MusicUtils.setSpinnerState(this);
196 private BroadcastReceiver mTrackListListener = new BroadcastReceiver() {
198 public void onReceive(Context context, Intent intent) {
199 getExpandableListView().invalidateViews();
200 MusicUtils.updateNowPlaying(ArtistAlbumBrowserActivity.this);
203 private BroadcastReceiver mScanListener = new BroadcastReceiver() {
205 public void onReceive(Context context, Intent intent) {
206 MusicUtils.setSpinnerState(ArtistAlbumBrowserActivity.this);
207 mReScanHandler.sendEmptyMessage(0);
208 if (intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
209 MusicUtils.clearAlbumArtCache();
214 private Handler mReScanHandler = new Handler() {
216 public void handleMessage(Message msg) {
217 if (mAdapter != null) {
218 getArtistCursor(mAdapter.getQueryHandler(), null);
224 public void onPause() {
225 unregisterReceiver(mTrackListListener);
226 mReScanHandler.removeCallbacksAndMessages(null);
230 public void init(Cursor c) {
232 if (mAdapter == null) {
235 mAdapter.changeCursor(c); // also sets mArtistCursor
237 if (mArtistCursor == null) {
238 MusicUtils.displayDatabaseError(this);
240 mReScanHandler.sendEmptyMessageDelayed(0, 1000);
244 // restore previous position
245 if (mLastListPosCourse >= 0) {
246 ExpandableListView elv = getExpandableListView();
247 elv.setSelectionFromTop(mLastListPosCourse, mLastListPosFine);
248 mLastListPosCourse = -1;
251 MusicUtils.hideDatabaseError(this);
252 MusicUtils.updateButtonBar(this, R.id.artisttab);
256 private void setTitle() {
257 setTitle(R.string.artists_title);
261 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
263 mCurrentAlbumId = Long.valueOf(id).toString();
265 Intent intent = new Intent(Intent.ACTION_PICK);
266 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
267 intent.putExtra("album", mCurrentAlbumId);
268 Cursor c = (Cursor) getExpandableListAdapter().getChild(groupPosition, childPosition);
269 String album = c.getString(c.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
270 if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) {
271 // unknown album, so we should include the artist ID to limit the songs to songs only by that artist
272 mArtistCursor.moveToPosition(groupPosition);
273 mCurrentArtistId = mArtistCursor.getString(mArtistCursor.getColumnIndex(MediaStore.Audio.Artists._ID));
274 intent.putExtra("artist", mCurrentArtistId);
276 startActivity(intent);
281 public boolean onCreateOptionsMenu(Menu menu) {
282 super.onCreateOptionsMenu(menu);
283 menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu()
284 menu.add(0, SHUFFLE_ALL, 0, R.string.shuffle_all).setIcon(R.drawable.ic_menu_shuffle);
289 public boolean onPrepareOptionsMenu(Menu menu) {
290 MusicUtils.setPartyShuffleMenuIcon(menu);
291 return super.onPrepareOptionsMenu(menu);
295 public boolean onOptionsItemSelected(MenuItem item) {
298 switch (item.getItemId()) {
300 MusicUtils.togglePartyShuffle();
304 cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
305 new String [] { MediaStore.Audio.Media._ID},
306 MediaStore.Audio.Media.IS_MUSIC + "=1", null,
307 MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
308 if (cursor != null) {
309 MusicUtils.shuffleAll(this, cursor);
314 return super.onOptionsItemSelected(item);
318 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {
319 menu.add(0, PLAY_SELECTION, 0, R.string.play_selection);
320 SubMenu sub = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0, R.string.add_to_playlist);
321 MusicUtils.makePlaylistMenu(this, sub);
322 menu.add(0, DELETE_ITEM, 0, R.string.delete_item);
324 ExpandableListContextMenuInfo mi = (ExpandableListContextMenuInfo) menuInfoIn;
326 int itemtype = ExpandableListView.getPackedPositionType(mi.packedPosition);
327 int gpos = ExpandableListView.getPackedPositionGroup(mi.packedPosition);
328 int cpos = ExpandableListView.getPackedPositionChild(mi.packedPosition);
329 if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
331 // this shouldn't happen
332 Log.d("Artist/Album", "no group");
335 gpos = gpos - getExpandableListView().getHeaderViewsCount();
336 mArtistCursor.moveToPosition(gpos);
337 mCurrentArtistId = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID));
338 mCurrentArtistName = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
339 mCurrentAlbumId = null;
340 mIsUnknownArtist = mCurrentArtistName == null ||
341 mCurrentArtistName.equals(MediaStore.UNKNOWN_STRING);
342 mIsUnknownAlbum = true;
343 if (mIsUnknownArtist) {
344 menu.setHeaderTitle(getString(R.string.unknown_artist_name));
346 menu.setHeaderTitle(mCurrentArtistName);
347 menu.add(0, SEARCH, 0, R.string.search_title);
350 } else if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
352 // this shouldn't happen
353 Log.d("Artist/Album", "no child");
356 Cursor c = (Cursor) getExpandableListAdapter().getChild(gpos, cpos);
357 c.moveToPosition(cpos);
358 mCurrentArtistId = null;
359 mCurrentAlbumId = Long.valueOf(mi.id).toString();
360 mCurrentAlbumName = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
361 gpos = gpos - getExpandableListView().getHeaderViewsCount();
362 mArtistCursor.moveToPosition(gpos);
363 mCurrentArtistNameForAlbum = mArtistCursor.getString(
364 mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
365 mIsUnknownArtist = mCurrentArtistNameForAlbum == null ||
366 mCurrentArtistNameForAlbum.equals(MediaStore.UNKNOWN_STRING);
367 mIsUnknownAlbum = mCurrentAlbumName == null ||
368 mCurrentAlbumName.equals(MediaStore.UNKNOWN_STRING);
369 if (mIsUnknownAlbum) {
370 menu.setHeaderTitle(getString(R.string.unknown_album_name));
372 menu.setHeaderTitle(mCurrentAlbumName);
374 if (!mIsUnknownAlbum || !mIsUnknownArtist) {
375 menu.add(0, SEARCH, 0, R.string.search_title);
381 public boolean onContextItemSelected(MenuItem item) {
382 switch (item.getItemId()) {
383 case PLAY_SELECTION: {
384 // play everything by the selected artist
386 mCurrentArtistId != null ?
387 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId))
388 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId));
390 MusicUtils.playAll(this, list, 0);
396 mCurrentArtistId != null ?
397 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId))
398 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId));
399 MusicUtils.addToCurrentPlaylist(this, list);
404 Intent intent = new Intent();
405 intent.setClass(this, CreatePlaylist.class);
406 startActivityForResult(intent, NEW_PLAYLIST);
410 case PLAYLIST_SELECTED: {
412 mCurrentArtistId != null ?
413 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId))
414 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId));
415 long playlist = item.getIntent().getLongExtra("playlist", 0);
416 MusicUtils.addToPlaylist(this, list, playlist);
423 if (mCurrentArtistId != null) {
424 list = MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId));
425 String f = getString(R.string.delete_artist_desc);
426 desc = String.format(f, mCurrentArtistName);
428 list = MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId));
429 String f = getString(R.string.delete_album_desc);
430 desc = String.format(f, mCurrentAlbumName);
432 Bundle b = new Bundle();
433 b.putString("description", desc);
434 b.putLongArray("items", list);
435 Intent intent = new Intent();
436 intent.setClass(this, DeleteItems.class);
438 startActivityForResult(intent, -1);
446 return super.onContextItemSelected(item);
450 CharSequence title = null;
453 Intent i = new Intent();
454 i.setAction(MediaStore.INTENT_ACTION_MEDIA_SEARCH);
455 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
457 if (mCurrentArtistId != null) {
458 title = mCurrentArtistName;
459 query = mCurrentArtistName;
460 i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, mCurrentArtistName);
461 i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
463 if (mIsUnknownAlbum) {
464 title = query = mCurrentArtistNameForAlbum;
466 title = query = mCurrentAlbumName;
467 if (!mIsUnknownArtist) {
468 query = query + " " + mCurrentArtistNameForAlbum;
471 i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, mCurrentArtistNameForAlbum);
472 i.putExtra(MediaStore.EXTRA_MEDIA_ALBUM, mCurrentAlbumName);
473 i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE);
475 title = getString(R.string.mediasearch, title);
476 i.putExtra(SearchManager.QUERY, query);
478 startActivity(Intent.createChooser(i, title));
482 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
483 switch (requestCode) {
485 if (resultCode == RESULT_CANCELED) {
488 getArtistCursor(mAdapter.getQueryHandler(), null);
493 if (resultCode == RESULT_OK) {
494 Uri uri = intent.getData();
497 if (mCurrentArtistId != null) {
498 list = MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId));
499 } else if (mCurrentAlbumId != null) {
500 list = MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId));
502 MusicUtils.addToPlaylist(this, list, Long.parseLong(uri.getLastPathSegment()));
509 private Cursor getArtistCursor(AsyncQueryHandler async, String filter) {
511 StringBuilder where = new StringBuilder();
512 where.append(MediaStore.Audio.Artists.ARTIST + " != ''");
514 // Add in the filtering constraints
515 String [] keywords = null;
516 if (filter != null) {
517 String [] searchWords = filter.split(" ");
518 keywords = new String[searchWords.length];
519 Collator col = Collator.getInstance();
520 col.setStrength(Collator.PRIMARY);
521 for (int i = 0; i < searchWords.length; i++) {
522 keywords[i] = '%' + MediaStore.Audio.keyFor(searchWords[i]) + '%';
524 for (int i = 0; i < searchWords.length; i++) {
525 where.append(" AND ");
526 where.append(MediaStore.Audio.Media.ARTIST_KEY + " LIKE ?");
530 String whereclause = where.toString();
531 String[] cols = new String[] {
532 MediaStore.Audio.Artists._ID,
533 MediaStore.Audio.Artists.ARTIST,
534 MediaStore.Audio.Artists.NUMBER_OF_ALBUMS,
535 MediaStore.Audio.Artists.NUMBER_OF_TRACKS
539 async.startQuery(0, null, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
540 cols, whereclause , keywords, MediaStore.Audio.Artists.ARTIST_KEY);
542 ret = MusicUtils.query(this, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
543 cols, whereclause , keywords, MediaStore.Audio.Artists.ARTIST_KEY);
548 static class ArtistAlbumListAdapter extends SimpleCursorTreeAdapter implements SectionIndexer {
550 private final Drawable mNowPlayingOverlay;
551 private final BitmapDrawable mDefaultAlbumIcon;
552 private int mGroupArtistIdIdx;
553 private int mGroupArtistIdx;
554 private int mGroupAlbumIdx;
555 private int mGroupSongIdx;
556 private final Context mContext;
557 private final Resources mResources;
558 private final String mAlbumSongSeparator;
559 private final String mUnknownAlbum;
560 private final String mUnknownArtist;
561 private final StringBuilder mBuffer = new StringBuilder();
562 private final Object[] mFormatArgs = new Object[1];
563 private final Object[] mFormatArgs3 = new Object[3];
564 private MusicAlphabetIndexer mIndexer;
565 private ArtistAlbumBrowserActivity mActivity;
566 private AsyncQueryHandler mQueryHandler;
567 private String mConstraint = null;
568 private boolean mConstraintIsValid = false;
570 static class ViewHolder {
573 ImageView play_indicator;
577 class QueryHandler extends AsyncQueryHandler {
578 QueryHandler(ContentResolver res) {
583 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
584 //Log.i("@@@", "query complete");
585 mActivity.init(cursor);
589 ArtistAlbumListAdapter(Context context, ArtistAlbumBrowserActivity currentactivity,
590 Cursor cursor, int glayout, String[] gfrom, int[] gto,
591 int clayout, String[] cfrom, int[] cto) {
592 super(context, cursor, glayout, gfrom, gto, clayout, cfrom, cto);
593 mActivity = currentactivity;
594 mQueryHandler = new QueryHandler(context.getContentResolver());
596 Resources r = context.getResources();
597 mNowPlayingOverlay = r.getDrawable(R.drawable.indicator_ic_mp_playing_list);
598 mDefaultAlbumIcon = (BitmapDrawable) r.getDrawable(R.drawable.albumart_mp_unknown_list);
599 // no filter or dither, it's a lot faster and we can't tell the difference
600 mDefaultAlbumIcon.setFilterBitmap(false);
601 mDefaultAlbumIcon.setDither(false);
604 getColumnIndices(cursor);
605 mResources = context.getResources();
606 mAlbumSongSeparator = context.getString(R.string.albumsongseparator);
607 mUnknownAlbum = context.getString(R.string.unknown_album_name);
608 mUnknownArtist = context.getString(R.string.unknown_artist_name);
611 private void getColumnIndices(Cursor cursor) {
612 if (cursor != null) {
613 mGroupArtistIdIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID);
614 mGroupArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
615 mGroupAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_ALBUMS);
616 mGroupSongIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_TRACKS);
617 if (mIndexer != null) {
618 mIndexer.setCursor(cursor);
620 mIndexer = new MusicAlphabetIndexer(cursor, mGroupArtistIdx,
621 mResources.getString(R.string.fast_scroll_alphabet));
626 public void setActivity(ArtistAlbumBrowserActivity newactivity) {
627 mActivity = newactivity;
630 public AsyncQueryHandler getQueryHandler() {
631 return mQueryHandler;
635 public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
636 View v = super.newGroupView(context, cursor, isExpanded, parent);
637 ImageView iv = (ImageView) v.findViewById(R.id.icon);
638 ViewGroup.LayoutParams p = iv.getLayoutParams();
639 p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
640 p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
641 ViewHolder vh = new ViewHolder();
642 vh.line1 = (TextView) v.findViewById(R.id.line1);
643 vh.line2 = (TextView) v.findViewById(R.id.line2);
644 vh.play_indicator = (ImageView) v.findViewById(R.id.play_indicator);
645 vh.icon = (ImageView) v.findViewById(R.id.icon);
646 vh.icon.setPadding(0, 0, 1, 0);
652 public View newChildView(Context context, Cursor cursor, boolean isLastChild,
654 View v = super.newChildView(context, cursor, isLastChild, parent);
655 ViewHolder vh = new ViewHolder();
656 vh.line1 = (TextView) v.findViewById(R.id.line1);
657 vh.line2 = (TextView) v.findViewById(R.id.line2);
658 vh.play_indicator = (ImageView) v.findViewById(R.id.play_indicator);
659 vh.icon = (ImageView) v.findViewById(R.id.icon);
660 vh.icon.setBackgroundDrawable(mDefaultAlbumIcon);
661 vh.icon.setPadding(0, 0, 1, 0);
667 public void bindGroupView(View view, Context context, Cursor cursor, boolean isexpanded) {
669 ViewHolder vh = (ViewHolder) view.getTag();
671 String artist = cursor.getString(mGroupArtistIdx);
672 String displayartist = artist;
673 boolean unknown = artist == null || artist.equals(MediaStore.UNKNOWN_STRING);
675 displayartist = mUnknownArtist;
677 vh.line1.setText(displayartist);
679 int numalbums = cursor.getInt(mGroupAlbumIdx);
680 int numsongs = cursor.getInt(mGroupSongIdx);
682 String songs_albums = MusicUtils.makeAlbumsLabel(context,
683 numalbums, numsongs, unknown);
685 vh.line2.setText(songs_albums);
687 long currentartistid = MusicUtils.getCurrentArtistId();
688 long artistid = cursor.getLong(mGroupArtistIdIdx);
689 if (currentartistid == artistid && !isexpanded) {
690 vh.play_indicator.setImageDrawable(mNowPlayingOverlay);
692 vh.play_indicator.setImageDrawable(null);
697 public void bindChildView(View view, Context context, Cursor cursor, boolean islast) {
699 ViewHolder vh = (ViewHolder) view.getTag();
701 String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM));
702 String displayname = name;
703 boolean unknown = name == null || name.equals(MediaStore.UNKNOWN_STRING);
705 displayname = mUnknownAlbum;
707 vh.line1.setText(displayname);
709 int numsongs = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS));
710 int numartistsongs = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS_FOR_ARTIST));
712 final StringBuilder builder = mBuffer;
713 builder.delete(0, builder.length());
715 numsongs = numartistsongs;
719 builder.append(context.getString(R.string.onesong));
721 if (numsongs == numartistsongs) {
722 final Object[] args = mFormatArgs;
724 builder.append(mResources.getQuantityString(R.plurals.Nsongs, numsongs, args));
726 final Object[] args = mFormatArgs3;
728 args[1] = numartistsongs;
729 args[2] = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST));
730 builder.append(mResources.getQuantityString(R.plurals.Nsongscomp, numsongs, args));
733 vh.line2.setText(builder.toString());
735 ImageView iv = vh.icon;
736 // We don't actually need the path to the thumbnail file,
737 // we just use it to see if there is album art or not
738 String art = cursor.getString(cursor.getColumnIndexOrThrow(
739 MediaStore.Audio.Albums.ALBUM_ART));
740 if (unknown || art == null || art.length() == 0) {
741 iv.setBackgroundDrawable(mDefaultAlbumIcon);
742 iv.setImageDrawable(null);
744 long artIndex = cursor.getLong(0);
745 Drawable d = MusicUtils.getCachedArtwork(context, artIndex, mDefaultAlbumIcon);
746 iv.setImageDrawable(d);
749 long currentalbumid = MusicUtils.getCurrentAlbumId();
750 long aid = cursor.getLong(0);
751 iv = vh.play_indicator;
752 if (currentalbumid == aid) {
753 iv.setImageDrawable(mNowPlayingOverlay);
755 iv.setImageDrawable(null);
761 protected Cursor getChildrenCursor(Cursor groupCursor) {
763 long id = groupCursor.getLong(groupCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID));
765 String[] cols = new String[] {
766 MediaStore.Audio.Albums._ID,
767 MediaStore.Audio.Albums.ALBUM,
768 MediaStore.Audio.Albums.NUMBER_OF_SONGS,
769 MediaStore.Audio.Albums.NUMBER_OF_SONGS_FOR_ARTIST,
770 MediaStore.Audio.Albums.ALBUM_ART
772 Cursor c = MusicUtils.query(mActivity,
773 MediaStore.Audio.Artists.Albums.getContentUri("external", id),
774 cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
776 class MyCursorWrapper extends CursorWrapper {
779 MyCursorWrapper(Cursor c, String artist) {
781 mArtistName = artist;
782 if (mArtistName == null || mArtistName.equals(MediaStore.UNKNOWN_STRING)) {
783 mArtistName = mUnknownArtist;
785 mMagicColumnIdx = c.getColumnCount();
789 public String getString(int columnIndex) {
790 if (columnIndex != mMagicColumnIdx) {
791 return super.getString(columnIndex);
797 public int getColumnIndexOrThrow(String name) {
798 if (MediaStore.Audio.Albums.ARTIST.equals(name)) {
799 return mMagicColumnIdx;
801 return super.getColumnIndexOrThrow(name);
805 public String getColumnName(int idx) {
806 if (idx != mMagicColumnIdx) {
807 return super.getColumnName(idx);
809 return MediaStore.Audio.Albums.ARTIST;
813 public int getColumnCount() {
814 return super.getColumnCount() + 1;
817 return new MyCursorWrapper(c, groupCursor.getString(mGroupArtistIdx));
821 public void changeCursor(Cursor cursor) {
822 if (mActivity.isFinishing() && cursor != null) {
826 if (cursor != mActivity.mArtistCursor) {
827 mActivity.mArtistCursor = cursor;
828 getColumnIndices(cursor);
829 super.changeCursor(cursor);
834 public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
835 String s = constraint.toString();
836 if (mConstraintIsValid && (
837 (s == null && mConstraint == null) ||
838 (s != null && s.equals(mConstraint)))) {
841 Cursor c = mActivity.getArtistCursor(null, s);
843 mConstraintIsValid = true;
847 public Object[] getSections() {
848 return mIndexer.getSections();
851 public int getPositionForSection(int sectionIndex) {
852 return mIndexer.getPositionForSection(sectionIndex);
855 public int getSectionForPosition(int position) {
860 private Cursor mArtistCursor;
862 public void onServiceConnected(ComponentName name, IBinder service) {
863 MusicUtils.updateNowPlaying(this);
866 public void onServiceDisconnected(ComponentName name) {