Add the ability to clear wikivoyage images cache
This commit is contained in:
parent
7f332a3e6e
commit
1a155356cb
3 changed files with 107 additions and 3 deletions
91
OsmAnd/src/net/osmand/PicassoUtils.java
Normal file
91
OsmAnd/src/net/osmand/PicassoUtils.java
Normal file
|
@ -0,0 +1,91 @@
|
|||
package net.osmand;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.StatFs;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.squareup.picasso.LruCache;
|
||||
import com.squareup.picasso.OkHttp3Downloader;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
|
||||
|
||||
public class PicassoUtils {
|
||||
|
||||
private static final String PICASSO_CACHE = "picasso-cache";
|
||||
private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
|
||||
|
||||
private static Cache diskCache;
|
||||
private static LruCache memoryCache;
|
||||
|
||||
private static boolean initialized;
|
||||
|
||||
public static void setupPicasso(@NonNull Context context) {
|
||||
if (!initialized) {
|
||||
File cacheDir = createDefaultCacheDir(context);
|
||||
|
||||
diskCache = new Cache(cacheDir, calculateDiskCacheSize(cacheDir));
|
||||
memoryCache = new LruCache(context);
|
||||
|
||||
Picasso picasso = new Picasso.Builder(context)
|
||||
.downloader(new OkHttp3Downloader(new OkHttpClient.Builder().cache(diskCache).build()))
|
||||
.memoryCache(memoryCache)
|
||||
.build();
|
||||
|
||||
Picasso.setSingletonInstance(picasso);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearAllPicassoCache() {
|
||||
if (memoryCache != null) {
|
||||
memoryCache.clear();
|
||||
}
|
||||
if (diskCache != null) {
|
||||
try {
|
||||
diskCache.evictAll();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static File createDefaultCacheDir(Context context) {
|
||||
File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
|
||||
if (!cache.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
cache.mkdirs();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
private static long calculateDiskCacheSize(File dir) {
|
||||
long size = MIN_DISK_CACHE_SIZE;
|
||||
|
||||
try {
|
||||
StatFs statFs = new StatFs(dir.getAbsolutePath());
|
||||
//noinspection deprecation
|
||||
long blockCount =
|
||||
SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockCount() : statFs.getBlockCountLong();
|
||||
//noinspection deprecation
|
||||
long blockSize =
|
||||
SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockSize() : statFs.getBlockSizeLong();
|
||||
long available = blockCount * blockSize;
|
||||
// Target 2% of the total space.
|
||||
size = available / 50;
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
|
||||
// Bound inside min/max size for disk cache.
|
||||
return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package net.osmand.plus.wikivoyage.explore;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
|
@ -18,6 +19,7 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.PicassoUtils;
|
||||
import net.osmand.plus.LockableViewPager;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
|
||||
|
@ -34,6 +36,15 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen
|
|||
private static final int EXPLORE_POSITION = 0;
|
||||
private static final int SAVED_ARTICLES_POSITION = 1;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
PicassoUtils.setupPicasso(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
|
|
@ -2,8 +2,9 @@ package net.osmand.plus.wikivoyage.explore;
|
|||
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import net.osmand.PicassoUtils;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -49,8 +50,9 @@ public class WikivoyageOptionsBottomSheetDialogFragment extends MenuBottomSheetD
|
|||
.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// TODO : implement clearing of cache
|
||||
Toast.makeText(getContext(), "Currently in development", Toast.LENGTH_SHORT).show();
|
||||
new WebView(getContext()).clearCache(true);
|
||||
PicassoUtils.clearAllPicassoCache();
|
||||
dismiss();
|
||||
}
|
||||
})
|
||||
.create();
|
||||
|
|
Loading…
Reference in a new issue