Add downloads to ui initial commit

This commit is contained in:
Vitaliy 2020-04-14 16:23:09 +03:00
parent 5de11ab32b
commit 44a4b93df1
7 changed files with 186 additions and 77 deletions

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="extra_maps_menu_group">Extra maps</string>
<string name="wiki_menu_download_descr">Additional maps are needed to view Wikipedia POIs on the map.</string>
<string name="wikipedia_poi_languages_promo">Some Wikipedia articles may not be available in your name, select the languages in which Wikipedia articles will appear on the map.\nYou will be able to switch between all available languages while reading the article.</string>
<string name="shared_string_all_languages">All languages</string>

View file

@ -14,7 +14,7 @@ import net.osmand.data.LatLon;
import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.plus.SettingsHelper.AvoidRoadsSettingsItem;
import net.osmand.plus.SettingsHelper.DownloadDataContainer;
import net.osmand.plus.SettingsHelper.CustomRegion;
import net.osmand.plus.SettingsHelper.MapSourcesSettingsItem;
import net.osmand.plus.SettingsHelper.PluginSettingsItem;
import net.osmand.plus.SettingsHelper.PoiUiFilterSettingsItem;
@ -65,7 +65,7 @@ public class CustomOsmandPlugin extends OsmandPlugin {
private List<String> rendererNames = new ArrayList<>();
private List<String> routerNames = new ArrayList<>();
private List<SuggestedDownloadItem> suggestedDownloadItems = new ArrayList<>();
private List<DownloadDataContainer> downloadDataContainers = new ArrayList<>();
private List<CustomRegion> customRegions = new ArrayList<>();
public CustomOsmandPlugin(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(app);
@ -438,13 +438,13 @@ public class CustomOsmandPlugin extends OsmandPlugin {
suggestedDownloadItems = new ArrayList<>(items);
}
public void updateDownloadItems(List<DownloadDataContainer> items) {
downloadDataContainers = new ArrayList<>(items);
public void updateDownloadItems(List<CustomRegion> items) {
customRegions = new ArrayList<>(items);
}
@Override
public List<DownloadDataContainer> getDownloadMaps() {
return downloadDataContainers;
public List<CustomRegion> getDownloadMaps() {
return customRegions;
}
@Override

View file

@ -44,7 +44,7 @@ import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.skimapsplugin.SkiMapsPlugin;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.SettingsHelper.DownloadDataContainer;
import net.osmand.plus.SettingsHelper.CustomRegion;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -183,7 +183,7 @@ public abstract class OsmandPlugin {
return Collections.emptyList();
}
public List<DownloadDataContainer> getDownloadMaps() {
public List<CustomRegion> getDownloadMaps() {
return Collections.emptyList();
}
@ -650,6 +650,14 @@ public abstract class OsmandPlugin {
return null;
}
public static List<CustomRegion> getCustomDownloadRegions() {
List<CustomRegion> l = new ArrayList<CustomRegion>();
for (OsmandPlugin plugin : getEnabledPlugins()) {
l.addAll(plugin.getDownloadMaps());
}
return l;
}
public static List<String> getDisabledRendererNames() {
List<String> l = new ArrayList<String>();
for (OsmandPlugin plugin : getNotEnabledPlugins()) {

View file

@ -16,12 +16,16 @@ import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.map.WorldRegion;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.plus.ApplicationMode.ApplicationModeBean;
import net.osmand.plus.ApplicationMode.ApplicationModeBuilder;
import net.osmand.plus.CustomOsmandPlugin.SuggestedDownloadItem;
import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo;
import net.osmand.plus.poi.PoiUIFilter;
@ -559,7 +563,7 @@ public class SettingsHelper {
public static class DownloadsItem extends SettingsItem {
private List<DownloadDataContainer> items;
private List<CustomRegion> items;
DownloadsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(app, json);
@ -594,7 +598,7 @@ public class SettingsHelper {
super.apply();
}
public List<DownloadDataContainer> getItems() {
public List<CustomRegion> getItems() {
return items;
}
@ -604,51 +608,22 @@ public class SettingsHelper {
if (!json.has("items")) {
return;
}
Map<String, CustomRegion> customRegions = new HashMap<>();
JSONArray jsonArray = json.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String scopeId = object.optString("scope-id", null);
String path = object.optString("path", null);
String type = object.optString("type", null);
String folder = object.optString("folder", null);
JSONObject namesJson = object.optJSONObject("name");
Map<String, String> names = new HashMap<>();
if (namesJson != null) {
for (Iterator<String> it = namesJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = namesJson.getString(localeKey);
names.put(localeKey, name);
CustomRegion region = CustomRegion.createCustomRegionFromJson(app, object);
customRegions.put(region.path, region);
}
for (CustomRegion region : customRegions.values()) {
if (!Algorithms.isEmpty(region.parentPath)) {
CustomRegion parentReg = customRegions.get(region.parentPath);
if (parentReg != null) {
parentReg.addSubregion(region);
}
} else {
items.add(region);
}
JSONObject iconJson = object.optJSONObject("icon");
Map<String, String> icons = new HashMap<>();
if (iconJson != null) {
for (Iterator<String> it = iconJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = iconJson.getString(localeKey);
icons.put(localeKey, name);
}
}
JSONObject headerJson = object.optJSONObject("header");
Map<String, String> headers = new HashMap<>();
if (headerJson != null) {
for (Iterator<String> it = headerJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = headerJson.getString(localeKey);
headers.put(localeKey, name);
}
}
String headerButton = object.optString("header-button", null);
JSONArray downloadItemsArray = object.optJSONArray("items");
DownloadDataContainer dataContainer = new DownloadDataContainer(scopeId, path, type, folder, downloadItemsArray, names, icons, headers, headerButton);
items.add(dataContainer);
}
} catch (JSONException e) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
@ -661,7 +636,7 @@ public class SettingsHelper {
JSONArray jsonArray = new JSONArray();
if (!items.isEmpty()) {
try {
for (DownloadDataContainer downloadItem : items) {
for (CustomRegion downloadItem : items) {
JSONObject jsonObject = new JSONObject();
if (!Algorithms.isEmpty(downloadItem.scopeId)) {
@ -704,8 +679,8 @@ public class SettingsHelper {
jsonObject.put("header", headerJson);
}
if (downloadItem.downloadItemsArray != null) {
jsonObject.put("items", downloadItem.downloadItemsArray);
if (downloadItem.downloadItems != null) {
// jsonObject.put("items", downloadItem.downloadItems);
}
jsonArray.put(jsonObject);
@ -732,33 +707,112 @@ public class SettingsHelper {
}
}
public static class DownloadDataContainer {
public static class CustomRegion extends WorldRegion {
String scopeId;
String path;
String type;
String folder;
String headerButton;
JSONArray downloadItemsArray;
Map<String, String> names;
Map<String, String> icons;
Map<String, String> headers;
public String scopeId;
public String path;
public String parentPath;
public String type;
public String folder;
public String headerButton;
public List<IndexItem> downloadItems = new ArrayList<>();
public Map<String, String> names;
public Map<String, String> icons;
public Map<String, String> headers;
public DownloadDataContainer(String scopeId, String path, String type, String folder,
JSONArray downloadItemsArray,
Map<String, String> names,
Map<String, String> icons,
Map<String, String> headers,
String headerButton) {
private CustomRegion(String scopeId, String path, String type) {
super(path, null);
this.scopeId = scopeId;
this.path = path;
this.type = type;
this.folder = folder;
this.downloadItemsArray = downloadItemsArray;
this.names = names;
this.icons = icons;
this.headers = headers;
this.headerButton = headerButton;
}
public static CustomRegion createCustomRegionFromJson(OsmandApplication app, JSONObject object) throws JSONException {
String scopeId = object.optString("scope-id", null);
String path = object.optString("path", null);
String type = object.optString("type", null);
String folder = object.optString("folder", null);
CustomRegion region = new CustomRegion(scopeId, path, type);
int index = path.lastIndexOf(File.separator);
if (index != -1) {
region.parentPath = path.substring(0, index);
}
JSONObject namesJson = object.optJSONObject("name");
Map<String, String> names = new HashMap<>();
if (namesJson != null) {
for (Iterator<String> it = namesJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = namesJson.getString(localeKey);
names.put(localeKey, name);
}
region.regionName = names.get("");
region.regionNameEn = names.get("");
region.regionFullName = names.get("");
region.regionNameLocale = names.get("");
if (Algorithms.isEmpty(region.regionName)) {
region.regionName = names.get("");
}
if (Algorithms.isEmpty(region.regionName)) {
region.regionName = DownloadResourceGroup.DownloadResourceGroupType.REGION.getDefaultId();
}
}
JSONObject iconJson = object.optJSONObject("icon");
Map<String, String> icons = new HashMap<>();
if (iconJson != null) {
for (Iterator<String> it = iconJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = iconJson.getString(localeKey);
icons.put(localeKey, name);
}
}
JSONObject headerJson = object.optJSONObject("header");
Map<String, String> headers = new HashMap<>();
if (headerJson != null) {
for (Iterator<String> it = headerJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = headerJson.getString(localeKey);
headers.put(localeKey, name);
}
}
String headerButton = object.optString("header-button", null);
JSONArray downloadItemsArray = object.optJSONArray("items");
if (downloadItemsArray != null) {
for (int i = 0; i < downloadItemsArray.length(); i++) {
JSONObject indexItemJson = downloadItemsArray.getJSONObject(i);
JSONObject indexNamesJson = indexItemJson.optJSONObject("name");
Map<String, String> indexNames = new HashMap<>();
if (namesJson != null) {
for (Iterator<String> it = namesJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = namesJson.getString(localeKey);
names.put(localeKey, name);
}
}
String fileName = indexItemJson.optString("filename");
String description = indexItemJson.optString("description");
long timestamp = indexItemJson.optLong("timestamp") * 1000;
String size = indexItemJson.optString("contentSize");
long contentSize = indexItemJson.optLong("contentSize");
long containerSize = indexItemJson.optLong("containerSize");
String indexType = indexItemJson.optString("type", null);
@NonNull DownloadActivityType tp = DownloadActivityType.getIndexType(indexType);
if (tp != null) {
IndexItem indexItem = new IndexItem(fileName, description, timestamp, size, contentSize, containerSize, tp);
region.downloadItems.add(indexItem);
}
}
}
return region;
}
}

View file

@ -349,6 +349,7 @@ public class DownloadIndexesThread {
app.getSettings().LAST_CHECKED_UPDATES.set(System.currentTimeMillis());
result.prepareData(indexFileList.getIndexFiles());
} catch (Exception e) {
LOG.error(e);
}
}
return result == null ? new DownloadResources(app) : result;

View file

@ -55,6 +55,7 @@ public class DownloadResourceGroup {
FONTS(R.string.fonts_header),
VOICE_REC(R.string.index_name_voice),
OTHER_MAPS(R.string.download_select_map_types),
EXTRA_MAPS(R.string.extra_maps_menu_group),
WORLD(-1),
REGION(-1);
@ -79,7 +80,7 @@ public class DownloadResourceGroup {
public boolean containsIndexItem() {
return isHeader() && this != SUBREGIONS && this != OTHER_GROUP && this != OTHER_MAPS_GROUP
&& this != NAUTICAL_MAPS_GROUP && this != TRAVEL_GROUP;
&& this != NAUTICAL_MAPS_GROUP && this != TRAVEL_GROUP && this != EXTRA_MAPS;
}
public boolean isHeader() {
@ -90,7 +91,8 @@ public class DownloadResourceGroup {
|| this == OTHER_MAPS_HEADER || this == OTHER_MAPS_GROUP
|| this == FONTS_HEADER
|| this == NAUTICAL_MAPS_HEADER || this == NAUTICAL_MAPS_GROUP
|| this == WIKIVOYAGE_HEADER || this == TRAVEL_GROUP;
|| this == WIKIVOYAGE_HEADER || this == TRAVEL_GROUP
|| this == EXTRA_MAPS;
}
public static String getVoiceTTSId() {

View file

@ -9,8 +9,11 @@ import net.osmand.data.LatLon;
import net.osmand.map.OsmandRegions;
import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.SettingsHelper.CustomRegion;
import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem;
import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -363,6 +366,15 @@ public class DownloadResources extends DownloadResourceGroup {
}
this.groupByRegion = groupByRegion;
List<CustomRegion> customRegions = OsmandPlugin.getCustomDownloadRegions();
if (!Algorithms.isEmpty(customRegions)) {
DownloadResourceGroup downloadResourceGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.EXTRA_MAPS);
addGroup(downloadResourceGroup);
for (CustomRegion region : customRegions) {
buildRegionsGroups(region, downloadResourceGroup);
}
}
LinkedList<WorldRegion> queue = new LinkedList<WorldRegion>();
LinkedList<DownloadResourceGroup> parent = new LinkedList<DownloadResourceGroup>();
DownloadResourceGroup worldSubregions = new DownloadResourceGroup(this, DownloadResourceGroupType.SUBREGIONS);
@ -433,6 +445,37 @@ public class DownloadResources extends DownloadResourceGroup {
return true;
}
private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) {
LinkedList<WorldRegion> queue = new LinkedList<WorldRegion>();
LinkedList<DownloadResourceGroup> parent = new LinkedList<DownloadResourceGroup>();
queue.add(region);
parent.add(group);
while (!queue.isEmpty()) {
WorldRegion reg = queue.pollFirst();
DownloadResourceGroup parentGroup = parent.pollFirst();
List<WorldRegion> subregions = reg.getSubregions();
DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId());
mainGrp.region = reg;
parentGroup.addGroup(mainGrp);
// List<IndexItem> list = groupByRegion.get(reg);
if (reg instanceof CustomRegion && ((CustomRegion) reg).downloadItems != null) {
DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.REGION_MAPS);
for (IndexItem ii : ((CustomRegion) reg).downloadItems) {
flatFiles.addItem(ii);
}
mainGrp.addGroup(flatFiles);
}
DownloadResourceGroup subRegions = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.SUBREGIONS);
mainGrp.addGroup(subRegions);
// add to processing queue
for (WorldRegion rg : subregions) {
queue.add(rg);
parent.add(subRegions);
}
}
}
/**
* @return smallest index item, if there are no downloaded index items; Downloaded item otherwise.
*/