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();
}
}
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();
}
}
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;
}
}
}
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();
}
}
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;
}
}
}
Aggregations