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

Commit cb7665d4 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Remove dummy search

We used to make a dummy search to detect when our
auth token needed to be refreshed.

This mechanism is not needed anymore since we are not
using the Authenticated API anymore to make searches.
parent 2c0fb94a
Loading
Loading
Loading
Loading
Loading
+0 −49
Original line number Diff line number Diff line
@@ -37,17 +37,10 @@ import foundation.e.apps.data.application.utils.toApplication
import foundation.e.apps.data.enums.Origin
import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.login.exceptions.CleanApkIOException
import foundation.e.apps.data.login.exceptions.GPlayIOException
import foundation.e.apps.data.preference.AppLoungePreference
import foundation.e.apps.utils.eventBus.AppEvent
import foundation.e.apps.utils.eventBus.EventBus
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@@ -66,15 +59,6 @@ class SearchApiImpl @Inject constructor(
    @ApplicationContext
    lateinit var context: Context

    companion object {
        private const val KEYWORD_TEST_SEARCH = "facebook"

        private val DUMMY_SEARCH_EXPECTED_APPS = listOf(
            "Facebook" to "com.facebook.katana",
            "Messenger" to "com.facebook.orca"
        )
    }

    override fun getSelectedAppTypes(): List<String> {
        val selectedAppTypes = mutableListOf<String>()
        if (appLoungePreference.isGplaySelected()) selectedAppTypes.add(APP_TYPE_ANY)
@@ -340,8 +324,6 @@ class SearchApiImpl @Inject constructor(
        nextPageSubBundle: Set<SearchBundle.SubBundle>?
    ): GplaySearchResult {
        val result = handleNetworkResult {
            coroutineScope { launch(Dispatchers.IO) { doDummySearch() } }

            val searchResults =
                appSources.gplayRepo.getSearchResult(query, nextPageSubBundle?.toMutableSet())

@@ -367,29 +349,6 @@ class SearchApiImpl @Inject constructor(
        )
    }

    // Initiate a dummy search to ensure Google Play returns enough results for the search query
    private suspend fun doDummySearch() {
        val (searchedApps, _) = appSources.gplayRepo.getSearchResult(KEYWORD_TEST_SEARCH, null)

        if (searchedApps.isEmpty()) {
            Timber.d("Search returned empty results, refreshing token...")
            refreshToken()
            return
        }

        val dummySearchPackageNames = DUMMY_SEARCH_EXPECTED_APPS.map { it.second }
        val searchedAppsPackageNames = searchedApps.map { it.packageName }

        val isSearchContainingResults =
            searchedAppsPackageNames.containsAll(dummySearchPackageNames)

        if (!isSearchContainingResults) {
            Timber.d("Search didn't return enough results, refreshing token...")
            refreshToken()
            return
        }
    }

    /*
     * This function will replace a GPlay app with F-Droid app if exists,
     * else will show the GPlay app itself.
@@ -414,12 +373,4 @@ class SearchApiImpl @Inject constructor(
            return gPlayApps.map { it.toApplication(context) }
        }
    }

    private fun refreshToken() {
        MainScope().launch {
            EventBus.invokeEvent(
                AppEvent.InvalidAuthEvent(AuthObject.GPlayAuth::class.java.simpleName)
            )
        }
    }
}
+0 −22
Original line number Diff line number Diff line
@@ -286,26 +286,4 @@ class SearchApiImplTest {
        val size = searchResultLiveData.data?.first?.size ?: -2
        assertEquals("getSearchResult", 4, size)
    }

    @Test
    fun testSearchResultWhenDataIsLimited() = runTest {
        preferenceManagerModule.isGplaySelectedFake = true
        formatterMocked.`when`<String> { Formatter.formatFileSize(any(), any()) }.thenReturn("15MB")
        Mockito.`when`(gPlayAPIRepository.getSearchResult(anyString(), eq(null)))
            .thenReturn(Pair(emptyList(), mutableSetOf()))
        Mockito.`when`(cleanApkAppsRepository.getAppDetails(any())).thenReturn(null)

        var isEventBusTriggered = false
        val job = launch {
            EventBus.events.collect {
                isEventBusTriggered = true
            }
        }

        fusedAPIImpl.getGplaySearchResult("anything", null)
        delay(500)
        job.cancel()

        assert(isEventBusTriggered)
    }
}