Loading app/src/main/java/foundation/e/apps/MainActivity.kt +1 −1 Original line number Diff line number Diff line Loading @@ -143,7 +143,7 @@ class MainActivity : AppCompatActivity() { // Observe and handle downloads viewModel.downloadList.observe(this) { list -> val shouldDownload = list.any { it.status == Status.INSTALLING || it.status == Status.DOWNLOADING it.status == Status.INSTALLING || it.status == Status.DOWNLOADING || it.status == Status.INSTALLED } if (!shouldDownload && list.isNotEmpty()) { for (item in list) { Loading app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +4 −0 Original line number Diff line number Diff line Loading @@ -37,6 +37,7 @@ import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.Dispatchers Loading Loading @@ -134,6 +135,9 @@ class MainActivityViewModel @Inject constructor( app.type, appIcon ) if (fusedDownload.status == Status.INSTALLATION_ISSUE) { fusedManagerRepository.clearInstallationIssue(fusedDownload) } fusedManagerRepository.addDownload(fusedDownload) } } Loading app/src/main/java/foundation/e/apps/manager/download/DownloadManagerBR.kt +2 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ class DownloadManagerBR : BroadcastReceiver() { companion object { private const val TAG = "DownloadManagerBR" val downloadedList = mutableListOf<Long>() } override fun onReceive(context: Context?, intent: Intent?) { Loading @@ -45,6 +46,7 @@ class DownloadManagerBR : BroadcastReceiver() { Log.d(TAG, "onReceive: $action") when (action) { DownloadManager.ACTION_DOWNLOAD_COMPLETE -> { downloadedList.add(id) downloadManagerUtils.updateDownloadStatus(id) } DownloadManager.ACTION_NOTIFICATION_CLICKED -> { Loading app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt +6 −24 Original line number Diff line number Diff line Loading @@ -18,10 +18,7 @@ package foundation.e.apps.manager.download import android.app.DownloadManager import foundation.e.apps.manager.download.data.DownloadProgressLD import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.Status import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope Loading @@ -30,10 +27,7 @@ import kotlinx.coroutines.launch import javax.inject.Inject class DownloadManagerUtils @Inject constructor( private val downloadManager: DownloadManager, private val downloadManagerQuery: DownloadManager.Query, private val fusedManagerRepository: FusedManagerRepository, private val pkgManagerModule: PkgManagerModule, private val fusedManagerRepository: FusedManagerRepository ) { private val TAG = DownloadManagerUtils::class.java.simpleName Loading @@ -47,24 +41,12 @@ class DownloadManagerUtils @Inject constructor( @DelicateCoroutinesApi fun updateDownloadStatus(downloadId: Long) { var downloaded = false DownloadProgressLD.downloadId.forEach { downloadManager.query(downloadManagerQuery.setFilterById(it)).use { cursor -> if (cursor.moveToFirst()) { val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) downloaded = status == DownloadManager.STATUS_SUCCESSFUL } } } if (downloaded) { GlobalScope.launch { delay(100) val fusedDownload = fusedManagerRepository.getFusedDownload(downloadId) if (!pkgManagerModule.isInstalled(fusedDownload.package_name)) { delay(100) if (DownloadManagerBR.downloadedList.size == fusedDownload.downloadIdMap.size) { fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING) } } } } } app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt +27 −8 Original line number Diff line number Diff line Loading @@ -10,11 +10,13 @@ import androidx.annotation.RequiresApi import androidx.lifecycle.LiveData import foundation.e.apps.manager.database.DatabaseRepository import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.download.DownloadManagerBR import foundation.e.apps.manager.download.data.DownloadProgressLD import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.modules.PWAManagerModule import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.delay import java.io.File import javax.inject.Inject Loading Loading @@ -54,10 +56,17 @@ class FusedManagerImpl @Inject constructor( return databaseRepository.getDownloadLiveList() } suspend fun clearInstallationIssue(fusedDownload: FusedDownload) { flushOldDownload(fusedDownload.package_name) databaseRepository.deleteDownload(fusedDownload) } @OptIn(DelicateCoroutinesApi::class) suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status) { if (status == Status.INSTALLED) { fusedDownload.status = status databaseRepository.updateDownload(fusedDownload) DownloadManagerBR.downloadedList.clear() delay(100) flushOldDownload(fusedDownload.package_name) databaseRepository.deleteDownload(fusedDownload) Loading @@ -67,6 +76,7 @@ class FusedManagerImpl @Inject constructor( databaseRepository.updateDownload(fusedDownload) delay(100) installApp(fusedDownload) delay(100) } } Loading @@ -77,27 +87,34 @@ class FusedManagerImpl @Inject constructor( } } fun installApp(fusedDownload: FusedDownload) { suspend fun installApp(fusedDownload: FusedDownload) { val list = mutableListOf<File>() when (fusedDownload.type) { Type.NATIVE -> { val parentPathFile = File("$cacheDir/${fusedDownload.package_name}") parentPathFile.listFiles()?.let { list.addAll(it) } list.sort() if (list.size != 0) pkgManagerModule.installApplication( list, fusedDownload.package_name ) if (list.size != 0) { pkgManagerModule.installApplication(list, fusedDownload.package_name) } } else -> { Log.d(TAG, "Unsupported application type!") fusedDownload.status = Status.INSTALLATION_ISSUE databaseRepository.updateDownload(fusedDownload) delay(100) } else -> Log.d(TAG, "Unsupported application type!") } } @OptIn(DelicateCoroutinesApi::class) suspend fun cancelDownload(fusedDownload: FusedDownload) { if (fusedDownload.id.isNotBlank()) { fusedDownload.downloadIdMap.forEach { (key, _) -> downloadManager.remove(key) } DownloadProgressLD.setDownloadId(-1) DownloadManagerBR.downloadedList.clear() // Reset the status before deleting download updateDownloadStatus(fusedDownload, fusedDownload.orgStatus) Loading Loading @@ -138,10 +155,12 @@ class FusedManagerImpl @Inject constructor( // Clean old downloads and re-create download dir flushOldDownload(fusedDownload.package_name) File(parentPath).mkdir() DownloadProgressLD.setDownloadId(-1L) File(parentPath).mkdirs() fusedDownload.status = Status.DOWNLOADING databaseRepository.updateDownload(fusedDownload) DownloadProgressLD.setDownloadId(-1) delay(100) fusedDownload.downloadURLList.forEach { count += 1 Loading Loading
app/src/main/java/foundation/e/apps/MainActivity.kt +1 −1 Original line number Diff line number Diff line Loading @@ -143,7 +143,7 @@ class MainActivity : AppCompatActivity() { // Observe and handle downloads viewModel.downloadList.observe(this) { list -> val shouldDownload = list.any { it.status == Status.INSTALLING || it.status == Status.DOWNLOADING it.status == Status.INSTALLING || it.status == Status.DOWNLOADING || it.status == Status.INSTALLED } if (!shouldDownload && list.isNotEmpty()) { for (item in list) { Loading
app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +4 −0 Original line number Diff line number Diff line Loading @@ -37,6 +37,7 @@ import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.modules.DataStoreModule import kotlinx.coroutines.Dispatchers Loading Loading @@ -134,6 +135,9 @@ class MainActivityViewModel @Inject constructor( app.type, appIcon ) if (fusedDownload.status == Status.INSTALLATION_ISSUE) { fusedManagerRepository.clearInstallationIssue(fusedDownload) } fusedManagerRepository.addDownload(fusedDownload) } } Loading
app/src/main/java/foundation/e/apps/manager/download/DownloadManagerBR.kt +2 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ class DownloadManagerBR : BroadcastReceiver() { companion object { private const val TAG = "DownloadManagerBR" val downloadedList = mutableListOf<Long>() } override fun onReceive(context: Context?, intent: Intent?) { Loading @@ -45,6 +46,7 @@ class DownloadManagerBR : BroadcastReceiver() { Log.d(TAG, "onReceive: $action") when (action) { DownloadManager.ACTION_DOWNLOAD_COMPLETE -> { downloadedList.add(id) downloadManagerUtils.updateDownloadStatus(id) } DownloadManager.ACTION_NOTIFICATION_CLICKED -> { Loading
app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt +6 −24 Original line number Diff line number Diff line Loading @@ -18,10 +18,7 @@ package foundation.e.apps.manager.download import android.app.DownloadManager import foundation.e.apps.manager.download.data.DownloadProgressLD import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.Status import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope Loading @@ -30,10 +27,7 @@ import kotlinx.coroutines.launch import javax.inject.Inject class DownloadManagerUtils @Inject constructor( private val downloadManager: DownloadManager, private val downloadManagerQuery: DownloadManager.Query, private val fusedManagerRepository: FusedManagerRepository, private val pkgManagerModule: PkgManagerModule, private val fusedManagerRepository: FusedManagerRepository ) { private val TAG = DownloadManagerUtils::class.java.simpleName Loading @@ -47,24 +41,12 @@ class DownloadManagerUtils @Inject constructor( @DelicateCoroutinesApi fun updateDownloadStatus(downloadId: Long) { var downloaded = false DownloadProgressLD.downloadId.forEach { downloadManager.query(downloadManagerQuery.setFilterById(it)).use { cursor -> if (cursor.moveToFirst()) { val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)) downloaded = status == DownloadManager.STATUS_SUCCESSFUL } } } if (downloaded) { GlobalScope.launch { delay(100) val fusedDownload = fusedManagerRepository.getFusedDownload(downloadId) if (!pkgManagerModule.isInstalled(fusedDownload.package_name)) { delay(100) if (DownloadManagerBR.downloadedList.size == fusedDownload.downloadIdMap.size) { fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING) } } } } }
app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt +27 −8 Original line number Diff line number Diff line Loading @@ -10,11 +10,13 @@ import androidx.annotation.RequiresApi import androidx.lifecycle.LiveData import foundation.e.apps.manager.database.DatabaseRepository import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.download.DownloadManagerBR import foundation.e.apps.manager.download.data.DownloadProgressLD import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.modules.PWAManagerModule import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.delay import java.io.File import javax.inject.Inject Loading Loading @@ -54,10 +56,17 @@ class FusedManagerImpl @Inject constructor( return databaseRepository.getDownloadLiveList() } suspend fun clearInstallationIssue(fusedDownload: FusedDownload) { flushOldDownload(fusedDownload.package_name) databaseRepository.deleteDownload(fusedDownload) } @OptIn(DelicateCoroutinesApi::class) suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status) { if (status == Status.INSTALLED) { fusedDownload.status = status databaseRepository.updateDownload(fusedDownload) DownloadManagerBR.downloadedList.clear() delay(100) flushOldDownload(fusedDownload.package_name) databaseRepository.deleteDownload(fusedDownload) Loading @@ -67,6 +76,7 @@ class FusedManagerImpl @Inject constructor( databaseRepository.updateDownload(fusedDownload) delay(100) installApp(fusedDownload) delay(100) } } Loading @@ -77,27 +87,34 @@ class FusedManagerImpl @Inject constructor( } } fun installApp(fusedDownload: FusedDownload) { suspend fun installApp(fusedDownload: FusedDownload) { val list = mutableListOf<File>() when (fusedDownload.type) { Type.NATIVE -> { val parentPathFile = File("$cacheDir/${fusedDownload.package_name}") parentPathFile.listFiles()?.let { list.addAll(it) } list.sort() if (list.size != 0) pkgManagerModule.installApplication( list, fusedDownload.package_name ) if (list.size != 0) { pkgManagerModule.installApplication(list, fusedDownload.package_name) } } else -> { Log.d(TAG, "Unsupported application type!") fusedDownload.status = Status.INSTALLATION_ISSUE databaseRepository.updateDownload(fusedDownload) delay(100) } else -> Log.d(TAG, "Unsupported application type!") } } @OptIn(DelicateCoroutinesApi::class) suspend fun cancelDownload(fusedDownload: FusedDownload) { if (fusedDownload.id.isNotBlank()) { fusedDownload.downloadIdMap.forEach { (key, _) -> downloadManager.remove(key) } DownloadProgressLD.setDownloadId(-1) DownloadManagerBR.downloadedList.clear() // Reset the status before deleting download updateDownloadStatus(fusedDownload, fusedDownload.orgStatus) Loading Loading @@ -138,10 +155,12 @@ class FusedManagerImpl @Inject constructor( // Clean old downloads and re-create download dir flushOldDownload(fusedDownload.package_name) File(parentPath).mkdir() DownloadProgressLD.setDownloadId(-1L) File(parentPath).mkdirs() fusedDownload.status = Status.DOWNLOADING databaseRepository.updateDownload(fusedDownload) DownloadProgressLD.setDownloadId(-1) delay(100) fusedDownload.downloadURLList.forEach { count += 1 Loading