From 7eacc082e560fac63bc4413a3ca566972b05ed03 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Thu, 5 May 2022 16:10:42 +0600 Subject: [PATCH] App Lounge: fixed notification showing on cancel --- .../java/foundation/e/apps/AppLoungeApplication.kt | 2 ++ app/src/main/java/foundation/e/apps/MainActivity.kt | 4 ++-- .../java/foundation/e/apps/MainActivityViewModel.kt | 2 ++ .../manager/database/fusedDownload/FusedDownloadDAO.kt | 2 +- .../e/apps/manager/workmanager/InstallWorkManager.kt | 10 ++++++++-- .../foundation/e/apps/updates/manager/UpdatesWorker.kt | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt index 4ecf0bda4..00a02f1a8 100644 --- a/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt +++ b/app/src/main/java/foundation/e/apps/AppLoungeApplication.kt @@ -24,6 +24,7 @@ import androidx.work.Configuration import dagger.hilt.android.HiltAndroidApp import foundation.e.apps.manager.pkg.PkgManagerBR import foundation.e.apps.manager.pkg.PkgManagerModule +import foundation.e.apps.manager.workmanager.InstallWorkManager import kotlinx.coroutines.DelicateCoroutinesApi import java.util.concurrent.Executors import javax.inject.Inject @@ -40,6 +41,7 @@ class AppLoungeApplication : Application(), Configuration.Provider { override fun onCreate() { super.onCreate() + InstallWorkManager.context = this // Register broadcast receiver for package manager val pkgManagerBR = object : PkgManagerBR() {} registerReceiver(pkgManagerBR, pkgManagerModule.getFilter()) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index ba46db295..3c6d100cd 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -239,7 +239,7 @@ class MainActivity : AppCompatActivity() { return@launch } viewModel.updateAwaiting(it) - InstallWorkManager.enqueueWork(applicationContext, it) + InstallWorkManager.enqueueWork(it) Log.d(TAG, "===> onCreate: AWAITING ${it.name}") } } @@ -251,7 +251,7 @@ class MainActivity : AppCompatActivity() { lifecycleScope.launch { val fusedDownload = viewModel.updateAwaitingForPurchasedApp(it) if (fusedDownload != null) { - InstallWorkManager.enqueueWork(applicationContext, fusedDownload) + InstallWorkManager.enqueueWork(fusedDownload) } else { showSnackbarMessage(getString(R.string.paid_app_anonymous_message)) } diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index 73ff62972..32abd84e9 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -40,6 +40,7 @@ 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.manager.pkg.PkgManagerModule +import foundation.e.apps.manager.workmanager.InstallWorkManager import foundation.e.apps.utils.enums.Origin import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type @@ -271,6 +272,7 @@ class MainActivityViewModel @Inject constructor( val fusedDownload = fusedManagerRepository.getFusedDownload(packageName = app.package_name) fusedManagerRepository.cancelDownload(fusedDownload) + InstallWorkManager.cancelWork(app.name) } } diff --git a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownloadDAO.kt b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownloadDAO.kt index cdb83411b..a5311262b 100644 --- a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownloadDAO.kt +++ b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownloadDAO.kt @@ -11,7 +11,7 @@ import androidx.room.Update @Dao interface FusedDownloadDAO { - @Insert(onConflict = OnConflictStrategy.IGNORE) + @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun addDownload(fusedDownload: FusedDownload) @Query("SELECT * FROM fuseddownload") diff --git a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallWorkManager.kt b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallWorkManager.kt index b94135fbd..809884ed0 100644 --- a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallWorkManager.kt +++ b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallWorkManager.kt @@ -1,6 +1,6 @@ package foundation.e.apps.manager.workmanager -import android.content.Context +import android.app.Application import androidx.work.Data import androidx.work.ExistingWorkPolicy import androidx.work.OneTimeWorkRequestBuilder @@ -9,7 +9,9 @@ import foundation.e.apps.manager.database.fusedDownload.FusedDownload object InstallWorkManager { const val INSTALL_WORK_NAME = "APP_LOUNGE_INSTALL_APP" - fun enqueueWork(context: Context, fusedDownload: FusedDownload) { + lateinit var context: Application + + fun enqueueWork(fusedDownload: FusedDownload) { WorkManager.getInstance(context).enqueueUniqueWork( INSTALL_WORK_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE, @@ -21,4 +23,8 @@ object InstallWorkManager { .build() ) } + + fun cancelWork(tag: String) { + WorkManager.getInstance(context).cancelAllWorkByTag(tag) + } } diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt index 2aa21aa8a..2244d136e 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt @@ -146,7 +146,7 @@ class UpdatesWorker @AssistedInject constructor( TAG, "startUpdateProcess: Enqueued for update: ${fusedDownload.name} ${fusedDownload.id} ${fusedDownload.status}" ) - InstallWorkManager.enqueueWork(context, fusedDownload) + InstallWorkManager.enqueueWork(fusedDownload) } } -- GitLab