Search in sources :

Example 51 with Query

use of android.app.DownloadManager.Query in project platform_frameworks_base by android.

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

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

the class DownloadManagerFunctionalTest method testRemoveDownload.

/**
     * Tests that we can remove a download from the download manager.
     */
@LargeTest
public void testRemoveDownload() throws Exception {
    int fileSize = 1024;
    byte[] blobData = generateData(fileSize, DataType.BINARY);
    long dlRequest = doBasicDownload(blobData, DOWNLOAD_TO_DOWNLOAD_CACHE_DIR);
    Cursor cursor = mDownloadManager.query(new Query().setFilterById(dlRequest));
    try {
        assertEquals("The count of downloads with this ID is not 1!", 1, cursor.getCount());
        mDownloadManager.remove(dlRequest);
        cursor.requery();
        assertEquals("The count of downloads with this ID is not 0!", 0, cursor.getCount());
    } finally {
        cursor.close();
    }
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 53 with Query

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

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...");
    Cursor cursor = mDownloadManager.query(new Query());
    try {
        if (cursor.moveToFirst()) {
            do {
                int index = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
                long downloadId = cursor.getLong(index);
                mDownloadManager.remove(downloadId);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor)

Example 54 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 55 with Query

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

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;
}
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