Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e93af01c authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Download in external storage

Download in external storage since SplitInstallService system
service cannot access internal cache of AppLounge on Android 11
even if SplitInstallService is uid system. This is probably due
to changes related to scoped storage.

This implementation might be improved later on.
parent 2214d9e4
Loading
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ class DownloadManager @Inject constructor(
) {
    private val downloadsMaps = HashMap<Long, Boolean>()

    companion object {
        const val EXTERNAL_STORAGE_TEMP_CACHE_DIR = "/sdcard/Download/AppLounge/SplitInstallApks"
    }

    fun downloadFileInCache(
        url: String,
        subDirectoryPath: String = "",
@@ -48,10 +52,37 @@ class DownloadManager @Inject constructor(
        downloadCompleted: ((Boolean, String) -> Unit)?
    ): Long {
        val directoryFile = File("$cacheDir/$subDirectoryPath")
        if (!directoryFile.exists()) {
            directoryFile.mkdirs()
        }

        val downloadFile = File("$cacheDir/$fileName")

        return downloadFile(url, downloadFile, downloadCompleted)
    }

    fun downloadFileInExternalStorage(
        url: String,
        subDirectoryPath: String,
        fileName: String,
        downloadCompleted: ((Boolean, String) -> Unit)?
    ): Long {

        val directoryFile = File("$EXTERNAL_STORAGE_TEMP_CACHE_DIR/$subDirectoryPath")
        if (!directoryFile.exists()) {
            directoryFile.mkdirs()
        }

        val downloadFile = File("$directoryFile/$fileName")

        return downloadFile(url, downloadFile, downloadCompleted)
    }

    private fun downloadFile(
        url: String,
        downloadFile: File,
        downloadCompleted: ((Boolean, String) -> Unit)?
    ): Long {
        val request = DownloadManager.Request(Uri.parse(url))
            .setTitle("Downloading...")
            .setDestinationUri(Uri.fromFile(downloadFile))
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ class SplitInstallBinder(
                versionCode, 1
            ) ?: return@withContext

            downloadManager.downloadFileInCache(
            downloadManager.downloadFileInExternalStorage(
                url, packageName, "$packageName.split.$moduleName.apk"
            ) { success, path ->
                if (success) {