From 1a155356cbc0138866b72a197e3a1ae6e771ef6e Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 13 Apr 2018 23:50:53 +0300 Subject: [PATCH] Add the ability to clear wikivoyage images cache --- OsmAnd/src/net/osmand/PicassoUtils.java | 91 +++++++++++++++++++ .../WikivoyageExploreDialogFragment.java | 11 +++ ...oyageOptionsBottomSheetDialogFragment.java | 8 +- 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 OsmAnd/src/net/osmand/PicassoUtils.java diff --git a/OsmAnd/src/net/osmand/PicassoUtils.java b/OsmAnd/src/net/osmand/PicassoUtils.java new file mode 100644 index 0000000000..17caf47246 --- /dev/null +++ b/OsmAnd/src/net/osmand/PicassoUtils.java @@ -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); + } +} diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java index 04d7be9e4b..b16dc7fb6c 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java @@ -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) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java index 8267318a80..a11039b7df 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java @@ -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();