Search in sources :

Example 66 with Query

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

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

Example 67 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 68 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 69 with Query

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

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 70 with Query

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

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

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