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

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

clear cookie if the app is updated

parent ed5d22ad
Loading
Loading
Loading
Loading
Loading
+36 −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
@@ -179,6 +183,8 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
     * Call performSync with default retry values
     */
    fun performSync() {
        packageChanged()

        val credentials = accountSettings.credentials()
        if (credentials.passwordNeedsUpdate) {
            val exception = UnauthorizedException(context.getString(R.string.sync_error_authentication_failed))
@@ -195,6 +201,33 @@ 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("LAST_VERSION_CODE", 0L)
        if (savedVersion != currentVersion) {
            Logger.log.warning("App has been updated!" +
                    "Previous: $savedVersion, Current: $currentVersion")
            prefs.edit {
                putLong("LAST_VERSION_CODE", currentVersion)
            }
            // Clear cookie since the app is updated.
            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
@@ -384,6 +417,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