Made progress with the folder feature.

This commit is contained in:
Karim Abou Zeid 2016-03-16 00:41:17 +01:00
commit 1e6ac6a227
38 changed files with 1423 additions and 148 deletions

View file

@ -83,12 +83,12 @@ public class SongLoader {
}
@Nullable
public static Cursor makeSongCursor(@NonNull final Context context, final String selection, final String[] values) {
return makeSongCursor(context, selection, values, PreferenceUtil.getInstance(context).getSongSortOrder());
public static Cursor makeSongCursor(@NonNull final Context context, @Nullable final String selection, final String[] selectionValues) {
return makeSongCursor(context, selection, selectionValues, PreferenceUtil.getInstance(context).getSongSortOrder());
}
@Nullable
public static Cursor makeSongCursor(@NonNull final Context context, @Nullable final String selection, final String[] values, final String sortOrder) {
public static Cursor makeSongCursor(@NonNull final Context context, @Nullable final String selection, final String[] selectionValues, final String sortOrder) {
String baseSelection = BASE_SELECTION;
if (selection != null && !selection.trim().equals("")) {
baseSelection += " AND " + selection;
@ -109,7 +109,7 @@ public class SongLoader {
AudioColumns.ARTIST_ID,// 9
AudioColumns.ARTIST,// 10
}, baseSelection, values, sortOrder);
}, baseSelection, selectionValues, sortOrder);
} catch (SecurityException e) {
return null;
}

View file

