📄 artistalbumbrowseractivity.java
字号:
/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.imusic;import java.text.Collator;import android.app.ExpandableListActivity;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.BroadcastReceiver;import android.content.res.Resources;import android.database.Cursor;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.media.AudioManager;import android.media.MediaFile;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.provider.MediaStore;import android.util.Log;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.ContextMenu.ContextMenuInfo;import android.widget.ExpandableListView.ExpandableListContextMenuInfo;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.SimpleCursorTreeAdapter;import android.widget.TextView;public class ArtistAlbumBrowserActivity extends ExpandableListActivity implements View.OnCreateContextMenuListener, MusicUtils.Defs{ private String mCurrentArtistId; private String mCurrentArtistName; private String mCurrentAlbumId; private String mCurrentAlbumName; public ArtistAlbumBrowserActivity() { } /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setVolumeControlStream(AudioManager.STREAM_MUSIC); if (icicle != null) { mCurrentAlbumId = icicle.getString("selectedalbum"); mCurrentAlbumName = icicle.getString("selectedalbumname"); mCurrentArtistId = icicle.getString("selectedartist"); mCurrentArtistName = icicle.getString("selectedartistname"); } MusicUtils.bindToService(this); IntentFilter f = new IntentFilter(); f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); f.addAction(Intent.ACTION_MEDIA_UNMOUNTED); f.addDataScheme("file"); registerReceiver(mScanListener, f); init(); } @Override public void onSaveInstanceState(Bundle outcicle) { // need to store the selected item so we don't lose it in case // of an orientation switch. Otherwise we could lose it while // in the middle of specifying a playlist to add the item to. outcicle.putString("selectedalbum", mCurrentAlbumId); outcicle.putString("selectedalbumname", mCurrentAlbumName); outcicle.putString("selectedartist", mCurrentArtistId); outcicle.putString("selectedartistname", mCurrentArtistName); super.onSaveInstanceState(outcicle); } @Override public void onDestroy() { MusicUtils.unbindFromService(this); if (mArtistCursor != null) { mArtistCursor.close(); } unregisterReceiver(mScanListener); super.onDestroy(); } @Override public void onResume() { super.onResume(); IntentFilter f = new IntentFilter(); f.addAction(MediaPlaybackService.META_CHANGED); f.addAction(MediaPlaybackService.QUEUE_CHANGED); registerReceiver(mTrackListListener, f); mTrackListListener.onReceive(null, null); MusicUtils.setSpinnerState(this); } private BroadcastReceiver mTrackListListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { getExpandableListView().invalidateViews(); } }; private BroadcastReceiver mScanListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { MusicUtils.setSpinnerState(ArtistAlbumBrowserActivity.this); mReScanHandler.sendEmptyMessage(0); if (intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) { MusicUtils.clearAlbumArtCache(); } } }; private Handler mReScanHandler = new Handler() { public void handleMessage(Message msg) { init(); if (mArtistCursor == null) { sendEmptyMessageDelayed(0, 1000); } } }; @Override public void onPause() { unregisterReceiver(mTrackListListener); mReScanHandler.removeCallbacksAndMessages(null); super.onPause(); } public void init() { mArtistCursor = getArtistCursor(null); if (mArtistCursor == null) { MusicUtils.displayDatabaseError(this); return; } setContentView(android.R.layout.expandable_list_content); // Map Cursor columns to views defined in media_list_item.xml ArtistAlbumListAdapter adapter = new ArtistAlbumListAdapter( this, mArtistCursor, R.layout.track_list_item_group, new String[] {}, new int[] {}, R.layout.track_list_item_child, new String[] {}, new int[] {}); setTitle(R.string.artists_title); setListAdapter(adapter); ExpandableListView lv = getExpandableListView(); lv.setOnCreateContextMenuListener(this); lv.setTextFilterEnabled(true); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { mCurrentAlbumId = Long.valueOf(id).toString(); Intent intent = new Intent(Intent.ACTION_PICK); intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/Track"); intent.putExtra("album", mCurrentAlbumId); mArtistCursor.moveToPosition(groupPosition); mCurrentArtistId = Long.valueOf(id).toString(); intent.putExtra("artist", mCurrentArtistId); startActivity(intent); return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, GOTO_START, 0, R.string.goto_start).setIcon(R.drawable.ic_menu_music_library); menu.add(0, GOTO_PLAYBACK, 0, R.string.goto_playback).setIcon(R.drawable.ic_menu_playback); menu.add(0, SHUFFLE_ALL, 0, R.string.shuffle_all).setIcon(R.drawable.ic_menu_shuffle); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.findItem(GOTO_PLAYBACK).setVisible(MusicUtils.isMusicLoaded()); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { Intent intent; Cursor cursor; switch (item.getItemId()) { case GOTO_START: intent = new Intent(); intent.setClass(this, MusicBrowserActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; case GOTO_PLAYBACK: intent = new Intent("com.android.imusic.playing"); startActivity(intent); return true; case SHUFFLE_ALL: cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String [] { MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC + "=1", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (cursor != null) { MusicUtils.shuffleAll(this, cursor); cursor.close(); } return true; } return super.onOptionsItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) { menu.add(0, PLAY_SELECTION, 0, R.string.play_selection); SubMenu sub = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0, R.string.add_to_playlist); MusicUtils.makePlaylistMenu(this, sub); menu.add(0, DELETE_ITEM, 0, R.string.delete_item); ExpandableListContextMenuInfo mi = (ExpandableListContextMenuInfo) menuInfoIn; int itemtype = ExpandableListView.getPackedPositionType(mi.packedPosition); int gpos = ExpandableListView.getPackedPositionGroup(mi.packedPosition); int cpos = ExpandableListView.getPackedPositionChild(mi.packedPosition); if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { if (gpos == -1) { // this shouldn't happen Log.d("Artist/Album", "no group"); return; } gpos = gpos - getExpandableListView().getHeaderViewsCount(); mArtistCursor.moveToPosition(gpos); mCurrentArtistId = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists._ID)); mCurrentArtistName = mArtistCursor.getString(mArtistCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)); mCurrentAlbumId = null; menu.setHeaderTitle(mCurrentArtistName); return; } else if (itemtype == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { if (cpos == -1) { // this shouldn't happen Log.d("Artist/Album", "no child"); return; } Cursor c = (Cursor) getExpandableListAdapter().getChild(gpos, cpos); c.moveToPosition(cpos); mCurrentArtistId = null; mCurrentAlbumId = Long.valueOf(mi.id).toString(); mCurrentAlbumName = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM)); menu.setHeaderTitle(mCurrentAlbumName); } } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case PLAY_SELECTION: { // play everything by the selected artist int [] list = mCurrentArtistId != null ? MusicUtils.getSongListForArtist(this, Integer.parseInt(mCurrentArtistId)) : MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); MusicUtils.playAll(this, list, 0); return true; } case QUEUE: { int [] list = mCurrentArtistId != null ? MusicUtils.getSongListForArtist(this, Integer.parseInt(mCurrentArtistId)) : MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); MusicUtils.addToCurrentPlaylist(this, list); return true; } case NEW_PLAYLIST: {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -