diff options
| author | Neil Fuller <nfuller@google.com> | 2015-01-20 13:21:58 +0000 |
|---|---|---|
| committer | Neil Fuller <nfuller@google.com> | 2015-01-20 13:21:58 +0000 |
| commit | 7014da74e861f8cfc34fcbd528b4c83a1dc97503 (patch) | |
| tree | dd262e7952902ec1e79eacf0708fe363ec36f357 /core/java/android/net/http | |
| parent | 66bbfb6b2ccedecba3cbd0843b5a9ae498c68c82 (diff) | |
| parent | 7382c02d6204806c0462a98ea4e3524007f1fc86 (diff) | |
| download | frameworks_base-7014da74e861f8cfc34fcbd528b4c83a1dc97503.zip frameworks_base-7014da74e861f8cfc34fcbd528b4c83a1dc97503.tar.gz frameworks_base-7014da74e861f8cfc34fcbd528b4c83a1dc97503.tar.bz2 | |
resolved conflicts for merge of 7382c02d to lmp-mr1-dev-plus-aosp
Change-Id: Idfa2750230aa56e7ae34fcc33d62558df942abd0
Diffstat (limited to 'core/java/android/net/http')
| -rw-r--r-- | core/java/android/net/http/HttpResponseCache.java | 73 |
1 files changed, 41 insertions, 32 deletions
diff --git a/core/java/android/net/http/HttpResponseCache.java b/core/java/android/net/http/HttpResponseCache.java index 2785a15..c40b1bf 100644 --- a/core/java/android/net/http/HttpResponseCache.java +++ b/core/java/android/net/http/HttpResponseCache.java @@ -16,20 +16,20 @@ package android.net.http; -import android.content.Context; +import com.android.okhttp.Cache; +import com.android.okhttp.AndroidShimResponseCache; +import com.android.okhttp.OkCacheContainer; + import java.io.Closeable; import java.io.File; import java.io.IOException; import java.net.CacheRequest; import java.net.CacheResponse; -import java.net.HttpURLConnection; import java.net.ResponseCache; import java.net.URI; import java.net.URLConnection; import java.util.List; import java.util.Map; -import javax.net.ssl.HttpsURLConnection; -import org.apache.http.impl.client.DefaultHttpClient; /** * Caches HTTP and HTTPS responses to the filesystem so they may be reused, @@ -40,7 +40,7 @@ import org.apache.http.impl.client.DefaultHttpClient; * <h3>Installing an HTTP response cache</h3> * Enable caching of all of your application's HTTP requests by installing the * cache at application startup. For example, this code installs a 10 MiB cache - * in the {@link Context#getCacheDir() application-specific cache directory} of + * in the {@link android.content.Context#getCacheDir() application-specific cache directory} of * the filesystem}: <pre> {@code * protected void onCreate(Bundle savedInstanceState) { * ... @@ -147,11 +147,11 @@ import org.apache.http.impl.client.DefaultHttpClient; * } catch (Exception httpResponseCacheNotAvailable) { * }}</pre> */ -public final class HttpResponseCache extends ResponseCache implements Closeable { +public final class HttpResponseCache extends ResponseCache implements Closeable, OkCacheContainer { - private final com.android.okhttp.HttpResponseCache delegate; + private final AndroidShimResponseCache delegate; - private HttpResponseCache(com.android.okhttp.HttpResponseCache delegate) { + private HttpResponseCache(AndroidShimResponseCache delegate) { this.delegate = delegate; } @@ -161,17 +161,14 @@ public final class HttpResponseCache extends ResponseCache implements Closeable */ public static HttpResponseCache getInstalled() { ResponseCache installed = ResponseCache.getDefault(); - if (installed instanceof com.android.okhttp.HttpResponseCache) { - return new HttpResponseCache( - (com.android.okhttp.HttpResponseCache) installed); + if (installed instanceof HttpResponseCache) { + return (HttpResponseCache) installed; } - return null; } /** - * Creates a new HTTP response cache and {@link ResponseCache#setDefault - * sets it} as the system default cache. + * Creates a new HTTP response cache and sets it as the system default cache. * * @param directory the directory to hold cache data. * @param maxSize the maximum size of the cache in bytes. @@ -180,26 +177,26 @@ public final class HttpResponseCache extends ResponseCache implements Closeable * Most applications should respond to this exception by logging a * warning. */ - public static HttpResponseCache install(File directory, long maxSize) throws IOException { + public static synchronized HttpResponseCache install(File directory, long maxSize) + throws IOException { ResponseCache installed = ResponseCache.getDefault(); - if (installed instanceof com.android.okhttp.HttpResponseCache) { - com.android.okhttp.HttpResponseCache installedCache = - (com.android.okhttp.HttpResponseCache) installed; + if (installed instanceof HttpResponseCache) { + HttpResponseCache installedResponseCache = (HttpResponseCache) installed; // don't close and reopen if an equivalent cache is already installed - if (installedCache.getDirectory().equals(directory) - && installedCache.getMaxSize() == maxSize - && !installedCache.isClosed()) { - return new HttpResponseCache(installedCache); + AndroidShimResponseCache trueResponseCache = installedResponseCache.delegate; + if (trueResponseCache.isEquivalent(directory, maxSize)) { + return installedResponseCache; } else { // The HttpResponseCache that owns this object is about to be replaced. - installedCache.close(); + trueResponseCache.close(); } } - com.android.okhttp.HttpResponseCache responseCache = - new com.android.okhttp.HttpResponseCache(directory, maxSize); - ResponseCache.setDefault(responseCache); - return new HttpResponseCache(responseCache); + AndroidShimResponseCache trueResponseCache = + AndroidShimResponseCache.create(directory, maxSize); + HttpResponseCache newResponseCache = new HttpResponseCache(trueResponseCache); + ResponseCache.setDefault(newResponseCache); + return newResponseCache; } @Override public CacheResponse get(URI uri, String requestMethod, @@ -214,10 +211,15 @@ public final class HttpResponseCache extends ResponseCache implements Closeable /** * Returns the number of bytes currently being used to store the values in * this cache. This may be greater than the {@link #maxSize} if a background - * deletion is pending. + * deletion is pending. {@code -1} is returned if the size cannot be determined. */ public long size() { - return delegate.getSize(); + try { + return delegate.size(); + } catch (IOException e) { + // This can occur if the cache failed to lazily initialize. + return -1; + } } /** @@ -225,7 +227,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable * its data. */ public long maxSize() { - return delegate.getMaxSize(); + return delegate.maxSize(); } /** @@ -271,7 +273,7 @@ public final class HttpResponseCache extends ResponseCache implements Closeable * will remain on the filesystem. */ @Override public void close() throws IOException { - if (ResponseCache.getDefault() == this.delegate) { + if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); } delegate.close(); @@ -281,9 +283,16 @@ public final class HttpResponseCache extends ResponseCache implements Closeable * Uninstalls the cache and deletes all of its stored contents. */ public void delete() throws IOException { - if (ResponseCache.getDefault() == this.delegate) { + if (ResponseCache.getDefault() == this) { ResponseCache.setDefault(null); } delegate.delete(); } + + /** @hide Needed for OkHttp integration. */ + @Override + public Cache getCache() { + return delegate.getCache(); + } + } |
