-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/asemlab/samples/download/DownloadCompleteReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.asemlab.samples.download | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.widget.Toast | ||
|
||
// TODO If you want to listener when downloading has finished | ||
class DownloadCompleteReceiver : BroadcastReceiver() { | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
if (intent.action == "android.intent.action.DOWNLOAD_COMPLETE") | ||
Toast.makeText(context, "Download Completed!", Toast.LENGTH_SHORT).show() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.asemlab.samples.download | ||
|
||
import android.app.DownloadManager | ||
import android.content.Context | ||
import android.os.Environment | ||
import androidx.core.net.toUri | ||
|
||
class FileDownloaderImp(private val context: Context) { | ||
|
||
private val downloadManager = | ||
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
|
||
// TODO Download any file with DownloadManager | ||
fun download(url: String, title: String, fileName: String, requireLongRun: Boolean): Long { | ||
val request = DownloadManager.Request(url.toUri()) | ||
.setTitle(title) | ||
// .setMimeType("") | ||
// .setDestinationInExternalFilesDir() // Use to download in Android/Data/packageName | ||
// .setRequiresCharging(false) | ||
.setDestinationInExternalPublicDir( | ||
Environment.DIRECTORY_DOWNLOADS, | ||
fileName | ||
) | ||
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI) | ||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) | ||
|
||
if (requireLongRun) | ||
request.setShowRunningNotification(true) | ||
|
||
return downloadManager.enqueue(request) | ||
} | ||
} |