use of android.app.DownloadManager.Query in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method removeAllCurrentDownloads.
/**
* Helper to remove all downloads that are registered with the DL Manager.
*
* Note: This gives us a clean slate b/c it includes downloads that are pending, running,
* paused, or have completed.
*/
protected void removeAllCurrentDownloads() {
Log.i(LOG_TAG, "Removing all current registered downloads...");
ArrayList<Long> ids = new ArrayList<Long>();
Cursor cursor = mDownloadManager.query(new Query());
try {
if (cursor.moveToFirst()) {
do {
int index = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
long downloadId = cursor.getLong(index);
ids.add(downloadId);
} while (cursor.moveToNext());
}
} finally {
cursor.close();
}
// delete all ids
for (long id : ids) {
mDownloadManager.remove(id);
}
// make sure the database is empty
cursor = mDownloadManager.query(new Query());
try {
assertEquals(0, cursor.getCount());
} finally {
cursor.close();
}
}
use of android.app.DownloadManager.Query in project platform_frameworks_base by android.
the class ConnectionUtil method downloadSuccessful.
/**
* Determines if a given download was successful by querying the DownloadManager.
*
* @param enqueue the id used to identify/query the DownloadManager with.
* @return true if download was successful, false otherwise.
*/
private boolean downloadSuccessful(long enqueue) {
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
Log.v(LOG_TAG, "Successfully downloaded file!");
return true;
}
}
return false;
}
use of android.app.DownloadManager.Query in project platform_frameworks_base by android.
the class DownloadManagerFunctionalTest method verifyDownload.
/**
* Helper to verify a standard single-file download from the mock server, and clean up after
* verification
*
* Note that this also calls the Download manager's remove, which cleans up the file from cache.
*
* @param requestId The id of the download to remove
* @param fileData The data to verify the file contains
*/
private void verifyDownload(long requestId, byte[] fileData) throws Exception {
int fileSize = fileData.length;
ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(requestId);
Cursor cursor = mDownloadManager.query(new Query().setFilterById(requestId));
try {
assertEquals(1, cursor.getCount());
assertTrue(cursor.moveToFirst());
verifyFileSize(pfd, fileSize);
verifyFileContents(pfd, fileData);
assertTrue(new File(CACHE_DIR + "/" + DEFAULT_FILENAME).exists());
} finally {
pfd.close();
cursor.close();
}
}
use of android.app.DownloadManager.Query in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method hasDownloadFinished.
/**
* Checks with the download manager if the give download is finished.
* @param id id of the download to check
* @return true if download is finished, false otherwise.
*/
private boolean hasDownloadFinished(long id) {
Query q = new Query();
q.setFilterById(id);
q.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor cursor = mDownloadManager.query(q);
boolean finished = cursor.getCount() == 1;
cursor.close();
return finished;
}
use of android.app.DownloadManager.Query in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method getCursor.
/**
* Performs a query based on ID and returns a Cursor for the query.
*
* @param id The id of the download in DL Manager; pass -1 to query all downloads
* @return A cursor for the query results
*/
protected Cursor getCursor(long id) throws Exception {
Query query = new Query();
if (id != -1) {
query.setFilterById(id);
}
Cursor cursor = mDownloadManager.query(query);
int currentWaitTime = 0;
try {
while (!cursor.moveToFirst()) {
Thread.sleep(DEFAULT_WAIT_POLL_TIME);
currentWaitTime += DEFAULT_WAIT_POLL_TIME;
if (currentWaitTime > DEFAULT_MAX_WAIT_TIME) {
fail("timed out waiting for a non-null query result");
}
cursor.requery();
}
} catch (Exception e) {
cursor.close();
throw e;
}
return cursor;
}
Aggregations