📄 albumbrowseractivity.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.ListActivity;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.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.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.AdapterView.AdapterContextMenuInfo;import android.widget.ImageView;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;public class AlbumBrowserActivity extends ListActivity implements View.OnCreateContextMenuListener, MusicUtils.Defs{ private String mCurrentAlbumId; private String mCurrentAlbumName; public AlbumBrowserActivity() { } /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { if (icicle != null) { mCurrentAlbumId = icicle.getString("selectedalbum"); mArtistId = icicle.getString("artist"); } else { mArtistId = getIntent().getStringExtra("artist"); } super.onCreate(icicle); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setVolumeControlStream(AudioManager.STREAM_MUSIC); 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("artist", mArtistId); super.onSaveInstanceState(outcicle); } @Override public void onDestroy() { MusicUtils.unbindFromService(this); if (mAlbumCursor != null) { mAlbumCursor.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) { getListView().invalidateViews(); } }; private BroadcastReceiver mScanListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { MusicUtils.setSpinnerState(AlbumBrowserActivity.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 (mAlbumCursor == null) { sendEmptyMessageDelayed(0, 1000); } } }; @Override public void onPause() { unregisterReceiver(mTrackListListener); mReScanHandler.removeCallbacksAndMessages(null); super.onPause(); } public void init() { mAlbumCursor = getAlbumCursor(null); /* The UI spec does not call for icons in the title bar if (mArtistId != null) { requestWindowFeature(Window.FEATURE_LEFT_ICON); } */ setContentView(R.layout.media_picker_activity); /* The UI spec does not call for icons in the title bar if (mArtistId != null) { // this call needs to happen after setContentView setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.person_title_icon); } */ if (mAlbumCursor == null) { MusicUtils.displayDatabaseError(this); return; } CharSequence fancyName = ""; if (mAlbumCursor.getCount() > 0) { mAlbumCursor.moveToFirst(); fancyName = mAlbumCursor.getString(3); if (MediaFile.UNKNOWN_STRING.equals(fancyName)) fancyName = getText(R.string.unknown_artist_name); if (mArtistId != null && fancyName != null) setTitle(fancyName); else setTitle(R.string.albums_title); } else { setTitle(R.string.no_albums_title); } // Map Cursor columns to views defined in media_list_item.xml AlbumListAdapter adapter = new AlbumListAdapter( this, R.layout.track_list_item, mAlbumCursor, new String[] {}, new int[] {}); setListAdapter(adapter); ListView lv = getListView(); lv.setOnCreateContextMenuListener(this); lv.setTextFilterEnabled(true); // this.set } @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); AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfoIn; mAlbumCursor.moveToPosition(mi.position); mCurrentAlbumId = mAlbumCursor.getString(mAlbumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID)); mCurrentAlbumName = mAlbumCursor.getString(mAlbumCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM)); menu.setHeaderTitle(mCurrentAlbumName); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case PLAY_SELECTION: { // play the selected album int [] list = MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); MusicUtils.playAll(this, list, 0); return true; } case QUEUE: { int [] list = MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); MusicUtils.addToCurrentPlaylist(this, list); return true; } case NEW_PLAYLIST: { Intent intent = new Intent(); intent.setClass(this, CreatePlaylist.class); startActivityForResult(intent, NEW_PLAYLIST); return true; } case PLAYLIST_SELECTED: { int [] list = MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); int playlist = item.getIntent().getIntExtra("playlist", 0); MusicUtils.addToPlaylist(this, list, playlist); return true; } case DELETE_ITEM: { int [] list = MusicUtils.getSongListForAlbum(this, Integer.parseInt(mCurrentAlbumId)); String f = getString(R.string.delete_album_desc); String desc = String.format(f, mCurrentAlbumName); Bundle b = new Bundle(); b.putString("description", desc); b.putIntArray("items", list); Intent intent = new Intent(); intent.setClass(this, DeleteItems.class); intent.putExtras(b); startActivityForResult(intent, -1); return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -