Make limiters work again
This commit is contained in:
parent
ab543e3871
commit
6bbd34952a
@ -40,24 +40,20 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
private List<Song> mObjects;
|
||||
private Song[] mAllObjects;
|
||||
private ArrayFilter mFilter;
|
||||
private int mLimiterField = -1;
|
||||
private Song mLimiterMedia;
|
||||
private CharSequence mLastFilter;
|
||||
private SongData mLimiter;
|
||||
private CharSequence mPublishedFilter;
|
||||
private int mPublishedLimiter;
|
||||
|
||||
private int mPrimaryField;
|
||||
private int mSecondaryField;
|
||||
|
||||
public MediaAdapter(Context context, Song[] allObjects, int primaryField, int secondaryField)
|
||||
public MediaAdapter(Context context, Song[] allObjects, int primaryField, int secondaryField, View.OnClickListener expanderListener)
|
||||
{
|
||||
mContext = context;
|
||||
mAllObjects = allObjects;
|
||||
mPrimaryField = primaryField;
|
||||
mSecondaryField = secondaryField;
|
||||
}
|
||||
|
||||
public void setExpanderListener(View.OnClickListener listener)
|
||||
{
|
||||
mExpanderListener = listener;
|
||||
mExpanderListener = expanderListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -116,29 +112,41 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
}
|
||||
|
||||
private class ArrayFilter extends Filter {
|
||||
class ArrayFilterResults extends FilterResults {
|
||||
public int limiterHash;
|
||||
|
||||
public ArrayFilterResults(List<Song> list, int limiterHash)
|
||||
{
|
||||
values = list;
|
||||
count = list.size();
|
||||
this.limiterHash = limiterHash;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FilterResults performFiltering(CharSequence filter)
|
||||
{
|
||||
FilterResults results = new FilterResults();
|
||||
List<Song> list;
|
||||
int limiterHash = mLimiter == null ? 0 : mLimiter.hashCode();
|
||||
|
||||
boolean noFilter = filter == null || filter.length() == 0;
|
||||
if (filter != null && filter.length() == 0)
|
||||
filter = null;
|
||||
|
||||
if (mLastFilter.equals(filter)) {
|
||||
results.values = mObjects;
|
||||
results.count = mObjects.size();
|
||||
} else if (noFilter && mLimiterField == -1) {
|
||||
results.values = Arrays.asList(mAllObjects);
|
||||
results.count = mAllObjects.length;
|
||||
if ((filter == null && mPublishedFilter == null || mPublishedFilter != null && mPublishedFilter.equals(filter)) && mPublishedLimiter == limiterHash) {
|
||||
list = mObjects;
|
||||
} else if (filter == null && mLimiter == null) {
|
||||
list = Arrays.asList(mAllObjects);
|
||||
} else {
|
||||
Matcher[] matchers = null;
|
||||
if (!noFilter) {
|
||||
if (filter != null) {
|
||||
String[] words = filter.toString().split("\\s+");
|
||||
matchers = new Matcher[words.length];
|
||||
for (int i = words.length; --i != -1; )
|
||||
matchers[i] = createMatcher(words[i]);
|
||||
}
|
||||
|
||||
int limiterId = mLimiterField == -1 ? 0 : mLimiterMedia.getFieldId(mLimiterField);
|
||||
int limiterField = mLimiter == null ? -1 : mLimiter.field;
|
||||
int limiterId = mLimiter == null ? -1 : mLimiter.media.getFieldId(limiterField);
|
||||
|
||||
int count = mAllObjects.length;
|
||||
ArrayList<Song> newValues = new ArrayList<Song>();
|
||||
@ -148,10 +156,10 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
for (int i = 0; i != count; ++i) {
|
||||
Song song = mAllObjects[i];
|
||||
|
||||
if (mLimiterField != -1 && song.getFieldId(mLimiterField) != limiterId)
|
||||
if (limiterField != -1 && song.getFieldId(limiterField) != limiterId)
|
||||
continue;
|
||||
|
||||
if (!noFilter) {
|
||||
if (filter != null) {
|
||||
for (int j = matchers.length; --j != -1; ) {
|
||||
if (matchers[j].reset(song.artist).find())
|
||||
continue;
|
||||
@ -168,19 +176,20 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
|
||||
newValues.trimToSize();
|
||||
|
||||
results.values = newValues;
|
||||
results.count = newValues.size();
|
||||
list = newValues;
|
||||
}
|
||||
|
||||
return results;
|
||||
return new ArrayFilterResults(list, limiterHash);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected void publishResults(CharSequence constraint, FilterResults results)
|
||||
protected void publishResults(CharSequence filter, FilterResults results)
|
||||
{
|
||||
mObjects = (List<Song>)results.values;
|
||||
mLastFilter = constraint;
|
||||
mPublishedFilter = filter == null || filter.length() == 0 ? null : filter;
|
||||
mPublishedLimiter = ((ArrayFilterResults)results).limiterHash;
|
||||
|
||||
if (results.count == 0)
|
||||
notifyDataSetInvalidated();
|
||||
else
|
||||
@ -194,22 +203,15 @@ public class MediaAdapter extends BaseAdapter implements Filterable {
|
||||
notifyDataSetInvalidated();
|
||||
}
|
||||
|
||||
public void setLimiter(int field, Song media)
|
||||
public void setLimiter(SongData limiter)
|
||||
{
|
||||
mLimiterField = field;
|
||||
mLimiterMedia = media;
|
||||
|
||||
getFilter().filter(mLastFilter);
|
||||
mLimiter = limiter;
|
||||
getFilter().filter(mPublishedFilter);
|
||||
}
|
||||
|
||||
public int getLimiterField()
|
||||
public SongData getLimiter()
|
||||
{
|
||||
return mLimiterField;
|
||||
}
|
||||
|
||||
public Song getLimiterMedia()
|
||||
{
|
||||
return mLimiterMedia;
|
||||
return mLimiter;
|
||||
}
|
||||
|
||||
public int getCount()
|
||||
|
@ -139,28 +139,18 @@ public class MediaView extends ViewGroup {
|
||||
if (mSecondaryLine != null)
|
||||
mSecondaryLine.setText(song.getField(secondaryField));
|
||||
if (mExpander != null) {
|
||||
ExpanderData data = null;
|
||||
SongData data = null;
|
||||
try {
|
||||
data = (ExpanderData)mExpander.getTag();
|
||||
data = (SongData)mExpander.getTag();
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
if (data == null) {
|
||||
data = new ExpanderData(primaryField);
|
||||
data = new SongData(primaryField, null);
|
||||
mExpander.setTag(data);
|
||||
}
|
||||
|
||||
data.media = song;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExpanderData {
|
||||
public ExpanderData(int field)
|
||||
{
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public int field;
|
||||
public Song media;
|
||||
}
|
||||
}
|
@ -301,4 +301,31 @@ public class Song implements Parcelable {
|
||||
Arrays.sort(result, comparator);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class SongData {
|
||||
public SongData(int field, Song media)
|
||||
{
|
||||
this.field = field;
|
||||
this.media = media;
|
||||
}
|
||||
|
||||
public SongData(SongData other)
|
||||
{
|
||||
this.field = other.field;
|
||||
this.media = other.media;
|
||||
}
|
||||
|
||||
public SongData()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return (field << 29) + media.getFieldId(field);
|
||||
}
|
||||
|
||||
public int field;
|
||||
public Song media;
|
||||
}
|
@ -53,7 +53,8 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
private TabHost mTabHost;
|
||||
private TextView mTextFilter;
|
||||
private View mClearButton;
|
||||
private ViewGroup mLimiters;
|
||||
|
||||
private ViewGroup mLimiterViews;
|
||||
|
||||
private int mDefaultAction;
|
||||
private boolean mDefaultIsLastAction;
|
||||
@ -73,10 +74,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
ListView view = (ListView)findViewById(id);
|
||||
view.setOnItemClickListener(this);
|
||||
view.setOnCreateContextMenuListener(this);
|
||||
|
||||
MediaAdapter adapter = new MediaAdapter(SongSelector.this, songs, lineA, lineB);
|
||||
adapter.setExpanderListener(this);
|
||||
view.setAdapter(adapter);
|
||||
view.setAdapter(new MediaAdapter(SongSelector.this, songs, lineA, lineB, this));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,7 +100,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
mClearButton = findViewById(R.id.clear_button);
|
||||
mClearButton.setOnClickListener(this);
|
||||
|
||||
mLimiters = (ViewGroup)findViewById(R.id.limiter_layout);
|
||||
mLimiterViews = (ViewGroup)findViewById(R.id.limiter_layout);
|
||||
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
int inputType;
|
||||
@ -180,37 +178,36 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
|
||||
private void updateLimiterViews()
|
||||
{
|
||||
if (mLimiters == null)
|
||||
if (mLimiterViews == null)
|
||||
return;
|
||||
|
||||
mLimiters.removeAllViews();
|
||||
mLimiterViews.removeAllViews();
|
||||
|
||||
MediaAdapter adapter = getAdapter(mTabHost.getCurrentTab());
|
||||
if (adapter == null)
|
||||
return;
|
||||
|
||||
int field = adapter.getLimiterField();
|
||||
if (field == -1)
|
||||
SongData limiter = adapter.getLimiter();
|
||||
if (limiter == null)
|
||||
return;
|
||||
|
||||
Song media = adapter.getLimiterMedia();
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
||||
params.leftMargin = 5;
|
||||
for (int i = Song.FIELD_ARTIST; i <= field; ++i) {
|
||||
for (int i = Song.FIELD_ARTIST; i <= limiter.field; ++i) {
|
||||
PaintDrawable background = new PaintDrawable(Color.GRAY);
|
||||
background.setCornerRadius(5);
|
||||
|
||||
TextView view = new TextView(this);
|
||||
view.setSingleLine();
|
||||
view.setEllipsize(TextUtils.TruncateAt.MARQUEE);
|
||||
view.setText(media.getField(i) + " | X");
|
||||
view.setText(limiter.media.getField(i) + " | X");
|
||||
view.setTextColor(Color.WHITE);
|
||||
view.setBackgroundDrawable(background);
|
||||
view.setLayoutParams(params);
|
||||
view.setPadding(5, 2, 5, 2);
|
||||
view.setTag(new MediaView.ExpanderData(i));
|
||||
view.setTag(new SongData(i, null));
|
||||
view.setOnClickListener(this);
|
||||
mLimiters.addView(view);
|
||||
mLimiterViews.addView(view);
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,9 +221,9 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
if (view == mClearButton) {
|
||||
mTextFilter.setText("");
|
||||
} else {
|
||||
MediaView.ExpanderData data = null;
|
||||
SongData data = null;
|
||||
try {
|
||||
data = (MediaView.ExpanderData)view.getTag();
|
||||
data = (SongData)view.getTag();
|
||||
} catch (ClassCastException e) {
|
||||
}
|
||||
|
||||
@ -234,18 +231,25 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
|
||||
return;
|
||||
|
||||
if (view instanceof TextView) {
|
||||
int newField = data.field == Song.FIELD_ARTIST ? -1 : data.field - 1;
|
||||
for (int i = mTabHost.getChildCount(); --i != -1; ) {
|
||||
SongData limiter = getAdapter(mTabHost.getCurrentTab()).getLimiter();
|
||||
int field = data.field - 1;
|
||||
if (limiter.field == 0)
|
||||
limiter = null;
|
||||
else
|
||||
limiter.field = field;
|
||||
for (int i = 3; --i != -1; ) {
|
||||
MediaAdapter adapter = getAdapter(i);
|
||||
if (adapter.getLimiterField() >= data.field)
|
||||
adapter.setLimiter(newField, adapter.getLimiterMedia());
|
||||
SongData currentLimiter = adapter.getLimiter();
|
||||
if (currentLimiter != null && currentLimiter.field > field)
|
||||
adapter.setLimiter(limiter);
|
||||
}
|
||||
updateLimiterViews();
|
||||
} else {
|
||||
SongData limiter = new SongData(data);
|
||||
for (int i = data.field; i != 3; ++i) {
|
||||
MediaAdapter adapter = getAdapter(i);
|
||||
adapter.setLimiter(data.field, data.media);
|
||||
adapter.hideAll();
|
||||
adapter.setLimiter(limiter);
|
||||
}
|
||||
mTabHost.setCurrentTab(data.field);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user