Commit ff738b53 authored by Sébastien Toque's avatar Sébastien Toque

Add audio browsing by artist, album & genre

parent 1624c364
......@@ -71,21 +71,24 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true" />
<LinearLayout
<ListView
android:id="@+id/artists_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_height="match_parent"
android:fastScrollEnabled="true" />
<ListView
android:id="@+id/albums_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
<ListView
android:id="@+id/genres_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_height="match_parent"
android:fastScrollEnabled="true" />
<ListView
android:id="@+id/playklists_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:fastScrollEnabled="true" />
</org.videolan.vlc.android.widget.FlingViewGroup>
</LinearLayout>
package org.videolan.vlc.android;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.videolan.vlc.android.widget.FlingViewGroup;
......@@ -27,13 +25,15 @@ public class AudioBrowserActivity extends Activity {
public final static String TAG = "VLC/AudioBrowserActivity";
private FlingViewGroup mFlingViewGroup;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private HorizontalScrollView mHeader;
private AudioServiceController mAudioController;
private MediaLibrary mMediaLibrary;
private AudioSongsListAdapter mSongsAdapter;
private AudioPlaylistAdapter mArtistsAdapter;
private AudioPlaylistAdapter mAlbumsAdapter;
private AudioPlaylistAdapter mGenresAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -50,23 +50,54 @@ public class AudioBrowserActivity extends Activity {
mMediaLibrary.addUpdateHandler(mHandler);
mSongsAdapter = new AudioSongsListAdapter(this, R.layout.audio_browser_item);
mArtistsAdapter = new AudioPlaylistAdapter(this, R.layout.audio_browser_item);
mAlbumsAdapter = new AudioPlaylistAdapter(this, R.layout.audio_browser_item);
mGenresAdapter = new AudioPlaylistAdapter(this, R.layout.audio_browser_item);
ListView songsList = (ListView) findViewById(R.id.songs_list);
ListView artistList = (ListView) findViewById(R.id.artists_list);
ListView albumList = (ListView) findViewById(R.id.albums_list);
ListView genreList = (ListView) findViewById(R.id.genres_list);
songsList.setAdapter(mSongsAdapter);
songsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
mAudioController.load(mSongsAdapter.getPaths(), p);
Intent intent = new Intent(AudioBrowserActivity.this, AudioPlayerActivity.class);
startActivity(intent);
}
});
artistList.setAdapter(mArtistsAdapter);
albumList.setAdapter(mAlbumsAdapter);
genreList.setAdapter(mGenresAdapter);
songsList.setOnItemClickListener(songListener);
artistList.setOnItemClickListener(playlistListener);
albumList.setOnItemClickListener(playlistListener);
genreList.setOnItemClickListener(playlistListener);
updateLists();
}
OnItemClickListener songListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
mAudioController.load(mSongsAdapter.getPaths(), p);
Intent intent = new Intent(AudioBrowserActivity.this, AudioPlayerActivity.class);
startActivity(intent);
}
};
OnItemClickListener playlistListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int p, long id) {
AudioPlaylistAdapter adapter = (AudioPlaylistAdapter) av.getAdapter();
List<String> playlist = adapter.getPlaylist(p);
if (playlist.isEmpty())
return;
mAudioController.load(playlist, 0);
Intent intent = new Intent(AudioBrowserActivity.this, AudioPlayerActivity.class);
startActivity(intent);
}
};
@Override
protected void onDestroy() {
mMediaLibrary.removeUpdateHandler(mHandler);
mSongsAdapter.clear();
mArtistsAdapter.clear();
mAlbumsAdapter.clear();
mGenresAdapter.clear();
super.onDestroy();
}
......@@ -109,19 +140,68 @@ public class AudioBrowserActivity extends Activity {
}
};
private Comparator<Media> byPath = new Comparator<Media>() {
public int compare(Media m1, Media m2) {
return String.CASE_INSENSITIVE_ORDER.compare(m1.getFile().getPath(), m2.getFile().getPath());
};
};
private Comparator<Media> byAlbum = new Comparator<Media>() {
public int compare(Media m1, Media m2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(m1.getAlbum(), m2.getAlbum());
if (res == 0)
res = byPath.compare(m1, m2);
return res;
};
};
private Comparator<Media> byArtist = new Comparator<Media>() {
public int compare(Media m1, Media m2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(m1.getArtist(), m2.getArtist());
if (res == 0)
res = byAlbum.compare(m1, m2);
return res;
};
};
private Comparator<Media> byGenre = new Comparator<Media>() {
public int compare(Media m1, Media m2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(m1.getGenre(), m2.getGenre());
if (res == 0)
res = byArtist.compare(m1, m2);
return res;
};
};
private void updateLists() {
List<Media> audioList = MediaLibrary.getInstance(this).getAudioItems();
Collections.sort(audioList, new Comparator<Media>() {
public int compare(Media m1, Media m2) {
return String.CASE_INSENSITIVE_ORDER.compare(m1.getFile().getPath(), m2.getFile().getPath());
};
});
mSongsAdapter.clear();
for (int i = 0; i < audioList.size(); i++) {
mArtistsAdapter.clear();
mAlbumsAdapter.clear();
mGenresAdapter.clear();
Collections.sort(audioList, byPath);
for (int i = 0; i < audioList.size(); i++)
mSongsAdapter.add(audioList.get(i));
Collections.sort(audioList, byArtist);
for (int i = 0; i < audioList.size(); i++) {
Media media = audioList.get(i);
mArtistsAdapter.add(media.getArtist(), media);
}
Collections.sort(audioList, byAlbum);
for (int i = 0; i < audioList.size(); i++) {
Media media = audioList.get(i);
mAlbumsAdapter.add(media.getAlbum(), media);
}
Collections.sort(audioList, byGenre);
for (int i = 0; i < audioList.size(); i++) {
Media media = audioList.get(i);
mGenresAdapter.add(media.getGenre(), media);
}
mSongsAdapter.notifyDataSetChanged();
mArtistsAdapter.notifyDataSetChanged();
mAlbumsAdapter.notifyDataSetChanged();
mGenresAdapter.notifyDataSetChanged();
}
}
package org.videolan.vlc.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class AudioPlaylistAdapter extends ArrayAdapter<String> {
private ArrayList<String> mTitles;
private HashMap<String, ArrayList<Media>> mPlaylists;
public AudioPlaylistAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mTitles = new ArrayList<String>();
mPlaylists = new HashMap<String, ArrayList<Media>>();
}
public void add(String title, Media media) {
ArrayList<Media> list;
if (!mTitles.contains(title)) {
list = new ArrayList<Media>();
mPlaylists.put(title, list);
mTitles.add(title);
super.add(title);
} else {
list = mPlaylists.get(title);
}
list.add(media);
}
@Override
public void clear() {
for (String item : mTitles) {
mPlaylists.get(item).clear();
mPlaylists.remove(item);
}
mTitles.clear();
super.clear();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.audio_browser_item, parent, false);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.title);
holder.artist = (TextView) v.findViewById(R.id.artist);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
String name = mTitles.get(position);
ArrayList<Media> list = mPlaylists.get(name);
holder.title.setText(name);
holder.artist.setText(R.string.songs);
holder.artist.setText(list.size() + " " + holder.artist.getText());
return v;
}
static class ViewHolder {
TextView title;
TextView artist;
}
public List<String> getPlaylist(int position) {
List<String> playlist = new ArrayList<String>();
if (position >= 0 && position < mTitles.size()) {
List<Media> mediaList = mPlaylists.get(mTitles.get(position));
for (int i = 0; i < mediaList.size(); i++) {
playlist.add(mediaList.get(i).getPath());
}
}
return playlist;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment