use of android.database.Cursor in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method doWaitForDownloadsOrTimeout.
/**
* Helper to wait for all downloads to finish, or else a timeout to occur
*
* @param query The query to pass to the download manager
* @param poll The poll time to wait between checks
* @param timeoutMillis The max amount of time (in ms) to wait for the download(s) to complete
*/
protected void doWaitForDownloadsOrTimeout(Query query, long poll, long timeoutMillis) throws TimeoutException {
int currentWaitTime = 0;
while (true) {
query.setFilterByStatus(DownloadManager.STATUS_PENDING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_RUNNING);
Cursor cursor = mDownloadManager.query(query);
try {
if (cursor.getCount() == 0) {
Log.i(LOG_TAG, "All downloads should be done...");
break;
}
currentWaitTime = timeoutWait(currentWaitTime, poll, timeoutMillis, "Timed out waiting for all downloads to finish");
} finally {
cursor.close();
}
}
}
use of android.database.Cursor 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;
}
use of android.database.Cursor in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method verifyAndCleanupSingleFileDownload.
/**
* 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
*/
protected void verifyAndCleanupSingleFileDownload(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);
} finally {
pfd.close();
cursor.close();
mDownloadManager.remove(requestId);
}
}
use of android.database.Cursor in project platform_frameworks_base by android.
the class DownloadManagerBaseTest method waitForDownloadToStart.
/**
* Synchronously waits for a download to start.
*
* @param dlRequest the download request id used by Download Manager to track the download.
* @throws Exception if timed out while waiting for SD card to mount
*/
protected void waitForDownloadToStart(long dlRequest) throws Exception {
Cursor cursor = getCursor(dlRequest);
try {
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int value = cursor.getInt(columnIndex);
int currentWaitTime = 0;
while (value != DownloadManager.STATUS_RUNNING && (value != DownloadManager.STATUS_FAILED) && (value != DownloadManager.STATUS_SUCCESSFUL)) {
Log.i(LOG_TAG, "Waiting for download to start...");
currentWaitTime = timeoutWait(currentWaitTime, WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME, "Timed out waiting for download to start!");
cursor.requery();
assertTrue(cursor.moveToFirst());
columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
value = cursor.getInt(columnIndex);
}
assertFalse("Download failed immediately after start", value == DownloadManager.STATUS_FAILED);
} finally {
cursor.close();
}
}
use of android.database.Cursor in project platform_frameworks_base by android.
the class DownloadManagerFunctionalTest method doErrorTest.
/**
* Verifies a particular error code was received from a download
*
* @param uri The uri to enqueue to the DownloadManager
* @param error The error code expected
* @throws Exception if the test fails
*/
public void doErrorTest(Uri uri, int error) throws Exception {
Request request = new Request(uri);
request.setTitle(DEFAULT_FILENAME);
long dlRequest = mDownloadManager.enqueue(request);
try {
waitForDownloadOrTimeout(dlRequest);
} catch (TimeoutException ex) {
// it is expected to timeout as download never finishes
}
Cursor cursor = getCursor(dlRequest);
try {
verifyInt(cursor, DownloadManager.COLUMN_REASON, error);
} finally {
cursor.close();
}
}
Aggregations