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

Commit 62732cd4 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

feat:4025: Improve errors handling and API

parent 1609eb48
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ class GetAppDetailsUseCase @Inject constructor(
    suspend operator fun invoke(packageName: String, source: Source? = null): Application {
        if (blockedAppRepository.isBlockedApp(packageName)) {
            throw UninstallableApp("Can't install $packageName, it is in NotWorkingApps list.").let {
                Timber.i(it)
                Timber.w(it)
                it
            }
        }
+13 −0
Original line number Diff line number Diff line
package foundation.e.apps.domain.install

import foundation.e.apps.data.Stores
import foundation.e.apps.data.enums.Source
import javax.inject.Inject

class GetEnabledSearchSourcesUseCase @Inject constructor(
    private val stores: Stores,
) {
    operator fun invoke(): List<Source> {
        return stores.getEnabledSearchSources()
    }
}
+16 −0
Original line number Diff line number Diff line
package foundation.e.apps.domain.install

import foundation.e.apps.data.enums.User
import foundation.e.apps.data.preference.AppLoungeDataStore
import kotlinx.coroutines.flow.first
import javax.inject.Inject

class GetGPlayAccountTypeUseCase @Inject constructor(
    private val appLoungeDataStore: AppLoungeDataStore,
) {
    suspend operator fun invoke(): User? {
        return runCatching {
            User.valueOf(appLoungeDataStore.userType.first())
        }.getOrNull()
    }
}
+46 −1
Original line number Diff line number Diff line
@@ -23,16 +23,23 @@ import android.os.IBinder
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.data.enums.Source
import foundation.e.apps.data.enums.Status
import foundation.e.apps.data.enums.User
import foundation.e.apps.domain.install.GetEnabledSearchSourcesUseCase
import foundation.e.apps.domain.install.GetGPlayAccountTypeUseCase
import foundation.e.apps.domain.install.InstallAppByIdUseCase
import foundation.e.apps.installapp.AppInstallationStatus
import foundation.e.apps.installapp.GPlayAccountTypes
import foundation.e.apps.installapp.IInstallAppCallback
import foundation.e.apps.installapp.IInstallAppService
import foundation.e.apps.installapp.SearchableSources
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

@AndroidEntryPoint
@@ -41,10 +48,26 @@ class InstallAppService : LifecycleService() {
    @Inject
    lateinit var installAppByIdUseCase: InstallAppByIdUseCase

    @Inject
    lateinit var getGPlayAccountTypeUseCase: GetGPlayAccountTypeUseCase

    @Inject
    lateinit var getEnabledSearchSourcesUseCase: GetEnabledSearchSourcesUseCase

    private val binder = object : IInstallAppService.Stub() {
        private var installAppJob: Job? = null

        override fun installAppId(packageName: String, callback: IInstallAppCallback) {
        override fun getGPlayAccountType(): String {
            return runBlocking {
                serializeUserForInstallAppLib(getGPlayAccountTypeUseCase())
            }
        }

        override fun getEnabledSearchSources(): List<String> {
            return serializeSourcesForInstallAppLib(getEnabledSearchSourcesUseCase())
        }

        override fun installApp(packageName: String, callback: IInstallAppCallback) {
            if (installAppJob?.isActive == true) {
                callback.onDone(AppInstallationStatus.SERVICE_BUSY.name)
                return
@@ -73,6 +96,8 @@ class InstallAppService : LifecycleService() {
    }
}

// Mapper from apps.domain to install-app-lib.

fun Status?.toAppInstallationStatus(): AppInstallationStatus {
    return when (this) {
        Status.UPDATABLE,
@@ -93,3 +118,23 @@ fun Status?.toAppInstallationStatus(): AppInstallationStatus {
        null -> AppInstallationStatus.UNKNOWN
    }
}

fun serializeUserForInstallAppLib(userType: User?): String {
    return when (userType) {
        User.NO_GOOGLE -> GPlayAccountTypes.NO_GOOGLE
        User.ANONYMOUS -> GPlayAccountTypes.ANONYMOUS
        User.GOOGLE -> GPlayAccountTypes.GOOGLE
        null -> GPlayAccountTypes.NOT_CONFIGURED
    }.name
}

fun serializeSourcesForInstallAppLib(sources: List<Source>): List<String> {
    return sources.mapNotNull { source ->
        when (source) {
            Source.OPEN_SOURCE -> SearchableSources.OPEN_SOURCE
            Source.PWA -> SearchableSources.PWA
            Source.SYSTEM_APP -> null
            Source.PLAY_STORE -> SearchableSources.PLAY_STORE
        }?.name
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ package foundation.e.apps.installapp;
import foundation.e.apps.installapp.IInstallAppCallback;

interface IInstallAppService {
    void installAppId(String packageName, IInstallAppCallback callback);
    List<String> getEnabledSearchSources();
    String getGPlayAccountType();
    void installApp(String packageName, IInstallAppCallback callback);
    void cancelCurrentInstallation();
}
Loading