Clear cache, configure cache size (#482)

decrease the compression to have better results

Move cache size outside of maxsize method. fix progress bar alignment for files

Add cache size to picasso

Use getAbsolutePath

Merge branch 'app-cache' of gitea.com:gitnex/GitNex into app-cache

Few imprvoements

Merge branch 'master' into app-cache

small improvement

Implement configure cache size and clear it

Co-authored-by: 6543 <6543@noreply.gitea.io>
Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/482
Reviewed-by: opyale <opyale@noreply.gitea.io>
This commit is contained in:
M M Arif
2020-05-09 14:50:45 +00:00
parent a6b424f5ff
commit ab8a5ed505
10 changed files with 349 additions and 9 deletions

View File

@@ -0,0 +1,84 @@
package org.mian.gitnex.helpers;
import java.io.File;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* Author M M Arif
*/
public class FilesData {
public static int returnOnlyNumber(String fileSize) {
return Integer.parseInt(fileSize.substring(0, fileSize.indexOf(" ")));
}
public static long getFileSizeRecursively(Set<File> alreadySeen, File dirDirectory) {
long fileSize = 0;
for (File filItem : Objects.requireNonNull(dirDirectory.listFiles())) {
if (filItem.isDirectory()) {
fileSize += getFileSize(filItem);
}
else {
alreadySeen.add(new File(filItem.getName()));
fileSize += filItem.length();
}
}
return fileSize;
}
private static long getFileSize(File subDirectory) {
long fileSize = 0;
Deque<File> unprocessedDirs = new ArrayDeque<>();
unprocessedDirs.add(subDirectory);
Set<File> alreadySeen = new HashSet<>();
while (!unprocessedDirs.isEmpty()) {
File dir = unprocessedDirs.removeFirst();
for (File filItem : Objects.requireNonNull(dir.listFiles())) {
if (filItem.isDirectory()) {
unprocessedDirs.addFirst(filItem);
}
else {
if (! alreadySeen.contains(filItem.getName())) {
alreadySeen.add(new File(filItem.getName()));
fileSize += filItem.length();
}
}
}
}
return fileSize;
}
}

View File

@@ -1,9 +1,11 @@
package org.mian.gitnex.helpers;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.squareup.picasso.Cache;
import org.mian.gitnex.util.TinyDB;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -20,21 +22,26 @@ import java.util.UUID;
public class PicassoCache implements Cache {
private Context ctx;
private String TAG = "PicassoCache";
private static final Bitmap.CompressFormat COMPRESS_FORMAT = Bitmap.CompressFormat.PNG;
private static final int COMPRESSION_QUALITY = 0; // 0 = high compression (low file size) | 100 = no compression
private static final int COMPRESSION_QUALITY = 50; // 0 = high compression (low file size) | 100 = no compression
private final int CACHE_SIZE;
private static final String CACHE_MAP_FILE = "cacheMap";
private static final int CACHE_SIZE = 25 * 1024 * 1024; // Cache can hold twenty-five megabytes
private File cachePath;
private HashMap<String, String> cacheMap;
public PicassoCache(File cachePath) throws IOException, ClassNotFoundException {
public PicassoCache(File cachePath, Context ctx) throws IOException, ClassNotFoundException {
TinyDB tinyDb = new TinyDB(ctx);
CACHE_SIZE = FilesData.returnOnlyNumber(tinyDb.getString("cacheSizeImagesStr")) * 1024 * 1024;
this.cachePath = cachePath;
cacheMap = new HashMap<>();
this.ctx = ctx;
if(cacheMapExists(cachePath)) {