ArtistAlbumBrowserActivity.java revision f33a575c2b09095d58ad7af527113f767e4c37b1
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package com.android.music; 18 19import com.android.music.MusicUtils.ServiceToken; 20import com.android.music.QueryBrowserActivity.QueryListAdapter.QueryHandler; 21 22import android.app.ExpandableListActivity; 23import android.app.SearchManager; 24import android.content.AsyncQueryHandler; 25import android.content.BroadcastReceiver; 26import android.content.ComponentName; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.content.ServiceConnection; 32import android.content.res.Resources; 33import android.database.Cursor; 34import android.database.CursorWrapper; 35import android.graphics.drawable.BitmapDrawable; 36import android.graphics.drawable.Drawable; 37import android.media.AudioManager; 38import android.net.Uri; 39import android.os.Bundle; 40import android.os.Handler; 41import android.os.IBinder; 42import android.os.Message; 43import android.os.Parcel; 44import android.os.Parcelable; 45import android.provider.MediaStore; 46import android.util.Log; 47import android.util.SparseArray; 48import android.view.ContextMenu; 49import android.view.Menu; 50import android.view.MenuItem; 51import android.view.SubMenu; 52import android.view.View; 53import android.view.ViewGroup; 54import android.view.Window; 55import android.view.ContextMenu.ContextMenuInfo; 56import android.widget.ExpandableListView; 57import android.widget.ImageView; 58import android.widget.SectionIndexer; 59import android.widget.SimpleCursorTreeAdapter; 60import android.widget.TextView; 61import android.widget.ExpandableListView.ExpandableListContextMenuInfo; 62 63import java.text.Collator; 64 65 66public class ArtistAlbumBrowserActivity extends ExpandableListActivity 67 implements View.OnCreateContextMenuListener, MusicUtils.Defs, ServiceConnection 68{ 69 private String mCurrentArtistId; 70 private String mCurrentArtistName; 71 private String mCurrentAlbumId; 72 private String mCurrentAlbumName; 73 private String mCurrentArtistNameForAlbum; 74 boolean mIsUnknownArtist; 75 boolean mIsUnknownAlbum; 76 private ArtistAlbumListAdapter mAdapter; 77 private boolean mAdapterSent; 78 private final static int SEARCH = CHILD_MENU_BASE; 79 private static int mLastListPosCourse = -1; 80 private static int mLastListPosFine = -1; 81 private ServiceToken mToken; 82 83 /** Called when the activity is first created. */ 84 @Override 85 public void onCreate(Bundle icicle) { 86 super.onCreate(icicle); 87 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 88 requestWindowFeature(Window.FEATURE_NO_TITLE); 89 setVolumeControlStream(AudioManager.STREAM_MUSIC); 90 if (icicle != null) { 91 mCurrentAlbumId = icicle.getString("selectedalbum"); 92 mCurrentAlbumName = icicle.getString("selectedalbumname"); 93 mCurrentArtistId = icicle.getString("selectedartist"); 94 mCurrentArtistName = icicle.getString("selectedartistname"); 95 } 96 mToken = MusicUtils.bindToService(this, this); 97 98 IntentFilter f = new IntentFilter(); 99 f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); 100 f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); 101 f.addAction(Intent.ACTION_MEDIA_UNMOUNTED); 102 f.addDataScheme("file"); 103 registerReceiver(mScanListener, f); 104 105 setContentView(R.layout.media_picker_activity_expanding); 106 MusicUtils.updateButtonBar(this, R.id.artisttab); 107 ExpandableListView lv = getExpandableListView(); 108 lv.setOnCreateContextMenuListener(this); 109 lv.setTextFilterEnabled(true); 110 111 mAdapter = (ArtistAlbumListAdapter) getLastNonConfigurationInstance(); 112 if (mAdapter == null) { 113 //Log.i("@@@", "starting query"); 114 mAdapter = new ArtistAlbumListAdapter( 115 getApplication(), 116 this, 117 null, // cursor 118 R.layout.track_list_item_group, 119 new String[] {}, 120 new int[] {}, 121 R.layout.track_list_item_child, 122 new String[] {}, 123 new int[] {}); 124 setListAdapter(mAdapter); 125 setTitle(R.string.working_artists); 126 getArtistCursor(mAdapter.getQueryHandler(), null); 127 } else { 128 mAdapter.setActivity(this); 129 setListAdapter(mAdapter); 130 mArtistCursor = mAdapter.getCursor(); 131 if (mArtistCursor != null) { 132 init(mArtistCursor); 133 } else { 134 getArtistCursor(mAdapter.getQueryHandler(), null); 135 } 136 } 137 } 138 139 @Override 140 public Object onRetainNonConfigurationInstance() { 141 mAdapterSent = true; 142 return mAdapter; 143 } 144 145 @Override 146 public void onSaveInstanceState(Bundle outcicle) { 147 // need to store the selected item so we don't lose it in case 148 // of an orientation switch. Otherwise we could lose it while 149 // in the middle of specifying a playlist to add the item to. 150 outcicle.putString("selectedalbum", mCurrentAlbumId); 151 outcicle.putString("selectedalbumname", mCurrentAlbumName); 152 outcicle.putString("selectedartist", mCurrentArtistId); 153 outcicle.putString("selectedartistname", mCurrentArtistName); 154 super.onSaveInstanceState(outcicle); 155 } 156 157 @Override 158 public void onDestroy() { 159 ExpandableListView lv = getExpandableListView(); 160 if (lv != null) { 161 mLastListPosCourse = lv.getFirstVisiblePosition(); 162 View cv = lv.getChildAt(0); 163 if (cv != null) { 164 mLastListPosFine = cv.getTop(); 165 } 166 } 167 168 MusicUtils.unbindFromService(mToken); 169 // If we have an adapter and didn't send it off to another activity yet, we should 170 // close its cursor, which we do by assigning a null cursor to it. Doing this 171 // instead of closing the cursor directly keeps the framework from accessing 172 // the closed cursor later. 173 if (!mAdapterSent && mAdapter != null) { 174 mAdapter.changeCursor(null); 175 } 176 // Because we pass the adapter to the next activity, we need to make 177 // sure it doesn't keep a reference to this activity. We can do this 178 // by clearing its DatasetObservers, which setListAdapter(null) does. 179 setListAdapter(null); 180 mAdapter = null; 181 unregisterReceiver(mScanListener); 182 setListAdapter(null); 183 super.onDestroy(); 184 } 185 186 @Override 187 public void onResume() { 188 super.onResume(); 189 IntentFilter f = new IntentFilter(); 190 f.addAction(MediaPlaybackService.META_CHANGED); 191 f.addAction(MediaPlaybackService.QUEUE_CHANGED); 192 registerReceiver(mTrackListListener, f); 193 mTrackListListener.onReceive(null, null); 194 195 MusicUtils.setSpinnerState(this); 196 } 197 198 private BroadcastReceiver mTrackListListener = new BroadcastReceiver() { 199 @Override 200 public void onReceive(Context context, Intent intent) { 201 getExpandableListView().invalidateViews(); 202 MusicUtils.updateNowPlaying(ArtistAlbumBrowserActivity.this); 203 } 204 }; 205 private BroadcastReceiver mScanListener = new BroadcastReceiver() { 206 @Override 207 public void onReceive(Context context, Intent intent) { 208 MusicUtils.setSpinnerState(ArtistAlbumBrowserActivity.this); 209 mReScanHandler.sendEmptyMessage(0); 210 if (intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) { 211 MusicUtils.clearAlbumArtCache(); 212 } 213 } 214 }; 215 216 private Handler mReScanHandler = new Handler() { 217 @Override 218 public void handleMessage(Message msg) { 219 if (mAdapter != null) { 220 getArtistCursor(mAdapter.getQueryHandler(), null); 221 } 222 } 223 }; 224 225 @Override 226 public void onPause() { 227 unregisterReceiver(mTrackListListener); 228 mReScanHandler.removeCallbacksAndMessages(null); 229 super.onPause(); 230 } 231 232 public void init(Cursor c) { 233 234 if (mAdapter == null) { 235 return; 236 } 237 mAdapter.changeCursor(c); // also sets mArtistCursor 238 239 if (mArtistCursor == null) { 240 MusicUtils.displayDatabaseError(this); 241 closeContextMenu(); 242 mReScanHandler.sendEmptyMessageDelayed(0, 1000); 243 return; 244 } 245 246 // restore previous position 247 if (mLastListPosCourse >= 0) { 248 ExpandableListView elv = getExpandableListView(); 249 elv.setSelectionFromTop(mLastListPosCourse, mLastListPosFine); 250 mLastListPosCourse = -1; 251 } 252 253 MusicUtils.hideDatabaseError(this); 254 MusicUtils.updateButtonBar(this, R.id.artisttab); 255 setTitle(); 256 } 257 258 private void setTitle() { 259 setTitle(R.string.artists_title); 260 } 261 262 @Override 263 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 264 265 mCurrentAlbumId = Long.valueOf(id).toString(); 266 267 Intent intent = new Intent(Intent.ACTION_PICK); 268 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track"); 269 intent.putExtra("album", mCurrentAlbumId); 270 Cursor c = (Cursor) getExpandableListAdapter().getChild(groupPosition, childPosition); 271 String album = c.getString(c.getColumnIndex(MediaStore.Audio.Albums.ALBUM)); 272 if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) { 273 // unknown album, so we should include the artist ID to limit the songs to songs only by that artist 274 mArtistCursor.moveToPosition(groupPosition); 275 mCurrentArtistId = mArtistCursor.getString(mArtistCursor.getColumnIndex(MediaStore.Audio.Artists._ID)); 276 intent.putExtra("artist", mCurrentArtistId); 277 } 278 startActivity(intent); 279 return true; 280 } 281 282 @Override 283 public boolean onCreateOptionsMenu(Menu menu) { 284 super.onCreateOptionsMenu(menu); 285 menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu() 286 menu.add(0, SHUFFLE_ALL, 0, R.string.shuffle_all).setIcon(R.drawable.ic_menu_shuffle); 287 return true; 288 } 289 290 @Override 291 public boolean onPrepareOptionsMenu(Menu menu) { 292 MusicUtils.setPartyShuffleMenuIcon(menu); 293 return super.onPrepareOptionsMenu(menu); 294 } 295 296 @Override 297 public boolean onOptionsItemSelected(MenuItem item) { 298 Intent intent; 299 Cursor cursor; 300 switch (item.getItemId()) { 301 case PARTY_SHUFFLE: 302 MusicUtils.togglePartyShuffle(); 303 break; 304 305 case SHUFFLE_ALL: 306 cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 307 new String [] { MediaStore.Audio.Media._ID}, 308 MediaStore.Audio.Media.IS_MUSIC + "=1", null, 309 MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 310 if (cursor != null) { 311 MusicUtils.shuffleAll(this, cursor); 312 cursor.close(); 313 } 314 return true; 315 } 316 return super.onOptionsItemSelected(item); 317 } 318 319 @Override 320 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) { 321 menu.add(0, PLAY_SELECTION, 0, R.string.play_selection); 322 SubMenu sub = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0, R.string.add_to_playlist); 323 MusicUtils.makePlaylistMenu(this, sub); 324 menu.add(0, DELETE_ITEM, 0, R.string.delete_item); 325 326 ExpandableListContextMenuInfo mi = (ExpandableListContextMenuInfo) menuInfoIn; 327 328 int itemtype = ExpandableListView.getPackedPositionType(mi.packedPosition); 329 int gpos = ExpandableListView.getPackedPositionGroup(mi.packedPosition); 330 int cpos = ExpandableListView.getPackedPositionChild(mi.packedPosition); 331 if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 332 if (gpos == -1) { 333 // this shouldn't happen 334 Log.d("Artist/Album", "no group"); 335 return; 336 } 337 gpos = gpos - getExpandableListView().getHeaderViewsCount(); 338 mArtistCursor.moveToPosition(gpos); 339 mCurrentArtistId = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID)); 340 mCurrentArtistName = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)); 341 mCurrentAlbumId = null; 342 mIsUnknownArtist = mCurrentArtistName == null || 343 mCurrentArtistName.equals(MediaStore.UNKNOWN_STRING); 344 mIsUnknownAlbum = true; 345 if (mIsUnknownArtist) { 346 menu.setHeaderTitle(getString(R.string.unknown_artist_name)); 347 } else { 348 menu.setHeaderTitle(mCurrentArtistName); 349 menu.add(0, SEARCH, 0, R.string.search_title); 350 } 351 return; 352 } else if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 353 if (cpos == -1) { 354 // this shouldn't happen 355 Log.d("Artist/Album", "no child"); 356 return; 357 } 358 Cursor c = (Cursor) getExpandableListAdapter().getChild(gpos, cpos); 359 c.moveToPosition(cpos); 360 mCurrentArtistId = null; 361 mCurrentAlbumId = Long.valueOf(mi.id).toString(); 362 mCurrentAlbumName = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM)); 363 gpos = gpos - getExpandableListView().getHeaderViewsCount(); 364 mArtistCursor.moveToPosition(gpos); 365 mCurrentArtistNameForAlbum = mArtistCursor.getString( 366 mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)); 367 mIsUnknownArtist = mCurrentArtistNameForAlbum == null || 368 mCurrentArtistNameForAlbum.equals(MediaStore.UNKNOWN_STRING); 369 mIsUnknownAlbum = mCurrentAlbumName == null || 370 mCurrentAlbumName.equals(MediaStore.UNKNOWN_STRING); 371 if (mIsUnknownAlbum) { 372 menu.setHeaderTitle(getString(R.string.unknown_album_name)); 373 } else { 374 menu.setHeaderTitle(mCurrentAlbumName); 375 } 376 if (!mIsUnknownAlbum || !mIsUnknownArtist) { 377 menu.add(0, SEARCH, 0, R.string.search_title); 378 } 379 } 380 } 381 382 @Override 383 public boolean onContextItemSelected(MenuItem item) { 384 switch (item.getItemId()) { 385 case PLAY_SELECTION: { 386 // play everything by the selected artist 387 long [] list = 388 mCurrentArtistId != null ? 389 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId)) 390 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId)); 391 392 MusicUtils.playAll(this, list, 0); 393 return true; 394 } 395 396 case QUEUE: { 397 long [] list = 398 mCurrentArtistId != null ? 399 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId)) 400 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId)); 401 MusicUtils.addToCurrentPlaylist(this, list); 402 return true; 403 } 404 405 case NEW_PLAYLIST: { 406 Intent intent = new Intent(); 407 intent.setClass(this, CreatePlaylist.class); 408 startActivityForResult(intent, NEW_PLAYLIST); 409 return true; 410 } 411 412 case PLAYLIST_SELECTED: { 413 long [] list = 414 mCurrentArtistId != null ? 415 MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId)) 416 : MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId)); 417 long playlist = item.getIntent().getLongExtra("playlist", 0); 418 MusicUtils.addToPlaylist(this, list, playlist); 419 return true; 420 } 421 422 case DELETE_ITEM: { 423 long [] list; 424 String desc; 425 if (mCurrentArtistId != null) { 426 list = MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId)); 427 String f = getString(R.string.delete_artist_desc); 428 desc = String.format(f, mCurrentArtistName); 429 } else { 430 list = MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId)); 431 String f = getString(R.string.delete_album_desc); 432 desc = String.format(f, mCurrentAlbumName); 433 } 434 Bundle b = new Bundle(); 435 b.putString("description", desc); 436 b.putLongArray("items", list); 437 Intent intent = new Intent(); 438 intent.setClass(this, DeleteItems.class); 439 intent.putExtras(b); 440 startActivityForResult(intent, -1); 441 return true; 442 } 443 444 case SEARCH: 445 doSearch(); 446 return true; 447 } 448 return super.onContextItemSelected(item); 449 } 450 451 void doSearch() { 452 CharSequence title = null; 453 String query = null; 454 455 Intent i = new Intent(); 456 i.setAction(MediaStore.INTENT_ACTION_MEDIA_SEARCH); 457 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 458 459 if (mCurrentArtistId != null) { 460 title = mCurrentArtistName; 461 query = mCurrentArtistName; 462 i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, mCurrentArtistName); 463 i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE); 464 } else { 465 if (mIsUnknownAlbum) { 466 title = query = mCurrentArtistNameForAlbum; 467 } else { 468 title = query = mCurrentAlbumName; 469 if (!mIsUnknownArtist) { 470 query = query + " " + mCurrentArtistNameForAlbum; 471 } 472 } 473 i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, mCurrentArtistNameForAlbum); 474 i.putExtra(MediaStore.EXTRA_MEDIA_ALBUM, mCurrentAlbumName); 475 i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE); 476 } 477 title = getString(R.string.mediasearch, title); 478 i.putExtra(SearchManager.QUERY, query); 479 480 startActivity(Intent.createChooser(i, title)); 481 } 482 483 @Override 484 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 485 switch (requestCode) { 486 case SCAN_DONE: 487 if (resultCode == RESULT_CANCELED) { 488 finish(); 489 } else { 490 getArtistCursor(mAdapter.getQueryHandler(), null); 491 } 492 break; 493 494 case NEW_PLAYLIST: 495 if (resultCode == RESULT_OK) { 496 Uri uri = intent.getData(); 497 if (uri != null) { 498 long [] list = null; 499 if (mCurrentArtistId != null) { 500 list = MusicUtils.getSongListForArtist(this, Long.parseLong(mCurrentArtistId)); 501 } else if (mCurrentAlbumId != null) { 502 list = MusicUtils.getSongListForAlbum(this, Long.parseLong(mCurrentAlbumId)); 503 } 504 MusicUtils.addToPlaylist(this, list, Long.parseLong(uri.getLastPathSegment())); 505 } 506 } 507 break; 508 } 509 } 510 511 private Cursor getArtistCursor(AsyncQueryHandler async, String filter) { 512 513 StringBuilder where = new StringBuilder(); 514 where.append(MediaStore.Audio.Artists.ARTIST + " != ''"); 515 516 // Add in the filtering constraints 517 String [] keywords = null; 518 if (filter != null) { 519 String [] searchWords = filter.split(" "); 520 keywords = new String[searchWords.length]; 521 Collator col = Collator.getInstance(); 522 col.setStrength(Collator.PRIMARY); 523 for (int i = 0; i < searchWords.length; i++) { 524 keywords[i] = '%' + MediaStore.Audio.keyFor(searchWords[i]) + '%'; 525 } 526 for (int i = 0; i < searchWords.length; i++) { 527 where.append(" AND "); 528 where.append(MediaStore.Audio.Media.ARTIST_KEY + " LIKE ?"); 529 } 530 } 531 532 String whereclause = where.toString(); 533 String[] cols = new String[] { 534 MediaStore.Audio.Artists._ID, 535 MediaStore.Audio.Artists.ARTIST, 536 MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, 537 MediaStore.Audio.Artists.NUMBER_OF_TRACKS 538 }; 539 Cursor ret = null; 540 if (async != null) { 541 async.startQuery(0, null, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, 542 cols, whereclause , keywords, MediaStore.Audio.Artists.ARTIST_KEY); 543 } else { 544 ret = MusicUtils.query(this, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, 545 cols, whereclause , keywords, MediaStore.Audio.Artists.ARTIST_KEY); 546 } 547 return ret; 548 } 549 550 static class ArtistAlbumListAdapter extends SimpleCursorTreeAdapter implements SectionIndexer { 551 552 private final Drawable mNowPlayingOverlay; 553 private final BitmapDrawable mDefaultAlbumIcon; 554 private int mGroupArtistIdIdx; 555 private int mGroupArtistIdx; 556 private int mGroupAlbumIdx; 557 private int mGroupSongIdx; 558 private final Context mContext; 559 private final Resources mResources; 560 private final String mAlbumSongSeparator; 561 private final String mUnknownAlbum; 562 private final String mUnknownArtist; 563 private final StringBuilder mBuffer = new StringBuilder(); 564 private final Object[] mFormatArgs = new Object[1]; 565 private final Object[] mFormatArgs3 = new Object[3]; 566 private MusicAlphabetIndexer mIndexer; 567 private ArtistAlbumBrowserActivity mActivity; 568 private AsyncQueryHandler mQueryHandler; 569 private String mConstraint = null; 570 private boolean mConstraintIsValid = false; 571 572 static class ViewHolder { 573 TextView line1; 574 TextView line2; 575 ImageView play_indicator; 576 ImageView icon; 577 } 578 579 class QueryHandler extends AsyncQueryHandler { 580 QueryHandler(ContentResolver res) { 581 super(res); 582 } 583 584 @Override 585 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 586 //Log.i("@@@", "query complete"); 587 mActivity.init(cursor); 588 } 589 } 590 591 ArtistAlbumListAdapter(Context context, ArtistAlbumBrowserActivity currentactivity, 592 Cursor cursor, int glayout, String[] gfrom, int[] gto, 593 int clayout, String[] cfrom, int[] cto) { 594 super(context, cursor, glayout, gfrom, gto, clayout, cfrom, cto); 595 mActivity = currentactivity; 596 mQueryHandler = new QueryHandler(context.getContentResolver()); 597 598 Resources r = context.getResources(); 599 mNowPlayingOverlay = r.getDrawable(R.drawable.indicator_ic_mp_playing_list); 600 mDefaultAlbumIcon = (BitmapDrawable) r.getDrawable(R.drawable.albumart_mp_unknown_list); 601 // no filter or dither, it's a lot faster and we can't tell the difference 602 mDefaultAlbumIcon.setFilterBitmap(false); 603 mDefaultAlbumIcon.setDither(false); 604 605 mContext = context; 606 getColumnIndices(cursor); 607 mResources = context.getResources(); 608 mAlbumSongSeparator = context.getString(R.string.albumsongseparator); 609 mUnknownAlbum = context.getString(R.string.unknown_album_name); 610 mUnknownArtist = context.getString(R.string.unknown_artist_name); 611 } 612 613 private void getColumnIndices(Cursor cursor) { 614 if (cursor != null) { 615 mGroupArtistIdIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID); 616 mGroupArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST); 617 mGroupAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_ALBUMS); 618 mGroupSongIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.NUMBER_OF_TRACKS); 619 if (mIndexer != null) { 620 mIndexer.setCursor(cursor); 621 } else { 622 mIndexer = new MusicAlphabetIndexer(cursor, mGroupArtistIdx, 623 mResources.getString(R.string.fast_scroll_alphabet)); 624 } 625 } 626 } 627 628 public void setActivity(ArtistAlbumBrowserActivity newactivity) { 629 mActivity = newactivity; 630 } 631 632 public AsyncQueryHandler getQueryHandler() { 633 return mQueryHandler; 634 } 635 636 @Override 637 public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { 638 View v = super.newGroupView(context, cursor, isExpanded, parent); 639 ImageView iv = (ImageView) v.findViewById(R.id.icon); 640 ViewGroup.LayoutParams p = iv.getLayoutParams(); 641 p.width = ViewGroup.LayoutParams.WRAP_CONTENT; 642 p.height = ViewGroup.LayoutParams.WRAP_CONTENT; 643 ViewHolder vh = new ViewHolder(); 644 vh.line1 = (TextView) v.findViewById(R.id.line1); 645 vh.line2 = (TextView) v.findViewById(R.id.line2); 646 vh.play_indicator = (ImageView) v.findViewById(R.id.play_indicator); 647 vh.icon = (ImageView) v.findViewById(R.id.icon); 648 vh.icon.setPadding(0, 0, 1, 0); 649 v.setTag(vh); 650 return v; 651 } 652 653 @Override 654 public View newChildView(Context context, Cursor cursor, boolean isLastChild, 655 ViewGroup parent) { 656 View v = super.newChildView(context, cursor, isLastChild, parent); 657 ViewHolder vh = new ViewHolder(); 658 vh.line1 = (TextView) v.findViewById(R.id.line1); 659 vh.line2 = (TextView) v.findViewById(R.id.line2); 660 vh.play_indicator = (ImageView) v.findViewById(R.id.play_indicator); 661 vh.icon = (ImageView) v.findViewById(R.id.icon); 662 vh.icon.setBackgroundDrawable(mDefaultAlbumIcon); 663 vh.icon.setPadding(0, 0, 1, 0); 664 v.setTag(vh); 665 return v; 666 } 667 668 @Override 669 public void bindGroupView(View view, Context context, Cursor cursor, boolean isexpanded) { 670 671 ViewHolder vh = (ViewHolder) view.getTag(); 672 673 String artist = cursor.getString(mGroupArtistIdx); 674 String displayartist = artist; 675 boolean unknown = artist == null || artist.equals(MediaStore.UNKNOWN_STRING); 676 if (unknown) { 677 displayartist = mUnknownArtist; 678 } 679 vh.line1.setText(displayartist); 680 681 int numalbums = cursor.getInt(mGroupAlbumIdx); 682 int numsongs = cursor.getInt(mGroupSongIdx); 683 684 String songs_albums = MusicUtils.makeAlbumsLabel(context, 685 numalbums, numsongs, unknown); 686 687 vh.line2.setText(songs_albums); 688 689 long currentartistid = MusicUtils.getCurrentArtistId(); 690 long artistid = cursor.getLong(mGroupArtistIdIdx); 691 if (currentartistid == artistid && !isexpanded) { 692 vh.play_indicator.setImageDrawable(mNowPlayingOverlay); 693 } else { 694 vh.play_indicator.setImageDrawable(null); 695 } 696 } 697 698 @Override 699 public void bindChildView(View view, Context context, Cursor cursor, boolean islast) { 700 701 ViewHolder vh = (ViewHolder) view.getTag(); 702 703 String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM)); 704 String displayname = name; 705 boolean unknown = name == null || name.equals(MediaStore.UNKNOWN_STRING); 706 if (unknown) { 707 displayname = mUnknownAlbum; 708 } 709 vh.line1.setText(displayname); 710 711 int numsongs = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS)); 712 int numartistsongs = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.NUMBER_OF_SONGS_FOR_ARTIST)); 713 714 final StringBuilder builder = mBuffer; 715 builder.delete(0, builder.length()); 716 if (unknown) { 717 numsongs = numartistsongs; 718 } 719 720 if (numsongs == 1) { 721 builder.append(context.getString(R.string.onesong)); 722 } else { 723 if (numsongs == numartistsongs) { 724 final Object[] args = mFormatArgs; 725 args[0] = numsongs; 726 builder.append(mResources.getQuantityString(R.plurals.Nsongs, numsongs, args)); 727 } else { 728 final Object[] args = mFormatArgs3; 729 args[0] = numsongs; 730 args[1] = numartistsongs; 731 args[2] = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)); 732 builder.append(mResources.getQuantityString(R.plurals.Nsongscomp, numsongs, args)); 733 } 734 } 735 vh.line2.setText(builder.toString()); 736 737 ImageView iv = vh.icon; 738 // We don't actually need the path to the thumbnail file, 739 // we just use it to see if there is album art or not 740 String art = cursor.getString(cursor.getColumnIndexOrThrow( 741 MediaStore.Audio.Albums.ALBUM_ART)); 742 if (unknown || art == null || art.length() == 0) { 743 iv.setBackgroundDrawable(mDefaultAlbumIcon); 744 iv.setImageDrawable(null); 745 } else { 746 long artIndex = cursor.getLong(0); 747 Drawable d = MusicUtils.getCachedArtwork(context, artIndex, mDefaultAlbumIcon); 748 iv.setImageDrawable(d); 749 } 750 751 long currentalbumid = MusicUtils.getCurrentAlbumId(); 752 long aid = cursor.getLong(0); 753 iv = vh.play_indicator; 754 if (currentalbumid == aid) { 755 iv.setImageDrawable(mNowPlayingOverlay); 756 } else { 757 iv.setImageDrawable(null); 758 } 759 } 760 761 762 @Override 763 protected Cursor getChildrenCursor(Cursor groupCursor) { 764 765 long id = groupCursor.getLong(groupCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID)); 766 767 String[] cols = new String[] { 768 MediaStore.Audio.Albums._ID, 769 MediaStore.Audio.Albums.ALBUM, 770 MediaStore.Audio.Albums.NUMBER_OF_SONGS, 771 MediaStore.Audio.Albums.NUMBER_OF_SONGS_FOR_ARTIST, 772 MediaStore.Audio.Albums.ALBUM_ART 773 }; 774 Cursor c = MusicUtils.query(mActivity, 775 MediaStore.Audio.Artists.Albums.getContentUri("external", id), 776 cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER); 777 778 class MyCursorWrapper extends CursorWrapper { 779 String mArtistName; 780 int mMagicColumnIdx; 781 MyCursorWrapper(Cursor c, String artist) { 782 super(c); 783 mArtistName = artist; 784 if (mArtistName == null || mArtistName.equals(MediaStore.UNKNOWN_STRING)) { 785 mArtistName = mUnknownArtist; 786 } 787 mMagicColumnIdx = c.getColumnCount(); 788 } 789 790 @Override 791 public String getString(int columnIndex) { 792 if (columnIndex != mMagicColumnIdx) { 793 return super.getString(columnIndex); 794 } 795 return mArtistName; 796 } 797 798 @Override 799 public int getColumnIndexOrThrow(String name) { 800 if (MediaStore.Audio.Albums.ARTIST.equals(name)) { 801 return mMagicColumnIdx; 802 } 803 return super.getColumnIndexOrThrow(name); 804 } 805 806 @Override 807 public String getColumnName(int idx) { 808 if (idx != mMagicColumnIdx) { 809 return super.getColumnName(idx); 810 } 811 return MediaStore.Audio.Albums.ARTIST; 812 } 813 814 @Override 815 public int getColumnCount() { 816 return super.getColumnCount() + 1; 817 } 818 } 819 return new MyCursorWrapper(c, groupCursor.getString(mGroupArtistIdx)); 820 } 821 822 @Override 823 public void changeCursor(Cursor cursor) { 824 if (mActivity.isFinishing() && cursor != null) { 825 cursor.close(); 826 cursor = null; 827 } 828 if (cursor != mActivity.mArtistCursor) { 829 mActivity.mArtistCursor = cursor; 830 getColumnIndices(cursor); 831 super.changeCursor(cursor); 832 } 833 } 834 835 @Override 836 public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 837 String s = constraint.toString(); 838 if (mConstraintIsValid && ( 839 (s == null && mConstraint == null) || 840 (s != null && s.equals(mConstraint)))) { 841 return getCursor(); 842 } 843 Cursor c = mActivity.getArtistCursor(null, s); 844 mConstraint = s; 845 mConstraintIsValid = true; 846 return c; 847 } 848 849 public Object[] getSections() { 850 return mIndexer.getSections(); 851 } 852 853 public int getPositionForSection(int sectionIndex) { 854 return mIndexer.getPositionForSection(sectionIndex); 855 } 856 857 public int getSectionForPosition(int position) { 858 return 0; 859 } 860 } 861 862 private Cursor mArtistCursor; 863 864 public void onServiceConnected(ComponentName name, IBinder service) { 865 MusicUtils.updateNowPlaying(this); 866 } 867 868 public void onServiceDisconnected(ComponentName name) { 869 finish(); 870 } 871} 872 873