in LeanbackShowcase/app/src/main/java/androidx/leanback/leanbackshowcase/app/room/network/NetworkManagerUtil.java [85:157]
private static long downloadFromNetworkHelper(long videoId, String downloadingUrl,
String category) {
// Currently we only support downloading for the following format.
String[] allowedTypes = {"png", "jpg", "jpeg", "gif", "webp", "mp4"};
// Extract the suffix and test if the suffix satisfies our requirement.
String suffix = downloadingUrl.substring(
downloadingUrl.lastIndexOf(SUFFIX_SEPARATOR) + 1).toLowerCase();
if (!Arrays.asList(allowedTypes).contains(suffix)) {
if (DEBUG) {
Log.e(TAG, "Unsupported file type, cannot download");
}
return 0L;
}
DownloadManager.Request request;
try {
request = new DownloadManager.Request(Uri.parse(downloadingUrl));
} catch (IllegalArgumentException e) {
if (DEBUG) {
Log.e(TAG, "Cannot get request object from download manager");
}
return 0L;
}
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
String mediaName;
// Configure request and media storage name according to resource category
switch (category) {
case VIDEO:
request.setTitle(DOWNLOAD_VIDEO);
request.setDescription(DOWNLOAD_SELECTED_VIDEO);
mediaName = VIDEO;
break;
case BACKGROUND:
request.setTitle(DOWNLOAD_BACKGROUND_IMAGE);
request.setDescription(DOWNLOADING_BACKGROUND_IMAGE_FILE);
mediaName = BACKGROUND;
break;
case CARD:
request.setTitle(DOWNLOAD_CARD_IMAGE);
request.setDescription(DOWNLOADING_CARD_IMAGE_FILE);
mediaName = CARD;
break;
default:
if (DEBUG) {
Log.d(TAG, "Not valid resource category");
}
return 0L;
}
// Set downloading location and file name.
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS
+ File.separator
+ mediaName,
mediaName + videoId + SUFFIX_SEPARATOR + suffix);
// submit the downloading task to download manager.
final DownloadManager dm =
(DownloadManager) SampleApplication.getInstance().getSystemService(
Context.DOWNLOAD_SERVICE);
final long DownloadManagerGeneratedId = dm.enqueue(request);
return DownloadManagerGeneratedId;
}