@ -23,7 +23,8 @@ import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import hugo.weaving.DebugLog;
/**
* This cursor basically wraps a song cursor and is given a list of the order of the ids of the
@ -36,97 +37,77 @@ public class SortedCursor extends AbstractCursor {
// the map of external indices to internal indices
private ArrayList<Integer> mOrderedPositions;
// this contains the ids that weren't found in the underlying cursor
private ArrayList<Long> mMissingIds;
private ArrayList<String> mMissingValues;
// this contains the mapped cursor positions and afterwards the extra ids that weren't found
private HashMap<Long, Integer> mMapCursorPositions;
// extra we want to store with the cursor
private ArrayList<Object> mExtraData;
private HashMap<String, Integer> mMapCursorPositions;
/**
* @param cursor to wrap
* @param order the list of unique ids in sorted order to display
* @param columnName the column name of the id to look up in the internal cursor
*/
public SortedCursor(final Cursor cursor, final long[] order, final String columnName,
final List<?> extraData) {
if (cursor == null) {
throw new IllegalArgumentException("Non-null cursor is needed");
}
@DebugLog
public SortedCursor(@NonNull final Cursor cursor, @Nullable final String[] order, final String columnName) {
mCursor = cursor;
mMissingIds = buildCursorPositionMapping(order, columnName, extraData);
mMissingValues = buildCursorPositionMapping(order, columnName);
}
/**
* This function populates mOrderedPositions with the cursor positions in the order based
* on the order passed in
*
* @param order the target order of the internal cursor
* @param extraData Extra data we want to add to the cursor
* @param order the target order of the internal cursor
* @return returns the ids that aren't found in the underlying cursor
*/
@NonNull
private ArrayList<Long> buildCursorPositionMapping(@Nullable final long[] order,
final String columnName, @Nullable final List<?> extraData) {
ArrayList<Long> missingIds = new ArrayList<>();
private ArrayList<String> buildCursorPositionMapping(@Nullable final String[] order, final String columnName) {
ArrayList<String> missingValues = new ArrayList<>();
mOrderedPositions = new ArrayList<>(mCursor.getCount());
mExtraData = new ArrayList<>();
mMapCursorPositions = new HashMap<>(mCursor.getCount());
final int idPosition = mCursor.getColumnIndex(columnName);
final int valueColumnIndex = mCursor.getColumnIndex(columnName);
if (mCursor.moveToFirst()) {
// first figure out where each of the ids are in the cursor
do {
mMapCursorPositions.put(mCursor.getLong(idPosition), mCursor.getPosition());
mMapCursorPositions.put(mCursor.getString(valueColumnIndex), mCursor.getPosition());
} while (mCursor.moveToNext());
// now create the ordered positions to map to the internal cursor given the
// external sort order
for (int i = 0; order != null && i < order.length; i++) {
final long id = order[i];
if (mMapCursorPositions.containsKey(id)) {
mOrderedPositions.add(mMapCursorPositions.get(id));
mMapCursorPositions.remove(id);
if (extraData != null) {
mExtraData.add(extraData.get(i));
if (order != null) {
// now create the ordered positions to map to the internal cursor given the
// external sort order
for (final String value : order) {
if (mMapCursorPositions.containsKey(value)) {
mOrderedPositions.add(mMapCursorPositions.get(value));
mMapCursorPositions.remove(value);
} else {
missingValues.add(value);
}
} else {
missingIds.add(id);
}
}
mCursor.moveToFirst();
}
return missingIds;
return missingValues;
}
/**
* @return the list of ids that weren't found in the underlying cursor
*/
public ArrayList<Long> getMissingIds() {
return mMissingIds;
public ArrayList<String> getMissingValues() {
return mMissingValues;
}
/**
* @return the list of ids that were in the underlying cursor but not part of the ordered list
*/
@NonNull
public Collection<Long> getExtraIds() {
public Collection<String> getExtraValues() {
return mMapCursorPositions.keySet();
}
/**
* @return the extra object data that was passed in to be attached to the current row
*/
@Nullable
public Object getExtraData() {
int position = getPosition();
return position < mExtraData.size() ? mExtraData.get(position) : null;
}
@Override
public void close() {
mCursor.close();

View file

@ -0,0 +1,169 @@
/*
* Copyright (C) 2014 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kabouzeid.gramophone.loader;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
/**
* This cursor basically wraps a song cursor and is given a list of the order of the ids of the
* contents of the cursor. It wraps the Cursor and simulates the internal cursor being sorted
* by moving the point to the appropriate spot
*/
public class SortedLongCursor extends AbstractCursor {
// cursor to wrap
private final Cursor mCursor;
// the map of external indices to internal indices
private ArrayList<Integer> mOrderedPositions;
// this contains the ids that weren't found in the underlying cursor
private ArrayList<Long> mMissingIds;
// this contains the mapped cursor positions and afterwards the extra ids that weren't found
private HashMap<Long, Integer> mMapCursorPositions;
/**
* @param cursor to wrap
* @param order the list of unique ids in sorted order to display
* @param columnName the column name of the id to look up in the internal cursor
*/
public SortedLongCursor(final Cursor cursor, final long[] order, final String columnName) {
mCursor = cursor;
mMissingIds = buildCursorPositionMapping(order, columnName);
}
/**
* This function populates mOrderedPositions with the cursor positions in the order based
* on the order passed in
*
* @param order the target order of the internal cursor
* @return returns the ids that aren't found in the underlying cursor
*/
@NonNull
private ArrayList<Long> buildCursorPositionMapping(@Nullable final long[] order, final String columnName) {
ArrayList<Long> missingIds = new ArrayList<>();
mOrderedPositions = new ArrayList<>(mCursor.getCount());
mMapCursorPositions = new HashMap<>(mCursor.getCount());
final int idPosition = mCursor.getColumnIndex(columnName);
if (mCursor.moveToFirst()) {
// first figure out where each of the ids are in the cursor
do {
mMapCursorPositions.put(mCursor.getLong(idPosition), mCursor.getPosition());
} while (mCursor.moveToNext());
// now create the ordered positions to map to the internal cursor given the
// external sort order
for (int i = 0; order != null && i < order.length; i++) {
final long id = order[i];
if (mMapCursorPositions.containsKey(id)) {
mOrderedPositions.add(mMapCursorPositions.get(id));
mMapCursorPositions.remove(id);
} else {
missingIds.add(id);
}
}
mCursor.moveToFirst();
}
return missingIds;
}
/**
* @return the list of ids that weren't found in the underlying cursor
*/
public ArrayList<Long> getMissingIds() {
return mMissingIds;
}
/**
* @return the list of ids that were in the underlying cursor but not part of the ordered list
*/
@NonNull
public Collection<Long> getExtraIds() {
return mMapCursorPositions.keySet();
}
@Override
public void close() {
mCursor.close();
super.close();
}
@Override
public int getCount() {
return mOrderedPositions.size();
}
@Override
public String[] getColumnNames() {
return mCursor.getColumnNames();
}
@Override
public String getString(int column) {
return mCursor.getString(column);
}
@Override
public short getShort(int column) {
return mCursor.getShort(column);
}
@Override
public int getInt(int column) {
return mCursor.getInt(column);
}
@Override
public long getLong(int column) {
return mCursor.getLong(column);
}
@Override
public float getFloat(int column) {
return mCursor.getFloat(column);
}
@Override
public double getDouble(int column) {
return mCursor.getDouble(column);
}
@Override
public boolean isNull(int column) {
return mCursor.isNull(column);
}
@Override
public boolean onMove(int oldPosition, int newPosition) {
if (newPosition >= 0 && newPosition < getCount()) {
mCursor.moveToPosition(mOrderedPositions.get(newPosition));
return true;
}
return false;
}
}

View file

@ -43,7 +43,7 @@ public class TopAndRecentlyPlayedTracksLoader {
@Nullable
public static Cursor makeRecentTracksCursorAndClearUpDatabase(@NonNull final Context context) {
SortedCursor retCursor = makeRecentTracksCursorImpl(context);
SortedLongCursor retCursor = makeRecentTracksCursorImpl(context);
// clean up the databases with any ids not found
if (retCursor != null) {
@ -59,7 +59,7 @@ public class TopAndRecentlyPlayedTracksLoader {
@Nullable
public static Cursor makeTopTracksCursorAndClearUpDatabase(@NonNull final Context context) {
SortedCursor retCursor = makeTopTracksCursorImpl(context);
SortedLongCursor retCursor = makeTopTracksCursorImpl(context);
// clean up the databases with any ids not found
if (retCursor != null) {
@ -74,7 +74,7 @@ public class TopAndRecentlyPlayedTracksLoader {
}
@Nullable
private static SortedCursor makeRecentTracksCursorImpl(@NonNull final Context context) {
private static SortedLongCursor makeRecentTracksCursorImpl(@NonNull final Context context) {
// first get the top results ids from the internal database
Cursor songs = HistoryStore.getInstance(context).queryRecentIds();
@ -89,7 +89,7 @@ public class TopAndRecentlyPlayedTracksLoader {
}
@Nullable
private static SortedCursor makeTopTracksCursorImpl(@NonNull final Context context) {
private static SortedLongCursor makeTopTracksCursorImpl(@NonNull final Context context) {
// first get the top results ids from the internal database
Cursor songs = SongPlayCountStore.getInstance(context).getTopPlayedResults(NUMBER_OF_TOP_TRACKS);
@ -104,8 +104,7 @@ public class TopAndRecentlyPlayedTracksLoader {
}
@Nullable
private static SortedCursor makeSortedCursor(@NonNull final Context context, @Nullable final Cursor cursor,
final int idColumn) {
private static SortedLongCursor makeSortedCursor(@NonNull final Context context, @Nullable final Cursor cursor, final int idColumn) {
if (cursor != null && cursor.moveToFirst()) {
// create the list of ids to select against
StringBuilder selection = new StringBuilder();
@ -133,7 +132,7 @@ public class TopAndRecentlyPlayedTracksLoader {
Cursor songCursor = SongLoader.makeSongCursor(context, selection.toString(), null);
if (songCursor != null) {
// now return the wrapped TopTracksCursor to handle sorting given order
return new SortedCursor(songCursor, order, BaseColumns._ID, null);
return new SortedLongCursor(songCursor, order, BaseColumns._ID);
}
}