Search in sources :

Example 1 with SVGBuilder

use of com.larvalabs.svgandroid.SVGBuilder in project iosched by google.

the class ConferenceDataHandler method processMapOverlayFiles.

/**
     * Synchronise the map overlay files either from the local assets (if available) or from a remote url.
     *
     * @param collection Set of tiles containing a local filename and remote url.
     * @throws IOException
     */
private void processMapOverlayFiles(Collection<Tile> collection, boolean downloadAllowed) throws IOException, SVGParseException {
    // clear the tile cache on disk if any tiles have been updated
    boolean shouldClearCache = false;
    // keep track of used files, unused files are removed
    ArrayList<String> usedTiles = new ArrayList<>();
    for (Tile tile : collection) {
        final String filename = tile.filename;
        final String url = tile.url;
        usedTiles.add(filename);
        if (!MapUtils.hasTile(mContext, filename)) {
            shouldClearCache = true;
            // copy or download the tile if it is not stored yet
            if (MapUtils.hasTileAsset(mContext, filename)) {
                // file already exists as an asset, copy it
                MapUtils.copyTileAsset(mContext, filename);
            } else if (downloadAllowed && !TextUtils.isEmpty(url)) {
                try {
                    // download the file only if downloads are allowed and url is not empty
                    File tileFile = MapUtils.getTileFile(mContext, filename);
                    BasicHttpClient httpClient = new BasicHttpClient();
                    httpClient.setRequestLogger(mQuietLogger);
                    IOUtils.authorizeHttpClient(mContext, httpClient);
                    HttpResponse httpResponse = httpClient.get(url, null);
                    IOUtils.writeToFile(httpResponse.getBody(), tileFile);
                    // ensure the file is valid SVG
                    InputStream is = new FileInputStream(tileFile);
                    SVG svg = new SVGBuilder().readFromInputStream(is).build();
                    is.close();
                } catch (IOException ex) {
                    LOGE(TAG, "FAILED downloading map overlay tile " + url + ": " + ex.getMessage(), ex);
                } catch (SVGParseException ex) {
                    LOGE(TAG, "FAILED parsing map overlay tile " + url + ": " + ex.getMessage(), ex);
                }
            } else {
                LOGD(TAG, "Skipping download of map overlay tile" + " (since downloadsAllowed=false)");
            }
        }
    }
    if (shouldClearCache) {
        MapUtils.clearDiskCache(mContext);
    }
    MapUtils.removeUnusedTiles(mContext, usedTiles);
}
Also used : SVG(com.larvalabs.svgandroid.SVG) ArrayList(java.util.ArrayList) Tile(com.google.samples.apps.iosched.io.map.model.Tile) BasicHttpClient(com.turbomanage.httpclient.BasicHttpClient) HttpResponse(com.turbomanage.httpclient.HttpResponse) SVGBuilder(com.larvalabs.svgandroid.SVGBuilder) SVGParseException(com.larvalabs.svgandroid.SVGParseException)

Aggregations

Tile (com.google.samples.apps.iosched.io.map.model.Tile)1 SVG (com.larvalabs.svgandroid.SVG)1 SVGBuilder (com.larvalabs.svgandroid.SVGBuilder)1 SVGParseException (com.larvalabs.svgandroid.SVGParseException)1 BasicHttpClient (com.turbomanage.httpclient.BasicHttpClient)1 HttpResponse (com.turbomanage.httpclient.HttpResponse)1 ArrayList (java.util.ArrayList)1