diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 7d9bb32..5097e7b 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -18,11 +18,12 @@
-
-
+
+
-
-
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/com/asemlab/samples/MainActivity.kt b/app/src/main/java/com/asemlab/samples/MainActivity.kt
index a28a2bb..65e57fa 100644
--- a/app/src/main/java/com/asemlab/samples/MainActivity.kt
+++ b/app/src/main/java/com/asemlab/samples/MainActivity.kt
@@ -1,7 +1,6 @@
package com.asemlab.samples
import android.Manifest
-import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
@@ -10,6 +9,7 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
+import com.asemlab.samples.download.FileDownloaderImp
class MainActivity : AppCompatActivity() {
@@ -27,9 +27,13 @@ class MainActivity : AppCompatActivity() {
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 200)
}
-
-
- // TODO Show notifications to see how LeakCanary works
+ // TODO Use DownloadManager
+ FileDownloaderImp(this).download(
+ "https://english.ahram.org.eg/Media/News/2011/7/1/2011-634451360987580451-758.jpg",
+ "Download image",
+ "Inter.jpg",
+ true
+ )
}
@@ -48,9 +52,5 @@ class MainActivity : AppCompatActivity() {
fragmentTransaction.commit()
}
- companion object {
- // TODO This object will leak the memory
- lateinit var context: Context
- }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/asemlab/samples/download/DownloadCompleteReceiver.kt b/app/src/main/java/com/asemlab/samples/download/DownloadCompleteReceiver.kt
new file mode 100644
index 0000000..73b0665
--- /dev/null
+++ b/app/src/main/java/com/asemlab/samples/download/DownloadCompleteReceiver.kt
@@ -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()
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt b/app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt
new file mode 100644
index 0000000..d8dbc2c
--- /dev/null
+++ b/app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt
@@ -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)
+ }
+}
\ No newline at end of file