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

Verified Commit 8fefe7b0 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: centralize install and update work constraints

parent 7990f021
Loading
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -18,15 +18,15 @@
package foundation.e.apps.data.install.updates

import android.content.Context
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import androidx.work.OutOfQuotaPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import foundation.e.apps.data.install.workmanager.WorkRequestConstraints
import foundation.e.apps.data.install.workmanager.WorkType
import timber.log.Timber
import java.util.concurrent.TimeUnit

@@ -46,11 +46,7 @@ object UpdatesWorkManager {

    private fun buildOneTimeWorkRequest(): OneTimeWorkRequest {
        return OneTimeWorkRequest.Builder(UpdatesWorker::class.java).apply {
            setConstraints(
                Constraints.Builder().apply {
                    setRequiredNetworkType(NetworkType.CONNECTED)
                }.build()
            )
            setConstraints(WorkRequestConstraints.build(WorkType.UpdateOneTime))
            setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
            addTag(USER_TAG)
        }.setInputData(
@@ -60,18 +56,13 @@ object UpdatesWorkManager {
        ).build()
    }

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

    private fun buildPeriodicWorkRequest(interval: Long): PeriodicWorkRequest {
        return PeriodicWorkRequest.Builder(
            UpdatesWorker::class.java,
            interval,
            TimeUnit.HOURS
        ).apply {
            setConstraints(buildWorkerConstraints())
            setConstraints(WorkRequestConstraints.build(WorkType.UpdatePeriodic))
            addTag(TAG)
        }.build()
    }
+1 −8
Original line number Diff line number Diff line
package foundation.e.apps.data.install.workmanager

import android.content.Context
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.Operation
import androidx.work.OutOfQuotaPolicy
@@ -21,17 +19,12 @@ object InstallWorkManager {
            .putBoolean(InstallAppWorker.IS_UPDATE_WORK, isUpdateWork)
            .build()

        val constraints = Constraints.Builder()
            .setRequiresStorageNotLow(true)
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build()

        val request = OneTimeWorkRequestBuilder<InstallAppWorker>()
            .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
            .setInputData(inputData)
            .addTag(appInstall.id)
            .addTag(INSTALL_WORK_NAME)
            .setConstraints(constraints)
            .setConstraints(WorkRequestConstraints.build(WorkType.InstallOneTime))
            .build()

        val operation = WorkManager.getInstance(context)
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2026 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
 * 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.data.install.workmanager

import androidx.work.Constraints
import androidx.work.NetworkType

object WorkRequestConstraints {
    fun build(workType: WorkType): Constraints {
        val constraintsBuilder = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .setRequiresStorageNotLow(true)

        if (workType == WorkType.UpdatePeriodic) {
            constraintsBuilder.setRequiresBatteryNotLow(true)
        }

        return constraintsBuilder.build()
    }
}

enum class WorkType {
    InstallOneTime,
    UpdateOneTime,
    UpdatePeriodic
}
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2026 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
 * 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.data.install.workmanager

import androidx.work.NetworkType
import com.google.common.truth.Truth.assertThat
import org.junit.Test

class WorkRequestConstraintsTest {

    @Test
    fun build_returnsConnectedAndStorageNotLow_forInstallOneTime() {
        val constraints = WorkRequestConstraints.build(WorkType.InstallOneTime)

        assertThat(constraints.requiredNetworkType).isEqualTo(NetworkType.CONNECTED)
        assertThat(constraints.requiresStorageNotLow()).isTrue()
        assertThat(constraints.requiresBatteryNotLow()).isFalse()
    }

    @Test
    fun build_returnsConnectedAndStorageNotLow_forUpdateOneTime() {
        val constraints = WorkRequestConstraints.build(WorkType.UpdateOneTime)

        assertThat(constraints.requiredNetworkType).isEqualTo(NetworkType.CONNECTED)
        assertThat(constraints.requiresStorageNotLow()).isTrue()
        assertThat(constraints.requiresBatteryNotLow()).isFalse()
    }

    @Test
    fun build_returnsConnectedAndBatteryNotLow_forUpdatePeriodic() {
        val constraints = WorkRequestConstraints.build(WorkType.UpdatePeriodic)

        assertThat(constraints.requiredNetworkType).isEqualTo(NetworkType.CONNECTED)
        assertThat(constraints.requiresStorageNotLow()).isTrue()
        assertThat(constraints.requiresBatteryNotLow()).isTrue()
    }

    @Test
    fun build_setsConnectedNetworkAndStorageNowLow_forAllWorkTypes() {
        WorkType.entries.forEach { workType ->
            val constraints = WorkRequestConstraints.build(workType)

            assertThat(constraints.requiredNetworkType).isEqualTo(NetworkType.CONNECTED)
            assertThat(constraints.requiresStorageNotLow()).isTrue()
        }
    }
}