Skip to content

Commit

Permalink
Implement DownloadManager
Browse files Browse the repository at this point in the history
  • Loading branch information
AsemLab committed Sep 25, 2024
1 parent 7e304e7 commit 41ae740
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 deletions.
39 changes: 25 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@
<activity
android:name=".passdata.f2f.ThirdActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
</activity>
<activity
android:name=".passdata.a2a.SecondActivity"
android:exported="false" />
<activity
android:name=".passdata.a2a.FirstActivity"
android:exported="true">
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->


<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
</activity>
<activity
android:name=".epoxy.EpoxyActivity"
Expand All @@ -50,15 +53,23 @@
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->


<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterForResultActivity" />

<receiver
android:name=".download.DownloadCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>

</application>

</manifest>
16 changes: 8 additions & 8 deletions app/src/main/java/com/asemlab/samples/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand All @@ -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
)

}

Expand All @@ -48,9 +52,5 @@ class MainActivity : AppCompatActivity() {
fragmentTransaction.commit()
}

companion object {
// TODO This object will leak the memory
lateinit var context: Context
}

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

0 comments on commit 41ae740

Please sign in to comment.