From 576bc24297292c04d2245fb9cf49b8c6bb892f20 Mon Sep 17 00:00:00 2001 From: hasibprince Date: Thu, 20 Oct 2022 11:40:28 +0600 Subject: [PATCH] notification is added for ended update --- .../manager/workmanager/InstallAppWorker.kt | 27 +++++++++++++ .../e/apps/updates/UpdatesNotifier.kt | 38 +++++++++++++++++-- .../e/apps/updates/manager/UpdatesWorker.kt | 13 ++----- .../e/apps/utils/modules/DataStoreManager.kt | 37 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/foundation/e/apps/utils/modules/DataStoreManager.kt diff --git a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt index 0a9ca9dcb..8d8135d4b 100644 --- a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt +++ b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt @@ -36,8 +36,11 @@ import foundation.e.apps.R import foundation.e.apps.manager.database.DatabaseRepository 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.updates.UpdatesNotifier import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type +import foundation.e.apps.utils.modules.DataStoreManager import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -46,6 +49,8 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.sync.Mutex import timber.log.Timber +import java.text.SimpleDateFormat +import java.util.Date import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -58,9 +63,12 @@ class InstallAppWorker @AssistedInject constructor( private val fusedManagerRepository: FusedManagerRepository, private val downloadManager: DownloadManager, private val downloadManagerQuery: DownloadManager.Query, + private val packageManagerModule: PkgManagerModule, + private val dataStoreManager: DataStoreManager ) : CoroutineWorker(context, params) { private var isDownloading: Boolean = false + private var isItUpdateWork = false companion object { private const val TAG = "InstallWorker" @@ -89,6 +97,8 @@ class InstallAppWorker @AssistedInject constructor( fusedDownload = databaseRepository.getDownloadById(fusedDownloadString) Timber.d(">>> dowork started for Fused download name " + fusedDownload?.name + " " + fusedDownloadString) fusedDownload?.let { + isItUpdateWork = packageManagerModule.isInstalled(it.packageName) + if (fusedDownload.status != Status.AWAITING) { return Result.success() } @@ -112,11 +122,28 @@ class InstallAppWorker @AssistedInject constructor( fusedManagerRepository.installationIssue(it) } } finally { + if (isItUpdateWork && databaseRepository.getDownloadList().isEmpty()) { // show notification for ended update + showNotificationOnUpdateEnded() + } + Timber.d("doWork: RESULT SUCCESS: ${fusedDownload?.name}") return Result.success() } } + private fun showNotificationOnUpdateEnded() { + val date = Date(System.currentTimeMillis()) + val dateFormat = + SimpleDateFormat("dd/MM/yyyy-HH:mm", dataStoreManager.getAuthData().locale) + + UpdatesNotifier.showNotification( + context, context.getString(R.string.update), + context.getString( + R.string.message_last_update_triggered, dateFormat.format(date) + ) + ) + } + private suspend fun startAppInstallationProcess( fusedDownload: FusedDownload ) { diff --git a/app/src/main/java/foundation/e/apps/updates/UpdatesNotifier.kt b/app/src/main/java/foundation/e/apps/updates/UpdatesNotifier.kt index 61a5116eb..1b6a7a57b 100644 --- a/app/src/main/java/foundation/e/apps/updates/UpdatesNotifier.kt +++ b/app/src/main/java/foundation/e/apps/updates/UpdatesNotifier.kt @@ -43,9 +43,7 @@ object UpdatesNotifier { isConnectedToUnmeteredNetwork: Boolean ): Notification { val notificationBuilder = - NotificationCompat.Builder(context, UPDATES_NOTIFICATION_CHANNEL_ID) - notificationBuilder.setSmallIcon(R.drawable.ic_app_updated_on) - notificationBuilder.priority = NotificationCompat.PRIORITY_DEFAULT + createNotificationBuilder(context) when (numberOfApps) { 0 -> { @@ -88,6 +86,15 @@ object UpdatesNotifier { return notificationBuilder.build() } + private fun createNotificationBuilder(context: Context): NotificationCompat.Builder { + val notificationBuilder = + NotificationCompat.Builder(context, UPDATES_NOTIFICATION_CHANNEL_ID) + + notificationBuilder.setSmallIcon(R.drawable.ic_app_updated_on) + notificationBuilder.priority = NotificationCompat.PRIORITY_DEFAULT + return notificationBuilder + } + private fun getClickIntent(context: Context): PendingIntent { val intent = Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TASK @@ -132,6 +139,31 @@ object UpdatesNotifier { } } + fun showNotification( + context: Context, + title: String, + message: String, + ) { + with(NotificationManagerCompat.from(context)) { + createNotificationChannel(context) + notify( + UPDATES_NOTIFICATION_ID, + getNotification( + context, + title, + message + ) + ) + } + } + + fun getNotification(context: Context, title: String, message: String): Notification { + val notificationBuilder = createNotificationBuilder(context) + notificationBuilder.setContentTitle(title) + notificationBuilder.setContentText(message) + return notificationBuilder.build() + } + fun cancelNotification(context: Context) { val notificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 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 067c2a85b..f38fd825b 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 @@ -13,7 +13,6 @@ import androidx.preference.PreferenceManager import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import com.aurora.gplayapi.data.models.AuthData -import com.google.gson.Gson import dagger.assisted.Assisted import dagger.assisted.AssistedInject import foundation.e.apps.R @@ -30,7 +29,7 @@ import foundation.e.apps.utils.enums.ResultStatus import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.eventBus.AppEvent import foundation.e.apps.utils.eventBus.EventBus -import foundation.e.apps.utils.modules.DataStoreModule +import foundation.e.apps.utils.modules.DataStoreManager import kotlinx.coroutines.delay import timber.log.Timber import java.io.ByteArrayOutputStream @@ -43,8 +42,7 @@ class UpdatesWorker @AssistedInject constructor( private val updatesManagerRepository: UpdatesManagerRepository, private val fusedAPIRepository: FusedAPIRepository, private val fusedManagerRepository: FusedManagerRepository, - private val dataStoreModule: DataStoreModule, - private val gson: Gson, + private val dataStoreManager: DataStoreManager, ) : CoroutineWorker(context, params) { companion object { const val IS_AUTO_UPDATE = "IS_AUTO_UPDATE" @@ -76,7 +74,7 @@ class UpdatesWorker @AssistedInject constructor( private suspend fun checkForUpdates() { loadSettings() val isConnectedToUnmeteredNetwork = isConnectedToUnmeteredNetwork(applicationContext) - val authData = getAuthData() + val authData = dataStoreManager.getAuthData() var resultStatus: ResultStatus val updateData = updatesManagerRepository.getUpdates(authData) @@ -157,11 +155,6 @@ class UpdatesWorker @AssistedInject constructor( } } - private fun getAuthData(): AuthData { - val authDataJson = dataStoreModule.getAuthDataSync() - return gson.fromJson(authDataJson, AuthData::class.java) - } - private suspend fun startUpdateProcess( appsNeededToUpdate: List, authData: AuthData diff --git a/app/src/main/java/foundation/e/apps/utils/modules/DataStoreManager.kt b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreManager.kt new file mode 100644 index 000000000..10a25fbe9 --- /dev/null +++ b/app/src/main/java/foundation/e/apps/utils/modules/DataStoreManager.kt @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2022 Murena SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.utils.modules + +import com.aurora.gplayapi.data.models.AuthData +import com.google.gson.Gson +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class DataStoreManager @Inject constructor() { + @Inject + lateinit var dataStoreModule: DataStoreModule + + @Inject + lateinit var gson: Gson + + fun getAuthData(): AuthData { + val authDataJson = dataStoreModule.getAuthDataSync() + return gson.fromJson(authDataJson, AuthData::class.java) + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bda2fcfa9..48f1865dd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -151,6 +151,7 @@ App updates will be installed automatically App updates will not be installed automatically Waiting for un-metered network + Last update triggered %1$s No runtime android permission found! -- GitLab