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

Commit 2b1709b9 authored by Saalim Quadri's avatar Saalim Quadri
Browse files

Merge branch '3554-updates-check' into 'main'

feat: Remove "daily" choice for automatic update and weekly as default

See merge request !582
parents 1e2f1983 b0cd839d
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ class AppLoungeApplication : Application(), Configuration.Provider {
            })
        }

        appLoungePreference.migrateAnonymousUserUpdateInterval()
        UpdatesWorkManager.enqueueWork(
            this,
            appLoungePreference.getUpdateInterval(),
+53 −5
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import foundation.e.apps.R
import foundation.e.apps.data.Constants.PREFERENCE_SHOW_FOSS
import foundation.e.apps.data.Constants.PREFERENCE_SHOW_GPLAY
import foundation.e.apps.data.Constants.PREFERENCE_SHOW_PWA
import foundation.e.apps.data.enums.User
import javax.inject.Inject
import javax.inject.Singleton

@@ -35,7 +36,8 @@ import javax.inject.Singleton
@Singleton
@OpenForTesting
class AppLoungePreference @Inject constructor(
    @ApplicationContext private val context: Context
    @ApplicationContext private val context: Context,
    private val appLoungeDataStore: AppLoungeDataStore
) {

    private val preferenceManager = PreferenceManager.getDefaultSharedPreferences(context)
@@ -63,13 +65,59 @@ class AppLoungePreference @Inject constructor(
    fun enableOpenSource() = preferenceManager.edit { putBoolean(PREFERENCE_SHOW_FOSS, true) }
    fun enablePwa() = preferenceManager.edit { putBoolean(PREFERENCE_SHOW_PWA, true) }

    fun getUpdateInterval() = preferenceManager.getString(
    fun getUpdateInterval(): Long {
        val currentUser = appLoungeDataStore.getUserType()
        return when (currentUser) {
            User.ANONYMOUS -> preferenceManager.getString(
                context.getString(R.string.update_check_intervals_anonymous),
                context.getString(R.string.preference_update_interval_default_anonymous)
            )!!.toLong()
            else -> preferenceManager.getString(
                context.getString(R.string.update_check_intervals),
                context.getString(R.string.preference_update_interval_default)
            )!!.toLong()
        }
    }

    fun shouldUpdateAppsFromOtherStores() = preferenceManager.getBoolean(
        context.getString(R.string.update_apps_from_other_stores),
        true
    )

    fun migrateAnonymousUserUpdateInterval() {
        val updateIntervals = context.resources.getStringArray(R.array.update_interval_values)
        val daily = updateIntervals[0]   // 24
        val weekly = updateIntervals[1]  // 168
        val monthly = updateIntervals[2] // 720
        val migrationCompleted = preferenceManager.getBoolean(
            context.getString(R.string.anonymous_update_migration_completed),
            false
        )

        if (migrationCompleted) return

        if (appLoungeDataStore.getUserType() == User.ANONYMOUS) {
            val currentInterval = preferenceManager.getString(
                context.getString(R.string.update_check_intervals),
                null
            )
            currentInterval?.let { interval ->
                val newVal = when (interval) {
                    daily -> weekly
                    weekly, monthly -> interval
                    else -> context.getString(R.string.preference_update_interval_default_anonymous)
                }
                preferenceManager.edit {
                    putString(
                        context.getString(R.string.update_check_intervals_anonymous),
                        newVal
                    )
                }
            }
        }

        preferenceManager.edit {
            putBoolean(context.getString(R.string.anonymous_update_migration_completed), true)
        }
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -21,15 +21,20 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.ExistingPeriodicWorkPolicy
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.data.preference.AppLoungePreference
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
class UpdatesBroadcastReceiver : BroadcastReceiver() {

    @Inject
    lateinit var appLoungePreference: AppLoungePreference

    override fun onReceive(context: Context, intent: Intent) {
        Timber.d("onReceive: ${intent.action}")
        if (intent.action in listOf(Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MY_PACKAGE_REPLACED)) {
            val appLoungePreference = AppLoungePreference(context)
            val interval = appLoungePreference.getUpdateInterval()
            UpdatesWorkManager.enqueueWork(context, interval, ExistingPeriodicWorkPolicy.REPLACE)
        }
+24 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.preference.CheckBoxPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
import androidx.preference.PreferenceFragmentCompat
@@ -99,8 +100,10 @@ class SettingsFragment : PreferenceFragmentCompat() {
        troubleShootPreference = findPreference(getString(R.string.having_troubles))

        val updateCheckInterval =
            preferenceManager.findPreference<Preference>(getString(R.string.update_check_intervals))
        updateCheckInterval?.setOnPreferenceChangeListener { _, newValue ->
            preferenceManager.findPreference<ListPreference>(getString(R.string.update_check_intervals))
        val updateCheckIntervalAnonymous =
            preferenceManager.findPreference<ListPreference>(getString(R.string.update_check_intervals_anonymous))
        val updateChangeListener = { _: Preference, newValue: Any ->
            Timber.d("onCreatePreferences: updated Value: $newValue")
            context?.let {
                UpdatesWorkManager.enqueueWork(
@@ -111,6 +114,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
            }
            true
        }
        updateCheckInterval?.setOnPreferenceChangeListener(updateChangeListener)
        updateCheckIntervalAnonymous?.setOnPreferenceChangeListener(updateChangeListener)
        configureUpdatePreferencesForUser(updateCheckInterval, updateCheckIntervalAnonymous)

        val versionInfo = findPreference<LongPressPreference>("versionInfo")
        versionInfo?.apply {
@@ -318,4 +324,20 @@ class SettingsFragment : PreferenceFragmentCompat() {
            stores.disableStore(source)
        }
    }

    private fun configureUpdatePreferencesForUser(
        updateCheckInterval: ListPreference?,
        updateCheckIntervalAnonymous: ListPreference?
    ) {
        when (user) {
            User.ANONYMOUS -> {
                updateCheckInterval?.isVisible = false
                updateCheckIntervalAnonymous?.isVisible = true
            }
            else -> {
                updateCheckInterval?.isVisible = true
                updateCheckIntervalAnonymous?.isVisible = false
            }
        }
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -30,4 +30,14 @@
        <item>720</item>
    </string-array>

    <string-array name="update_interval_names_anonymous">
        <item>@string/weekly</item>
        <item>@string/monthly</item>
    </string-array>

    <string-array name="update_interval_values_anonymous">
        <item>168</item>
        <item>720</item>
    </string-array>

</resources>
 No newline at end of file
Loading