Commit 31e0cb20 authored by David Papazian's avatar David Papazian Committed by Geoffrey Métais

Add Album & Artist sort

Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent f78ef697
......@@ -27,6 +27,16 @@
android:orderInCategory="2"
android:id="@+id/ml_menu_sortby_name"
android:title="@string/sortby_name" />
<item
android:orderInCategory="2"
android:id="@+id/ml_menu_sortby_artist_name"
android:title="@string/sortby_artist_name"
android:visible="false" />
<item
android:orderInCategory="2"
android:id="@+id/ml_menu_sortby_album_name"
android:title="@string/sortby_album_name"
android:visible="false" />
<item
android:orderInCategory="2"
android:id="@+id/ml_menu_sortby_length"
......
......@@ -9,12 +9,16 @@
<string name="sortby">Sort by&#8230;</string>
<string name="sortby_name">Name</string>
<string name="sortby_name_desc">Name (desc)</string>
<string name="sortby_artist_name">Artist name</string>
<string name="sortby_artist_name_desc">Artist name (desc)</string>
<string name="sortby_album_name">Album name</string>
<string name="sortby_album_name_desc">Album name (desc)</string>
<string name="sortby_length">Length</string>
<string name="sortby_length_desc">Length (desc)</string>
<string name="sortby_date">Date</string>
<string name="sortby_date_desc">Date (desc)</string>
<string name="sortby_number">Tracks</string>
<string name="sortby_number_desc">Tracks (desc)</string>
<string name="sortby_number">Nb tracks</string>
<string name="sortby_number_desc">Nb tracks (desc)</string>
<string name="searchable_hint">Search…</string>
<string name="history">History</string>
......@@ -51,6 +55,7 @@
<string name="unknown_artist">Unknown Artist</string>
<string name="unknown_album">Unknown Album</string>
<string name="unknown_genre">Unknown Genre</string>
<string name="unknown_number">Unknown number of Tracks</string>
<string name="songs">Songs</string>
<plurals name="songs_quantity">
<item quantity="one">1 song</item>
......
......@@ -121,7 +121,7 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
finish();
return;
}
Permissions.checkReadStoragePermission(this, false);
/*** Start initializing the UI ***/
......
......@@ -30,7 +30,6 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
......@@ -42,7 +41,6 @@ import android.widget.Filter;
import org.videolan.medialibrary.Medialibrary;
import org.videolan.medialibrary.media.Album;
import org.videolan.medialibrary.media.Artist;
import org.videolan.medialibrary.media.DummyItem;
import org.videolan.medialibrary.media.Genre;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
......@@ -62,10 +60,6 @@ import org.videolan.vlc.util.FileUtils;
import org.videolan.vlc.util.Util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import static org.videolan.vlc.R.string.albums;
public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeRefreshLayout.OnRefreshListener, TabLayout.OnTabSelectedListener {
......@@ -95,6 +89,8 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
AudioBrowserAdapter.sMediaComparator.setSortDefault();
mMediaLibrary = VLCApplication.getMLInstance();
mItem = (MediaLibraryItem) (savedInstanceState != null ?
......@@ -124,9 +120,11 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
ContextMenuRecyclerView songsList = (ContextMenuRecyclerView) mViewPager.getChildAt(MODE_SONG);
mLists = new ContextMenuRecyclerView[]{albumsList, songsList};
String[] titles = new String[] {getString(albums), getString(R.string.songs)};
mAlbumsAdapter = new AudioBrowserAdapter(getActivity(), MediaLibraryItem.TYPE_ALBUM, this, false);
mSongsAdapter = new AudioBrowserAdapter(getActivity(), MediaLibraryItem.TYPE_MEDIA, this, false);
String[] titles = new String[] {getString(R.string.albums), getString(R.string.songs)};
mAlbumsAdapter = new AudioBrowserAdapter(getActivity(), MediaLibraryItem.TYPE_ALBUM, this, true);
mSongsAdapter = new AudioBrowserAdapter(getActivity(), MediaLibraryItem.TYPE_MEDIA, this, true);
mAlbumsAdapter.setParentAdapterType(mItem.getItemType());
mSongsAdapter.setParentAdapterType(mItem.getItemType());
mAdapters = new AudioBrowserAdapter[]{mAlbumsAdapter, mSongsAdapter};
songsList.setAdapter(mSongsAdapter);
......@@ -297,20 +295,16 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
albums = Util.arrayToMediaArrayList(((Genre) mItem).getAlbums());
else
return;
final LinkedList<MediaLibraryItem> songs = new LinkedList<>();
final ArrayList<MediaLibraryItem> songs = new ArrayList<>();
for (MediaLibraryItem album : albums) {
String title = album.getTitle();
if (TextUtils.isEmpty(title))
title = getString(R.string.unknown_album);
songs.add(new DummyItem(title));
songs.addAll(Arrays.asList(album.getTracks()));
songs.addAll(Util.arrayToArrayList(album.getTracks()));
}
mHandler.post(new Runnable() {
@Override
public void run() {
mAlbumsAdapter.update(albums);
mSongsAdapter.update(new ArrayList<>(songs));
mFastScroller.setRecyclerView(mLists[mViewPager.getCurrentItem()]);
mFastScroller.setRecyclerView(getCurrentRV());
mSwipeRefreshLayout.setRefreshing(false);
}
});
......
......@@ -39,6 +39,7 @@ import android.widget.Filterable;
import org.videolan.medialibrary.media.DummyItem;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.BR;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
......@@ -71,6 +72,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
private IEventsHandler mIEventsHandler;
private int mSelectionCount = 0;
private int mType;
private int mParentType = 0;
private BitmapDrawable mDefaultCover;
public static MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(MediaLibraryItemComparator.ADAPTER_AUDIO);
......@@ -87,6 +89,15 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
return mType;
}
public void setParentAdapterType(int type) {
mParentType = type;
}
public int getParentAdapterType() {
return mParentType;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
......@@ -221,8 +232,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
if (TextUtils.isEmpty(item.getDescription()))
item.setDescription(mContext.getString(R.string.unknown_artist));
}
} else if (generateSections)
break;
}
}
}
......@@ -234,7 +244,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
return newList;
}
private ArrayList<MediaLibraryItem> generateList(ArrayList<MediaLibraryItem> items, int sortby) {
private ArrayList<MediaLibraryItem> generateSections(ArrayList<? extends MediaLibraryItem> items, int sortby) {
ArrayList<MediaLibraryItem> datalist = new ArrayList<>();
switch(sortby) {
case MediaLibraryItemComparator.SORT_BY_TITLE:
......@@ -252,6 +262,42 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_ARTIST:
String currentArtist = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
String artist = ((MediaWrapper)item).getArtist();
if (artist == null)
artist = "";
if (currentArtist == null || !TextUtils.equals(currentArtist, artist)) {
currentArtist = artist;
DummyItem sep = new DummyItem(TextUtils.isEmpty(currentArtist)
? mContext.getResources().getString(R.string.unknown_artist)
: currentArtist);
datalist.add(sep);
}
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_ALBUM:
String currentAlbum = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
String album = ((MediaWrapper)item).getAlbum();
if (album == null)
album = "";
if (currentAlbum == null || !TextUtils.equals(currentAlbum, album)) {
currentAlbum = album;
DummyItem sep = new DummyItem(TextUtils.isEmpty(currentAlbum)
? mContext.getResources().getString(R.string.unknown_album)
: currentAlbum);
datalist.add(sep);
}
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_LENGTH :
String currentLengthCategory = null;
for (MediaLibraryItem item : items) {
......@@ -282,15 +328,16 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
}
break;
case MediaLibraryItemComparator.SORT_BY_NUMBER :
String currentNumber = null;
int currentNumber = 0;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
int nb = MediaLibraryItemComparator.getTracksCount(item);
String number = (nb == 0) ? "Unknown" : String.valueOf(nb);
if (currentNumber == null || !TextUtils.equals(currentNumber, number)) {
int number = MediaLibraryItemComparator.getTracksCount(item);
if (currentNumber != number) {
currentNumber = number;
DummyItem sep = new DummyItem(currentNumber);
DummyItem sep = new DummyItem(currentNumber == 0
? mContext.getResources().getString(R.string.unknown_number)
: mContext.getResources().getQuantityString(R.plurals.songs_quantity, currentNumber, currentNumber));
datalist.add(sep);
}
datalist.add(item);
......@@ -328,12 +375,12 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
mUpdateExecutor.execute(new Runnable() {
@Override
public void run() {
final ArrayList<MediaLibraryItem> newListWithSections = prepareNewList(items);
final ArrayList<MediaLibraryItem> newListWithSections = prepareNewList(items, mMakeSections);
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataset, newListWithSections), detectMoves);
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
addAll(newListWithSections, false);
addAll(newListWithSections, true);
result.dispatchUpdatesTo(AudioBrowserAdapter.this);
processQueue();
}
......@@ -347,12 +394,20 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
}
private ArrayList<MediaLibraryItem> prepareNewList(final ArrayList<MediaLibraryItem> items) {
private ArrayList<MediaLibraryItem> prepareNewList(final ArrayList<MediaLibraryItem> items, boolean sections) {
ArrayList<MediaLibraryItem> newListWithSections;
ArrayList<MediaLibraryItem> newList = removeSections(items);
Collections.sort(newList, sMediaComparator);
int realSortby = sMediaComparator.getRealSort(mType);
newListWithSections = generateList(newList, realSortby);
if (sections) {
if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
newListWithSections = generateSections(items, MediaLibraryItemComparator.getDefaultSort(mType, mParentType));
} else {
ArrayList<MediaLibraryItem> newList = removeSections(items);
Collections.sort(newList, sMediaComparator);
newListWithSections = generateSections(newList, sMediaComparator.getRealSort(mType, mParentType));
}
} else {
Collections.sort(items, sMediaComparator);
newListWithSections = new ArrayList<>(items);
}
return newListWithSections;
}
......@@ -495,26 +550,24 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem, Aud
return sMediaComparator.sortDirection;
}
int getDefaultSort(){
return MediaLibraryItemComparator.getDefaultSort(mType, mParentType);
}
int getDefaultDirection() {
return MediaLibraryItemComparator.getDefaultDirection(mType, mParentType);
}
boolean isSortAllowed(int sortby) {
return MediaLibraryItemComparator.isSortAllowed(mType, mParentType, sortby);
}
int getSortBy() {
return sMediaComparator.sortBy;
}
void sortBy(int sortby, int direction) {
boolean sort;
switch (sortby){
case MediaLibraryItemComparator.SORT_BY_LENGTH:
sort = (mType == MediaLibraryItem.TYPE_ALBUM) || (mType == MediaLibraryItem.TYPE_MEDIA);
break;
case MediaLibraryItemComparator.SORT_BY_DATE:
sort = (mType == MediaLibraryItem.TYPE_ALBUM);
break;
case MediaLibraryItemComparator.SORT_BY_NUMBER:
sort = (mType == MediaLibraryItem.TYPE_ALBUM) || (mType == MediaLibraryItem.TYPE_PLAYLIST);
break;
default:
sort = true;
break;
}
boolean sort = isSortAllowed(sortby);
if (sort) {
sMediaComparator.sortBy(sortby, direction);
update(mDataset, true);
......
......@@ -829,18 +829,6 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
mHandler.sendEmptyMessage(UPDATE_LIST);
}
@Override
public void sortBy(int sortby) {
int sortDirection = mAlbumsAdapter.getSortDirection();
int sortBy = mAlbumsAdapter.getSortBy();
if (sortby == sortBy)
sortDirection*=-1;
else
sortDirection = 1;
for (AudioBrowserAdapter adapter : mAdapters)
adapter.sortBy(sortby, sortDirection);
}
@Override
public int sortDirection(int sortby) {
return getCurrentAdapter().sortDirection(sortby);
......
......@@ -41,6 +41,7 @@ import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.interfaces.Filterable;
import org.videolan.vlc.interfaces.IEventsHandler;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -48,7 +49,7 @@ import java.util.List;
public abstract class BaseAudioBrowser extends SortableFragment implements IEventsHandler, Filterable {
abstract protected AudioBrowserAdapter getCurrentAdapter();
abstract public AudioBrowserAdapter getCurrentAdapter();
public View mSearchButtonView;
public ContentActivity mActivity;
protected AudioBrowserAdapter[] mAdapters;
......@@ -62,14 +63,14 @@ public abstract class BaseAudioBrowser extends SortableFragment implements IEven
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
int type = getCurrentAdapter().getAdapterType();
boolean album = type == MediaLibraryItem.TYPE_ALBUM;
boolean songs = type == MediaLibraryItem.TYPE_MEDIA;
boolean playlist = type == MediaLibraryItem.TYPE_PLAYLIST;
menu.findItem(R.id.ml_menu_sortby_length).setVisible(album||playlist||songs);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(album);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(album||playlist);
AudioBrowserAdapter adapter = getCurrentAdapter();
menu.findItem(R.id.ml_menu_last_playlist).setVisible(true);
menu.findItem(R.id.ml_menu_sortby_name).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_TITLE));
menu.findItem(R.id.ml_menu_sortby_artist_name).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_ARTIST));
menu.findItem(R.id.ml_menu_sortby_album_name).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_ALBUM));
menu.findItem(R.id.ml_menu_sortby_length).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_LENGTH));
menu.findItem(R.id.ml_menu_sortby_date).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_DATE));
menu.findItem(R.id.ml_menu_sortby_number).setVisible(adapter.isSortAllowed(MediaLibraryItemComparator.SORT_BY_NUMBER));
}
@Override
......@@ -208,15 +209,19 @@ public abstract class BaseAudioBrowser extends SortableFragment implements IEven
}
@Override
public void sortBy(int sortby) {
public void sortBy(int newSortby) {
AudioBrowserAdapter adapter = mAdapters[0];
int sortDirection = adapter.getSortDirection();
int sortBy = adapter.getSortBy();
if (sortby == sortBy)
int oldSortby = adapter.getSortBy();
int delfaultSortby = getCurrentAdapter().getDefaultSort();
int defaultDirection = getCurrentAdapter().getDefaultDirection();
if (newSortby == oldSortby)
sortDirection*=-1;
else if (newSortby == delfaultSortby)
sortDirection = defaultDirection*-1;
else
sortDirection = 1;
for (AudioBrowserAdapter audioBrowserAdapter : mAdapters)
audioBrowserAdapter.sortBy(sortby, sortDirection);
audioBrowserAdapter.sortBy(newSortby, sortDirection);
}
}
......@@ -29,8 +29,8 @@ import android.view.Menu;
import android.view.MenuItem;
import org.videolan.vlc.R;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import org.videolan.vlc.util.Util;
public abstract class SortableFragment extends MediaBrowserFragment {
......@@ -39,7 +39,7 @@ public abstract class SortableFragment extends MediaBrowserFragment {
@Override
public void onPrepareOptionsMenu(Menu menu) {
Util.updateSortTitles(this, menu);
UiTools.updateSortTitles(this, menu);
super.onPrepareOptionsMenu(menu);
}
......@@ -56,6 +56,14 @@ public abstract class SortableFragment extends MediaBrowserFragment {
sortBy(MediaLibraryItemComparator.SORT_BY_TITLE);
onPrepareOptionsMenu(mMenu);
return true;
case R.id.ml_menu_sortby_artist_name:
sortBy(MediaLibraryItemComparator.SORT_BY_ARTIST);
onPrepareOptionsMenu(mMenu);
return true;
case R.id.ml_menu_sortby_album_name:
sortBy(MediaLibraryItemComparator.SORT_BY_ALBUM);
onPrepareOptionsMenu(mMenu);
return true;
case R.id.ml_menu_sortby_length:
sortBy(MediaLibraryItemComparator.SORT_BY_LENGTH);
onPrepareOptionsMenu(mMenu);
......
......@@ -50,6 +50,8 @@ import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
......@@ -65,7 +67,10 @@ import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.BuildConfig;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.audio.BaseAudioBrowser;
import org.videolan.vlc.gui.browser.SortableFragment;
import org.videolan.vlc.gui.dialogs.SavePlaylistDialog;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -325,4 +330,69 @@ public class UiTools {
return outBitmap;
}
public static void updateSortTitles(SortableFragment sortable, Menu menu) {
if (sortable instanceof BaseAudioBrowser && sortable.sortDirection(MediaLibraryItemComparator.SORT_DEFAULT) == 1) {
int type = ((BaseAudioBrowser)sortable).getCurrentAdapter().getAdapterType();
int parentType = ((BaseAudioBrowser)sortable).getCurrentAdapter().getParentAdapterType();
int defaultSortby = MediaLibraryItemComparator.getDefaultSort(type, parentType);
int defaultDirection = MediaLibraryItemComparator.getDefaultDirection(type, parentType);
menu.findItem(R.id.ml_menu_sortby_length).setTitle(R.string.sortby_length);
menu.findItem(R.id.ml_menu_sortby_number).setTitle(R.string.sortby_number);
menu.findItem(R.id.ml_menu_sortby_artist_name).setTitle(R.string.sortby_artist_name);
menu.findItem(R.id.ml_menu_sortby_name).setTitle(defaultSortby == MediaLibraryItemComparator.SORT_BY_TITLE && defaultDirection == 1
? R.string.sortby_name_desc
: R.string.sortby_name);
menu.findItem(R.id.ml_menu_sortby_date).setTitle(defaultSortby == MediaLibraryItemComparator.SORT_BY_DATE && defaultDirection == 1
? R.string.sortby_date_desc
: R.string.sortby_date);
menu.findItem(R.id.ml_menu_sortby_album_name).setTitle(defaultSortby == MediaLibraryItemComparator.SORT_BY_ALBUM && defaultDirection == 1
? R.string.sortby_album_name_desc
: R.string.sortby_album_name);
return;
}
MenuItem item = menu.findItem(R.id.ml_menu_sortby_name);
if (item != null) {
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_TITLE) == 1)
item.setTitle(R.string.sortby_name_desc);
else
item.setTitle(R.string.sortby_name);
}
item = menu.findItem(R.id.ml_menu_sortby_artist_name);
if (item != null) {
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_ARTIST) == 1)
item.setTitle(R.string.sortby_artist_name_desc);
else
item.setTitle(R.string.sortby_artist_name);
}
item = menu.findItem(R.id.ml_menu_sortby_album_name);
if (item != null) {
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_ALBUM) == 1)
item.setTitle(R.string.sortby_album_name_desc);
else
item.setTitle(R.string.sortby_album_name);
}
item = menu.findItem(R.id.ml_menu_sortby_length);
if (item != null) {
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_LENGTH) == 1)
item.setTitle(R.string.sortby_length_desc);
else
item.setTitle(R.string.sortby_length);
}
item = menu.findItem(R.id.ml_menu_sortby_date);
if (item != null) {
if(sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_DATE) == 1)
item.setTitle(R.string.sortby_date_desc);
else
item.setTitle(R.string.sortby_date);
}
item = menu.findItem(R.id.ml_menu_sortby_number);
if (item != null) {
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_NUMBER) == 1)
item.setTitle(R.string.sortby_number_desc);
else
item.setTitle(R.string.sortby_number);
}
}
}
......@@ -108,6 +108,8 @@ public class VideoGridFragment extends SortableFragment implements MediaUpdatedC
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.ml_menu_sortby_artist_name).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_album_name).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_length).setVisible(true);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(true);
menu.findItem(R.id.ml_menu_last_playlist).setVisible(true);
......
package org.videolan.vlc.util;
import android.text.TextUtils;
import org.videolan.medialibrary.media.Album;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
......@@ -12,10 +14,14 @@ public class MediaLibraryItemComparator implements Comparator<MediaLibraryItem>
public volatile int sortDirection;
public volatile int sortBy;
public final static int SORT_DEFAULT = -1;
public final static int SORT_BY_TITLE = 0;
public final static int SORT_BY_LENGTH = 1;
public final static int SORT_BY_DATE = 2;
public final static int SORT_BY_NUMBER = 3;
public final static int SORT_BY_ARTIST = 1;
public final static int SORT_BY_ALBUM = 2;
public final static int SORT_BY_LENGTH = 3;
public final static int SORT_BY_DATE = 4;
public final static int SORT_BY_NUMBER = 5;
public final static int ADAPTER_VIDEO = 0;
public final static int ADAPTER_AUDIO = 1;
public final static int ADAPTER_FILE = 2;
......@@ -39,6 +45,11 @@ public class MediaLibraryItemComparator implements Comparator<MediaLibraryItem>
sortDirection = sortdirection;
}
public void setSortDefault() {
sortBy = SORT_DEFAULT;
sortDirection = 1;
}
@Override
public int compare(MediaLibraryItem item1, MediaLibraryItem item2) {
if (item1 == null)
......@@ -60,28 +71,48 @@ public class MediaLibraryItemComparator implements Comparator<MediaLibraryItem>
case SORT_BY_TITLE:
compare = item1.getTitle().toUpperCase(Locale.ENGLISH).compareTo(item2.getTitle().toUpperCase(Locale.ENGLISH));
break;
case SORT_BY_ARTIST:
if (item1.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
String artist1 = ((MediaWrapper)item1).getArtist();
String artist2 = ((MediaWrapper)item2).getArtist();
if (TextUtils.equals(artist1,artist2))
compare = item1.getTitle().toUpperCase(Locale.ENGLISH).compareTo(item2.getTitle().toUpperCase(Locale.ENGLISH));
else
compare = artist1.toUpperCase(Locale.ENGLISH).compareTo(artist2.toUpperCase(Locale.ENGLISH));
}
break;
case SORT_BY_ALBUM:
if (item1.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
String album1 = ((MediaWrapper)item1).getAlbum();
String album2 = ((MediaWrapper)item2).getAlbum();
if (album1.equals(album2))
return ((MediaWrapper) item1).getTrackNumber() - ((MediaWrapper) item2).getTrackNumber();
else
compare = album1.toUpperCase(Locale.ENGLISH).compareTo(album2.toUpperCase(Locale.ENGLISH));
}
break;
case SORT_BY_LENGTH:
if (item1.getItemType() == MediaLibraryItem.TYPE_ALBUM) {
compare = ((Album)item1).getDuration() >= (((Album