Ejemplo n.º 1
0
  private static void addFolderToZip(
      final File folder, final ZipOutputStream zip, final String baseName) throws IOException {
    final File[] files = folder.listFiles();
    /* For each child (subdirectory/child-file). */
    for (final File file : files) {
      if (file.isDirectory()) {
        /* If the file is a folder, do recursrion with this folder.*/
        addFolderToZip(file, zip, baseName);
      } else {
        /* Otherwise zip it as usual. */
        String name;

        if (baseName == null) {
          name = file.getAbsolutePath();
        } else {
          name = file.getAbsolutePath().substring(baseName.length());
        }

        final ZipEntry zipEntry = new ZipEntry(name);
        zip.putNextEntry(zipEntry);
        final FileInputStream fileIn = new FileInputStream(file);
        StreamUtils.copy(fileIn, zip);
        //				StreamUtils.closeStream(fileIn);
        fileIn.close();
        zip.closeEntry();
      }
    }
  }
    @Override
    public Drawable loadTile(final MapTileRequestState pState) {

      ITileSource tileSource = mTileSource.get();
      if (tileSource == null) {
        return null;
      }

      final MapTile pTile = pState.getMapTile();

      // if there's no sdcard then don't do anything
      if (!getSdCardAvailable()) {
        if (OpenStreetMapTileProviderConstants.DEBUGMODE) {
          Log.d(IMapView.LOGTAG, "No sdcard - do nothing for tile: " + pTile);
        }
        return null;
      }

      InputStream inputStream = null;
      try {
        if (OpenStreetMapTileProviderConstants.DEBUGMODE) {
          Log.d(IMapView.LOGTAG, "Tile doesn't exist: " + pTile);
        }

        inputStream = getInputStream(pTile, tileSource);
        if (inputStream != null) {
          if (OpenStreetMapTileProviderConstants.DEBUGMODE) {
            Log.d(IMapView.LOGTAG, "Use tile from archive: " + pTile);
          }
          final Drawable drawable = tileSource.getDrawable(inputStream);
          return drawable;
        }
      } catch (final Throwable e) {
        Log.e(IMapView.LOGTAG, "Error loading tile", e);
      } finally {
        if (inputStream != null) {
          StreamUtils.closeStream(inputStream);
        }
      }

      return null;
    }
Ejemplo n.º 3
0
  public Route request(
      final Context ctx,
      final DirectionsLanguage nat,
      final GeoPoint start,
      final List<GeoPoint> vias,
      final GeoPoint end,
      final RoutePreferenceType pRoutePreference,
      final boolean pProvideGeometry,
      final boolean pAvoidTolls,
      final boolean pAvoidHighways,
      final boolean pRequestHandle,
      final ArrayList<AreaOfInterest> pAvoidAreas,
      final boolean pSaveRoute)
      throws MalformedURLException, IOException, SAXException, ORSException {
    final String cloudmadeurl =
        "http://routes.cloudmade.com/"
            + CloudmadeUtil.getCloudmadeKey()
            + "/api/0.3/"
            + CloudmadeRSRequestComposer.create(nat, start, vias, end, pRoutePreference);
    Log.d(OSMConstants.DEBUGTAG, "Cloudmade url " + cloudmadeurl);
    final URL requestURL = new URL(cloudmadeurl);

    final HttpURLConnection acon = (HttpURLConnection) requestURL.openConnection();
    acon.setAllowUserInteraction(false);
    acon.setRequestMethod("GET");
    acon.setRequestProperty("Content-Type", "application/xml");
    acon.setDoOutput(true);
    acon.setDoInput(true);
    acon.setUseCaches(false);

    /* Get a SAXParser from the SAXPArserFactory. */
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp;
    try {
      sp = spf.newSAXParser();
    } catch (final ParserConfigurationException e) {
      throw new SAXException(e);
    }

    /* Get the XMLReader of the SAXParser we created. */
    final XMLReader xr = sp.getXMLReader();
    /* Create a new ContentHandler and apply it to the XML-Reader*/
    final CloudmadeRSParser openLSParser = new CloudmadeRSParser();
    xr.setContentHandler(openLSParser);

    /* Parse the xml-data from our URL. */
    xr.parse(new InputSource(new BufferedInputStream(acon.getInputStream())));

    /* The Handler now provides the parsed data to us. */
    final Route r = openLSParser.getRoute();
    r.finalizeRoute(vias);

    if (pSaveRoute) {
      /* Exception would have been thrown in invalid route. */
      try {
        // Ensure folder exists
        final String traceFolderPath =
            Util.getAndRoadExternalStoragePath() + SDCARD_SAVEDROUTES_PATH;
        new File(traceFolderPath).mkdirs();

        // Create file and ensure that needed folders exist.
        final String filename = traceFolderPath + SDF.format(new Date(System.currentTimeMillis()));
        final File dest = new File(filename + ".route");

        // Write Data
        final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));

        out.writeObject(r);
        StreamUtils.closeStream(out);
      } catch (final Exception e) {
        Log.e(OSMConstants.DEBUGTAG, "File-Writing-Error", e);
      }
    }

    return r;
  }