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

Commit 9adf00b5 authored by Hasib Prince's avatar Hasib Prince
Browse files

App Lounge: refactoring.

TAG for Logging is moved to companion object as Constant
builder method name changed, starting with build
Year changed in Copyright string
strings resources are marked as non translatable which won't be translated
UpdatesWorker method extraction
updated package for classes UpdateManager,UpdateManager
fixing lint issues
parent ff61740c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -33,7 +33,10 @@ class DownloadManagerBR : BroadcastReceiver() {

    @Inject
    lateinit var downloadManagerUtils: DownloadManagerUtils
    private val TAG = DownloadManagerBR::class.simpleName

    companion object {
        private const val TAG = "DownloadManagerBR"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        val action = intent?.action
+7 −6
Original line number Diff line number Diff line
/*
 * Apps  Quickly and easily install Android apps onto your device!
 * Copyright (C) 2021  E FOUNDATION
 * Copyright (C) 2022  E FOUNDATION
 *
 * 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
@@ -24,7 +24,6 @@ import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.work.ExistingPeriodicWorkPolicy
@@ -37,7 +36,7 @@ import foundation.e.apps.MainActivityViewModel
import foundation.e.apps.R
import foundation.e.apps.databinding.CustomPreferenceBinding
import foundation.e.apps.setup.signin.SignInViewModel
import foundation.e.apps.updates.UpdatesManager
import foundation.e.apps.updates.manager.UpdatesManager
import foundation.e.apps.utils.enums.User
import javax.inject.Inject

@@ -52,7 +51,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
    @Inject
    lateinit var gson: Gson

    private val TAG = SettingsFragment::class.simpleName
    companion object {
        private const val TAG = "SettingsFragment"
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.settings_preferences, rootKey)
@@ -63,8 +64,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
        val showPWAApplications = findPreference<RadioButtonPreference>("showPWAApplications")

        val updateCheckInterval =
            preferenceManager.findPreference<Preference>(getString(R.string.update_check_intervals)) as ListPreference
        updateCheckInterval.setOnPreferenceChangeListener { _, newValue ->
            preferenceManager.findPreference<Preference>(getString(R.string.update_check_intervals))
        updateCheckInterval?.setOnPreferenceChangeListener { _, newValue ->
            Log.d(TAG, "onCreatePreferences: updated Value: $newValue")
            context?.let { UpdatesManager().enqueueWork(it, newValue.toString().toLong(), ExistingPeriodicWorkPolicy.REPLACE) }
            true
+9 −10
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019-2021  E FOUNDATION
 * Copyright (C) 2019-2022  E FOUNDATION
 *
 * 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
@@ -28,12 +28,13 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import foundation.e.apps.MainActivity
import foundation.e.apps.R

class UpdatesNotifier {
    companion object {
        const val UPDATES_NOTIFICATION_ID = 76
        const val UPDATES_NOTIFICATION_CHANNEL_ID = "updates_notification"
        const val UPDATES_NOTIFICATION_CHANNEL_TITLE = "App updates"
        const val UPDATES_NOTIFICATION_CLICK_EXTRA = "updates_notification_click_extra"
        private const val UPDATES_NOTIFICATION_ID = 76
        private const val UPDATES_NOTIFICATION_CHANNEL_ID = "updates_notification"
        private const val UPDATES_NOTIFICATION_CHANNEL_TITLE = "App updates"
    }

    private fun getNotification(
@@ -42,8 +43,7 @@ class UpdatesNotifier {
        installAutomatically: Boolean,
        unmeteredNetworkOnly: Boolean,
        isConnectedToUnmeteredNetwork: Boolean
    ):
        Notification {
    ): Notification {
        val notificationBuilder =
            NotificationCompat.Builder(context, UPDATES_NOTIFICATION_CHANNEL_ID)
        notificationBuilder.setSmallIcon(R.drawable.ic_app_updated_on)
@@ -66,7 +66,7 @@ class UpdatesNotifier {
            )
        }
        if (installAutomatically) {
            notificationBuilder.setContentText(context.getString(R.string.AUTOMATICALLY_INSTALL_updates_notification_text))
            notificationBuilder.setContentText(context.getString(R.string.automatically_install_updates_notification_text))
            if (unmeteredNetworkOnly && !isConnectedToUnmeteredNetwork) {
                notificationBuilder.setSubText(
                    context
@@ -74,7 +74,7 @@ class UpdatesNotifier {
                )
            }
        } else {
            notificationBuilder.setContentText(context.getString(R.string.MANUALLY_INSTALL_updates_notification_text))
            notificationBuilder.setContentText(context.getString(R.string.manually_install_updates_notification_text))
        }
        notificationBuilder.setContentIntent(getClickIntent(context))
        notificationBuilder.setAutoCancel(true)
@@ -98,8 +98,7 @@ class UpdatesNotifier {
                importance
            )
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as
                    NotificationManager
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }
+14 −10
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019-2021  E FOUNDATION
 * Copyright (C) 2019-2022  E FOUNDATION
 *
 * 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
@@ -15,26 +15,30 @@
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.updates
package foundation.e.apps.updates.manager

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.preference.PreferenceManager
import androidx.work.*
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import foundation.e.apps.R
import java.util.concurrent.TimeUnit

class UpdatesManager : BroadcastReceiver() {
    private val TAG = "UpdatesManager"

    companion object {
        const val UPDATES_WORK_NAME = "updates_work"
        private const val TAG = "UpdatesManager"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        if (context != null && intent?.action == Intent.ACTION_BOOT_COMPLETED) {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            val preferences = PreferenceManager.getDefaultSharedPreferences(context)
            val interval =
                preferences.getString(
@@ -45,18 +49,18 @@ class UpdatesManager : BroadcastReceiver() {
        }
    }

    private fun getWorkerConstraints() = Constraints.Builder().apply {
    private fun buildWorkerConstraints() = Constraints.Builder().apply {
        setRequiresBatteryNotLow(true)
        setRequiredNetworkType(NetworkType.CONNECTED)
    }.build()

    private fun getPeriodicWorkRequest(interval: Long): PeriodicWorkRequest {
    private fun buildPeriodicWorkRequest(interval: Long): PeriodicWorkRequest {
        return PeriodicWorkRequest.Builder(
            UpdatesWorker::class.java,
            interval,
            TimeUnit.HOURS
        ).apply {
            setConstraints(getWorkerConstraints())
            setConstraints(buildWorkerConstraints())
        }.build()
    }

@@ -64,7 +68,7 @@ class UpdatesManager : BroadcastReceiver() {
        Log.i(TAG, "UpdatesWorker interval: $interval hours")
        WorkManager.getInstance(context).enqueueUniquePeriodicWork(
            UPDATES_WORK_NAME,
            existingPeriodicWorkPolicy, getPeriodicWorkRequest(interval)
            existingPeriodicWorkPolicy, buildPeriodicWorkRequest(interval)
        )
        Log.i(TAG, "UpdatesWorker started")
    }
+11 −9
Original line number Diff line number Diff line
package foundation.e.apps.updates
package foundation.e.apps.updates.manager

import android.Manifest
import android.content.Context
@@ -23,7 +23,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.updates.manager.UpdatesManagerRepository
import foundation.e.apps.updates.UpdatesNotifier
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.Type
@@ -128,7 +128,6 @@ class UpdatesWorker @AssistedInject constructor(
            fusedManagerRepository.addDownload(fusedDownload)
            Log.d(TAG, "doWork: triggering update for: ${fusedApp.name} downloading...")
            fusedManagerRepository.downloadApp(fusedDownload)
            Log.d(TAG, "doWork: triggering update for: ${fusedApp.name} downloaded...")
        }
        observeFusedDownload()
    }
@@ -136,14 +135,18 @@ class UpdatesWorker @AssistedInject constructor(
    private suspend fun observeFusedDownload() {
        fusedManagerRepository.getDownloadListFlow().collect {
            it.forEach { fusedDownload ->
                checkForInstall(fusedDownload)
            }
        }
    }

    private fun checkForInstall(fusedDownload: FusedDownload) {
        if (fusedDownload.type == Type.NATIVE && fusedDownload.status == Status.INSTALLING && fusedDownload.downloadIdMap.all { it.value }) {
            Log.d(TAG, "doWork: triggering update for: ${fusedDownload.name} installing...")
            fusedManagerRepository.installApp(fusedDownload)
            Log.d(TAG, "doWork: triggering update for: ${fusedDownload.name} installed")
        }
    }
        }
    }

    private fun loadSettings() {
        val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
@@ -208,7 +211,6 @@ class UpdatesWorker @AssistedInject constructor(
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val capabilities =
            connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)

        if (capabilities != null) {
            if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) {
                return true
Loading