示例#1
0
        @Override
        protected Void doInBackground(final Void... params) {
          final Waypoint waypoint = new Waypoint(name, type, own);
          waypoint.setGeocode(geocode);
          waypoint.setPrefix(prefix);
          waypoint.setLookup(lookup);
          waypoint.setCoords(coordsToSave);
          waypoint.setNote(noteText);
          waypoint.setVisited(visited);
          waypoint.setId(waypointId);

          final Geocache cache = DataStore.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
          if (cache == null) {
            finishHandler.sendEmptyMessage(SAVE_ERROR);
            return null;
          }
          final Waypoint oldWaypoint = cache.getWaypointById(waypointId);
          if (cache.addOrChangeWaypoint(waypoint, true)) {
            DataStore.saveCache(cache, EnumSet.of(SaveFlag.DB));
            if (!StaticMapsProvider.hasAllStaticMapsForWaypoint(geocode, waypoint)) {
              StaticMapsProvider.removeWpStaticMaps(oldWaypoint, geocode);
              if (Settings.isStoreOfflineWpMaps()) {
                StaticMapsProvider.storeWaypointStaticMap(cache, waypoint).subscribe();
              }
            }
            if (modifyLocal.isChecked() || modifyBoth.isChecked()) {
              if (!cache.hasUserModifiedCoords()) {
                final Waypoint origWaypoint =
                    new Waypoint(
                        CgeoApplication.getInstance()
                            .getString(R.string.cache_coordinates_original),
                        WaypointType.ORIGINAL,
                        false);
                origWaypoint.setCoords(cache.getCoords());
                cache.addOrChangeWaypoint(origWaypoint, false);
                cache.setUserModifiedCoords(true);
              }
              cache.setCoords(waypoint.getCoords());
              DataStore.saveChangedCache(cache);
            }
            if (modifyBoth.isChecked() && waypoint.getCoords() != null) {
              finishHandler.sendEmptyMessage(UPLOAD_START);

              if (cache.supportsOwnCoordinates()) {
                final boolean result = uploadModifiedCoords(cache, waypoint.getCoords());
                finishHandler.sendEmptyMessage(result ? SUCCESS : UPLOAD_ERROR);
              } else {
                showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
                finishHandler.sendEmptyMessage(UPLOAD_NOT_POSSIBLE);
              }
            } else {
              finishHandler.sendEmptyMessage(SUCCESS);
            }
          } else {
            finishHandler.sendEmptyMessage(SAVE_ERROR);
          }
          return null;
        }
  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);
    }
  }