Add blacklist option and minor code improvements

This commit is contained in:
Karim Abou Zeid 2017-09-08 16:22:18 +02:00
commit 8a75b8b68f
13 changed files with 574 additions and 28 deletions

View file

@ -9,10 +9,13 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.provider.BlacklistStore;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import java.util.ArrayList;
import hugo.weaving.DebugLog;
/**
* @author Karim Abou Zeid (kabouzeid)
*/
@ -87,11 +90,20 @@ public class SongLoader {
return makeSongCursor(context, selection, selectionValues, PreferenceUtil.getInstance(context).getSongSortOrder());
}
@DebugLog
@Nullable
public static Cursor makeSongCursor(@NonNull final Context context, @Nullable final String selection, final String[] selectionValues, final String sortOrder) {
String baseSelection = BASE_SELECTION;
public static Cursor makeSongCursor(@NonNull final Context context, @Nullable String selection, String[] selectionValues, final String sortOrder) {
if (selection != null && !selection.trim().equals("")) {
baseSelection += " AND " + selection;
selection = BASE_SELECTION + " AND " + selection;
} else {
selection = BASE_SELECTION;
}
// Blacklist
ArrayList<String> paths = BlacklistStore.getInstance(context).getPaths();
if (!paths.isEmpty()) {
selection = generateBlacklistSelection(selection, paths.size());
selectionValues = addBlacklistSelectionValues(selectionValues, paths);
}
try {
@ -109,9 +121,30 @@ public class SongLoader {
AudioColumns.ARTIST_ID,// 9
AudioColumns.ARTIST,// 10
}, baseSelection, selectionValues, sortOrder);
}, selection, selectionValues, sortOrder);
} catch (SecurityException e) {
return null;
}
}
@DebugLog
private static String generateBlacklistSelection(String selection, int pathCount) {
String newSelection = selection != null && !selection.trim().equals("") ? selection + " AND " : "";
newSelection += AudioColumns.DATA + " NOT LIKE ?";
for (int i = 0; i < pathCount - 1; i++) {
newSelection += " AND " + AudioColumns.DATA + " NOT LIKE ?";
}
return newSelection;
}
@DebugLog
private static String[] addBlacklistSelectionValues(String[] selectionValues, ArrayList<String> paths) {
if (selectionValues == null) selectionValues = new String[0];
String[] newSelectionValues = new String[selectionValues.length + paths.size()];
System.arraycopy(selectionValues, 0, newSelectionValues, 0, selectionValues.length);
for (int i = selectionValues.length; i < newSelectionValues.length; i++) {
newSelectionValues[i] = paths.get(i - selectionValues.length) + "%";
}
return newSelectionValues;
}
}