Search in sources :

Example 6 with IndexItem

use of net.osmand.plus.download.IndexItem in project Osmand by osmandapp.

the class MenuController method buildMapDownloadButtonAndSizeInfo.

public void buildMapDownloadButtonAndSizeInfo(final LatLon latLon) {
    new AsyncTask<Void, Void, BinaryMapDataObject>() {

        ResourceManager rm;

        OsmandRegions osmandRegions;

        String selectedFullName = "";

        @Override
        protected void onPreExecute() {
            rm = getMapActivity().getMyApplication().getResourceManager();
            osmandRegions = rm.getOsmandRegions();
        }

        @Override
        protected BinaryMapDataObject doInBackground(Void... voids) {
            int point31x = MapUtils.get31TileNumberX(latLon.getLongitude());
            int point31y = MapUtils.get31TileNumberY(latLon.getLatitude());
            List<BinaryMapDataObject> mapDataObjects = null;
            try {
                mapDataObjects = osmandRegions.queryBbox(point31x, point31x, point31y, point31y);
            } catch (IOException e) {
                e.printStackTrace();
            }
            BinaryMapDataObject binaryMapDataObject = null;
            if (mapDataObjects != null) {
                Iterator<BinaryMapDataObject> it = mapDataObjects.iterator();
                while (it.hasNext()) {
                    BinaryMapDataObject o = it.next();
                    if (o.getTypes() != null) {
                        boolean isRegion = true;
                        for (int i = 0; i < o.getTypes().length; i++) {
                            TagValuePair tp = o.getMapIndex().decodeType(o.getTypes()[i]);
                            if ("boundary".equals(tp.value)) {
                                isRegion = false;
                                break;
                            }
                        }
                        if (!isRegion || !osmandRegions.contain(o, point31x, point31y)) {
                            it.remove();
                        }
                    }
                }
                double smallestArea = -1;
                for (BinaryMapDataObject o : mapDataObjects) {
                    String downloadName = osmandRegions.getDownloadName(o);
                    if (!Algorithms.isEmpty(downloadName)) {
                        boolean downloaded = checkIfObjectDownloaded(rm, downloadName);
                        if (downloaded) {
                            binaryMapDataObject = null;
                            break;
                        } else {
                            String fullName = osmandRegions.getFullName(o);
                            WorldRegion region = osmandRegions.getRegionData(fullName);
                            if (region != null && region.isRegionMapDownload()) {
                                double area = OsmandRegions.getArea(o);
                                if (smallestArea == -1) {
                                    smallestArea = area;
                                    selectedFullName = fullName;
                                    binaryMapDataObject = o;
                                } else if (area < smallestArea) {
                                    smallestArea = area;
                                    selectedFullName = fullName;
                                    binaryMapDataObject = o;
                                }
                            }
                        }
                    }
                }
            }
            return binaryMapDataObject;
        }

        @Override
        protected void onPostExecute(BinaryMapDataObject binaryMapDataObject) {
            downloadMapDataObject = binaryMapDataObject;
            downloaded = downloadMapDataObject == null;
            if (!downloaded) {
                downloadThread = getMapActivity().getMyApplication().getDownloadThread();
                downloadRegion = osmandRegions.getRegionData(selectedFullName);
                if (downloadRegion != null && downloadRegion.isRegionMapDownload()) {
                    List<IndexItem> indexItems = downloadThread.getIndexes().getIndexItems(downloadRegion);
                    for (IndexItem item : indexItems) {
                        if (item.getType() == DownloadActivityType.NORMAL_FILE && (item.isDownloaded() || downloadThread.isDownloading(item))) {
                            indexItem = item;
                        }
                    }
                }
                leftDownloadButtonController = new TitleButtonController() {

                    @Override
                    public void buttonPressed() {
                        if (indexItem != null) {
                            if (indexItem.getType() == DownloadActivityType.NORMAL_FILE) {
                                new DownloadValidationManager(getMapActivity().getMyApplication()).startDownload(getMapActivity(), indexItem);
                            }
                        }
                    }
                };
                leftDownloadButtonController.caption = downloadRegion != null ? downloadRegion.getLocaleName() : getMapActivity().getString(R.string.shared_string_download);
                leftDownloadButtonController.leftIconId = R.drawable.ic_action_import;
                titleProgressController = new TitleProgressController() {

                    @Override
                    public void buttonPressed() {
                        if (indexItem != null) {
                            downloadThread.cancelDownload(indexItem);
                        }
                    }
                };
                if (!downloadThread.getIndexes().isDownloadedFromInternet) {
                    if (getMapActivity().getMyApplication().getSettings().isInternetConnectionAvailable()) {
                        downloadThread.runReloadIndexFiles();
                    }
                }
                if (mapContextMenu != null) {
                    mapContextMenu.updateMenuUI();
                }
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : WorldRegion(net.osmand.map.WorldRegion) ResourceManager(net.osmand.plus.resources.ResourceManager) SpannableString(android.text.SpannableString) IOException(java.io.IOException) IndexItem(net.osmand.plus.download.IndexItem) OsmandRegions(net.osmand.map.OsmandRegions) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair) DownloadValidationManager(net.osmand.plus.download.DownloadValidationManager)

Example 7 with IndexItem

use of net.osmand.plus.download.IndexItem in project Osmand by osmandapp.

the class PerformLiveUpdateAsyncTask method onPostExecute.

@Override
protected void onPostExecute(IncrementalChangesManager.IncrementalUpdateList result) {
    LOG.debug("onPostExecute");
    if (context instanceof AbstractDownloadActivity) {
        AbstractDownloadActivity activity = (AbstractDownloadActivity) context;
        activity.setSupportProgressBarIndeterminateVisibility(false);
    }
    final OsmandApplication application = getMyApplication();
    final OsmandSettings settings = application.getSettings();
    if (result.errorMessage != null) {
        LOG.info(result.errorMessage);
        tryRescheduleDownload(context, settings, localIndexFileName);
    } else {
        settings.LIVE_UPDATES_RETRIES.resetToDefault();
        List<IncrementalChangesManager.IncrementalUpdate> ll = result.getItemsForUpdate();
        if (ll != null && !ll.isEmpty()) {
            ArrayList<IndexItem> itemsToDownload = new ArrayList<>(ll.size());
            for (IncrementalChangesManager.IncrementalUpdate iu : ll) {
                IndexItem indexItem = new IndexItem(iu.fileName, "Incremental update", iu.timestamp, iu.sizeText, iu.contentSize, iu.containerSize, DownloadActivityType.LIVE_UPDATES_FILE);
                itemsToDownload.add(indexItem);
            }
            DownloadIndexesThread downloadThread = application.getDownloadThread();
            if (context instanceof DownloadIndexesThread.DownloadEvents) {
                downloadThread.setUiActivity((DownloadIndexesThread.DownloadEvents) context);
            }
            boolean downloadViaWiFi = LiveUpdatesHelper.preferenceDownloadViaWiFi(localIndexFileName, settings).get();
            if (getMyApplication().getSettings().isInternetConnectionAvailable()) {
                if (forceUpdate || settings.isWifiConnected() || !downloadViaWiFi) {
                    long szLong = 0;
                    int i = 0;
                    for (IndexItem es : downloadThread.getCurrentDownloadingItems()) {
                        szLong += es.getContentSize();
                        i++;
                    }
                    for (IndexItem es : itemsToDownload) {
                        szLong += es.getContentSize();
                        i++;
                    }
                    double sz = ((double) szLong) / (1 << 20);
                    // get availabile space
                    double asz = downloadThread.getAvailableSpace();
                    if (asz == -1 || asz <= 0 || sz / asz <= 0.4) {
                        IndexItem[] itemsArray = new IndexItem[itemsToDownload.size()];
                        itemsArray = itemsToDownload.toArray(itemsArray);
                        downloadThread.runDownloadFiles(itemsArray);
                        if (context instanceof DownloadIndexesThread.DownloadEvents) {
                            ((DownloadIndexesThread.DownloadEvents) context).downloadInProgress();
                        }
                    }
                }
            }
        } else {
            if (context instanceof DownloadIndexesThread.DownloadEvents) {
                ((DownloadIndexesThread.DownloadEvents) context).downloadInProgress();
            }
        }
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) AbstractDownloadActivity(net.osmand.plus.download.AbstractDownloadActivity) ArrayList(java.util.ArrayList) OsmandSettings(net.osmand.plus.OsmandSettings) IndexItem(net.osmand.plus.download.IndexItem) IncrementalChangesManager(net.osmand.plus.resources.IncrementalChangesManager) DownloadIndexesThread(net.osmand.plus.download.DownloadIndexesThread)

Example 8 with IndexItem

use of net.osmand.plus.download.IndexItem in project Osmand by osmandapp.

the class LocalIndexesFragment method reloadData.

public void reloadData() {
    List<IndexItem> itemsToUpdate = getDownloadActivity().getDownloadThread().getIndexes().getItemsToUpdate();
    filesToUpdate.clear();
    for (IndexItem ii : itemsToUpdate) {
        filesToUpdate.put(ii.getTargetFileName(), ii);
    }
    LoadLocalIndexTask current = asyncLoader;
    if (current == null || current.getStatus() == AsyncTask.Status.FINISHED || current.isCancelled() || current.getResult() != null) {
        asyncLoader = new LoadLocalIndexTask();
        asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : IndexItem(net.osmand.plus.download.IndexItem)

Example 9 with IndexItem

use of net.osmand.plus.download.IndexItem in project Osmand by osmandapp.

the class UpdatesIndexFragment method onListItemClick.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    if (listAdapter.isShowOsmLiveBanner() && position == 0) {
        Intent intent = new Intent(getMyActivity(), OsmLiveActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra(OsmLiveActivity.OPEN_SUBSCRIPTION_INTENT_PARAM, true);
        getMyActivity().startActivity(intent);
    } else {
        final IndexItem e = (IndexItem) getListAdapter().getItem(position);
        ItemViewHolder vh = (ItemViewHolder) v.getTag();
        OnClickListener ls = vh.getRightButtonAction(e, vh.getClickAction(e));
        ls.onClick(v);
    }
}
Also used : OnClickListener(android.view.View.OnClickListener) Intent(android.content.Intent) IndexItem(net.osmand.plus.download.IndexItem)

Example 10 with IndexItem

use of net.osmand.plus.download.IndexItem in project Osmand by osmandapp.

the class UpdatesIndexFragment method invalidateListView.

public void invalidateListView(Activity a) {
    DownloadResources indexes = getMyApplication().getDownloadThread().getIndexes();
    List<IndexItem> indexItems = indexes.getItemsToUpdate();
    final OsmandRegions osmandRegions = getMyApplication().getResourceManager().getOsmandRegions();
    OsmandSettings settings = getMyApplication().getSettings();
    listAdapter = new UpdateIndexAdapter(a, R.layout.download_index_list_item, indexItems, !settings.LIVE_UPDATES_PURCHASED.get() || settings.SHOULD_SHOW_FREE_VERSION_BANNER.get());
    listAdapter.sort(new Comparator<IndexItem>() {

        @Override
        public int compare(IndexItem indexItem, IndexItem indexItem2) {
            return indexItem.getVisibleName(getMyApplication(), osmandRegions).compareTo(indexItem2.getVisibleName(getMyApplication(), osmandRegions));
        }
    });
    setListAdapter(listAdapter);
    updateErrorMessage();
}
Also used : DownloadResources(net.osmand.plus.download.DownloadResources) OsmandRegions(net.osmand.map.OsmandRegions) IndexItem(net.osmand.plus.download.IndexItem) OsmandSettings(net.osmand.plus.OsmandSettings)

Aggregations

IndexItem (net.osmand.plus.download.IndexItem)27 View (android.view.View)9 TextView (android.widget.TextView)8 OnClickListener (android.view.View.OnClickListener)6 ContextMenuItem (net.osmand.plus.ContextMenuItem)5 IOException (java.io.IOException)4 LinkedList (java.util.LinkedList)4 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)4 LatLon (net.osmand.data.LatLon)4 OsmandRegions (net.osmand.map.OsmandRegions)4 WorldRegion (net.osmand.map.WorldRegion)4 OsmandApplication (net.osmand.plus.OsmandApplication)4 OsmandSettings (net.osmand.plus.OsmandSettings)4 DownloadIndexesThread (net.osmand.plus.download.DownloadIndexesThread)4 Intent (android.content.Intent)3 ArrayAdapter (android.widget.ArrayAdapter)3 ExpandableListView (android.widget.ExpandableListView)3 ImageView (android.widget.ImageView)3 ContextMenuAdapter (net.osmand.plus.ContextMenuAdapter)3 DownloadResources (net.osmand.plus.download.DownloadResources)3