Search in sources :

Example 16 with Request

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

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 17 with Request

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

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)

Example 18 with Request

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

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 19 with Request

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

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 20 with Request

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

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)

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