Search in sources :

Example 61 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.

the class DownloadManagerBaseTest method getBytesDownloaded.

private long getBytesDownloaded(long id) {
    DownloadManager.Query q = new DownloadManager.Query();
    q.setFilterById(id);
    Cursor response = mDownloadManager.query(q);
    if (response.getCount() < 1) {
        Log.i(LOG_TAG, String.format("Query to download manager returned nothing for id %d", id));
        response.close();
        return -1;
    }
    while (response.moveToNext()) {
        int index = response.getColumnIndex(DownloadManager.COLUMN_ID);
        if (id == response.getLong(index)) {
            break;
        }
    }
    int index = response.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
    if (index < 0) {
        Log.i(LOG_TAG, String.format("No downloaded bytes for id %d", id));
        response.close();
        return -1;
    }
    long size = response.getLong(index);
    response.close();
    return size;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) Query(android.app.DownloadManager.Query)

Example 62 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.

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);
    }
}
Also used : Query(android.app.DownloadManager.Query) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Cursor(android.database.Cursor)

Example 63 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by DirtyUnicorns.

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();
    }
}
Also used : Query(android.app.DownloadManager.Query) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Cursor(android.database.Cursor) File(java.io.File)

Example 64 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by ResurrectionRemix.

the class DownloadManagerStressTest method testMultipleDownloads.

/**
     * Attempts to download several files simultaneously
     */
@LargeTest
public void testMultipleDownloads() throws Exception {
    // need to be sure all current downloads have stopped first
    removeAllCurrentDownloads();
    int NUM_FILES = 10;
    // 10 kb
    int MAX_FILE_SIZE = 10 * 1024;
    Random r = new LoggingRng();
    for (int i = 0; i < NUM_FILES; ++i) {
        int size = r.nextInt(MAX_FILE_SIZE);
        byte[] blobData = generateData(size, DataType.TEXT);
        Uri uri = getServerUri(DEFAULT_FILENAME + i);
        Request request = new Request(uri);
        request.setTitle(String.format("%s--%d", DEFAULT_FILENAME + i, i));
        // Prepare the mock server with a standard response
        enqueueResponse(buildResponse(HTTP_OK, blobData));
        long requestID = mDownloadManager.enqueue(request);
    }
    waitForDownloadsOrTimeout(WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME);
    Cursor cursor = mDownloadManager.query(new Query());
    try {
        assertEquals(NUM_FILES, cursor.getCount());
        if (cursor.moveToFirst()) {
            do {
                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
                String errorString = String.format("File %s failed to download successfully. Status code: %d", filename, status);
                assertEquals(errorString, DownloadManager.STATUS_SUCCESSFUL, status);
            } while (cursor.moveToNext());
        }
        assertEquals(NUM_FILES, mReceiver.numDownloadsCompleted());
    } finally {
        cursor.close();
    }
}
Also used : Random(java.util.Random) Query(android.app.DownloadManager.Query) Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 65 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by ResurrectionRemix.

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;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Query (android.app.DownloadManager.Query)76 Cursor (android.database.Cursor)76 DownloadManager (android.app.DownloadManager)15 ArrayList (java.util.ArrayList)13 ParcelFileDescriptor (android.os.ParcelFileDescriptor)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 TimeoutException (java.util.concurrent.TimeoutException)12 File (java.io.File)11 IOException (java.io.IOException)7 Request (android.app.DownloadManager.Request)6 Uri (android.net.Uri)6 FileNotFoundException (java.io.FileNotFoundException)6 Random (java.util.Random)6