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

Commit 73812170 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

fix: clear cookie if the app is updated

parent d2820027
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -10,12 +10,16 @@ import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.content.SyncResult
import android.content.pm.PackageManager
import android.net.Uri
import android.os.RemoteException
import android.provider.CalendarContract
import android.provider.ContactsContract
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.edit
import androidx.core.content.pm.PackageInfoCompat
import androidx.preference.PreferenceManager
import at.bitfire.dav4jvm.*
import at.bitfire.dav4jvm.exception.*
import at.bitfire.dav4jvm.property.GetCTag
@@ -175,6 +179,8 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L

    val workDispatcher = getWorkDispatcher()

    private val PREF_LAST_VERSION_CODE = "LAST_VERSION_CODE"

    /**
     * Call performSync with default retry values
     */
@@ -195,6 +201,34 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
        refreshAuthTokenAndSync(authState)
    }

    private fun packageChanged(): Boolean {
        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        val currentVersion = getCurrentVersionCode()
        val savedVersion = prefs.getLong(PREF_LAST_VERSION_CODE, 0L)
        if (savedVersion != currentVersion) {
            Logger.log.warning("App has been updated!" +
                    "Previous: $savedVersion, Current: $currentVersion")
            prefs.edit {
                putLong(PREF_LAST_VERSION_CODE, currentVersion)
            }
            // Clear cookie since the app is updated.
            Logger.log.warning("Clear cookies for ${accountSettings.credentials().userName}")
            accountSettings.clearCookie()
            return true
        }

        return false
    }

    private fun getCurrentVersionCode(): Long {
        return try {
            val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            PackageInfoCompat.getLongVersionCode(packageInfo)
        } catch (e: PackageManager.NameNotFoundException) {
            0
        }
    }

    private fun refreshAuthTokenAndSync(authState: AuthState) {
        val tokenRequest = authState.createTokenRefreshRequest()
        val clientSecretString = accountSettings.credentials().clientSecret
@@ -388,6 +422,9 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L

                is UnauthorizedException -> {
                    Logger.log.log(Level.WARNING, "Got 401 Unauthorized", e)
                    if (packageChanged()) {
                        return@unwrapExceptions
                    }
                    val credentials = accountSettings.credentials()
                    if (credentials.authState == null) {
                        credentials.passwordNeedsUpdate = true