Exemple #1
0
  public ResourceManager(OsmandApplication context) {

    this.context = context;
    this.renderer = new MapRenderRepositories(context);
    asyncLoadingThread.start();
    renderingBufferImageThread = new HandlerThread("RenderingBaseImage");
    renderingBufferImageThread.start();

    tileDownloader = MapTileDownloader.getInstance(Version.getFullVersion(context));
    dateFormat = DateFormat.getDateFormat(context);
    resetStoreDirectory();
    WindowManager mgr = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    mgr.getDefaultDisplay().getMetrics(dm);
    // Only 8 MB (from 16 Mb whole mem) available for images : image 64K * 128 = 8 MB (8 bit), 64 -
    // 16 bit, 32 - 32 bit
    // at least 3*9?
    float tiles = (dm.widthPixels / 256 + 2) * (dm.heightPixels / 256 + 2) * 3;
    log.info("Tiles to load in memory : " + tiles);
    maxImgCacheSize = (int) (tiles);
  }
  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();
  }