Copyright (c) 2014, Impeccable Labs, LLC. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions ar...
If you think the Android project PlayTunes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.ideabag.playtunes.adapter.search;
//www.java2s.comimport com.ideabag.playtunes.adapter.AlbumListAdapter;
import com.ideabag.playtunes.database.MediaQuery;
import com.ideabag.playtunes.util.ISearchableAdapter;
import android.content.Context;
import android.provider.MediaStore;
publicclass SearchAlbumsAdapter extends AlbumListAdapter implements ISearchableAdapter {
publicstaticfinal String TAG = "SongAlbumsAdapter";
String mSearchTerms;
privateint mTruncateAmount;
public SearchAlbumsAdapter( Context context, String searchTerms, int truncated, MediaQuery.OnQueryCompletedListener listener ) {
super( context );
mTruncateAmount = truncated;
setOnQueryCompletedListener( listener );
if ( null != searchTerms && searchTerms.length() > 0 ) {
setSearchTerms( searchTerms );
}
}
//
// Compare both the album name and artist
//
publicvoid setSearchTerms( String queryString ) {
if ( null != queryString && !queryString.equals( mSearchTerms ) ) {
mSearchTerms = queryString;
String[] terms = mSearchTerms.split( "\\s+" );
String mRelevance = "(";
for ( int i = 0, count = terms.length; i < count; i++ ) {
String term = terms[ i ];
if ( i > 0 ) {
mRelevance += "+";
}
mRelevance += "2 * (" + MediaStore.Audio.Albums.ALBUM + " LIKE '%" + term + "%' )";
mRelevance += "+ 20 * (" + MediaStore.Audio.Albums.ALBUM + " LIKE '" + term + "%' )";
mRelevance += "+ 6 * (" + MediaStore.Audio.Albums.ALBUM + " LIKE '% +" + term + "%' )";
mRelevance += "+ (" + MediaStore.Audio.Albums.ARTIST + " LIKE '%" + term + "%' )";
mRelevance += "+ 10 * (" + MediaStore.Audio.Albums.ARTIST + " LIKE '" + term + "%' )";
mRelevance += "+ 3 * (" + MediaStore.Audio.Albums.ARTIST + " LIKE '% +" + term + "%' )";
}
mRelevance += ") WEIGHT";
String[] allAlbumsSelection = new String[] {
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.NUMBER_OF_SONGS,
MediaStore.Audio.Albums.ALBUM_ART,
MediaStore.Audio.Albums._ID,
mRelevance,
};
//android.util.Log.i( TAG + "@setQuery", "Weight projection: " + mRelevance );
MediaQuery query = new MediaQuery(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
allAlbumsSelection,
"WEIGHT > 0",
null,
"WEIGHT DESC"
);
setMediaQuery( query );
}
}
@Override publicint getCount() {
int count = 0;
if ( null != mCursor ) {
if ( mTruncateAmount <= 0 ) {
count = mCursor.getCount();
} else {
count = ( mCursor.getCount() > mTruncateAmount ? mTruncateAmount : mCursor.getCount() );
}
}
return count;
}
publicint hasMore() {
return ( mCursor != null && mTruncateAmount > 0 && mCursor.getCount() > mTruncateAmount ? mCursor.getCount() - mTruncateAmount : 0 );
}
publicvoid setTruncateAmount( int mAmt ) {
mTruncateAmount = mAmt;
notifyDataSetChanged();
}
}