added auto cleaning cache
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.vk.core.ui.themes;
|
||||
|
||||
import com.vk.s.R;
|
||||
|
||||
import ru.vtosters.lite.utils.Themes;
|
||||
|
||||
public enum VKTheme {
|
||||
|
||||
@@ -4,7 +4,6 @@ import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static android.widget.Toast.makeText;
|
||||
import static ru.vtosters.lite.utils.Globals.getIdentifier;
|
||||
import static ru.vtosters.lite.utils.Globals.getString;
|
||||
import static ru.vtosters.lite.utils.Preferences.getDateFormat;
|
||||
import static ru.vtosters.lite.utils.Proxy.getApiCom;
|
||||
import static ru.vtosters.lite.utils.Proxy.proxy;
|
||||
|
||||
@@ -15,7 +14,6 @@ import android.support.v7.app.AlertDialog;
|
||||
import androidx.exifinterface.media.ExifInterface;
|
||||
|
||||
import com.vk.core.dialogs.alert.VkAlertDialog;
|
||||
import com.vtosters.lite.R;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
|
||||
@@ -45,6 +45,20 @@ public class PreferencesUtil {
|
||||
fragment.ay().d(preference); // fragment.getPreferenceScreen().addPreference(preference)
|
||||
}
|
||||
|
||||
public static void addListPreferenceIcon(MaterialPreferenceToolbarFragment fragment, String key, String def, CharSequence title, String icon, CharSequence summary, CharSequence[] entries, CharSequence[] entriesValue) {
|
||||
ListPreference preference = new ListPreference(getContext());
|
||||
preference.a(entries);
|
||||
preference.b(entriesValue);
|
||||
preference.b(summary); // setSummary
|
||||
preference.c(title); // setTitle
|
||||
preference.a(title); // setDialogTitle
|
||||
preference.d(key); // setKey
|
||||
preference.b(def); // setDefaultValue
|
||||
if (icon != null)
|
||||
preference.a(setTint(getContext(), getDrawable(getContext(), icon))); // preference.setIcon
|
||||
fragment.ay().d(preference); // fragment.getPreferenceScreen().addPreference(preference)
|
||||
}
|
||||
|
||||
public static void addMaterialSwitchPreference(MaterialPreferenceToolbarFragment materialPreferenceToolbarFragment, String key, CharSequence title, CharSequence summary, boolean defValue) {
|
||||
MaterialSwitchPreference materialSwitchPreference = new MaterialSwitchPreference(getContext());
|
||||
materialSwitchPreference.c(title); // setTitle
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.vtosters.lite.ui.fragments;
|
||||
|
||||
import static ru.vtosters.lite.utils.Globals.deleteCache;
|
||||
import static ru.vtosters.lite.utils.CacheUtils.deleteCache;
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
import static ru.vtosters.lite.utils.Globals.restartApplicationWithTimer;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package ru.vtosters.lite.ui.fragments;
|
||||
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static android.widget.Toast.makeText;
|
||||
import static ru.vtosters.lite.utils.Globals.deleteCache;
|
||||
import static ru.vtosters.lite.utils.CacheUtils.deleteCache;
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
import static ru.vtosters.lite.utils.Globals.getString;
|
||||
import static ru.vtosters.lite.utils.Globals.getUserToken;
|
||||
|
||||
@@ -8,6 +8,7 @@ import static ru.vtosters.lite.ui.fragments.multiaccount.MultiAccountManager.wit
|
||||
import static ru.vtosters.lite.ui.fragments.multiaccount.MultiAccountManager.workingAccounts;
|
||||
import static ru.vtosters.lite.utils.About.getBuildNumber;
|
||||
import static ru.vtosters.lite.utils.About.getCommitLink;
|
||||
import static ru.vtosters.lite.utils.CacheUtils.humanReadableByteCountBin;
|
||||
import static ru.vtosters.lite.utils.Globals.drawableFromUrl;
|
||||
import static ru.vtosters.lite.utils.Globals.edit;
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
@@ -62,6 +63,7 @@ import ru.vtosters.lite.ui.fragments.dockbar.DockBarFragment;
|
||||
import ru.vtosters.lite.ui.fragments.dockbar.DockBarManager;
|
||||
import ru.vtosters.lite.ui.fragments.multiaccount.MultiAccountFragment;
|
||||
import ru.vtosters.lite.ui.fragments.tgstickers.StickersFragment;
|
||||
import ru.vtosters.lite.utils.CacheUtils;
|
||||
|
||||
public class VTSettings extends MaterialPreferenceToolbarFragment {
|
||||
|
||||
@@ -308,6 +310,13 @@ public class VTSettings extends MaterialPreferenceToolbarFragment {
|
||||
return false;
|
||||
});
|
||||
|
||||
PreferencesUtil.addListPreferenceIcon(this, "clearcache", "Default", getString("autoclearcache"), "ic_delete_24", getString("autoclearcachesumm") + " " + humanReadableByteCountBin(CacheUtils.getInstance().size), new CharSequence[]{
|
||||
getString("autoclearcachedisabled"), "100 MB", "500 MB", "1 GB", "2 GB", "5 GB"
|
||||
}, new String[]{
|
||||
"Default", "100mb", "500mb", "1gb", "2gb", "5gb"
|
||||
});
|
||||
|
||||
|
||||
PreferencesUtil.addPreference(this, "", getString("vtlother"), othersumm, "ic_more_24", preference -> {
|
||||
Context context = getContext();
|
||||
Intent a2 = new Navigator(OtherFragment.class).a(context);
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package ru.vtosters.lite.utils;
|
||||
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
import static ru.vtosters.lite.utils.Globals.sendToast;
|
||||
import static ru.vtosters.lite.utils.Preferences.dev;
|
||||
import static ru.vtosters.lite.utils.Preferences.getSizeForDelete;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.CharacterIterator;
|
||||
import java.text.StringCharacterIterator;
|
||||
import java.util.Objects;
|
||||
|
||||
public class CacheUtils {
|
||||
private static CacheUtils sInstance = new CacheUtils();
|
||||
public long size = 0;
|
||||
|
||||
public static CacheUtils getInstance() {
|
||||
if (sInstance == null)
|
||||
sInstance = new CacheUtils();
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public void autoCleaningCache() {
|
||||
size = getDirSize(getContext().getCacheDir());
|
||||
|
||||
if(size >= getSizeForDelete()) {
|
||||
if(dev()) sendToast("Кэш очищен");
|
||||
deleteCache();
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public long getDirSize(File dir){
|
||||
long size = 0;
|
||||
for (File file : Objects.requireNonNull(dir.listFiles())) {
|
||||
if (file != null && file.isDirectory()) {
|
||||
size += getDirSize(file);
|
||||
} else if (file != null && file.isFile()) {
|
||||
size += file.length();
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
public static void deleteCache() {
|
||||
try {
|
||||
File dir = getContext().getCacheDir();
|
||||
deleteDir(dir);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteDir(File dir) {
|
||||
if (dir != null && dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
for (int i=0; i < Objects.requireNonNull(children).length; i++) {
|
||||
boolean success = deleteDir(new File(dir, children[i]));
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return dir.delete();
|
||||
} else if(dir!= null && dir.isFile()) {
|
||||
return dir.delete();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
public static String humanReadableByteCountBin(long bytes) {
|
||||
long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
|
||||
if (absB < 1024) {
|
||||
return bytes + " B";
|
||||
}
|
||||
long value = absB;
|
||||
CharacterIterator ci = new StringCharacterIterator("KMGTPE");
|
||||
for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
|
||||
value >>= 10;
|
||||
ci.next();
|
||||
}
|
||||
value *= Long.signum(bytes);
|
||||
return String.format("%.1f %cB", value / 1024.0, ci.current());
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,6 @@ import com.vtosters.lite.api.ExtendedUserProfile;
|
||||
import com.vtosters.lite.auth.VKAccountManager;
|
||||
import com.vtosters.lite.im.ImEngineProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
@@ -50,7 +49,6 @@ import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
import ru.vtosters.lite.ui.dialogs.DisableBattery;
|
||||
import ru.vtosters.lite.ui.dialogs.InstallGMS;
|
||||
@@ -202,31 +200,6 @@ public class Globals {
|
||||
return null;
|
||||
} // Getting the global context through reflection to use context on application initialization
|
||||
|
||||
public static void deleteCache() {
|
||||
try {
|
||||
File dir = getContext().getCacheDir();
|
||||
deleteDir(dir);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteDir(File dir) {
|
||||
if (dir != null && dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
for (int i=0; i < Objects.requireNonNull(children).length; i++) {
|
||||
boolean success = deleteDir(new File(dir, children[i]));
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return dir.delete();
|
||||
} else if(dir!= null && dir.isFile()) {
|
||||
return dir.delete();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static boolean isGmsInstalled() {
|
||||
try {
|
||||
getContext().getPackageManager().getPackageInfo("com.google.android.gms", 0);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package ru.vtosters.lite.utils;
|
||||
|
||||
import static android.os.Build.VERSION;
|
||||
import static java.lang.Long.MAX_VALUE;
|
||||
import static ru.vtosters.lite.f0x1d.VTVerifications.hasPrometheus;
|
||||
import static ru.vtosters.lite.f0x1d.VTVerifications.isVerified;
|
||||
import static ru.vtosters.lite.ui.fragments.multiaccount.MultiAccountManager.migrate;
|
||||
import static ru.vtosters.lite.utils.CacheUtils.getInstance;
|
||||
import static ru.vtosters.lite.utils.DeletedMessagesHandler.reloadMessagesList;
|
||||
import static ru.vtosters.lite.utils.Globals.fixGapps;
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
@@ -58,13 +60,16 @@ public class Preferences {
|
||||
LoadLibrary("vkopustest");
|
||||
}
|
||||
systemThemeChanger();
|
||||
getInstance().autoCleaningCache();
|
||||
} // VK Init
|
||||
|
||||
public static void forceOffline() {
|
||||
setupFilters();
|
||||
getInstance().autoCleaningCache();
|
||||
|
||||
if (setoffline() && !offline()) {
|
||||
Users.b();
|
||||
}
|
||||
setupFilters();
|
||||
}
|
||||
|
||||
public static boolean getBoolValue(String key, Boolean value) {
|
||||
@@ -438,6 +443,23 @@ public class Preferences {
|
||||
}
|
||||
}
|
||||
|
||||
public static long getSizeForDelete() {
|
||||
switch (getPrefsValue("clearcache")) {
|
||||
case "100mb":
|
||||
return 104857600L;
|
||||
case "500mb":
|
||||
return 524288000L;
|
||||
case "1gb":
|
||||
return 1073741824L;
|
||||
case "2gb":
|
||||
return 2147483648L;
|
||||
case "5gb":
|
||||
return 5368709120L;
|
||||
default:
|
||||
return MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
public static String nullOnline() {
|
||||
return getString("onlineNull");
|
||||
}
|
||||
|
||||
@@ -4201,6 +4201,9 @@
|
||||
<string name="proxysocks">Socks Прокси</string>
|
||||
<string name="proxystatic">Static Домен</string>
|
||||
<string name="reportbug">Сообщить о баге</string>
|
||||
<string name="autoclearcache">Авто очистка кэша</string>
|
||||
<string name="autoclearcachesumm">Размер кэша:</string>
|
||||
<string name="autoclearcachedisabled">Не очищать кэш</string>
|
||||
<string name="reportbugsumm">Репорты багов помогают нам их исправлять</string>
|
||||
<string name="restartapp">Перезапустите приложение</string>
|
||||
<string name="savedmsg">Сохраненные сообщения</string>
|
||||
|
||||
@@ -4326,6 +4326,9 @@ You need to restart the application to apply proxy settings"</string>
|
||||
<string name="proxysocks">Socks Proxy</string>
|
||||
<string name="proxystatic">Static Domain</string>
|
||||
<string name="reportbug">Report a bug</string>
|
||||
<string name="autoclearcache">Auto-cleaning app cache</string>
|
||||
<string name="autoclearcachesumm">Cache size:</string>
|
||||
<string name="autoclearcachedisabled">"Don't clear cache"</string>
|
||||
<string name="reportbugsumm">Bug reports help us fix them</string>
|
||||
<string name="restartapp">Please, restart app</string>
|
||||
<string name="savedmsg">Saved messages</string>
|
||||
|
||||
Reference in New Issue
Block a user