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

Verified Commit 55fedc7d authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

refactor: use kotlinx serialization for handling AuthData instead of Gson

parent 709ee8d2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ plugins {
    id 'kotlin-android'
    id 'com.google.devtools.ksp'
    alias libs.plugins.ktlint
    alias libs.plugins.kotlin.serialization
    id 'androidx.navigation.safeargs.kotlin'
    id 'com.google.dagger.hilt.android'
    id 'kotlin-allopen'
@@ -227,6 +228,7 @@ dependencies {
    implementation(libs.kotlinx.coroutines.android)
    testImplementation(libs.kotlinx.coroutines.test)
    testImplementation(libs.kotlin.test)
    implementation(libs.kotlinx.serialization.json)

    // Testing dependencies
    testImplementation(libs.truth)
+6 −4
Original line number Diff line number Diff line
@@ -18,10 +18,12 @@

package foundation.e.apps.data.blockedApps

import com.google.gson.annotations.SerializedName
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AppWarningInfo(
    @SerializedName("not_working_apps") val notWorkingApps: List<String>,
    @SerializedName("zero_privacy_apps") val zeroPrivacyApps: List<String>,
    @SerializedName("third_party_store_apps") val thirdPartyStoreApps: List<String> = emptyList()
    @SerialName("not_working_apps") val notWorkingApps: List<String>,
    @SerialName("zero_privacy_apps") val zeroPrivacyApps: List<String>,
    @SerialName("third_party_store_apps") val thirdPartyStoreApps: List<String> = emptyList(),
)
+3 −3
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
 */
package foundation.e.apps.data.blockedApps

import com.google.gson.Gson
import foundation.e.apps.data.DownloadManager
import foundation.e.apps.data.install.FileManager
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.serialization.json.Json
import timber.log.Timber
import java.io.File
import javax.inject.Inject
@@ -31,7 +31,7 @@ import kotlin.coroutines.resume
@Singleton
class BlockedAppRepository @Inject constructor(
    private val downloadManager: DownloadManager,
    private val gson: Gson,
    private val json: Json,
    @Named("cacheDir") private val cacheDir: String,
) {

@@ -79,7 +79,7 @@ class BlockedAppRepository @Inject constructor(
            Timber.d("Blocked list file exists: ${downloadedFile.exists()}")
            val blockedAppInfoJson = String(downloadedFile.inputStream().readBytes())
            Timber.d("Blocked list file contents: $blockedAppInfoJson")
            gson.fromJson(blockedAppInfoJson, AppWarningInfo::class.java)
            json.decodeFromString<AppWarningInfo>(blockedAppInfoJson)
        } catch (exception: Exception) {
            Timber.e(exception.localizedMessage ?: "", exception)
            AppWarningInfo(listOf(), listOf(), listOf())
+5 −5
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ package foundation.e.apps.data.login

import android.content.Context
import com.aurora.gplayapi.data.models.AuthData
import com.google.gson.Gson
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.ResultStatus
@@ -33,6 +32,7 @@ import foundation.e.apps.data.preference.AppLoungeDataStore
import foundation.e.apps.data.preference.AppLoungePreference
import foundation.e.apps.data.preference.getSync
import foundation.e.apps.data.retryWithBackoff
import kotlinx.serialization.json.Json
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject
@@ -47,7 +47,7 @@ import javax.inject.Singleton
@Singleton
class PlayStoreAuthenticator @Inject constructor(
    @ApplicationContext private val context: Context,
    private val gson: Gson,
    private val json: Json,
    private val appLoungeDataStore: AppLoungeDataStore,
    private val appLoungePreference: AppLoungePreference,
) : StoreAuthenticator, AuthDataValidator {
@@ -122,7 +122,7 @@ class PlayStoreAuthenticator @Inject constructor(
        val authJson = appLoungeDataStore.authData.getSync()
        return if (authJson.isBlank()) null
        else try {
            gson.fromJson(authJson, AuthData::class.java)
            json.decodeFromString<AuthData>(authJson)
        } catch (e: Exception) {
            e.printStackTrace()
            null
@@ -149,8 +149,8 @@ class PlayStoreAuthenticator @Inject constructor(
     * Converting [authData] to Json and back to [AuthData] fixed it.
     */
    private fun formatAuthData(authData: AuthData): AuthData {
        val localAuthDataJson = gson.toJson(authData)
        return gson.fromJson(localAuthDataJson, AuthData::class.java)
        val localAuthDataJson = json.encodeToString(authData)
        return json.decodeFromString<AuthData>(localAuthDataJson)
    }

    private suspend fun getAuthDataAnonymously(): ResultSupreme<AuthData?> {
+6 −8
Original line number Diff line number Diff line
@@ -19,17 +19,17 @@ package foundation.e.apps.data.login.api

import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.data.models.PlayResponse
import com.google.gson.Gson
import foundation.e.apps.data.playstore.utils.CustomAuthValidator
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import java.util.Properties

class AnonymousLoginManager(
    private val gPlayHttpClient: GPlayHttpClient,
    private val nativeDeviceProperty: Properties,
    private val gson: Gson,
    private val json: Json,
) : PlayStoreLoginManager {

    private val tokenUrl: String = "https://eu.gtoken.ecloud.global"
@@ -42,8 +42,9 @@ class AnonymousLoginManager(
    override suspend fun login(): AuthData? {
        var authData: AuthData? = null
        withContext(Dispatchers.IO) {
            val response =
                gPlayHttpClient.postAuth(tokenUrl, gson.toJson(nativeDeviceProperty).toByteArray())
            val response = gPlayHttpClient.postAuth(
                tokenUrl, json.encodeToString(nativeDeviceProperty).toByteArray()
            )
            if (response.code != 200 || !response.isSuccessful) {
                throw Exception(
                    "Error fetching Anonymous credentials\n" +
@@ -55,10 +56,7 @@ class AnonymousLoginManager(
                        }
                )
            } else {
                authData = gson.fromJson(
                    String(response.responseBytes),
                    AuthData::class.java
                )
                authData = json.decodeFromString<AuthData>(String(response.responseBytes))
            }
        }
        return authData
Loading