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

Sort option in audio

Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent 8f94743c
......@@ -39,6 +39,9 @@
<item
android:id="@+id/ml_menu_sortby_date"
android:title="@string/sortby_date" />
<item
android:id="@+id/ml_menu_sortby_number"
android:title="@string/sortby_number" />
</menu>
</item>
<item
......
......@@ -13,6 +13,8 @@
<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="searchable_hint">Search…</string>
<string name="history">History</string>
......
......@@ -51,7 +51,6 @@ import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
......@@ -63,6 +62,7 @@ import android.view.Window;
import android.widget.FilterQueryProvider;
import org.videolan.medialibrary.Medialibrary;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.vlc.BuildConfig;
import org.videolan.vlc.MediaParsingService;
import org.videolan.vlc.PlaybackService;
......@@ -79,13 +79,11 @@ import org.videolan.vlc.gui.browser.ExtensionBrowser;
import org.videolan.vlc.gui.browser.FileBrowserFragment;
import org.videolan.vlc.gui.browser.MediaBrowserFragment;
import org.videolan.vlc.gui.browser.NetworkBrowserFragment;
import org.videolan.vlc.gui.browser.StorageBrowserFragment;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.gui.network.MRLPanelFragment;
import org.videolan.vlc.gui.preferences.PreferencesActivity;
import org.videolan.vlc.gui.preferences.PreferencesFragment;
import org.videolan.vlc.gui.video.VideoGridFragment;
import org.videolan.vlc.gui.video.VideoListAdapter;
import org.videolan.vlc.gui.view.HackyDrawerLayout;
import org.videolan.vlc.interfaces.Filterable;
import org.videolan.vlc.interfaces.IHistory;
......@@ -93,6 +91,7 @@ import org.videolan.vlc.interfaces.IRefreshable;
import org.videolan.vlc.interfaces.ISortable;
import org.videolan.vlc.media.MediaDatabase;
import org.videolan.vlc.media.MediaUtils;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import org.videolan.vlc.util.Permissions;
import org.videolan.vlc.util.VLCInstance;
......@@ -503,7 +502,6 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
if (menu == null)
return false;
Fragment current = getCurrentFragment();
MenuItem item = menu.findItem(R.id.ml_menu_sortby);
if (item == null)
return false;
......@@ -517,26 +515,67 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
item = menu.findItem(R.id.ml_menu_sortby);
item.setEnabled(true);
item.setVisible(true);
item = menu.findItem(R.id.ml_menu_sortby_name);
if (sortable.sortDirection(VideoListAdapter.SORT_BY_TITLE) == 1)
item.setTitle(R.string.sortby_name_desc);
else
item.setTitle(R.string.sortby_name);
if (current instanceof NetworkBrowserFragment) {
menu.findItem(R.id.ml_menu_sortby_length).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(false);
} else {
item = menu.findItem(R.id.ml_menu_sortby_length);
if (sortable.sortDirection(VideoListAdapter.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 (sortable.sortDirection(VideoListAdapter.SORT_BY_DATE) == 1)
item.setTitle(R.string.sortby_date_desc);
else
item.setTitle(R.string.sortby_date);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(false);
}
else if (current instanceof FileBrowserFragment) {
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_sortby_number).setVisible(false);
}
else if (current instanceof VideoGridFragment) {
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_sortby_number).setVisible(false);
}
else if (current instanceof AudioBrowserFragment) {
int type = ((AudioBrowserFragment) current).getCurrentAdapter().getAdapterType();
switch (type) {
case MediaLibraryItem.TYPE_ARTIST :
menu.findItem(R.id.ml_menu_sortby_length).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(false);
break;
case MediaLibraryItem.TYPE_ALBUM:
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_sortby_number).setVisible(true);
break;
case MediaLibraryItem.TYPE_MEDIA:
menu.findItem(R.id.ml_menu_sortby_length).setVisible(true);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(false);
break;
case MediaLibraryItem.TYPE_GENRE :
menu.findItem(R.id.ml_menu_sortby_length).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(false);
break;
case MediaLibraryItem.TYPE_PLAYLIST:
menu.findItem(R.id.ml_menu_sortby_length).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_date).setVisible(false);
menu.findItem(R.id.ml_menu_sortby_number).setVisible(false);
break;
}
}
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_TITLE) == 1)
menu.findItem(R.id.ml_menu_sortby_name).setTitle(R.string.sortby_name_desc);
else
menu.findItem(R.id.ml_menu_sortby_name).setTitle(R.string.sortby_name);
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_LENGTH) == 1)
menu.findItem(R.id.ml_menu_sortby_length).setTitle(R.string.sortby_length_desc);
else
menu.findItem(R.id.ml_menu_sortby_length).setTitle(R.string.sortby_length);
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_DATE) == 1)
menu.findItem(R.id.ml_menu_sortby_date).setTitle(R.string.sortby_date_desc);
else
menu.findItem(R.id.ml_menu_sortby_date).setTitle(R.string.sortby_date);
if (sortable.sortDirection(MediaLibraryItemComparator.SORT_BY_NUMBER) == 1)
menu.findItem(R.id.ml_menu_sortby_number).setTitle(R.string.sortby_number_desc);
else
menu.findItem(R.id.ml_menu_sortby_number).setTitle(R.string.sortby_number);
}
if (current instanceof NetworkBrowserFragment &&
......@@ -553,6 +592,8 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
menu.findItem(R.id.ml_menu_save).setVisible(false);
if (current instanceof IHistory)
menu.findItem(R.id.ml_menu_clean).setVisible(!((IHistory) current).isEmpty());
else
menu.findItem(R.id.ml_menu_clean).setVisible(false);
boolean showLast = current instanceof AudioBrowserFragment || current instanceof VideoGridFragment;
menu.findItem(R.id.ml_menu_last_playlist).setVisible(showLast);
menu.findItem(R.id.ml_menu_filter).setVisible(current instanceof Filterable && ((Filterable)current).enableSearchOption());
......@@ -572,19 +613,28 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
// Handle item selection
switch (item.getItemId()) {
case R.id.ml_menu_sortby_name:
if (current == null)
break;
((ISortable) current).sortBy(MediaLibraryItemComparator.SORT_BY_TITLE);
supportInvalidateOptionsMenu();
break;
case R.id.ml_menu_sortby_length:
if (current == null)
break;
((ISortable) current).sortBy(MediaLibraryItemComparator.SORT_BY_LENGTH);
supportInvalidateOptionsMenu();
break;
case R.id.ml_menu_sortby_date:
if (current == null)
break;
if (current instanceof ISortable) {
int sortBy = VideoListAdapter.SORT_BY_TITLE;
if (item.getItemId() == R.id.ml_menu_sortby_length)
sortBy = VideoListAdapter.SORT_BY_LENGTH;
else if(item.getItemId() == R.id.ml_menu_sortby_date)
sortBy = VideoListAdapter.SORT_BY_DATE;
((ISortable) current).sortBy(sortBy);
supportInvalidateOptionsMenu();
}
((ISortable) current).sortBy(MediaLibraryItemComparator.SORT_BY_DATE);
supportInvalidateOptionsMenu();
break;
case R.id.ml_menu_sortby_number:
if (current == null)
break;
((ISortable) current).sortBy(MediaLibraryItemComparator.SORT_BY_NUMBER);
supportInvalidateOptionsMenu();
break;
case R.id.ml_menu_equalizer:
new EqualizerFragment().show(getSupportFragmentManager(), "equalizer");
......
......@@ -51,9 +51,11 @@ import org.videolan.vlc.gui.view.FastScroller;
import org.videolan.vlc.interfaces.IEventsHandler;
import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.MediaItemFilter;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import org.videolan.vlc.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
......@@ -72,6 +74,8 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
private int mType;
private BitmapDrawable mDefaultCover;
public static MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(MediaLibraryItemComparator.ADAPTER_AUDIO);
public AudioBrowserAdapter(Activity context, int type, IEventsHandler eventsHandler, boolean sections) {
mContext = context;
mIEventsHandler = eventsHandler;
......@@ -80,6 +84,10 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
mDefaultCover = getIconDrawable();
}
public int getAdapterType() {
return mType;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
......@@ -199,7 +207,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
public void addAll(ArrayList<? extends MediaLibraryItem> items, boolean generateSections) {
if (mContext == null)
return;
mDataList = generateSections ? generateList(items) : items;
mDataList = new ArrayList<>(items);
for (MediaLibraryItem item : mDataList) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
......@@ -219,31 +227,76 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
}
}
public ArrayList<MediaLibraryItem> removeSections(ArrayList<? extends MediaLibraryItem> items) {
ArrayList<MediaLibraryItem> newList = new ArrayList<>();
for (MediaLibraryItem item : items)
if (item.getItemType() != MediaLibraryItem.TYPE_DUMMY)
newList.add(item);
return newList;
}
private ArrayList<? extends MediaLibraryItem> generateList(ArrayList<? extends MediaLibraryItem> items) {
private ArrayList<? extends MediaLibraryItem> generateList(ArrayList<? extends MediaLibraryItem> items, int sortby) {
ArrayList<MediaLibraryItem> datalist = new ArrayList<>();
boolean isLetter, emptyTitle;
String firstLetter = null, currentLetter = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
String title = item.getTitle();
emptyTitle = title.isEmpty();
isLetter = !emptyTitle && Character.isLetter(title.charAt(0));
if (isLetter)
firstLetter = title.substring(0, 1).toUpperCase();
if (currentLetter == null) {
currentLetter = isLetter ? firstLetter : "#";
DummyItem sep = new DummyItem(currentLetter);
datalist.add(sep);
}
//Add a new separator
if (isLetter && !TextUtils.equals(currentLetter, firstLetter)) {
currentLetter = firstLetter;
DummyItem sep = new DummyItem(currentLetter);
datalist.add(sep);
}
datalist.add(item);
switch(sortby) {
case MediaLibraryItemComparator.SORT_BY_TITLE:
String currentLetter = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
String title = item.getTitle();
String letter = (title.isEmpty() || !Character.isLetter(title.charAt(0))) ? "#" : title.substring(0, 1).toUpperCase();
if (currentLetter == null || !TextUtils.equals(currentLetter, letter)) {
currentLetter = letter;
DummyItem sep = new DummyItem(currentLetter);
datalist.add(sep);
}
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_LENGTH :
String currentLengthCategory = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
int length = MediaLibraryItemComparator.getLength(item);
String lengthCategory = MediaLibraryItemComparator.lengthToCategory(length);
if (currentLengthCategory == null || !TextUtils.equals(currentLengthCategory, lengthCategory)) {
currentLengthCategory = lengthCategory;
DummyItem sep = new DummyItem(currentLengthCategory);
datalist.add(sep);
}
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_DATE :
String currentYear = null;
for (MediaLibraryItem item : items) {
if (item.getItemType() == MediaLibraryItem.TYPE_DUMMY)
continue;
String year = MediaLibraryItemComparator.getYear(item);
if (currentYear == null || !TextUtils.equals(currentYear, year)) {
currentYear = year;
DummyItem sep = new DummyItem(currentYear);
datalist.add(sep);
}
datalist.add(item);
}
break;
case MediaLibraryItemComparator.SORT_BY_NUMBER :
String currentNumber = null;
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)) {
currentNumber = number;
DummyItem sep = new DummyItem(currentNumber);
datalist.add(sep);
}
datalist.add(item);
}
break;
}
return datalist;
}
......@@ -272,7 +325,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
public void restoreList() {
if (mOriginalDataSet != null) {
update(new ArrayList<>(mOriginalDataSet));
update(new ArrayList<>(mOriginalDataSet), false);
mOriginalDataSet = null;
}
}
......@@ -281,12 +334,12 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
final ArrayList<? extends MediaLibraryItem> newList = (mOriginalDataSet == null && hasSections()) ? generateList(items) : items;
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataList, newList), detectMoves);
final ArrayList<? extends MediaLibraryItem> newListWithSections = prepareNewList(items, detectMoves);
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataList, newListWithSections), detectMoves);
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
addAll(newList, false);
addAll(newListWithSections, false);
result.dispatchUpdatesTo(AudioBrowserAdapter.this);
mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
processQueue();
......@@ -296,6 +349,19 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
});
}
private ArrayList<? extends MediaLibraryItem> prepareNewList(final ArrayList<? extends MediaLibraryItem> items, final boolean detectMoves) {
ArrayList<? extends MediaLibraryItem> newListWithSections;
if (detectMoves || mDataList.isEmpty()) {
ArrayList<? extends MediaLibraryItem> newList = removeSections(items);
Collections.sort(newList, sMediaComparator);
int realSortby = sMediaComparator.getRealSort(mType);
newListWithSections = generateList(newList, realSortby);
}
else
newListWithSections = new ArrayList<>(items);
return newListWithSections;
}
@MainThread
public List<MediaLibraryItem> getSelection() {
List<MediaLibraryItem> selection = new LinkedList<>();
......@@ -426,4 +492,38 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<ArrayList<? extends M
}
}
int sortDirection(int sortby) {
return sMediaComparator.sortDirection(sortby);
}
int getSortDirection() {
return sMediaComparator.sortDirection;
}
int getSortBy() {
return sMediaComparator.sortBy;
}
void sortBy(int sortby, int direction) {
boolean sort;
switch (sortby){
case MediaLibraryItemComparator.SORT_BY_LENGTH:
sort = !((mType == MediaLibraryItem.TYPE_ARTIST) || (mType == MediaLibraryItem.TYPE_GENRE) || (mType == MediaLibraryItem.TYPE_PLAYLIST));
break;
case MediaLibraryItemComparator.SORT_BY_DATE:
sort = !((mType == MediaLibraryItem.TYPE_ARTIST) || (mType == MediaLibraryItem.TYPE_GENRE) || (mType == MediaLibraryItem.TYPE_PLAYLIST));
break;
case MediaLibraryItemComparator.SORT_BY_NUMBER:
sort = !(mType == MediaLibraryItem.TYPE_MEDIA);
break;
default:
sort = true;
break;
}
if (sort) {
sMediaComparator.sortBy(sortby, direction);
update(mDataList, true);
}
}
}
......@@ -62,6 +62,7 @@ import org.videolan.vlc.gui.view.ContextMenuRecyclerView;
import org.videolan.vlc.gui.view.FastScroller;
import org.videolan.vlc.gui.view.SwipeRefreshLayout;
import org.videolan.vlc.interfaces.Filterable;
import org.videolan.vlc.interfaces.ISortable;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.FileUtils;
import org.videolan.vlc.util.Util;
......@@ -72,7 +73,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefreshLayout.OnRefreshListener, MediaBrowser.EventListener, ViewPager.OnPageChangeListener, Medialibrary.ArtistsAddedCb, Medialibrary.ArtistsModifiedCb, Medialibrary.AlbumsAddedCb, Medialibrary.AlbumsModifiedCb, MediaAddedCb, MediaUpdatedCb, TabLayout.OnTabSelectedListener, Filterable {
public class AudioBrowserFragment extends BaseAudioBrowser implements ISortable, SwipeRefreshLayout.OnRefreshListener, MediaBrowser.EventListener, ViewPager.OnPageChangeListener, Medialibrary.ArtistsAddedCb, Medialibrary.ArtistsModifiedCb, Medialibrary.AlbumsAddedCb, Medialibrary.AlbumsModifiedCb, MediaAddedCb, MediaUpdatedCb, TabLayout.OnTabSelectedListener, Filterable {
public final static String TAG = "VLC/AudioBrowserFragment";
private MainActivity mMainActivity;
......@@ -486,6 +487,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
@Override
public void onTabSelected(TabLayout.Tab tab) {
getActivity().supportInvalidateOptionsMenu();
mFastScroller.setRecyclerView(mLists[tab.getPosition()]);
}
......@@ -660,7 +662,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
});
}
protected AudioBrowserAdapter getCurrentAdapter() {
public AudioBrowserAdapter getCurrentAdapter() {
return (AudioBrowserAdapter) (getCurrentRV()).getAdapter();
}
......@@ -733,7 +735,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mArtistsAdapter.update(artists);
mArtistsAdapter.update(artists, true);
}
});
}
......@@ -748,7 +750,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mAlbumsAdapter.update(albums);
mAlbumsAdapter.update(albums, true);
}
});
}
......@@ -763,7 +765,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mSongsAdapter.update(media);
mSongsAdapter.update(media, true);
}
});
}
......@@ -778,7 +780,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mGenresAdapter.update(genres);
mGenresAdapter.update(genres, true);
}
});
}
......@@ -793,7 +795,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mPlaylistAdapter.update(playlists);
mPlaylistAdapter.update(playlists, true);
}
});
}
......@@ -837,4 +839,21 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
protected void onParsingServiceFinished() {
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);
}
}
package org.videolan.vlc.util;
import org.videolan.medialibrary.media.Album;
import org.videolan.medialibrary.media.MediaLibraryItem;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.medialibrary.media.Playlist;
import java.util.Comparator;
import java.util.Locale;
public class MediaLibraryItemComparator implements Comparator<MediaLibraryItem> {
public volatile int sortDirection;
public volatile int sortBy;
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 ADAPTER_VIDEO = 0;
public final static int ADAPTER_AUDIO = 1;
public final static int ADAPTER_FILE = 2;
private int adapterType;
public MediaLibraryItemComparator(int adapterType) {
sortBy = SORT_BY_TITLE;
sortDirection = 1;
this.adapterType = adapterType;
}
public int sortDirection(int sortby) {
if (sortBy == sortby)
return sortDirection;
else
return -1;
}
public void sortBy(int sortby, int sortdirection) {
sortBy = sortby;
sortDirection = sortdirection;
}
@Override
public int compare(MediaLibraryItem item1, MediaLibraryItem item2) {
if (item1 == null)
return item2 == null ? 0 : -1;
else if (item2 == null)
return 1;
if (item1.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
int type1 = ((MediaWrapper)item1).getType();
int type2 = ((MediaWrapper)item2).getType();
if (type1 == MediaWrapper.TYPE_DIR && type2 != MediaWrapper.TYPE_DIR)
return -1;
else if (type1 != MediaWrapper.TYPE_DIR && type2 == MediaWrapper.TYPE_DIR)
return 1;
}
int compare = 0;
switch (sortBy) {
case SORT_BY_TITLE:
compare = item1.getTitle().toUpperCase(Locale.ENGLISH).compareTo(item2.getTitle().toUpperCase(Locale.ENGLISH));
break;
case SORT_BY_LENGTH:
if (item1.getItemType() == MediaLibraryItem.TYPE_ALBUM) {
compare = ((Album)item1).getDuration() >= (((Album)item2).getDuration()) ? 1 : -1;
} else if (item1.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
compare = ((Long) ((MediaWrapper)item1).getLength()).compareTo(((MediaWrapper)item2).getLength());
}
break;
case SORT_BY_DATE:
if (item1.getItemType() == MediaLibraryItem.TYPE_ALBUM) {
compare = ((Album)item1).getReleaseYear() >= ((Album)item2).getReleaseYear() ? 1 : -1;
} else if (item1.getItemType() == MediaLibraryItem.TYPE_MEDIA) {
if (adapterType == ADAPTER_AUDIO) {
int date1 = (((MediaWrapper) item1).getDate() == null) ? 0 : Integer.valueOf(((MediaWrapper) item1).getDate());
int date2 = (((MediaWrapper) item2).getDate() == null) ? 0 : Integer.valueOf(((MediaWrapper) item2).getDate());
compare = date1 >= date2 ? 1 : -1;
} else if (adapterType == ADAPTER_FILE || adapterType == ADAPTER_VIDEO)
compare = ((Long) ((MediaWrapper)item1).getLastModified()).compareTo(((MediaWrapper)item2).getLastModified());
}
break;
case SORT_BY_NUMBER:
if (item1.getItemType() == MediaLibraryItem.TYPE_ALBUM) {
compare = ((Album)item1).getTracksCount() >= ((Album)item2).getTracksCount() ? 1 : -1;