Search in sources :

Example 21 with Query

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

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

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

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

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

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

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

the class DownloadsUtil method getAllForUrl.

public static List<DownloadInfo> getAllForUrl(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);
    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);
    List<DownloadInfo> downloads = new ArrayList<>();
    while (c.moveToNext()) {
        if (!url.equals(c.getString(columnUri)))
            continue;
        int status = c.getInt(columnStatus);
        String localFilename = c.getString(columnFilename);
        if (status == DownloadManager.STATUS_SUCCESSFUL && !new File(localFilename).isFile()) {
            dm.remove(c.getLong(columnId));
            continue;
        }
        downloads.add(new DownloadInfo(c.getLong(columnId), c.getString(columnUri), c.getString(columnTitle), c.getLong(columnLastMod), localFilename, status, c.getInt(columnTotalSize), c.getInt(columnBytesDownloaded), c.getInt(columnReason)));
    }
    c.close();
    Collections.sort(downloads);
    return downloads;
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 25 with Query

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

the class DownloadsUtil method removeAllForLocalFile.

public static void removeAllForLocalFile(Context context, File file) {
    file.delete();
    String filename;
    try {
        filename = file.getCanonicalPath();
    } catch (IOException e) {
        Log.w(XposedApp.TAG, "Could not resolve path for " + file.getAbsolutePath(), e);
        return;
    }
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnFilename = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME);
    List<Long> idsList = new ArrayList<>(1);
    while (c.moveToNext()) {
        String itemFilename = c.getString(columnFilename);
        if (itemFilename != null) {
            if (filename.equals(itemFilename)) {
                idsList.add(c.getLong(columnId));
            } else {
                try {
                    if (filename.equals(new File(itemFilename).getCanonicalPath())) {
                        idsList.add(c.getLong(columnId));
                    }
                } catch (IOException ignored) {
                }
            }
        }
    }
    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) IOException(java.io.IOException) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

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