Exemple #1
0
 public synchronized void close() {
   imagesOnFS.clear();
   indexFileNames.clear();
   basemapFileNames.clear();
   renderer.clearAllResources();
   closeAmenities();
   closeRouteFiles();
   closeAddresses();
   closeTransport();
 }
Exemple #2
0
  public List<String> indexingMaps(final IProgress progress) {
    long val = System.currentTimeMillis();
    ArrayList<File> files = new ArrayList<File>();
    File appPath = context.getAppPath(null);
    File roadsPath = context.getAppPath(IndexConstants.ROADS_INDEX_DIR);
    roadsPath.mkdirs();

    collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
    renameRoadsFiles(files, roadsPath);
    collectFiles(roadsPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
    if (!Version.isFreeVersion(context)) {
      collectFiles(
          context.getAppPath(IndexConstants.WIKI_INDEX_DIR),
          IndexConstants.BINARY_MAP_INDEX_EXT,
          files);
    }
    if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) {
      collectFiles(
          context.getAppPath(IndexConstants.SRTM_INDEX_DIR),
          IndexConstants.BINARY_MAP_INDEX_EXT,
          files);
    }

    if (context.getSettings().BETA_TESTING_LIVE_UPDATES.get()) {
      changesManager.collectChangesFiles(
          context.getAppPath(IndexConstants.LIVE_INDEX_DIR),
          IndexConstants.BINARY_MAP_INDEX_EXT,
          files);
    }

    Collections.sort(files, Algorithms.getFileVersionComparator());
    List<String> warnings = new ArrayList<String>();
    renderer.clearAllResources();
    CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes();
    File indCache = context.getAppPath(INDEXES_CACHE);
    if (indCache.exists()) {
      try {
        cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION);
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }
    File liveDir = context.getAppPath(IndexConstants.LIVE_INDEX_DIR);
    for (File f : files) {
      progress.startTask(
          context.getString(R.string.indexing_map) + " " + f.getName(), -1); // $NON-NLS-1$
      try {
        BinaryMapIndexReader mapReader = null;
        try {
          mapReader = cachedOsmandIndexes.getReader(f);
          if (mapReader.getVersion() != IndexConstants.BINARY_MAP_VERSION) {
            mapReader = null;
          }
          if (mapReader != null) {
            renderer.initializeNewResource(progress, f, mapReader);
          }
        } catch (IOException e) {
          log.error(String.format("File %s could not be read", f.getName()), e);
        }
        if (mapReader == null
            || (Version.isFreeVersion(context)
                && (f.getName().contains("_wiki") || f.getName().contains(".wiki")))) {
          warnings.add(
              MessageFormat.format(
                  context.getString(R.string.version_index_is_not_supported),
                  f.getName())); // $NON-NLS-1$
        } else {
          if (mapReader.isBasemap()) {
            basemapFileNames.put(f.getName(), f.getName());
          }
          long dateCreated = mapReader.getDateCreated();
          if (dateCreated == 0) {
            dateCreated = f.lastModified();
          }
          if (f.getParentFile().getName().equals(liveDir.getName())) {
            boolean toUse = changesManager.index(f, dateCreated, mapReader);
            if (!toUse) {
              try {
                mapReader.close();
              } catch (IOException e) {
                log.error(e.getMessage(), e);
              }
              continue;
            }
          } else {
            changesManager.indexMainMap(f, dateCreated);
            indexFileNames.put(f.getName(), dateFormat.format(dateCreated)); // $NON-NLS-1$
          }
          if (!mapReader.getRegionNames().isEmpty()) {
            try {
              RandomAccessFile raf = new RandomAccessFile(f, "r"); // $NON-NLS-1$
              RegionAddressRepositoryBinary rarb =
                  new RegionAddressRepositoryBinary(
                      this, new BinaryMapIndexReader(raf, mapReader), f.getName());
              addressMap.put(f.getName(), rarb);
            } catch (IOException e) {
              log.error("Exception reading " + f.getAbsolutePath(), e); // $NON-NLS-1$
              warnings.add(
                  MessageFormat.format(
                      context.getString(R.string.version_index_is_not_supported),
                      f.getName())); // $NON-NLS-1$
            }
          }
          if (mapReader.hasTransportData()) {
            try {
              RandomAccessFile raf = new RandomAccessFile(f, "r"); // $NON-NLS-1$
              transportRepositories.put(
                  f.getName(),
                  new TransportIndexRepositoryBinary(new BinaryMapIndexReader(raf, mapReader)));
            } catch (IOException e) {
              log.error("Exception reading " + f.getAbsolutePath(), e); // $NON-NLS-1$
              warnings.add(
                  MessageFormat.format(
                      context.getString(R.string.version_index_is_not_supported),
                      f.getName())); // $NON-NLS-1$
            }
          }
          if (mapReader.containsRouteData()) {
            try {
              RandomAccessFile raf = new RandomAccessFile(f, "r"); // $NON-NLS-1$
              routingMapFiles.put(f.getName(), new BinaryMapIndexReader(raf, mapReader));
            } catch (IOException e) {
              log.error("Exception reading " + f.getAbsolutePath(), e); // $NON-NLS-1$
              warnings.add(
                  MessageFormat.format(
                      context.getString(R.string.version_index_is_not_supported),
                      f.getName())); // $NON-NLS-1$
            }
          }
          if (mapReader.containsPoiData()) {
            try {
              RandomAccessFile raf = new RandomAccessFile(f, "r"); // $NON-NLS-1$
              amenityRepositories.put(
                  f.getName(),
                  new AmenityIndexRepositoryBinary(new BinaryMapIndexReader(raf, mapReader)));
            } catch (IOException e) {
              log.error("Exception reading " + f.getAbsolutePath(), e); // $NON-NLS-1$
              warnings.add(
                  MessageFormat.format(
                      context.getString(R.string.version_index_is_not_supported),
                      f.getName())); // $NON-NLS-1$
            }
          }
        }
      } catch (SQLiteException e) {
        log.error("Exception reading " + f.getAbsolutePath(), e); // $NON-NLS-1$
        warnings.add(
            MessageFormat.format(
                context.getString(R.string.version_index_is_not_supported),
                f.getName())); // $NON-NLS-1$
      } catch (OutOfMemoryError oome) {
        log.error("Exception reading " + f.getAbsolutePath(), oome); // $NON-NLS-1$
        warnings.add(
            MessageFormat.format(
                context.getString(R.string.version_index_is_big_for_memory), f.getName()));
      }
    }
    log.debug("All map files initialized " + (System.currentTimeMillis() - val) + " ms");
    if (files.size() > 0 && (!indCache.exists() || indCache.canWrite())) {
      try {
        cachedOsmandIndexes.writeToFile(indCache);
      } catch (Exception e) {
        log.error("Index file could not be written", e);
      }
    }
    return warnings;
  }