Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Volley.newRequestQueue() to be called on main thread #274

Merged
merged 6 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions src/main/java/com/android/volley/toolbox/DiskBasedCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public class DiskBasedCache implements Cache {
/** Total amount of space currently used by the cache in bytes. */
private long mTotalSize = 0;

/** The root directory to use for the cache. */
private final File mRootDirectory;
/** The supplier for the root directory to use for the cache. */
private final FileSupplier mRootDirectorySupplier;

/** The maximum size of the cache in bytes. */
private final int mMaxCacheSizeInBytes;
Expand All @@ -78,8 +78,27 @@ public class DiskBasedCache implements Cache {
* briefly exceed this size on disk when writing a new entry that pushes it over the limit
* until the ensuing pruning completes.
*/
public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) {
mRootDirectory = rootDirectory;
public DiskBasedCache(final File rootDirectory, int maxCacheSizeInBytes) {
mRootDirectorySupplier =
new FileSupplier() {
@Override
public File get() {
return rootDirectory;
}
};
mMaxCacheSizeInBytes = maxCacheSizeInBytes;
}

/**
* Constructs an instance of the DiskBasedCache at the specified directory.
*
* @param rootDirectorySupplier The supplier for the root directory of the cache.
* @param maxCacheSizeInBytes The maximum size of the cache in bytes. Note that the cache may
* briefly exceed this size on disk when writing a new entry that pushes it over the limit
* until the ensuing pruning completes.
*/
public DiskBasedCache(FileSupplier rootDirectorySupplier, int maxCacheSizeInBytes) {
mRootDirectorySupplier = rootDirectorySupplier;
mMaxCacheSizeInBytes = maxCacheSizeInBytes;
}

Expand All @@ -93,10 +112,20 @@ public DiskBasedCache(File rootDirectory) {
this(rootDirectory, DEFAULT_DISK_USAGE_BYTES);
}

/**
* Constructs an instance of the DiskBasedCache at the specified directory using the default
* maximum cache size of 5MB.
*
* @param rootDirectorySupplier The supplier for the root directory of the cache.
*/
public DiskBasedCache(FileSupplier rootDirectorySupplier) {
this(rootDirectorySupplier, DEFAULT_DISK_USAGE_BYTES);
}

/** Clears the cache. Deletes all cached files from disk. */
@Override
public synchronized void clear() {
File[] files = mRootDirectory.listFiles();
File[] files = mRootDirectorySupplier.get().listFiles();
if (files != null) {
for (File file : files) {
file.delete();
Expand Down Expand Up @@ -150,13 +179,14 @@ public synchronized Entry get(String key) {
*/
@Override
public synchronized void initialize() {
if (!mRootDirectory.exists()) {
if (!mRootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
File rootDirectory = mRootDirectorySupplier.get();
if (!rootDirectory.exists()) {
if (!rootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", rootDirectory.getAbsolutePath());
}
return;
}
File[] files = mRootDirectory.listFiles();
File[] files = rootDirectory.listFiles();
if (files == null) {
return;
}
Expand Down Expand Up @@ -262,7 +292,12 @@ private String getFilenameForKey(String key) {

/** Returns a file object for the given cache key. */
public File getFileForKey(String key) {
return new File(mRootDirectory, getFilenameForKey(key));
return new File(mRootDirectorySupplier.get(), getFilenameForKey(key));
}

/** Represents a supplier for {@link File}s. */
public interface FileSupplier {
File get();
}

/** Prunes the cache to fit the maximum size. */
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/android/volley/toolbox/Volley.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,22 @@ public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
}

private static RequestQueue newRequestQueue(Context context, Network network) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
final Context appContext = context.getApplicationContext();
// Use a lazy supplier for the cache directory so that newRequestQueue() can be called on
// main thread without causing strict mode violation.
DiskBasedCache.FileSupplier cacheSupplier =
new DiskBasedCache.FileSupplier() {
Poligun marked this conversation as resolved.
Show resolved Hide resolved
private File cacheDir = null;

@Override
public File get() {
if (cacheDir == null) {
cacheDir = new File(appContext.getCacheDir(), DEFAULT_CACHE_DIR);
}
return cacheDir;
}
};
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheSupplier), network);
queue.start();
return queue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,10 @@ public void serializeHeaders() throws Exception {
public void publicMethods() throws Exception {
// Catch-all test to find API-breaking changes.
assertNotNull(DiskBasedCache.class.getConstructor(File.class, int.class));
assertNotNull(
DiskBasedCache.class.getConstructor(DiskBasedCache.FileSupplier.class, int.class));
assertNotNull(DiskBasedCache.class.getConstructor(File.class));
assertNotNull(DiskBasedCache.class.getConstructor(DiskBasedCache.FileSupplier.class));

assertNotNull(DiskBasedCache.class.getMethod("getFileForKey", String.class));
}
Expand Down