Search in sources :

Example 1 with DiskLruCache

use of com.jakewharton.disklrucache.DiskLruCache in project iosched by google.

the class TileLoadingTask method loadInBackground.

@Override
public List<TileEntry> loadInBackground() {
    List<TileEntry> list = null;
    // Create a URI to get a cursor for all map tile entries.
    final Uri uri = ScheduleContract.MapTiles.buildUri();
    Cursor cursor = getContext().getContentResolver().query(uri, OverlayQuery.PROJECTION, null, null, null);
    if (cursor != null) {
        // Create a TileProvider for each entry in the cursor
        final int count = cursor.getCount();
        // Initialise the tile cache that is reused for all TileProviders.
        // Note that the cache *MUST* be closed when the encapsulating Fragment is stopped.
        DiskLruCache tileCache = MapUtils.openDiskCache(getContext());
        list = new ArrayList<>(count);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            final int floor = cursor.getInt(OverlayQuery.TILE_FLOOR);
            final String file = cursor.getString(OverlayQuery.TILE_FILE);
            File f = MapUtils.getTileFile(getContext().getApplicationContext(), file);
            if (f == null || !f.exists()) {
                // Skip the file if it is invalid or does not exist.
                LOGE(TAG, "Tile file not found for floor " + floor);
                break;
            }
            CachedTileProvider provider;
            try {
                SVGTileProvider svgProvider = new SVGTileProvider(f, mDPI);
                // Wrap the SVGTileProvider in a CachedTileProvider for caching on disk.
                provider = new CachedTileProvider(Integer.toString(floor), svgProvider, tileCache);
            } catch (IOException e) {
                LOGD(TAG, "Could not create Tile Provider.");
                break;
            }
            list.add(new TileEntry(floor, provider));
            cursor.moveToNext();
        }
        cursor.close();
    }
    return list;
}
Also used : DiskLruCache(com.jakewharton.disklrucache.DiskLruCache) IOException(java.io.IOException) Cursor(android.database.Cursor) Uri(android.net.Uri) File(java.io.File)

Example 2 with DiskLruCache

use of com.jakewharton.disklrucache.DiskLruCache in project iosched by google.

the class MapUtils method clearDiskCache.

public static void clearDiskCache(Context c) {
    DiskLruCache cache = openDiskCache(c);
    if (cache != null) {
        try {
            LOGD(TAG, "Clearing map tile disk cache");
            cache.delete();
            cache.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : DiskLruCache(com.jakewharton.disklrucache.DiskLruCache) IOException(java.io.IOException)

Example 3 with DiskLruCache

use of com.jakewharton.disklrucache.DiskLruCache in project enroscar by stanfy.

the class BaseFileCacheTest method testCache.

@Test
public void testCache() throws IOException {
    final String text = "ABC";
    getWebServer().enqueue(new MockResponse().setBody(text));
    final URL url = getWebServer().getUrl("/");
    final URLConnection connection = url.openConnection();
    // real request has been successfully performed
    assertResponse(connection, text, false);
    // cache entry has been written
    assertThat(cache.getWriteSuccessCount()).isEqualTo(1);
    assertThat(cache.getHitCount()).isZero();
    final URLConnection secondConnection = url.openConnection();
    // real request has not been performed
    assertResponse(secondConnection, text, true);
    // nothing has been written
    assertThat(cache.getWriteSuccessCount()).isEqualTo(1);
    assertThat(cache.getHitCount()).isEqualTo(1);
    // check disk content
    final DiskLruCache diskCache = cache.getDiskCache();
    assertThat(diskCache.size()).isGreaterThan((long) text.length());
    // 3 filed should be here: journal, body, metadata
    assertThat(diskCache.getDirectory().list().length).isEqualTo(3);
}
Also used : MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) DiskLruCache(com.jakewharton.disklrucache.DiskLruCache) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Aggregations

DiskLruCache (com.jakewharton.disklrucache.DiskLruCache)3 IOException (java.io.IOException)2 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)1 File (java.io.File)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Test (org.junit.Test)1