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

Commit 807c17a6 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Fix TokenMismatchException after deleting and readding an account

parent 35ad855d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -250,7 +250,7 @@ public class NotesListViewActivity extends LockedActivity implements ItemAdapter
                selectAccount(localAccount.getAccountName());
            });
            v.findViewById(R.id.delete).setOnClickListener(clickedView -> {
                db.deleteAccount(localAccount.getId());
                db.deleteAccount(localAccount);
                if (localAccount.getId() == this.localAccount.getId()) {
                    List<LocalAccount> remainingAccounts = db.getAccounts();
                    if (remainingAccounts.size() > 0) {
+34 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.pm.PackageInfo;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;

import com.google.gson.GsonBuilder;
@@ -212,4 +213,36 @@ public class NotesClient {
            return nextcloudAPI;
        }
    }

    /**
     * Invalidates thes API cache for the given ssoAccount
     *
     * @param ssoAccount the ssoAccount for which the API cache should be cleared.
     */
    public static void invalidateAPICache(@NonNull SingleSignOnAccount ssoAccount) {
        Log.v(TAG, "Invalidating API cache for " + ssoAccount.name);
        if (mNextcloudAPIs.containsKey(ssoAccount.name)) {
            final NextcloudAPI nextcloudAPI = mNextcloudAPIs.get(ssoAccount.name);
            if (nextcloudAPI != null) {
                nextcloudAPI.stop();
            }
            mNextcloudAPIs.remove(ssoAccount.name);
        }
    }

    /**
     * Invalidates the whole API cache for all accounts
     */
    public static void invalidateAPICache() {
        for (String key : mNextcloudAPIs.keySet()) {
            Log.v(TAG, "Invalidating API cache for " + key);
            if (mNextcloudAPIs.containsKey(key)) {
                final NextcloudAPI nextcloudAPI = mNextcloudAPIs.get(key);
                if (nextcloudAPI != null) {
                    nextcloudAPI.stop();
                }
                mNextcloudAPIs.remove(key);
            }
        }
    }
}
+18 −8
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.util.ArrayList;
@@ -708,21 +710,29 @@ public class NotesDatabase extends AbstractNotesDatabase {
    }

    /**
     * @param accountId the id of the account that should be deleted
     * @param localAccount the account that should be deleted
     * @throws IllegalArgumentException if no account has been deleted by the given accountId
     */
    public void deleteAccount(long accountId) throws IllegalArgumentException {
        validateAccountId(accountId);
    public void deleteAccount(@NonNull LocalAccount localAccount) throws IllegalArgumentException {
        validateAccountId(localAccount.getId());
        SQLiteDatabase db = this.getWritableDatabase();
        int deletedAccounts = db.delete(table_accounts, key_id + " = ?", new String[]{accountId + ""});
        int deletedAccounts = db.delete(table_accounts, key_id + " = ?", new String[]{String.valueOf(localAccount.getId())});
        if (deletedAccounts < 1) {
            Log.e(TAG, "AccountId '" + accountId + "' did not delete any account");
            Log.e(TAG, "AccountId '" + localAccount.getId() + "' did not delete any account");
            throw new IllegalArgumentException("The given accountId does not delete any row");
        } else if (deletedAccounts > 1) {
            Log.e(TAG, "AccountId '" + accountId + "' deleted unexpectedly '" + deletedAccounts + "' accounts");
            Log.e(TAG, "AccountId '" + localAccount.getId() + "' deleted unexpectedly '" + deletedAccounts + "' accounts");
        }

        try {
            NotesClient.invalidateAPICache(AccountImporter.getSingleSignOnAccount(getContext(), localAccount.getAccountName()));
        } catch (NextcloudFilesAppAccountNotFoundException e) {
            e.printStackTrace();
            NotesClient.invalidateAPICache();
        }
        final int deletedNotes = db.delete(table_notes, key_account_id + " = ?", new String[]{accountId + ""});
        Log.v(TAG, "Deleted " + deletedNotes + " notes from account " + accountId);

        final int deletedNotes = db.delete(table_notes, key_account_id + " = ?", new String[]{String.valueOf(localAccount.getId())});
        Log.v(TAG, "Deleted " + deletedNotes + " notes from account " + localAccount.getId());
    }

    void updateETag(long accountId, String etag) {