public static void testDownloadStaticMaps() {
    final double lat = 52.354176d;
    final double lon = 9.745685d;
    String geocode = "GCTEST1";

    boolean backupStore = Settings.isStoreOfflineMaps();
    boolean backupStoreWP = Settings.isStoreOfflineWpMaps();
    TestSettings.setStoreOfflineMaps(true);
    TestSettings.setStoreOfflineWpMaps(true);
    try {
      Geopoint gp = new Geopoint(lat + 0.25d, lon + 0.25d);
      Geocache cache = new Geocache();
      cache.setGeocode(geocode);
      cache.setCoords(gp);
      cache.setCacheId(String.valueOf(1));

      Waypoint theFinal = new Waypoint("Final", WaypointType.FINAL, false);
      Geopoint finalGp = new Geopoint(lat + 0.25d + 1, lon + 0.25d + 1);
      theFinal.setCoords(finalGp);
      theFinal.setId(1);
      cache.addOrChangeWaypoint(theFinal, false);

      Waypoint trailhead = new Waypoint("Trail head", WaypointType.TRAILHEAD, false);
      Geopoint trailheadGp = new Geopoint(lat + 0.25d + 2, lon + 0.25d + 2);
      trailhead.setCoords(trailheadGp);
      trailhead.setId(2);
      cache.addOrChangeWaypoint(trailhead, false);

      // make sure we don't have stale downloads
      deleteCacheDirectory(geocode);
      assertThat(StaticMapsProvider.hasStaticMap(cache)).isFalse();
      assertThat(StaticMapsProvider.hasStaticMapForWaypoint(geocode, theFinal)).isFalse();
      assertThat(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead)).isFalse();

      // download
      StaticMapsProvider.downloadMaps(cache).await();

      try {
        Thread.sleep(10000);
      } catch (InterruptedException e) {
        fail();
      }

      // check download
      assertThat(StaticMapsProvider.hasStaticMap(cache)).isTrue();
      assertThat(StaticMapsProvider.hasStaticMapForWaypoint(geocode, theFinal)).isTrue();
      assertThat(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead)).isTrue();

      // waypoint static maps hashcode dependent
      trailhead.setCoords(new Geopoint(lat + 0.24d + 2, lon + 0.25d + 2));
      assertThat(StaticMapsProvider.hasStaticMapForWaypoint(geocode, trailhead)).isFalse();
    } finally {
      TestSettings.setStoreOfflineWpMaps(backupStoreWP);
      TestSettings.setStoreOfflineMaps(backupStore);
      deleteCacheDirectory(geocode);
    }
  }
