Exemplo n.º 1
0
 private void updateLabel(
     final int zoom,
     final QuadRect latlonRect,
     final TextView downloadText,
     final String template,
     int progress) {
   int numberTiles = 0;
   for (int z = zoom; z <= progress + zoom; z++) {
     int x1 = (int) MapUtils.getTileNumberX(z, latlonRect.left);
     int x2 = (int) MapUtils.getTileNumberX(z, latlonRect.right);
     int y1 = (int) MapUtils.getTileNumberY(z, latlonRect.top);
     int y2 = (int) MapUtils.getTileNumberY(z, latlonRect.bottom);
     numberTiles += (x2 - x1 + 1) * (y2 - y1 + 1);
   }
   downloadText.setText(
       MessageFormat.format(
           template,
           (progress + zoom) + "", // $NON-NLS-1$
           numberTiles,
           (double) numberTiles * 12 / 1000));
 }
Exemplo n.º 2
0
  public void run(
      final int zoom, final int progress, final QuadRect latlonRect, final ITileSource map) {
    cancel = false;
    int numberTiles = 0;
    for (int z = zoom; z <= progress + zoom; z++) {
      int x1 = (int) MapUtils.getTileNumberX(z, latlonRect.left);
      int x2 = (int) MapUtils.getTileNumberX(z, latlonRect.right);
      int y1 = (int) MapUtils.getTileNumberY(z, latlonRect.top);
      int y2 = (int) MapUtils.getTileNumberY(z, latlonRect.bottom);
      numberTiles += (x2 - x1 + 1) * (y2 - y1 + 1);
    }
    final ProgressDialog progressDlg = new ProgressDialog(ctx);
    progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDlg.setMessage(
        ctx.getString(R.string.shared_string_downloading)
            + ctx.getString(R.string.shared_string_ellipsis));
    progressDlg.setCancelable(true);
    progressDlg.setMax(numberTiles);
    progressDlg.setOnCancelListener(
        new DialogInterface.OnCancelListener() {

          @Override
          public void onCancel(DialogInterface dialog) {
            cancel = true;
          }
        });

    final MapTileDownloader instance = MapTileDownloader.getInstance(Version.getFullVersion(app));

    final List<IMapDownloaderCallback> previousCallbacks = instance.getDownloaderCallbacks();
    instance.clearCallbacks();
    callback =
        new IMapDownloaderCallback() {
          @Override
          public void tileDownloaded(DownloadRequest request) {
            if (request != null) {
              progressDlg.setProgress(progressDlg.getProgress() + 1);
            }
          }
        };
    instance.addDownloaderCallback(callback);

    Runnable r =
        new Runnable() {
          @Override
          public void run() {
            int requests = 0;
            int limitRequests = 50;
            try {
              ResourceManager rm = app.getResourceManager();
              for (int z = zoom; z <= zoom + progress && !cancel; z++) {
                int x1 = (int) MapUtils.getTileNumberX(z, latlonRect.left);
                int x2 = (int) MapUtils.getTileNumberX(z, latlonRect.right);
                int y1 = (int) MapUtils.getTileNumberY(z, latlonRect.top);
                int y2 = (int) MapUtils.getTileNumberY(z, latlonRect.bottom);
                for (int x = x1; x <= x2 && !cancel; x++) {
                  for (int y = y1; y <= y2 && !cancel; y++) {
                    String tileId = rm.calculateTileId(map, x, y, z);
                    if (rm.tileExistOnFileSystem(tileId, map, x, y, z)) {
                      progressDlg.setProgress(progressDlg.getProgress() + 1);
                    } else {
                      rm.getTileImageForMapSync(tileId, map, x, y, z, true);
                      requests++;
                    }
                    if (!cancel) {
                      if (requests >= limitRequests) {
                        requests = 0;

                        while (instance.isSomethingBeingDownloaded()) {
                          try {
                            Thread.sleep(500);
                          } catch (InterruptedException e) {
                            throw new IllegalArgumentException(e);
                          }
                        }
                      }
                    }
                  }
                }
              }
              if (cancel) {
                instance.refuseAllPreviousRequests();
              } else {
                while (instance.isSomethingBeingDownloaded()) {
                  try {
                    Thread.sleep(500);
                  } catch (InterruptedException e) {
                    throw new IllegalArgumentException(e);
                  }
                }
              }
              mapView.refreshMap();
              callback = null;
            } catch (Exception e) {
              log.error("Exception while downloading tiles ", e); // $NON-NLS-1$
              instance.refuseAllPreviousRequests();
            } finally {
              instance.clearCallbacks();
              for (IMapDownloaderCallback cbck : previousCallbacks) {
                instance.addDownloaderCallback(cbck);
              }
              app.getResourceManager().reloadTilesFromFS();
            }
            progressDlg.dismiss();
          }
        };

    new Thread(r, "Downloading tiles").start(); // $NON-NLS-1$
    progressDlg.show();
  }