From 415d5d54473c23024d95389be3d6237977460211 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 23 Mar 2017 22:24:38 +0000 Subject: [PATCH] Clear journal cache on account deletion (fix account re-add issue) Clearing the cache is a good idea regardless, though because of the unique constraints in the cache on the journal name, this was causing issues when deleting an account and then adding it back. --- .../syncadapter/AccountUpdateService.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/AccountUpdateService.java b/app/src/main/java/com/etesync/syncadapter/AccountUpdateService.java index 95e94571..af5d6a31 100644 --- a/app/src/main/java/com/etesync/syncadapter/AccountUpdateService.java +++ b/app/src/main/java/com/etesync/syncadapter/AccountUpdateService.java @@ -13,6 +13,7 @@ import android.accounts.AccountManager; import android.annotation.SuppressLint; import android.app.Service; import android.content.Intent; +import android.database.Cursor; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.os.Binder; @@ -20,6 +21,10 @@ import android.os.IBinder; import android.support.annotation.NonNull; import android.text.TextUtils; +import com.etesync.syncadapter.model.JournalEntity; +import com.etesync.syncadapter.model.ServiceDB.OpenHelper; +import com.etesync.syncadapter.model.ServiceDB.Services; + import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Iterator; @@ -27,8 +32,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; -import com.etesync.syncadapter.model.ServiceDB.OpenHelper; -import com.etesync.syncadapter.model.ServiceDB.Services; +import io.requery.Persistable; +import io.requery.sql.EntityDataStore; public class AccountUpdateService extends Service { @@ -109,10 +114,20 @@ public class AccountUpdateService extends Service { for (Account account : am.getAccountsByType(Constants.ACCOUNT_TYPE)) sqlAccountNames.add(DatabaseUtils.sqlEscapeString(account.name)); - if (sqlAccountNames.isEmpty()) + EntityDataStore data = ((App) getApplication()).getData(); + + if (sqlAccountNames.isEmpty()) { + data.delete(JournalEntity.class).get().value(); db.delete(Services._TABLE, null, null); - else + } else { + Cursor cur = db.query(Services._TABLE, new String[]{Services.ID}, Services.ACCOUNT_NAME + " NOT IN (" + TextUtils.join(",", sqlAccountNames) + ")", null, null, null, null); + cur.moveToFirst(); + while(!cur.isAfterLast()) { + data.delete(JournalEntity.class).where(JournalEntity.SERVICE.eq(cur.getLong(0))).get().value(); + cur.moveToNext(); + } db.delete(Services._TABLE, Services.ACCOUNT_NAME + " NOT IN (" + TextUtils.join(",", sqlAccountNames) + ")", null); + } } finally { dbHelper.close(); }