use of android.app.DownloadManager.Request in project android_frameworks_base by AOSPA.
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();
}
}
use of android.app.DownloadManager.Request in project SmartMesh_Android by SmartMeshFoundation.
the class AdvancedWebView method handleDownload.
/**
* Handles a download by loading the file from `fromUrl` and saving it to `toFilename` on the external storage
* <p/>
* This requires the two permissions `android.permission.INTERNET` and `android.permission.WRITE_EXTERNAL_STORAGE`
* <p/>
* Only supported on API level 9 (Android 2.3) and above
*
* @param context a valid `Context` reference
* @param fromUrl the URL of the file to download, e.g. the one from `AdvancedWebView.onDownloadRequested(...)`
* @param toFilename the name of the destination file where the download should be saved, e.g. `myImage.jpg`
* @return whether the download has been successfully handled or not
*/
@SuppressLint("NewApi")
public static boolean handleDownload(final Context context, final String fromUrl, final String toFilename) {
if (Build.VERSION.SDK_INT < 9) {
throw new RuntimeException("Method requires API level 9 or above");
}
final Request request = new Request(Uri.parse(fromUrl));
if (Build.VERSION.SDK_INT >= 11) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, toFilename);
final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
try {
try {
dm.enqueue(request);
} catch (SecurityException e) {
if (Build.VERSION.SDK_INT >= 11) {
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
}
dm.enqueue(request);
}
return true;
}// if the download manager app has been disabled on the device
catch (IllegalArgumentException e) {
// show the settings screen where the user can enable the download manager app again
openAppSettings(context, AdvancedWebView.PACKAGE_NAME_DOWNLOAD_MANAGER);
return false;
}
}
use of android.app.DownloadManager.Request in project Awful.apk by Awful.
the class ThreadDisplayFragment method enqueueDownload.
public void enqueueDownload(Uri link) {
if (AwfulUtils.isMarshmallow()) {
int permissionCheck = ContextCompat.checkSelfPermission(this.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
downloadLink = link;
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, Constants.AWFUL_PERMISSION_WRITE_EXTERNAL_STORAGE);
return;
}
}
Request request = new Request(link);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
if (link.getLastPathSegment().equals("attachment.php") && link.getHost().equals("forums.somethingawful.com")) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "attachment.png");
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(Constants.COOKIE_DOMAIN));
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, link.getLastPathSegment());
}
request.allowScanningByMediaScanner();
DownloadManager dlManager = (DownloadManager) getAwfulActivity().getSystemService(AwfulActivity.DOWNLOAD_SERVICE);
dlManager.enqueue(request);
}
use of android.app.DownloadManager.Request in project Fairphone by Kwamecorp.
the class FairphoneUpdater method createDownloadRequest.
private Request createDownloadRequest(String url, String fileName, String downloadTitle) {
Request request = new Request(Uri.parse(url));
Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + VersionParserHelper.UPDATER_FOLDER).mkdirs();
request.setDestinationInExternalPublicDir(VersionParserHelper.UPDATER_FOLDER, fileName);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle(downloadTitle);
return request;
}
use of android.app.DownloadManager.Request in project Fairphone by Kwamecorp.
the class UpdaterService method createDownloadRequest.
private Request createDownloadRequest(String url, String fileName) {
Request request = new Request(Uri.parse(url));
Environment.getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + VersionParserHelper.UPDATER_FOLDER).mkdirs();
request.setDestinationInExternalPublicDir(VersionParserHelper.UPDATER_FOLDER, fileName);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
Resources resources = getApplicationContext().getResources();
request.setTitle(resources.getString(R.string.downloadUpdateTitle));
return request;
}
Aggregations