Search in sources :

Example 1 with MapTileDownloader

use of net.osmand.map.MapTileDownloader in project Osmand by osmandapp.

the class DownloadTilesDialog method run.

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.hasTileForMapSync(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) {
                // $NON-NLS-1$
                log.error("Exception while downloading tiles ", e);
                instance.refuseAllPreviousRequests();
            } finally {
                instance.clearCallbacks();
                for (IMapDownloaderCallback cbck : previousCallbacks) {
                    instance.addDownloaderCallback(cbck);
                }
                app.getResourceManager().reloadTilesFromFS();
            }
            progressDlg.dismiss();
        }
    };
    // $NON-NLS-1$
    new Thread(r, "Downloading tiles").start();
    progressDlg.show();
}
Also used : IMapDownloaderCallback(net.osmand.map.MapTileDownloader.IMapDownloaderCallback) DialogInterface(android.content.DialogInterface) DownloadRequest(net.osmand.map.MapTileDownloader.DownloadRequest) ResourceManager(net.osmand.plus.resources.ResourceManager) ProgressDialog(android.app.ProgressDialog) MapTileDownloader(net.osmand.map.MapTileDownloader)

Example 2 with MapTileDownloader

use of net.osmand.map.MapTileDownloader in project OsmAnd-tools by osmandapp.

the class TileBundleDownloadDialog method downloadTiles.

public void downloadTiles() {
    setVisible(false);
    // $NON-NLS-1$
    final ProgressDialog progressDialog = new ProgressDialog(this, Messages.getString("TileBundleDownloadDialog.DOWNLOADING.TILES"));
    int numberTiles = 0;
    final int startZoom = (Integer) startSpinner.getValue();
    final int endZoom = (Integer) endSpinner.getValue();
    for (int zoom = startZoom; zoom <= endZoom; zoom++) {
        int x1 = (int) MapUtils.getTileNumberX(zoom, selectionArea.getLon1());
        int x2 = (int) MapUtils.getTileNumberX(zoom, selectionArea.getLon2());
        int y1 = (int) MapUtils.getTileNumberY(zoom, selectionArea.getLat1());
        int y2 = (int) MapUtils.getTileNumberY(zoom, selectionArea.getLat2());
        numberTiles += (x2 - x1 + 1) * (y2 - y1 + 1);
    }
    final int number = numberTiles;
    final MapTileDownloader instance = MapTileDownloader.getInstance(MapCreatorVersion.APP_MAP_CREATOR_VERSION);
    progressDialog.setRunnable(new Runnable() {

        @Override
        public void run() {
            // $NON-NLS-1$
            progressDialog.startTask(Messages.getString("TileBundleDownloadDialog.LOADING"), number);
            for (int zoom = startZoom; zoom <= endZoom; zoom++) {
                int x1 = (int) MapUtils.getTileNumberX(zoom, selectionArea.getLon1());
                int x2 = (int) MapUtils.getTileNumberX(zoom, selectionArea.getLon2());
                int y1 = (int) MapUtils.getTileNumberY(zoom, selectionArea.getLat1());
                int y2 = (int) MapUtils.getTileNumberY(zoom, selectionArea.getLat2());
                for (int x = x1; x <= x2; x++) {
                    for (int y = y1; y <= y2; y++) {
                        String file = getFileForImage(x, y, zoom, map.getTileFormat());
                        if (new File(tilesLocation, file).exists()) {
                            progressDialog.progress(1);
                        } else {
                            DownloadRequest req = new DownloadRequest(map.getUrlToLoad(x, y, zoom), new File(tilesLocation, file), x, y, zoom);
                            instance.requestToDownload(req);
                        }
                    }
                }
                while (instance.isSomethingBeingDownloaded()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new IllegalArgumentException(e);
                    }
                }
            }
        }
    });
    ArrayList<IMapDownloaderCallback> previousCallbacks = new ArrayList<IMapDownloaderCallback>(instance.getDownloaderCallbacks());
    instance.getDownloaderCallbacks().clear();
    instance.addDownloaderCallback(new IMapDownloaderCallback() {

        @Override
        public void tileDownloaded(DownloadRequest request) {
            // TODO request could be null if bundle loading?
            progressDialog.progress(1);
        }
    });
    try {
        progressDialog.run();
        instance.refuseAllPreviousRequests();
    } catch (InvocationTargetException e) {
        ExceptionHandler.handle(e.getCause());
    } catch (InterruptedException e) {
        ExceptionHandler.handle(e);
    } finally {
        instance.getDownloaderCallbacks().clear();
        instance.getDownloaderCallbacks().addAll(previousCallbacks);
    }
}
Also used : IMapDownloaderCallback(net.osmand.map.MapTileDownloader.IMapDownloaderCallback) ArrayList(java.util.ArrayList) DownloadRequest(net.osmand.map.MapTileDownloader.DownloadRequest) InvocationTargetException(java.lang.reflect.InvocationTargetException) MapTileDownloader(net.osmand.map.MapTileDownloader) File(java.io.File)

Aggregations

MapTileDownloader (net.osmand.map.MapTileDownloader)2 DownloadRequest (net.osmand.map.MapTileDownloader.DownloadRequest)2 IMapDownloaderCallback (net.osmand.map.MapTileDownloader.IMapDownloaderCallback)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 ResourceManager (net.osmand.plus.resources.ResourceManager)1