[Android] Audio browser: add a new fragment to display the albums and songs of an artist or a genre

Adrien Maglo git at videolan.org
Tue Nov 19 16:03:34 CET 2013


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Tue Nov 19 16:01:09 2013 +0100| [40d36ab98bb2f19234b46d3c128afcbb5b4e3407] | committer: Adrien Maglo

Audio browser: add a new fragment to display the albums and songs of an artist or a genre

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=40d36ab98bb2f19234b46d3c128afcbb5b4e3407
---

 vlc-android/res/layout/audio_albums_songs.xml      |   43 +++++
 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    |  175 ++++++++++++++++++++
 2 files changed, 218 insertions(+)

diff --git a/vlc-android/res/layout/audio_albums_songs.xml b/vlc-android/res/layout/audio_albums_songs.xml
new file mode 100644
index 0000000..869f76a
--- /dev/null
+++ b/vlc-android/res/layout/audio_albums_songs.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<TabHost
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@android:id/tabhost"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <LinearLayout
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:orientation="vertical" >
+
+        <TabWidget
+            android:id="@android:id/tabs"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" >
+        </TabWidget>
+
+        <FrameLayout
+            android:id="@android:id/tabcontent"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" >
+
+            <ListView
+                android:id="@+id/albums"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:fastScrollEnabled="true"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp" />
+
+            <ListView
+                android:id="@+id/songs"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:fastScrollEnabled="true"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp" />
+
+        </FrameLayout>
+    </LinearLayout>
+
+</TabHost>
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
new file mode 100644
index 0000000..20dd3c8
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -0,0 +1,175 @@
+/*****************************************************************************
+ * AudioListActivity.java
+ *****************************************************************************
+ * Copyright © 2011-2012 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.vlc.gui.audio;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import org.videolan.libvlc.Media;
+import org.videolan.vlc.AudioServiceController;
+import org.videolan.vlc.R;
+import org.videolan.vlc.Util;
+import org.videolan.vlc.gui.MainActivity;
+
+import android.os.Bundle;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.LayoutInflater;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.AdapterView;
+import android.widget.ListView;
+import android.widget.TabHost;
+import com.actionbarsherlock.app.SherlockFragment;
+
+public class AudioAlbumsSongsFragment extends SherlockFragment {
+
+    public final static String TAG = "VLC/AudioAlbumsSongsFragment";
+
+    AudioServiceController mAudioController;
+
+    private AudioBrowserListAdapter mSongsAdapter;
+    private AudioBrowserListAdapter mAlbumsAdapter;
+
+    public final static String EXTRA_NAME = "name";
+    public final static String EXTRA_NAME2 = "name2";
+    public final static String EXTRA_MODE = "mode";
+
+    private ArrayList<Media> mediaList;
+
+    private static final MediaComparators mComparators = new MediaComparators();
+
+    /* All subclasses of Fragment must include a public empty constructor. */
+    public AudioAlbumsSongsFragment() { }
+
+    public AudioAlbumsSongsFragment(ArrayList<Media> mediaList) {
+        this.mediaList = mediaList;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        mAlbumsAdapter = new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_NORMAL);
+        mSongsAdapter = new AudioBrowserListAdapter(getActivity(), AudioBrowserListAdapter.ITEM_NORMAL);
+
+        mAudioController = AudioServiceController.getInstance();
+    }
+
+    public void onViewCreated(View view, Bundle savedInstanceState) {
+        super.onViewCreated(view, savedInstanceState);
+        updateList();
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+        View v = inflater.inflate(R.layout.audio_albums_songs, container, false);
+
+        TabHost tabHost = (TabHost) v.findViewById(android.R.id.tabhost);
+        ListView albumsList = (ListView) v.findViewById(R.id.albums);
+        ListView songsList = (ListView) v.findViewById(R.id.songs);
+
+        songsList.setAdapter(mSongsAdapter);
+        albumsList.setAdapter(mAlbumsAdapter);
+
+        songsList.setOnItemClickListener(songsListener);
+        albumsList.setOnItemClickListener(albumsListener);
+
+        tabHost.setup();
+
+        tabHost.addTab(tabHost.newTabSpec("albums").setIndicator("Albums").setContent(R.id.albums));
+        tabHost.addTab(tabHost.newTabSpec("songs").setIndicator("Songs").setContent(R.id.songs));
+
+        return v;
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+    }
+
+    @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater inflater = getActivity().getMenuInflater();
+        inflater.inflate(R.menu.audio_list_browser, menu);
+        if (!Util.isPhone())
+            menu.setGroupVisible(R.id.phone_only, false);
+    }
+
+    @Override
+    public boolean onContextItemSelected(MenuItem item) {
+        return super.onContextItemSelected(item);
+    }
+
+    private void updateList() {
+        if (mediaList == null)
+            return;
+
+        Collections.sort(mediaList, mComparators.byAlbum);
+        for (int i = 0; i < mediaList.size(); ++i) {
+            Media media = mediaList.get(i);
+            mAlbumsAdapter.add(media.getAlbum(), null, media);
+        }
+
+        Collections.sort(mediaList, mComparators.byName);
+        for (int i = 0; i < mediaList.size(); ++i) {
+            Media media = mediaList.get(i);
+            mSongsAdapter.add(media.getTitle(), null, media);
+        }
+    }
+
+    OnItemClickListener albumsListener = new OnItemClickListener() {
+        @Override
+        public void onItemClick(AdapterView<?> av, View v, int p, long id) {
+            String name = mAlbumsAdapter.getMedia(p).get(0).getAlbum();
+
+            AudioListFragment audioList = new AudioListFragment();
+            Bundle b = new Bundle();
+            b.putString(AudioListFragment.EXTRA_NAME, name);
+            b.putString(AudioListFragment.EXTRA_NAME2, null);
+            b.putInt(AudioListFragment.EXTRA_MODE, AudioBrowserFragment.MODE_ALBUM);
+            audioList.setArguments(b);
+
+            MainActivity.ShowFragment(getActivity(), "tracks", audioList);
+        }
+    };
+
+    OnItemClickListener songsListener = new OnItemClickListener() {
+        @Override
+        public void onItemClick(AdapterView<?> av, View v, int p, long id) {
+            ArrayList<String> mediaLocation = new ArrayList<String>();
+            mediaLocation.add(mSongsAdapter.getMedia(p).get(0).getLocation());
+            mAudioController.load(mediaLocation, p);
+            AudioPlayerFragment.start(getActivity());
+        }
+    };
+}



More information about the Android mailing list