Skip to content

Commit

Permalink
Add ExternalCache that falls back to InternalCache if not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
anpez authored and sjudd committed Oct 5, 2017
1 parent d96a1e7 commit 56a4275
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
* disk cache directory.
*
* <p><b>Images can be read by everyone when using external disk cache.</b>
*
* @deprecated use {@link ExternalPreferredCacheDiskCacheFactory} instead.
*/
@Deprecated
public final class ExternalCacheDiskCacheFactory extends DiskLruCacheFactory {

public ExternalCacheDiskCacheFactory(Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.bumptech.glide.load.engine.cache;

import android.content.Context;

import android.support.annotation.Nullable;
import java.io.File;

/**
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the external
* disk cache directory, which falls back to the internal disk cache if no external storage is
* available. If ever fell back to the internal disk cache, will use that one from that moment on.
*
* <p><b>Images can be read by everyone when using external disk cache.</b>
*/
public final class ExternalPreferredCacheDiskCacheFactory extends DiskLruCacheFactory {

public ExternalPreferredCacheDiskCacheFactory(Context context) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR,
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
}

public ExternalPreferredCacheDiskCacheFactory(Context context, int diskCacheSize) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
}

public ExternalPreferredCacheDiskCacheFactory(final Context context, final String diskCacheName,
final int diskCacheSize) {
super(new CacheDirectoryGetter() {
@Nullable
private File getInternalCacheDirectory() {
File cacheDirectory = context.getCacheDir();
if (cacheDirectory == null) {
return null;
}
if (diskCacheName != null) {
return new File(cacheDirectory, diskCacheName);
}
return cacheDirectory;
}

@Override
public File getCacheDirectory() {
File internalCacheDirectory = getInternalCacheDirectory();

// Already used internal cache, so keep using that one,
// thus avoiding using both external and internal with transient errors.
if ((null != internalCacheDirectory) && internalCacheDirectory.exists()) {
return internalCacheDirectory;
}

File cacheDirectory = context.getExternalCacheDir();

// Shared storage is not available.
if (cacheDirectory == null) {
return internalCacheDirectory;
}
if (diskCacheName != null) {
return new File(cacheDirectory, diskCacheName);
}
return cacheDirectory;
}
}, diskCacheSize);
}
}

0 comments on commit 56a4275

Please sign in to comment.