Search in sources :

Example 6 with Query

use of android.app.DownloadManager.Query in project XposedInstaller by rovo89.

the class DownloadsUtil method removeAllForUrl.

public static void removeAllForUrl(Context context, String url) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnUri = c.getColumnIndexOrThrow(DownloadManager.COLUMN_URI);
    List<Long> idsList = new ArrayList<>(1);
    while (c.moveToNext()) {
        if (url.equals(c.getString(columnUri)))
            idsList.add(c.getLong(columnId));
    }
    c.close();
    if (idsList.isEmpty())
        return;
    long[] ids = new long[idsList.size()];
    for (int i = 0; i < ids.length; i++) ids[i] = idsList.get(i);
    dm.remove(ids);
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager)

Example 7 with Query

use of android.app.DownloadManager.Query in project XposedInstaller by rovo89.

the class DownloadsUtil method getById.

public static DownloadInfo getById(Context context, long id) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query().setFilterById(id));
    if (!c.moveToFirst()) {
        c.close();
        return null;
    }
    int columnUri = c.getColumnIndexOrThrow(DownloadManager.COLUMN_URI);
    int columnTitle = c.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE);
    int columnLastMod = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP);
    int columnFilename = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME);
    int columnStatus = c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
    int columnTotalSize = c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
    int columnBytesDownloaded = c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
    int columnReason = c.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
    int status = c.getInt(columnStatus);
    String localFilename = c.getString(columnFilename);
    if (status == DownloadManager.STATUS_SUCCESSFUL && !new File(localFilename).isFile()) {
        dm.remove(id);
        c.close();
        return null;
    }
    DownloadInfo info = new DownloadInfo(id, c.getString(columnUri), c.getString(columnTitle), c.getLong(columnLastMod), localFilename, status, c.getInt(columnTotalSize), c.getInt(columnBytesDownloaded), c.getInt(columnReason));
    c.close();
    return info;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 8 with Query

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...");
    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 9 with Query

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

Example 10 with Query

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