Example usage for android.webkit CacheManager getCacheFile

List of usage examples for android.webkit CacheManager getCacheFile

Introduction

In this page you can find the example usage for android.webkit CacheManager getCacheFile.

Prototype

@Deprecated
@Nullable
@UnsupportedAppUsage
public static CacheResult getCacheFile(String url, Map<String, String> headers) 

Source Link

Document

Gets the cache entry for the specified URL, or null if none is found.

Usage

From source file:android.webkit.cts.CacheManager_CacheResultTest.java

@TestTargets({ @TestTargetNew(level = TestLevel.COMPLETE, method = "getInputStream", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getContentLength", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getETag", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getLastModified", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getLocalPath", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getLocation", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getMimeType", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getOutputStream", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getExpires", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getHttpStatusCode", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "getEncoding", args = {}),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "setEncoding", args = { String.class }),
        @TestTargetNew(level = TestLevel.COMPLETE, method = "setInputStream", args = { InputStream.class }) })
public void testCacheResult() throws Exception {
    final long validity = 5 * 50 * 1000; // 5 min
    final long age = 30 * 60 * 1000; // 30 min
    final long tolerance = 5 * 1000; // 5s

    mWebServer = new CtsTestServer(getActivity());
    final String url = mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL);
    mWebServer.setDocumentAge(age);/*from  www .  j a  v  a  2  s.  c om*/
    mWebServer.setDocumentValidity(validity);

    mWebView.clearCache(true);
    new DelayedCheck(NETWORK_OPERATION_DELAY) {
        @Override
        protected boolean check() {
            CacheResult result = CacheManager.getCacheFile(url, null);
            return result == null;
        }
    }.run();
    final long time = System.currentTimeMillis();
    loadUrl(url);
    CacheResult result = CacheManager.getCacheFile(url, null);
    assertNotNull(result);
    assertNotNull(result.getInputStream());
    assertTrue(result.getContentLength() > 0);
    assertNull(result.getETag());
    assertEquals(time - age, DateUtils.parseDate(result.getLastModified()).getTime(), tolerance);
    File file = new File(CacheManager.getCacheFileBaseDir().getPath(), result.getLocalPath());
    assertTrue(file.exists());
    assertNull(result.getLocation());
    assertEquals("text/html", result.getMimeType());
    assertNull(result.getOutputStream());
    assertEquals(time + validity, result.getExpires(), tolerance);
    assertEquals(HttpStatus.SC_OK, result.getHttpStatusCode());
    assertNotNull(result.getEncoding());

    result.setEncoding("iso-8859-1");
    assertEquals("iso-8859-1", result.getEncoding());

    result.setInputStream(null);
    assertNull(result.getInputStream());
}

From source file:android.webkit.LoadListener.java

/**
 * Check the cache for the current URL, and load it if it is valid.
 *
 * @param headers for the request/*  w w w . j  a v a  2 s .co  m*/
 * @return true if cached response is used.
 */
boolean checkCache(Map<String, String> headers) {
    // Get the cache file name for the current URL
    CacheResult result = CacheManager.getCacheFile(url(), headers);

    if (result != null) {
        CacheLoader cacheLoader = new CacheLoader(this, result);

        // If I got a cachedUrl and the revalidation header was not
        // added, then the cached content valid, we should use it.
        if (!headers.containsKey(CacheManager.HEADER_KEY_IFNONEMATCH)
                && !headers.containsKey(CacheManager.HEADER_KEY_IFMODIFIEDSINCE)) {
            if (Config.LOGV) {
                Log.v(LOGTAG, "FrameLoader: HTTP URL in cache " + "and usable: " + url());
            }
            // Load the cached file
            cacheLoader.load();
            return true;
        } else {
            // The contents of the cache need to be revalidated
            // so just provide the listener with the cache loader
            // in the case that the server response positively to
            // the cached content.
            setCacheLoader(cacheLoader);
        }
    }
    return false;
}

From source file:android.webkit.LoadListener.java

static boolean willLoadFromCache(String url) {
    boolean inCache = CacheManager.getCacheFile(url, null) != null;
    if (Config.LOGV) {
        Log.v(LOGTAG, "willLoadFromCache: " + url + " in cache: " + inCache);
    }//from w  w w.  jav a2s.c o  m
    return inCache;
}