Esempio n. 2
0
  /**
   * @param data Retrieved data.
   * @return SearchResult. Never null.
   */
  public static SearchResult parseMapJSON(
      final String data, final Tile tile, final Bitmap bitmap, final LivemapStrategy strategy) {
    final SearchResult searchResult = new SearchResult();

    try {

      final LeastRecentlyUsedMap<String, String> nameCache =
          new LeastRecentlyUsedMap.LruCache<>(2000); // JSON id, cache name

      if (StringUtils.isEmpty(data)) {
        throw new ParserException("No page given");
      }

      // Example JSON information
      // {"grid":[....],
      //
      // "keys":["","55_55","55_54","17_25","55_53","17_27","17_26","57_53","57_55","3_62","3_61","57_54","3_60","15_27","15_26","15_25","4_60","4_61","4_62","16_25","16_26","16_27","2_62","2_60","2_61","56_53","56_54","56_55"],
      //  "data":{"55_55":[{"i":"gEaR","n":"Spiel & Sport"}],"55_54":[{"i":"gEaR","n":"Spiel &
      // Sport"}],"17_25":[{"i":"Rkzt","n":"EDSSW:  Rathaus "}],"55_53":[{"i":"gEaR","n":"Spiel &
      // Sport"}],"17_27":[{"i":"Rkzt","n":"EDSSW:  Rathaus "}],"17_26":[{"i":"Rkzt","n":"EDSSW:
      // Rathaus "}],"57_53":[{"i":"gEaR","n":"Spiel & Sport"}],"57_55":[{"i":"gEaR","n":"Spiel &
      // Sport"}],"3_62":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"3_61":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"57_54":[{"i":"gEaR","n":"Spiel & Sport"}],"3_60":[{"i":"gOWz","n":"Baumarktserie -
      // Wer Wo Was -"}],"15_27":[{"i":"Rkzt","n":"EDSSW:  Rathaus
      // "}],"15_26":[{"i":"Rkzt","n":"EDSSW:  Rathaus "}],"15_25":[{"i":"Rkzt","n":"EDSSW:  Rathaus
      // "}],"4_60":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"4_61":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"4_62":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"16_25":[{"i":"Rkzt","n":"EDSSW:  Rathaus "}],"16_26":[{"i":"Rkzt","n":"EDSSW:
      // Rathaus "}],"16_27":[{"i":"Rkzt","n":"EDSSW:  Rathaus
      // "}],"2_62":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"2_60":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"2_61":[{"i":"gOWz","n":"Baumarktserie - Wer Wo Was
      // -"}],"56_53":[{"i":"gEaR","n":"Spiel & Sport"}],"56_54":[{"i":"gEaR","n":"Spiel &
      // Sport"}],"56_55":[{"i":"gEaR","n":"Spiel & Sport"}]}
      //  }

      final ObjectNode json = (ObjectNode) JsonUtils.reader.readTree(data);

      final ArrayNode grid = (ArrayNode) json.get("grid");
      if (grid == null || grid.size() != (UTFGrid.GRID_MAXY + 1)) {
        throw new ParserException("No grid inside JSON");
      }
      final ArrayNode keys = (ArrayNode) json.get("keys");
      if (keys == null) {
        throw new ParserException("No keys inside JSON");
      }
      final ObjectNode dataObject = (ObjectNode) json.get("data");
      if (dataObject == null) {
        throw new ParserException("No data inside JSON");
      }

      // iterate over the data and construct all caches in this tile
      final Map<String, List<UTFGridPosition>> positions = new HashMap<>(); // JSON id as key
      final Map<String, List<UTFGridPosition>> singlePositions = new HashMap<>(); // JSON id as key

      for (final JsonNode rawKey : keys) {
        final String key = rawKey.asText();
        if (StringUtils.isNotBlank(key)) { // index 0 is empty
          final UTFGridPosition pos = UTFGridPosition.fromString(key);
          final ArrayNode dataForKey = (ArrayNode) dataObject.get(key);
          for (final JsonNode cacheInfo : dataForKey) {
            final String id = cacheInfo.get("i").asText();
            nameCache.put(id, cacheInfo.get("n").asText());

            List<UTFGridPosition> listOfPositions = positions.get(id);
            List<UTFGridPosition> singleListOfPositions = singlePositions.get(id);

            if (listOfPositions == null) {
              listOfPositions = new ArrayList<>();
              positions.put(id, listOfPositions);
              singleListOfPositions = new ArrayList<>();
              singlePositions.put(id, singleListOfPositions);
            }

            listOfPositions.add(pos);
            if (dataForKey.size() == 1) {
              singleListOfPositions.add(pos);
            }
          }
        }
      }

      final List<Geocache> caches = new ArrayList<>();
      for (final Entry<String, List<UTFGridPosition>> entry : positions.entrySet()) {
        final String id = entry.getKey();
        final List<UTFGridPosition> pos = entry.getValue();
        final UTFGridPosition xy = UTFGrid.getPositionInGrid(pos);
        final Geocache cache = new Geocache();
        cache.setDetailed(false);
        cache.setReliableLatLon(false);
        cache.setGeocode(id);
        cache.setName(nameCache.get(id));
        cache.setCoords(tile.getCoord(xy), tile.getZoomLevel());
        if (strategy.flags.contains(LivemapStrategy.Flag.PARSE_TILES) && bitmap != null) {
          for (final UTFGridPosition singlePos : singlePositions.get(id)) {
            if (IconDecoder.parseMapPNG(cache, bitmap, singlePos, tile.getZoomLevel())) {
              break; // cache parsed
            }
          }
        } else {
          cache.setType(CacheType.UNKNOWN, tile.getZoomLevel());
        }

        boolean exclude = false;
        if (Settings.isExcludeMyCaches()
            && (cache.isFound() || cache.isOwner())) { // workaround for BM
          exclude = true;
        }
        if (Settings.isExcludeDisabledCaches() && cache.isDisabled()) {
          exclude = true;
        }
        if (!Settings.getCacheType().contains(cache)
            && cache.getType() != CacheType.UNKNOWN) { // workaround for BM
          exclude = true;
        }
        if (!exclude) {
          caches.add(cache);
        }
      }
      searchResult.addAndPutInCache(caches);
      Log.d("Retrieved " + searchResult.getCount() + " caches for tile " + tile.toString());

    } catch (RuntimeException | ParserException | IOException e) {
      Log.e("GCMap.parseMapJSON", e);
    }

    return searchResult;
  }