Search in sources :

Example 66 with Request

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

the class DownloadManagerFunctionalTest method testDownloadToProhibitedDirectory.

/**
     * Tests trying to download a file to the system partition.
     */
@LargeTest
public void testDownloadToProhibitedDirectory() throws Exception {
    File downloadedFile = new File(PROHIBITED_DIRECTORY, DEFAULT_FILENAME);
    try {
        byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
        // Prepare the mock server with a standard response
        enqueueResponse(buildResponse(HTTP_OK, blobData));
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Request request = new Request(uri);
        Uri localUri = Uri.fromFile(downloadedFile);
        request.setDestinationUri(localUri);
        try {
            mDownloadManager.enqueue(request);
            fail("Failed to throw SecurityException when trying to write to /system.");
        } catch (SecurityException s) {
            assertFalse(downloadedFile.exists());
        }
    } finally {
        // Just in case file somehow got created, make sure to delete it
        downloadedFile.delete();
    }
}
Also used : Request(android.app.DownloadManager.Request) File(java.io.File) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 67 with Request

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

the class DownloadManagerFunctionalTest method testDownloadToExternal.

/**
     * Tests trying to download a file to SD card.
     */
@LargeTest
public void testDownloadToExternal() throws Exception {
    String localDownloadDirectory = Environment.getExternalStorageDirectory().getPath();
    File downloadedFile = new File(localDownloadDirectory, DEFAULT_FILENAME);
    // make sure the file doesn't already exist in the directory
    downloadedFile.delete();
    try {
        byte[] blobData = generateData(DEFAULT_FILE_SIZE, DataType.TEXT);
        // Prepare the mock server with a standard response
        enqueueResponse(buildResponse(HTTP_OK, blobData));
        Uri uri = getServerUri(DEFAULT_FILENAME);
        Request request = new Request(uri);
        Uri localUri = Uri.fromFile(downloadedFile);
        request.setDestinationUri(localUri);
        long dlRequest = mDownloadManager.enqueue(request);
        // wait for the download to complete
        waitForDownloadOrTimeout(dlRequest);
        verifyAndCleanupSingleFileDownload(dlRequest, blobData);
        assertEquals(1, mReceiver.numDownloadsCompleted());
    } finally {
        downloadedFile.delete();
    }
}
Also used : Request(android.app.DownloadManager.Request) File(java.io.File) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 68 with Request

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

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_5MB_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(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 69 with Request

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

the class DownloadManagerTestApp method runDownloadMultipleAirplaneModeEnableDisable.

/**
     * Tests that downloads resume when switching on/off Airplane mode numerous times at
     * various intervals.
     *
     * Note: Device has no mobile access when running this test.
     *
     * @throws Exception if unsuccessful
     */
public void runDownloadMultipleAirplaneModeEnableDisable() throws Exception {
    String filename = DOWNLOAD_5MB_FILENAME;
    long filesize = DOWNLOAD_5MB_FILESIZE;
    // make sure WiFi is enabled, and airplane mode is not on
    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_WIFI);
        dlRequest = mDownloadManager.enqueue(request);
        waitForDownloadToStart(dlRequest);
        // are we making any progress?
        waitForFileToGrow(downloadedFile);
        // download disable
        Log.i(LOG_TAG, "Turning on Airplane mode...");
        setAirplaneModeOn(true);
        // wait 1 minute
        Thread.sleep(60 * 1000);
        // download enable
        Log.i(LOG_TAG, "Turning off Airplane mode...");
        setAirplaneModeOn(false);
        // make sure we're starting to download some data...
        waitForFileToGrow(downloadedFile);
        // reenable the connection to start up the download again
        Log.i(LOG_TAG, "Turning on Airplane mode again...");
        setAirplaneModeOn(true);
        // wait 20 seconds
        Thread.sleep(20 * 1000);
        // Finish up the download...
        Log.i(LOG_TAG, "Turning off Airplane mode again...");
        setAirplaneModeOn(false);
        Log.i(LOG_TAG, "Waiting up to 3 minutes for donwload to complete...");
        // wait up to 3 mins before timeout
        waitForDownloadsOrTimeout(dlRequest, 180 * 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 70 with Request

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

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_10MB_FILENAME;
    long filesize = DOWNLOAD_10MB_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...
    waitForDownloadOrTimeout(dlRequest, WAIT_FOR_DOWNLOAD_POLL_TIME, 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)

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