Better sort interface

This commit is contained in:
Christopher Eby committed 2011-10-30 02:54:48 -05:00
1 parent 4d43970c27
commit bd8f4bfd12
6 files changed
+151 -33

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2011 Christopher Eby <kreed@kreed.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sort_direction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
style="@style/AlertDialogItem"
android:id="@+id/ascending"
android:text="@string/ascending"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1" />
<RadioButton
style="@style/AlertDialogItem"
android:id="@+id/descending"
android:text="@string/descending"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1" />
</RadioGroup>
+3
View File
@@ -23,4 +23,7 @@ THE SOFTWARE.
<resources>
<style name="Dialog" parent="android:Theme.Holo.Dialog" />
<style name="Theme" parent="android:Theme.Holo" />
<style name="AlertDialogItem">
<item name="android:textColor">?android:textColorAlertDialogListItem</item>
</style>
</resources>
+3
View File
@@ -98,6 +98,9 @@ THE SOFTWARE.
<string name="artist_album_title">Artist, album, title</string>
<string name="artist_year">Artist, year</string>
<string name="ascending">Ascending</string>
<string name="descending">Descending</string>
<!-- Notification -->
<string name="notification_title_paused">%s (Paused)</string>
+3
View File
@@ -34,6 +34,9 @@ THE SOFTWARE.
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@null</item>
</style>
<style name="AlertDialogItem">
<item name="android:textColor">@android:color/primary_text_light</item>
</style>
<string-array name="entry_values">
<!-- Note: even if this was an integer-array, these would be converted
to strings anyway.
+66 -18
View File
@@ -22,7 +22,9 @@
package org.kreed.vanilla;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.SharedPreferences;
@@ -52,6 +54,7 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
@@ -65,6 +68,8 @@ public class LibraryActivity
implements AdapterView.OnItemClickListener
, TextWatcher
, TabHost.OnTabChangeListener
, DialogInterface.OnClickListener
, DialogInterface.OnDismissListener
{
private static final int ACTION_PLAY = 0;
private static final int ACTION_ENQUEUE = 1;
@@ -711,25 +716,9 @@ public class LibraryActivity
return super.onCreateOptionsMenu(menu);
}
public static final int GROUP_SORT = 100;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getGroupId() == GROUP_SORT) {
MediaAdapter adapter = mCurrentAdapter;
ListView view = (ListView)mTabHost.getCurrentView();
int i = item.getItemId();
adapter.setSortMode(i);
requestRequery(adapter);
// Force a new FastScroller to be created so the scroll section
// are updated.
view.setFastScrollEnabled(false);
view.setFastScrollEnabled(true);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SAVE_SORT, adapter));
return true;
}
switch (item.getItemId()) {
case MENU_SEARCH:
setSearchBoxVisible(!mSearchBoxVisible);
@@ -737,9 +726,38 @@ public class LibraryActivity
case MENU_PLAYBACK:
startActivity(new Intent(this, FullPlaybackActivity.class));
return true;
case MENU_SORT:
mCurrentAdapter.buildSortMenu(item.getSubMenu());
case MENU_SORT: {
MediaAdapter adapter = mCurrentAdapter;
int mode = adapter.getSortMode();
int check;
if (mode < 0) {
check = R.id.descending;
mode = ~mode;
} else {
check = R.id.ascending;
}
int[] itemIds = adapter.getSortEntries();
String[] items = new String[itemIds.length];
Resources res = getResources();
for (int i = itemIds.length; --i != -1; ) {
items[i] = res.getString(itemIds[i]);
}
RadioGroup header = (RadioGroup)getLayoutInflater().inflate(R.layout.sort_dialog, null);
header.check(check);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.sort_by);
builder.setSingleChoiceItems(items, mode + 1, this); // add 1 for header
builder.setNeutralButton(R.string.done, null);
AlertDialog dialog = builder.create();
dialog.getListView().addHeaderView(header);
dialog.setOnDismissListener(this);
dialog.show();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
@@ -977,4 +995,34 @@ public class LibraryActivity
int sort = PlaybackService.getSettings(this).getInt(key, def);
adapter.setSortMode(sort);
}
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
@Override
public void onDismiss(DialogInterface dialog)
{
ListView list = ((AlertDialog)dialog).getListView();
// subtract 1 for header
int which = list.getCheckedItemPosition() - 1;
RadioGroup group = (RadioGroup)list.findViewById(R.id.sort_direction);
if (group.getCheckedRadioButtonId() == R.id.descending)
which = ~which;
MediaAdapter adapter = mCurrentAdapter;
adapter.setSortMode(which);
requestRequery(adapter);
// Force a new FastScroller to be created so the scroll sections
// are updated.
ListView view = (ListView)mTabHost.getCurrentView();
view.setFastScrollEnabled(false);
view.setFastScrollEnabled(true);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SAVE_SORT, adapter));
}
}
+33 -15
View File
@@ -28,7 +28,6 @@ import android.database.DatabaseUtils;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
@@ -114,8 +113,20 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
* True if the data is stale and the query should be re-run.
*/
private boolean mNeedsRequery;
/**
* The human-readable descriptions for each sort mode.
*/
private int[] mSortEntries;
/**
* An array ORDER BY expressions for each sort mode. %1$s is replaced by
* ASC or DESC as appropriate before being passed to the query.
*/
private String[] mSortValues;
/**
* The index of the current of the current sort mode in mSortValues, or
* the inverse of the index (in which case sort should be descending
* instead of ascending).
*/
private int mSortMode;
/**
@@ -150,7 +161,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
mFieldKeys = new String[] { MediaStore.Audio.Artists.ARTIST_KEY };
mSongSort = MediaUtils.DEFAULT_SORT;
mSortEntries = new int[] { R.string.name, R.string.number_of_tracks };
mSortValues = new String[] { "artist_key", "number_of_tracks" };
mSortValues = new String[] { "artist_key %1$s", "number_of_tracks %1$s,artist_key %1$s" };
break;
case MediaUtils.TYPE_ALBUM:
mStore = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
@@ -159,28 +170,28 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
mFieldKeys = new String[] { "artist_key", MediaStore.Audio.Albums.ALBUM_KEY };
mSongSort = "album_key,track";
mSortEntries = new int[] { R.string.name, R.string.artist_album, R.string.year, R.string.number_of_tracks };
mSortValues = new String[] { "album_key", "artist_key,album_key", "minyear", "numsongs" };
mSortValues = new String[] { "album_key %1$s", "artist_key %1$s,album_key %1$s", "minyear %1$s,album_key %1$s", "numsongs %1$s,album_key %1$s" };
break;
case MediaUtils.TYPE_SONG:
mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
mFields = new String[] { MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE };
mFieldKeys = new String[] { MediaStore.Audio.Media.ARTIST_KEY, MediaStore.Audio.Media.ALBUM_KEY, MediaStore.Audio.Media.TITLE_KEY };
mSortEntries = new int[] { R.string.name, R.string.artist_album_track, R.string.artist_album_title, R.string.artist_year, R.string.year };
mSortValues = new String[] { "title_key", "artist_key,album_key,track", "artist_key,album_key,title_key", "artist_key,year", "year" };
mSortValues = new String[] { "title_key %1$s", "artist_key %1$s,album_key %1$s,track %1$s", "artist_key %1$s,album_key %1$s,title_key %1$s", "artist_key %1$s,year %1$s,track %1$s", "year %1$s,title_key %1$s" };
break;
case MediaUtils.TYPE_PLAYLIST:
mStore = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
mFields = new String[] { MediaStore.Audio.Playlists.NAME };
mFieldKeys = null;
mSortEntries = new int[] { R.string.name, R.string.date_added };
mSortValues = new String[] { "name", "date_added" };
mSortValues = new String[] { "name %1$s", "date_added %1$s" };
break;
case MediaUtils.TYPE_GENRE:
mStore = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
mFields = new String[] { MediaStore.Audio.Genres.NAME };
mFieldKeys = null;
mSortEntries = new int[] { R.string.name };
mSortValues = new String[] { "name" };
mSortValues = new String[] { "name %1$s" };
break;
default:
throw new IllegalArgumentException("Invalid value for type: " + type);
@@ -306,7 +317,15 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
StringBuilder selection = new StringBuilder();
String[] selectionArgs = null;
String sort = mSortValues[mSortMode];
int mode = mSortMode;
String sortDir;
if (mode < 0) {
mode = ~mode;
sortDir = "DESC";
} else {
sortDir = "ASC";
}
String sort = String.format(mSortValues[mode], sortDir);
if (mType == MediaUtils.TYPE_SONG || forceMusicCheck)
selection.append("is_music!=0");
@@ -538,23 +557,22 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
}
/**
* Build a menu of sort modes appropriate for this adapter.
* Return the available sort modes for this adapter.
*
* @param menu The menu the items will be added to.
* @return An array containing the resource ids of the sort mode strings.
*/
public void buildSortMenu(SubMenu menu)
public int[] getSortEntries()
{
int[] entries = mSortEntries;
menu.clear();
for (int i = 0; i != entries.length; ++i)
menu.add(LibraryActivity.GROUP_SORT, i, 0, entries[i]);
return mSortEntries;
}
/**
* Set the sorting mode. The adapter should be re-queried after changing
* this.
*
* @param i The index of the sort mode.
* @param i The index of the sort mode in the sort entries array. If this
* is negative, the inverse of the index will be used and sort order will
* be reversed.
*/
public void setSortMode(int i)
{