Search in sources :

Example 1 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by ParanoidAndroid.

the class ConnectionUtil method startDownloadAndWait.

/**
     * Start a download on a given url and wait for completion.
     *
     * @param targetUrl the target to download.x
     * @param timeout to wait for download to finish
     * @return true if we successfully downloaded the requestedUrl, false otherwise.
     */
public boolean startDownloadAndWait(String targetUrl, long timeout) {
    if (targetUrl.length() == 0 || targetUrl == null) {
        Log.v(LOG_TAG, "Empty or Null target url requested to DownloadManager");
        return true;
    }
    Request request = new Request(Uri.parse(targetUrl));
    long enqueue = mDownloadManager.enqueue(request);
    Log.v(LOG_TAG, "Sending download request of " + targetUrl + " to DownloadManager");
    long startTime = System.currentTimeMillis();
    while (true) {
        if ((System.currentTimeMillis() - startTime) > timeout) {
            Log.v(LOG_TAG, "startDownloadAndWait timed out, failed to fetch " + targetUrl + " within " + timeout);
            return downloadSuccessful(enqueue);
        }
        Log.v(LOG_TAG, "Waiting for the download to finish " + targetUrl);
        synchronized (mDownloadMonitor) {
            try {
                mDownloadMonitor.wait(SHORT_TIMEOUT);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!downloadSuccessful(enqueue)) {
                continue;
            }
            return true;
        }
    }
}
Also used : Request(android.app.DownloadManager.Request)

Example 2 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerBaseTest method doEnqueue.

private long doEnqueue(int location) throws Exception {
    Uri uri = getServerUri(DEFAULT_FILENAME);
    Request request = new Request(uri).setTitle(DEFAULT_FILENAME);
    if (location == DOWNLOAD_TO_SYSTEM_CACHE) {
        request.setDestinationToSystemCache();
    }
    return mDownloadManager.enqueue(request);
}
Also used : Request(android.app.DownloadManager.Request) Uri(android.net.Uri)

Example 3 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerFunctionalTest method testDownloadNoWifi.

/**
     * Tests that a download set for Wifi does not progress while Wifi is disabled, but resumes
     * once Wifi is re-enabled.
     */
@LargeTest
public void testDownloadNoWifi() throws Exception {
    // wait only 60 seconds before giving up
    long timeout = 60 * 1000;
    // 140k
    int fileSize = 1024;
    byte[] blobData = generateData(fileSize, DataType.TEXT);
    setWiFiStateOn(false);
    enqueueResponse(buildResponse(HTTP_OK, blobData));
    try {
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Request request = new Request(uri);
        request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
        long dlRequest = mDownloadManager.enqueue(request);
        // wait for the download to complete
        boolean success = waitForDownloadOrTimeoutNoThrow(dlRequest, WAIT_FOR_DOWNLOAD_POLL_TIME, timeout);
        assertFalse("Download proceeded without Wifi connection!", success);
        setWiFiStateOn(true);
        waitForDownloadOrTimeout(dlRequest);
        assertEquals(1, mReceiver.numDownloadsCompleted());
    } finally {
        setWiFiStateOn(true);
    }
}
Also used : Request(android.app.DownloadManager.Request) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 4 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerFunctionalTest method testDownloadToExternal_fileExists.

/**
     * Tests trying to download to SD card when the file with same name already exists.
     */
@LargeTest
public void testDownloadToExternal_fileExists() throws Exception {
    File existentFile = createFileOnSD(null, 1, DataType.TEXT, null);
    byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
    // Prepare the mock server with a standard response
    enqueueResponse(buildResponse(HTTP_OK, blobData));
    try {
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Request request = new Request(uri);
        Uri localUri = Uri.fromFile(existentFile);
        request.setDestinationUri(localUri);
        long dlRequest = mDownloadManager.enqueue(request);
        // wait for the download to complete
        waitForDownloadOrTimeout(dlRequest);
        Cursor cursor = getCursor(dlRequest);
        try {
            verifyInt(cursor, DownloadManager.COLUMN_STATUS, DownloadManager.STATUS_SUCCESSFUL);
        } finally {
            cursor.close();
        }
    } finally {
        existentFile.delete();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) File(java.io.File) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 5 with Request

use of android.app.DownloadManager.Request in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerFunctionalTest method testSetTitle.

/**
     * Tests that we can set the title of a download.
     */
@LargeTest
public void testSetTitle() throws Exception {
    int fileSize = 1024;
    byte[] blobData = generateData(fileSize, DataType.BINARY);
    enqueueResponse(buildResponse(HTTP_OK, blobData));
    // An arbitrary unicode string title
    final String title = "¥123;\"ŒŽ Ջ ਇ 쳠 栠Ψ尴" + "ඩ샅솨  #\'";
    Uri uri = getServerUri(DEFAULT_FILENAME);
    Request request = new Request(uri);
    request.setTitle(title);
    long dlRequest = mDownloadManager.enqueue(request);
    waitForDownloadOrTimeout(dlRequest);
    Cursor cursor = getCursor(dlRequest);
    try {
        verifyString(cursor, DownloadManager.COLUMN_TITLE, title);
    } finally {
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) Cursor(android.database.Cursor) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

Request (android.app.DownloadManager.Request)122 Uri (android.net.Uri)86 LargeTest (android.test.suitebuilder.annotation.LargeTest)41 File (java.io.File)38 Cursor (android.database.Cursor)32 ParcelFileDescriptor (android.os.ParcelFileDescriptor)24 DownloadManager (android.app.DownloadManager)17 Suppress (android.test.suitebuilder.annotation.Suppress)10 Query (android.app.DownloadManager.Query)6 DataOutputStream (java.io.DataOutputStream)6 FileOutputStream (java.io.FileOutputStream)6 HashSet (java.util.HashSet)6 Random (java.util.Random)6 Resources (android.content.res.Resources)5 UiObject (android.support.test.uiautomator.UiObject)5 MockResponse (com.google.mockwebserver.MockResponse)4 TimeoutException (java.util.concurrent.TimeoutException)4 AlertDialog (android.app.AlertDialog)3 Context (android.content.Context)3 DialogInterface (android.content.DialogInterface)3