X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FRefreshDescriptionsTask.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FRefreshDescriptionsTask.java;h=cb15980fed66b95d1ce4330a82634010e60110a7;hp=0000000000000000000000000000000000000000;hb=45a7596af05e4a9469081a21c054774fcea93f8a;hpb=94aa0aa57db3dce40f31fc0321c718998d9cb48b diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java new file mode 100644 index 00000000..cb15980f --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java @@ -0,0 +1,73 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.async; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import android.util.Log; + +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.utils.MLDB; + +import java.util.HashMap; +import java.util.Map; + +public class RefreshDescriptionsTask extends AsyncTask { + @Override + protected Void doInBackground(Void... voids) { + Map unique = new HashMap<>(); + + Log.d("descriptions", "Starting refresh"); + SQLiteDatabase db = MLDB.getWritableDatabase(); + + Data.backgroundTaskCount.incrementAndGet(); + try { + db.beginTransaction(); + try { + db.execSQL("UPDATE description_history set keep=0"); + try (Cursor c = db + .rawQuery("SELECT distinct description from transactions", null)) + { + while (c.moveToNext()) { + String description = c.getString(0); + String descriptionUpper = description.toUpperCase(); + if (unique.containsKey(descriptionUpper)) continue; + + db.execSQL( + "replace into description_history(description, description_upper, " + + "keep) values(?, ?, 1)", new String[]{description, descriptionUpper}); + unique.put(descriptionUpper, true); + } + } + db.execSQL("DELETE from description_history where keep=0"); + db.setTransactionSuccessful(); + Log.d("descriptions", "Refresh successful"); + } + finally { + db.endTransaction(); + } + } + finally { + Data.backgroundTaskCount.decrementAndGet(); + Log.d("descriptions", "Refresh done"); + } + + return null; + } +}