public void setupMBTilesBackgroundConfiguration() {

    final String defType =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext())
            .getString("mapsforge_background_type", null);
    final String mbTilesPath =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext())
            .getString("mapsforge_background_filepath", null);

    if (mbTilesPath == null
        && defType == null) { // only on first run, check files and set geocollect defaults

      // geocollect default not setup yet, check if mbtiles file is available
      final File dir =
          new File(MapFilesProvider.getEnvironmentDirPath(null) + MapFilesProvider.getBaseDir());
      File mbtileFile = null;
      File files[] = dir.listFiles();
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
          final String path = files[i].getAbsolutePath();
          if (path.substring(path.lastIndexOf(".") + 1).equals("mbtiles")) {
            mbtileFile = files[i];
            break;
          }
        }
        final Editor ed = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        if (mbtileFile != null) {
          ed.putString("mapsforge_background_filepath", mbtileFile.getAbsolutePath());
        }
        ed.putString("mapsforge_background_type", "1");
        ed.commit();
      }
    }

    final BackgroundSourceType type =
        BackgroundSourceType.values()[Integer.parseInt(defType == null ? "1" : defType)];
    MapFilesProvider.setBackgroundSourceType(type);
  }
Exemple #2
0
  /**
   * checks if the preferences of the background renderer changed if so, the mapview is informed and
   * is cleared and redrawed
   */
  public void checkIfMapViewNeedsBackgroundUpdate() {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final boolean thingsChanged =
        prefs.getBoolean(MapView.MAPSFORGE_BACKGROUND_FILEPATH_CHANGED, false);
    if (!thingsChanged) return;

    final BackgroundSourceType currentMapRendererType = this.mapView.getMapRendererType();

    String filePath = prefs.getString(MapView.MAPSFORGE_BACKGROUND_FILEPATH, null);
    final String defaultType =
        getApplicationContext().getPackageName().equals("it.geosolutions.geocollect.android.app")
            ? "1"
            : "0";
    BackgroundSourceType type =
        BackgroundSourceType.values()[
            Integer.parseInt(
                prefs.getString(MapView.MAPSFORGE_BACKGROUND_RENDERER_TYPE, defaultType))];

    final Editor ed = prefs.edit();
    ed.putBoolean(MapView.MAPSFORGE_BACKGROUND_FILEPATH_CHANGED, false);
    ed.commit();

    File mapFile = new File(filePath);
    if (mapFile == null || !mapFile.exists()) {
      mapFile = MapFilesProvider.getBackgroundMapFile();
      filePath = mapFile.getPath();
      type = BackgroundSourceType.MAPSFORGE;
    }

    // 1. renderer changed
    if (type != currentMapRendererType) {

      MapRenderer mapRenderer = null;
      switch (type) {
        case MAPSFORGE:
          if (filePath == null) {
            throw new IllegalArgumentException(
                "no filepath selected to change to mapsforge renderer");
          }
          mapView.setMapFile(new File(filePath));
          mapRenderer = new DatabaseRenderer(mapView.getMapDatabase());
          // TODO it was MBTILES with no or dimmed mbtiles layer, add MBTiles layer ?

          MSMMap map = SpatialDbUtils.mapFromDb(false);
          Log.d(
              MapsActivity.class.getSimpleName(),
              "to mapsforge maps includes " + map.layers.size() + " layers");

          addLayersOrdered(map.layers);

          break;
        case MBTILES:
          mapRenderer = new MbTilesDatabaseRenderer(getBaseContext(), filePath);

          MSMMap map2 = SpatialDbUtils.mapFromDb(true);

          layerManager.setLayers(map2.layers);

          break;
        case GEOCOLLECT:
          // TODO
          break;
        default:
          break;
      }
      if (mDrawerToggle != null) {
        mDrawerToggle.syncState();
      }
      mapView.setRenderer(mapRenderer, true);
      mapView.clearAndRedrawMapView();
      MapFilesProvider.setBackgroundSourceType(type);

    } else if (filePath != null && !filePath.equals(mapView.getMapRenderer().getFileName())) {

      // 2.renderer is the same but file changed
      switch (type) {
        case MAPSFORGE:
          mapView.setMapFile(new File(filePath));
          break;
        case MBTILES:
          mapView.setRenderer(new MbTilesDatabaseRenderer(getBaseContext(), filePath), true);
          break;
        case GEOCOLLECT:
          // TODO
          break;
        default:
          break;
      }
      mapView.clearAndRedrawMapView();
    }
  }