Merge "Add support for search in DownloadManager."

This commit is contained in:
Ben Lin
2016-05-10 23:32:02 +00:00
committed by Android (Google) Code Review
3 changed files with 92 additions and 7 deletions

View File

@@ -832,6 +832,7 @@ public class DownloadManager {
private long[] mIds = null;
private Integer mStatusFlags = null;
private String mFilterString = null;
private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION;
private int mOrderDirection = ORDER_DESCENDING;
private boolean mOnlyIncludeVisibleInDownloadsUi = false;
@@ -845,6 +846,17 @@ public class DownloadManager {
return this;
}
/**
*
* Include only the downloads that contains the given string in its name.
* @return this object
* @hide
*/
public Query setFilterByString(@Nullable String filter) {
mFilterString = filter;
return this;
}
/**
* Include only downloads with status matching any the given status flags.
* @param flags any combination of the STATUS_* bit flags
@@ -904,9 +916,20 @@ public class DownloadManager {
List<String> selectionParts = new ArrayList<String>();
String[] selectionArgs = null;
if (mIds != null) {
selectionParts.add(getWhereClauseForIds(mIds));
selectionArgs = getWhereArgsForIds(mIds);
int whereArgsCount = (mIds == null) ? 0 : mIds.length;
whereArgsCount = (mFilterString == null) ? whereArgsCount : whereArgsCount + 1;
selectionArgs = new String[whereArgsCount];
if (whereArgsCount > 0) {
if (mIds != null) {
selectionParts.add(getWhereClauseForIds(mIds));
getWhereArgsForIds(mIds, selectionArgs);
}
if (mFilterString != null) {
selectionParts.add(Downloads.Impl.COLUMN_TITLE + " LIKE ?");
selectionArgs[selectionArgs.length - 1] = "%" + mFilterString + "%";
}
}
if (mStatusFlags != null) {
@@ -1450,12 +1473,22 @@ public class DownloadManager {
*/
static String[] getWhereArgsForIds(long[] ids) {
String[] whereArgs = new String[ids.length];
for (int i = 0; i < ids.length; i++) {
whereArgs[i] = Long.toString(ids[i]);
}
return whereArgs;
return getWhereArgsForIds(ids, whereArgs);
}
/**
* Get selection args for a clause returned by {@link #getWhereClauseForIds(long[])}
* and write it to the supplied args array.
*/
static String[] getWhereArgsForIds(long[] ids, String[] args) {
assert(args.length >= ids.length);
for (int i = 0; i < ids.length; i++) {
args[i] = Long.toString(ids[i]);
}
return args;
}
/**
* This class wraps a cursor returned by DownloadProvider -- the "underlying cursor" -- and
* presents a different set of columns, those defined in the DownloadManager.COLUMN_* constants.