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

Commit e9b878dd authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch '6043-notification_update_end' into 'release-1.5-rc2'

show notification for ended update process

See merge request !223
parents 030742b4 576bc242
Loading
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -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
    ) {
+35 −3
Original line number Diff line number Diff line
@@ -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
+3 −10
Original line number Diff line number Diff line
@@ -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<FusedApp>,
        authData: AuthData
+37 −0
Original line number Diff line number Diff line
/*
 *  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 <https://www.gnu.org/licenses/>.
 */

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)
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -151,6 +151,7 @@
    <string name="automatically_install_updates_notification_text">App updates will be installed automatically</string>
    <string name="manually_install_updates_notification_text">App updates will not be installed automatically</string>
    <string name="updates_notification_unmetered_network_warning">Waiting for un-metered network</string>
    <string name="message_last_update_triggered">Last update triggered %1$s</string>

    <!-- Trackers & Permission Dialog-->
    <string name="no_permission_found">No runtime android permission found!</string>