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

Commit 13c267f2 authored by Hasib Prince's avatar Hasib Prince
Browse files

App lounge: install worker added

parent cdda2a9b
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
package foundation.e.apps.manager.database

import androidx.lifecycle.LiveData
import androidx.lifecycle.asFlow
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.database.fusedDownload.FusedDownloadDAO
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class DatabaseRepository @Inject constructor(
@@ -28,4 +30,12 @@ class DatabaseRepository @Inject constructor(
    suspend fun deleteDownload(fusedDownload: FusedDownload) {
        return fusedDownloadDAO.deleteDownload(fusedDownload)
    }

    suspend fun getDownloadById(id: String): FusedDownload? {
        return fusedDownloadDAO.getDownloadById(id)
    }

    suspend fun getDownloadFlowById(id: String): Flow<FusedDownload> {
        return fusedDownloadDAO.getDownloadFlowById(id).asFlow()
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -20,6 +20,12 @@ interface FusedDownloadDAO {
    @Query("SELECT * FROM fuseddownload")
    suspend fun getDownloadList(): List<FusedDownload>

    @Query("SELECT * FROM fuseddownload where id = :id")
    suspend fun getDownloadById(id: String): FusedDownload?

    @Query("SELECT * FROM fuseddownload where id = :id")
    suspend fun getDownloadFlowById(id: String): LiveData<FusedDownload>

    @Update
    suspend fun updateDownload(fusedDownload: FusedDownload)

+64 −0
Original line number Diff line number Diff line
package foundation.e.apps.manager.workmanager

import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.google.gson.Gson
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import foundation.e.apps.manager.database.DatabaseRepository
import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.manager.pkg.PkgManagerModule
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.modules.PWAManagerModule
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import javax.inject.Named

class InstallAppWorker @AssistedInject constructor(
    @Assisted private val context: Context,
    @Assisted private val params: WorkerParameters,
    private val packagemanagerModule: PkgManagerModule,
    private val pwaManagerModule: PWAManagerModule,
    private val databaseRepository: DatabaseRepository,
    private val fusedManagerRepository: FusedManagerRepository
) : CoroutineWorker(context, params) {

    companion object {
        private const val TAG = "InstallWorker"
        const val INPUT_DATA_FUSED_DOWNLOAD = "input_data_fused_download"
    }

    override suspend fun doWork(): Result {
        val fusedDownloadString = params.inputData.getString(INPUT_DATA_FUSED_DOWNLOAD) ?: ""
        Log.d(TAG, "Fused download name $fusedDownloadString")

        val fusedDownload = databaseRepository.getDownloadById(fusedDownloadString)
        fusedDownload?.let {
            fusedManagerRepository.downloadApp(it)
            var isDownloading = true
            databaseRepository.getDownloadFlowById(it.id).collect { fusedDownload ->
                when(it.status) {
                    Status.INSTALLING -> {
                        fusedManagerRepository.installApp(fusedDownload)
                        isDownloading = false
                    }
                    Status.INSTALLED,Status.INSTALLATION_ISSUE -> {
                        isDownloading = false
                    }
                    else -> {
                        Log.wtf(TAG, "${fusedDownload.name} is in wrong state")
                    }
                }
            }

            while (isDownloading) {
                delay(2000)
            }
        }

        return Result.success()
    }

}
 No newline at end of file
+2 −1
Original line number Diff line number Diff line
@@ -27,5 +27,6 @@ enum class Status {
    UNINSTALLING,
    QUEUED,
    BLOCKED,
    INSTALLATION_ISSUE
    INSTALLATION_ISSUE,
    AWAITING
}