Search in sources :

Example 76 with Request

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

the class DownloadManagerTestApp method initiateDownload.

/**
     * Initiates a download.
     *
     * Queues up a download to the download manager, and saves the DownloadManager's assigned
     * download ID for this download to a file.
     *
     * @throws Exception if unsuccessful
     */
public void initiateDownload() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    mContext.deleteFile(DOWNLOAD_STARTED_FLAG);
    FileOutputStream fileOutput = mContext.openFileOutput(DOWNLOAD_STARTED_FLAG, 0);
    DataOutputStream outputFile = null;
    doCommonDownloadSetup();
    try {
        long dlRequest = -1;
        // Make sure there are no pending downloads currently going on
        removeAllCurrentDownloads();
        Uri remoteUri = getExternalFileUri(filename);
        Request request = new Request(remoteUri);
        dlRequest = mDownloadManager.enqueue(request);
        waitForDownloadToStart(dlRequest);
        assertTrue("request id is -1 from download manager", dlRequest != -1);
        // Store ID of download for later retrieval
        outputFile = new DataOutputStream(fileOutput);
        outputFile.writeLong(dlRequest);
    } finally {
        if (outputFile != null) {
            outputFile.flush();
            outputFile.close();
        }
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) Request(android.app.DownloadManager.Request) Uri(android.net.Uri)

Example 77 with Request

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

the class DownloadManagerTestApp method runLargeDownloadOverWiFi.

/**
     * Tests downloading a large file over WiFi (~10 Mb).
     *
     * @throws Exception if unsuccessful
     */
public void runLargeDownloadOverWiFi() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    long filesize = DOWNLOAD_FILESIZE;
    long dlRequest = -1;
    doCommonDownloadSetup();
    // Make sure there are no pending downloads currently going on
    removeAllCurrentDownloads();
    Uri remoteUri = getExternalFileUri(filename);
    Request request = new Request(remoteUri);
    request.setMimeType("application/vnd.android.package-archive");
    dlRequest = mDownloadManager.enqueue(request);
    // Rather large file, so wait up to 15 mins...
    assertTrue("download not finished", waitForDownload(dlRequest, 15 * 60 * 1000));
    Cursor cursor = getCursor(dlRequest);
    ParcelFileDescriptor pfd = null;
    try {
        Log.i(LOG_TAG, "Verifying download information...");
        // Verify specific info about the file (size, name, etc)...
        pfd = mDownloadManager.openDownloadedFile(dlRequest);
        verifyFileSize(pfd, filesize);
    } finally {
        if (pfd != null) {
            pfd.close();
        }
        mDownloadManager.remove(dlRequest);
        cursor.close();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 78 with Request

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

the class DownloadManagerTestApp method runDownloadMultipleSwitching.

/**
     * Tests that downloads resume when switching back and forth from having connectivity to
     * having no connectivity using both WiFi and airplane mode.
     *
     * Note: Device has no mobile access when running this test.
     *
     * @throws Exception if unsuccessful
     */
public void runDownloadMultipleSwitching() throws Exception {
    String filename = DOWNLOAD_FILENAME;
    long filesize = DOWNLOAD_FILESIZE;
    doCommonDownloadSetup();
    String localDownloadDirectory = Environment.getExternalStorageDirectory().getPath();
    File downloadedFile = new File(localDownloadDirectory, filename);
    long dlRequest = -1;
    try {
        downloadedFile.delete();
        // Make sure there are no pending downloads currently going on
        removeAllCurrentDownloads();
        Uri remoteUri = getExternalFileUri(filename);
        Request request = new Request(remoteUri);
        // Local destination of downloaded file
        Uri localUri = Uri.fromFile(downloadedFile);
        Log.i(LOG_TAG, "setting localUri to: " + localUri.getPath());
        request.setDestinationUri(localUri);
        request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
        dlRequest = mDownloadManager.enqueue(request);
        waitForDownloadToStart(dlRequest);
        // make sure we're starting to download some data...
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download disable
        setWiFiStateOn(false);
        // download disable
        Log.i(LOG_TAG, "Turning on airplane mode...");
        setAirplaneModeOn(true);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        // download disable
        setWiFiStateOn(true);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download enable
        Log.i(LOG_TAG, "Turning off airplane mode...");
        setAirplaneModeOn(false);
        // wait 5 seconds
        Thread.sleep(5 * 1000);
        waitToReceiveData(dlRequest, EXPECTED_PROGRESS);
        // download disable
        Log.i(LOG_TAG, "Turning off WiFi...");
        setWiFiStateOn(false);
        // wait 5 secs
        Thread.sleep(5 * 1000);
        // finally, turn WiFi back on and finish up the download
        Log.i(LOG_TAG, "Turning on WiFi...");
        setWiFiStateOn(true);
        Log.i(LOG_TAG, "Waiting up to 10 minutes for download to complete...");
        assertTrue("download not finished", waitForDownload(dlRequest, 10 * 60 * 1000));
        ParcelFileDescriptor pfd = mDownloadManager.openDownloadedFile(dlRequest);
        verifyFileSize(pfd, filesize);
    } finally {
        Log.i(LOG_TAG, "Cleaning up files...");
        if (dlRequest != -1) {
            mDownloadManager.remove(dlRequest);
        }
        downloadedFile.delete();
    }
}
Also used : Request(android.app.DownloadManager.Request) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) Uri(android.net.Uri)

Example 79 with Request

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

the class DownloadManagerTestApp method runDownloadMultipleSimultaneously.

/**
     * Tests 15 concurrent downloads of 1,000,000-byte files.
     *
     * @throws Exception if test failed
     */
public void runDownloadMultipleSimultaneously() throws Exception {
    final int TOTAL_DOWNLOADS = 15;
    HashSet<Long> downloadIds = new HashSet<Long>(TOTAL_DOWNLOADS);
    // Make sure there are no pending downloads currently going on
    removeAllCurrentDownloads();
    try {
        for (int i = 0; i < TOTAL_DOWNLOADS; ++i) {
            long dlRequest = -1;
            String filename = FILE_CONCURRENT_DOWNLOAD_FILE_PREFIX + i + FILE_CONCURRENT_DOWNLOAD_FILE_EXTENSION;
            Uri remoteUri = getExternalFileUri(filename);
            Request request = new Request(remoteUri);
            request.setTitle(filename);
            dlRequest = mDownloadManager.enqueue(request);
            assertTrue("request id is -1 from download manager", dlRequest != -1);
            downloadIds.add(dlRequest);
        }
        // wait 15 mins max
        assertTrue("download not finished", waitForMultipleDownloads(downloadIds, 15 * 60 * 2000));
    } finally {
        removeAllCurrentDownloads();
    }
}
Also used : Request(android.app.DownloadManager.Request) Uri(android.net.Uri) HashSet(java.util.HashSet)

Example 80 with Request

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

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)

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