Search in sources :

Example 61 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DownloadManagerBaseTest method doWaitForDownloadsOrTimeout.

/**
     * Helper to wait for all downloads to finish, or else a timeout to occur
     *
     * @param query The query to pass to the download manager
     * @param poll The poll time to wait between checks
     * @param timeoutMillis The max amount of time (in ms) to wait for the download(s) to complete
     */
protected void doWaitForDownloadsOrTimeout(Query query, long poll, long timeoutMillis) throws TimeoutException {
    int currentWaitTime = 0;
    while (true) {
        query.setFilterByStatus(DownloadManager.STATUS_PENDING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_RUNNING);
        Cursor cursor = mDownloadManager.query(query);
        try {
            if (cursor.getCount() == 0) {
                Log.i(LOG_TAG, "All downloads should be done...");
                break;
            }
            currentWaitTime = timeoutWait(currentWaitTime, poll, timeoutMillis, "Timed out waiting for all downloads to finish");
        } finally {
            cursor.close();
        }
    }
}
Also used : Cursor(android.database.Cursor)

Example 62 with Cursor

use of android.database.Cursor 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 63 with Cursor

use of android.database.Cursor 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)

Example 64 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DownloadManagerBaseTest method waitForDownloadToStart.

/**
     * Synchronously waits for a download to start.
     *
     * @param dlRequest the download request id used by Download Manager to track the download.
     * @throws Exception if timed out while waiting for SD card to mount
     */
protected void waitForDownloadToStart(long dlRequest) throws Exception {
    Cursor cursor = getCursor(dlRequest);
    try {
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int value = cursor.getInt(columnIndex);
        int currentWaitTime = 0;
        while (value != DownloadManager.STATUS_RUNNING && (value != DownloadManager.STATUS_FAILED) && (value != DownloadManager.STATUS_SUCCESSFUL)) {
            Log.i(LOG_TAG, "Waiting for download to start...");
            currentWaitTime = timeoutWait(currentWaitTime, WAIT_FOR_DOWNLOAD_POLL_TIME, MAX_WAIT_FOR_DOWNLOAD_TIME, "Timed out waiting for download to start!");
            cursor.requery();
            assertTrue(cursor.moveToFirst());
            columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            value = cursor.getInt(columnIndex);
        }
        assertFalse("Download failed immediately after start", value == DownloadManager.STATUS_FAILED);
    } finally {
        cursor.close();
    }
}
Also used : Cursor(android.database.Cursor)

Example 65 with Cursor

use of android.database.Cursor in project platform_frameworks_base by android.

the class DownloadManagerFunctionalTest method doErrorTest.

/**
     * Verifies a particular error code was received from a download
     *
     * @param uri The uri to enqueue to the DownloadManager
     * @param error The error code expected
     * @throws Exception if the test fails
     */
public void doErrorTest(Uri uri, int error) throws Exception {
    Request request = new Request(uri);
    request.setTitle(DEFAULT_FILENAME);
    long dlRequest = mDownloadManager.enqueue(request);
    try {
        waitForDownloadOrTimeout(dlRequest);
    } catch (TimeoutException ex) {
    // it is expected to timeout as download never finishes
    }
    Cursor cursor = getCursor(dlRequest);
    try {
        verifyInt(cursor, DownloadManager.COLUMN_REASON, error);
    } finally {
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Cursor (android.database.Cursor)4002 ArrayList (java.util.ArrayList)547 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)527 Uri (android.net.Uri)467 ContentValues (android.content.ContentValues)334 ContentResolver (android.content.ContentResolver)193 Test (org.junit.Test)183 RemoteException (android.os.RemoteException)182 File (java.io.File)170 IOException (java.io.IOException)159 MatrixCursor (android.database.MatrixCursor)154 Intent (android.content.Intent)140 SQLException (android.database.SQLException)126 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 HashMap (java.util.HashMap)108 SQLiteException (android.database.sqlite.SQLiteException)94 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)93 SQLiteCursor (android.database.sqlite.SQLiteCursor)88 Query (android.app.DownloadManager.Query)76 MergeCursor (android.database.MergeCursor)75