⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trackbrowseractivity.java

📁 Android平台上的media player, iPhone风格
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * 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 android.app.ListActivity;import android.content.*;import android.database.AbstractCursor;import android.database.CharArrayBuffer;import android.database.Cursor;import android.media.AudioManager;import android.media.MediaFile;import android.net.Uri;import android.os.Bundle;import android.os.Debug;import android.os.RemoteException;import android.os.Handler;import android.os.Message;import android.provider.MediaStore;import android.provider.MediaStore.Audio.Playlists;import android.util.Log;import android.view.ContextMenu;import android.view.KeyEvent;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.FrameLayout;import android.widget.ImageView;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;import android.widget.Toast;import java.text.Collator;import java.util.Arrays;import java.util.Map;public class TrackBrowserActivity extends ListActivity        implements View.OnCreateContextMenuListener, MusicUtils.Defs{    private final int Q_SELECTED = CHILD_MENU_BASE;    private final int Q_ALL = CHILD_MENU_BASE + 1;    private final int SAVE_AS_PLAYLIST = CHILD_MENU_BASE + 2;    private final int PLAY_ALL = CHILD_MENU_BASE + 3;    private final int CLEAR_PLAYLIST = CHILD_MENU_BASE + 4;    private final int REMOVE = CHILD_MENU_BASE + 5;    private final int PLAY_NOW = 0;    private final int ADD_TO_QUEUE = 1;    private final int PLAY_NEXT = 2;    private final int JUMP_TO = 3;    private final int CLEAR_HISTORY = 4;    private final int CLEAR_ALL = 5;    private static final String LOGTAG = "TrackBrowser";    private String[] mCursorCols;    private String[] mPlaylistMemberCols;    private boolean mDeletedOneRow = false;    private boolean mEditMode = false;    private String mCurrentTrackName;    public TrackBrowserActivity()    {    }    /** 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) {            mSelectedId = icicle.getLong("selectedtrack");            mAlbumId = icicle.getString("album");            mArtistId = icicle.getString("artist");            mPlaylist = icicle.getString("playlist");            mGenre = icicle.getString("genre");            mEditMode = icicle.getBoolean("editmode", false);        } else {            mAlbumId = getIntent().getStringExtra("album");            // If we have an album, show everything on the album, not just stuff            // by a particular artist.            Intent intent = getIntent();            mArtistId = intent.getStringExtra("artist");            mPlaylist = intent.getStringExtra("playlist");            mGenre = intent.getStringExtra("genre");            mEditMode = intent.getAction().equals(Intent.ACTION_EDIT);        }        mCursorCols = new String[] {                MediaStore.Audio.Media._ID,                MediaStore.Audio.Media.TITLE,                MediaStore.Audio.Media.TITLE_KEY,                MediaStore.Audio.Media.DATA,                MediaStore.Audio.Media.ALBUM,                MediaStore.Audio.Media.ARTIST,                MediaStore.Audio.Media.ARTIST_ID,                MediaStore.Audio.Media.DURATION        };        mPlaylistMemberCols = new String[] {                MediaStore.Audio.Playlists.Members._ID,                MediaStore.Audio.Media.TITLE,                MediaStore.Audio.Media.TITLE_KEY,                MediaStore.Audio.Media.DATA,                MediaStore.Audio.Media.ALBUM,                MediaStore.Audio.Media.ARTIST,                MediaStore.Audio.Media.ARTIST_ID,                MediaStore.Audio.Media.DURATION,                MediaStore.Audio.Playlists.Members.PLAY_ORDER,                MediaStore.Audio.Playlists.Members.AUDIO_ID        };        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();        //Debug.startMethodTracing();    }    @Override    public void onDestroy() {        //Debug.stopMethodTracing();        MusicUtils.unbindFromService(this);        try {            if ("nowplaying".equals(mPlaylist)) {                unregisterReceiver(mNowPlayingListener);            } else {                unregisterReceiver(mTrackListListener);            }        } catch (IllegalArgumentException ex) {            // we end up here in case we never registered the listeners        }        if (mTrackCursor != null) {            mTrackCursor.close();        }        unregisterReceiver(mScanListener);        super.onDestroy();   }        @Override    public void onResume() {        super.onResume();        if (mTrackCursor != null) {            getListView().invalidateViews();        }        MusicUtils.setSpinnerState(this);    }    @Override    public void onPause() {        mReScanHandler.removeCallbacksAndMessages(null);        super.onPause();    }    private BroadcastReceiver mScanListener = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            MusicUtils.setSpinnerState(TrackBrowserActivity.this);            mReScanHandler.sendEmptyMessage(0);        }    };        private Handler mReScanHandler = new Handler() {        public void handleMessage(Message msg) {            init();            if (mTrackCursor == null) {                sendEmptyMessageDelayed(0, 1000);            }        }    };        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.putLong("selectedtrack", mSelectedId);        outcicle.putString("artist", mArtistId);        outcicle.putString("album", mAlbumId);        outcicle.putString("playlist", mPlaylist);        outcicle.putString("genre", mGenre);        outcicle.putBoolean("editmode", mEditMode);        super.onSaveInstanceState(outcicle);    }        public void init() {        mTrackCursor = getTrackCursor(null);        setContentView(R.layout.media_picker_activity);        mTrackList = (ListView) findViewById(android.R.id.list);        if (mEditMode) {            //((TouchInterceptor) mTrackList).setDragListener(mDragListener);            ((TouchInterceptor) mTrackList).setDropListener(mDropListener);            ((TouchInterceptor) mTrackList).setRemoveListener(mRemoveListener);        }        if (mTrackCursor == null) {            MusicUtils.displayDatabaseError(this);            return;        }        int numresults = mTrackCursor.getCount();        if (numresults > 0) {            mTrackCursor.moveToFirst();            CharSequence fancyName = null;            if (mAlbumId != null) {                fancyName = mTrackCursor.getString(mTrackCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));                // For compilation albums show only the album title,                // but for regular albums show "artist - album".                // To determine whether something is a compilation                // album, do a query for the artist + album of the                // first item, and see if it returns the same number                // of results as the album query.                String where = MediaStore.Audio.Media.ALBUM_ID + "='" + mAlbumId +                        "' AND " + MediaStore.Audio.Media.ARTIST_ID + "='" +                         mTrackCursor.getString(mTrackCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)) + "'";                Cursor cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,                    new String[] {MediaStore.Audio.Media.ALBUM}, where, null, null);                if (cursor != null) {                    if (cursor.getCount() != numresults) {                        // compilation album                        fancyName = mTrackCursor.getString(mTrackCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));                    }                        cursor.deactivate();                }            } else if (mPlaylist != null) {                if (mPlaylist.equals("nowplaying")) {                    if (MusicUtils.getCurrentShuffleMode() == MediaPlaybackService.SHUFFLE_AUTO) {                        fancyName = getText(R.string.partyshuffle_title);                    } else {                        fancyName = getText(R.string.nowplaying_title);                    }                } else {                    String [] cols = new String [] {                    MediaStore.Audio.Playlists.NAME                    };                    Cursor cursor = MusicUtils.query(this,                            ContentUris.withAppendedId(Playlists.EXTERNAL_CONTENT_URI, Long.valueOf(mPlaylist)),                            cols, null, null, null);                    if (cursor != null) {                        if (cursor.getCount() != 0) {                            cursor.moveToFirst();                            fancyName = cursor.getString(0);                        }                        cursor.deactivate();                    }                }            } else if (mGenre != null) {                String [] cols = new String [] {                MediaStore.Audio.Genres.NAME                };                Cursor cursor = MusicUtils.query(this,                        ContentUris.withAppendedId(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, Long.valueOf(mGenre)),                        cols, null, null, null);                if (cursor != null) {                    if (cursor.getCount() != 0) {                        cursor.moveToFirst();                        fancyName = cursor.getString(0);                    }                    cursor.deactivate();                }            }            if (fancyName != null) {                setTitle(fancyName);            } else {                setTitle(R.string.tracks_title);            }        } else {            setTitle(R.string.no_tracks_title);        }        TrackListAdapter adapter = new TrackListAdapter(                this,                mEditMode ? R.layout.edit_track_list_item : R.layout.track_list_item2,                mTrackCursor,                new String[] {},                new int[] {},                "nowplaying".equals(mPlaylist));        setListAdapter(adapter);        ListView lv = getListView();        lv.setOnCreateContextMenuListener(this);        lv.setCacheColorHint(0);        if (!mEditMode) {            lv.setTextFilterEnabled(true);        }        // When showing the queue, position the selection on the currently playing track        // Otherwise, position the selection on the first matching artist, if any        IntentFilter f = new IntentFilter();        f.addAction(MediaPlaybackService.META_CHANGED);        f.addAction(MediaPlaybackService.QUEUE_CHANGED);        if ("nowplaying".equals(mPlaylist)) {            try {                int cur = MusicUtils.sService.getQueuePosition();                setSelection(cur);                registerReceiver(mNowPlayingListener, new IntentFilter(f));                mNowPlayingListener.onReceive(this, new Intent(MediaPlaybackService.META_CHANGED));            } catch (RemoteException ex) {            }        } else {            String key = getIntent().getStringExtra("artist");            if (key != null) {                int keyidx = mTrackCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID);                mTrackCursor.moveToFirst();                while (! mTrackCursor.isAfterLast()) {                    String artist = mTrackCursor.getString(keyidx);                    if (artist.equals(key)) {                        setSelection(mTrackCursor.getPosition());                        break;                    }                    mTrackCursor.moveToNext();                }            }            registerReceiver(mTrackListListener, new IntentFilter(f));            mTrackListListener.onReceive(this, new Intent(MediaPlaybackService.META_CHANGED));        }    }        private TouchInterceptor.DragListener mDragListener =        new TouchInterceptor.DragListener() {        public void drag(int from, int to) {            if (mTrackCursor instanceof NowPlayingCursor) {                NowPlayingCursor c = (NowPlayingCursor) mTrackCursor;                c.moveItem(from, to);                ((TrackListAdapter)getListAdapter()).notifyDataSetChanged();                getListView().invalidateViews();                mDeletedOneRow = true;            }        }    };    private TouchInterceptor.DropListener mDropListener =        new TouchInterceptor.DropListener() {        public void drop(int from, int to) {            if (mTrackCursor instanceof NowPlayingCursor) {                // update the currently playing list                NowPlayingCursor c = (NowPlayingCursor) mTrackCursor;                c.moveItem(from, to);                ((TrackListAdapter)getListAdapter()).notifyDataSetChanged();                getListView().invalidateViews();                mDeletedOneRow = true;            } else {                // update a saved playlist                int colidx = mTrackCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.PLAY_ORDER);                if (from < to) {                    // move the item to somewhere later in the list                    mTrackCursor.moveToPosition(to);                    int toidx = mTrackCursor.getInt(colidx);                    mTrackCursor.moveToPosition(from);//                    mTrackCursor.updateInt(colidx, toidx);                    for (int i = from + 1; i <= to; i++) {                        mTrackCursor.moveToPosition(i);//                        mTrackCursor.updateInt(colidx, i - 1);                    }//                    mTrackCursor.commitUpdates();                } else if (from > to) {                    // move the item to somewhere earlier in the list

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -