Improve cache clear

This commit is contained in:
Thomas 2022-11-14 10:08:18 +01:00
parent 13f180e6f3
commit f66fda968e
5 changed files with 82 additions and 8 deletions

View file

@ -133,7 +133,17 @@ public class CacheActivity extends BaseActivity {
size = size / 1000000.0f;
}
binding.fileCacheSize.setText(String.format("%s %s", String.format(Locale.getDefault(), "%.2f", size), getString(R.string.cache_units)));
cacheAdapter.notifyDataSetChanged();
AlertDialog.Builder restartBuilder = new AlertDialog.Builder(CacheActivity.this, Helper.dialogStyle());
restartBuilder.setMessage(getString(R.string.restart_the_app));
restartBuilder.setNegativeButton(R.string.no, (dialogRestart, whichRestart) -> {
recreate();
dialogRestart.dismiss();
});
restartBuilder.setPositiveButton(R.string.restart, (dialogRestart, whichRestart) -> {
dialogRestart.dismiss();
Helper.restart(CacheActivity.this);
});
restartBuilder.create().show();
}));
dialog.dismiss();
});

View file

@ -224,9 +224,10 @@ public class StatusCache {
throw new DBException("db is null. Wrong initialization.");
}
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUS_CACHE
+ " where " + Sqlite.COL_TYPE + " = '" + Timeline.TimeLineEnum.HOME.getValue() + "'"
+ " AND " + Sqlite.COL_INSTANCE + " = '" + baseAccount.instance + "'"
+ " AND " + Sqlite.COL_USER_ID + "= '" + baseAccount.user_id + "'", null);
+ " where " + Sqlite.COL_TYPE + " = ?"
+ " AND " + Sqlite.COL_INSTANCE + " = ?"
+ " AND " + Sqlite.COL_USER_ID + "= ?",
new String[]{Timeline.TimeLineEnum.HOME.getValue(), baseAccount.instance, baseAccount.user_id});
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();
@ -457,14 +458,35 @@ public class StatusCache {
* @return long - db id
* @throws DBException exception with database
*/
public long deleteForAccount(BaseAccount account) throws DBException {
public long deleteHomeForAccount(BaseAccount account) throws DBException {
if (db == null) {
throw new DBException("db is null. Wrong initialization.");
}
try {
return db.delete(Sqlite.TABLE_STATUS_CACHE,
Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
new String[]{account.user_id, account.instance});
Sqlite.COL_TYPE + " = ? AND " + Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
new String[]{Timeline.TimeLineEnum.HOME.getValue(), account.user_id, account.instance});
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/**
* delete all cache for an account
*
* @param account - Account
* @return long - db id
* @throws DBException exception with database
*/
public long deleteOthersForAccount(BaseAccount account) throws DBException {
if (db == null) {
throw new DBException("db is null. Wrong initialization.");
}
try {
return db.delete(Sqlite.TABLE_STATUS_CACHE,
Sqlite.COL_TYPE + " != ? AND " + Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
new String[]{Timeline.TimeLineEnum.HOME.getValue(), account.user_id, account.instance});
} catch (Exception e) {
e.printStackTrace();
return -1;

View file

@ -15,18 +15,24 @@ package app.fedilab.android.helper;
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import androidx.preference.PreferenceManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import app.fedilab.android.R;
import app.fedilab.android.client.entities.app.BaseAccount;
import app.fedilab.android.client.entities.app.CacheAccount;
import app.fedilab.android.client.entities.app.StatusCache;
import app.fedilab.android.client.entities.app.StatusDraft;
import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.exception.DBException;
public class CacheHelper {
@ -112,10 +118,29 @@ public class CacheHelper {
deleteDir(dir);
}
}
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedpreferences.edit();
for (CacheAccount cacheAccount : cacheAccounts) {
if (cacheAccount.clear_home) {
try {
new StatusCache(context).deleteForAccount(cacheAccount.account);
new StatusCache(context).deleteHomeForAccount(cacheAccount.account);
editor.putString(context.getString(R.string.SET_INNER_MARKER) + cacheAccount.account.user_id + cacheAccount.account.instance + Timeline.TimeLineEnum.HOME.getValue(), null);
editor.apply();
} catch (DBException e) {
e.printStackTrace();
}
}
if (cacheAccount.clear_other) {
try {
new StatusCache(context).deleteOthersForAccount(cacheAccount.account);
Map<String, ?> keys = sharedpreferences.getAll();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
if (entry.getKey().startsWith(context.getString(R.string.SET_INNER_MARKER) + cacheAccount.account.user_id + cacheAccount.account.instance) && !entry.getKey().endsWith(Timeline.TimeLineEnum.HOME.getValue())) {
editor.putString(entry.getKey(), null);
editor.apply();
}
}
} catch (DBException e) {
e.printStackTrace();
}

View file

@ -30,6 +30,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
@ -1894,6 +1895,20 @@ public class Helper {
}
}
/**
* Restart the app
*
* @param context
*/
public static void restart(Context context) {
Context ctx = context.getApplicationContext();
PackageManager pm = ctx.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(ctx.getPackageName());
Intent mainIntent = Intent.makeRestartActivityTask(intent.getComponent());
ctx.startActivity(mainIntent);
Runtime.getRuntime().exit(0);
}
//Enum that described actions to replace inside a toot content
public enum PatternType {
MENTION,

View file

@ -1597,4 +1597,6 @@
<string name="account_silenced">Account silenced</string>
<string name="report">Report</string>
<string name="state">State</string>
<string name="restart_the_app">Restart the app?</string>
<string name="restart">Restart</string>
</resources>