Merge branch 'master' of github.com:osmandapp/Osmand

This commit is contained in:
Victor Shcherb 2015-10-05 22:21:32 +03:00
commit eb2a587e5f
7 changed files with 214 additions and 204 deletions

View file

@ -282,4 +282,17 @@ public class WorldRegion implements Serializable {
return s; return s;
} }
} }
public WorldRegion getRegionById(String regionId) {
if (regionId == null) {
return this;
} else {
for (WorldRegion region : flattenedSubregions) {
if (region.getRegionId().equals(regionId)) {
return region;
}
}
}
return null;
}
} }

View file

@ -19,6 +19,7 @@ import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.activities.ActionBarProgressActivity;
import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.items.ItemsListBuilder;
import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -122,6 +123,24 @@ public class BaseDownloadActivity extends ActionBarProgressActivity {
} }
public ItemsListBuilder getItemsBuilder() {
if (downloadListIndexThread.isDataPrepared()) {
return new ItemsListBuilder(getMyApplication(), null, downloadListIndexThread.getResourcesByRegions(),
downloadListIndexThread.getVoiceRecItems(), downloadListIndexThread.getVoiceTTSItems());
} else {
return null;
}
}
public ItemsListBuilder getItemsBuilder(String regionId) {
if (downloadListIndexThread.isDataPrepared()) {
return new ItemsListBuilder(getMyApplication(), regionId, downloadListIndexThread.getResourcesByRegions(),
downloadListIndexThread.getVoiceRecItems(), downloadListIndexThread.getVoiceTTSItems());
} else {
return null;
}
}
public boolean startDownload(IndexItem item) { public boolean startDownload(IndexItem item) {
if (downloadListIndexThread.getCurrentRunningTask() != null && getEntriesToDownload().get(item) == null) { if (downloadListIndexThread.getCurrentRunningTask() != null && getEntriesToDownload().get(item) == null) {
downloadQueue.add(item); downloadQueue.add(item);

View file

@ -15,13 +15,17 @@ import android.os.StatFs;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.Collator;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.WorldRegion;
import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning;
import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem;
@ -39,13 +43,17 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@SuppressLint("NewApi") @SuppressLint("NewApi")
@ -64,6 +72,12 @@ public class DownloadIndexesThread {
private java.text.DateFormat dateFormat; private java.text.DateFormat dateFormat;
private List<IndexItem> itemsToUpdate = new ArrayList<IndexItem>(); private List<IndexItem> itemsToUpdate = new ArrayList<IndexItem>();
private Map<WorldRegion, Map<String, IndexItem>> resourcesByRegions = new HashMap<>();
private List<IndexItem> voiceRecItems = new LinkedList<>();
private List<IndexItem> voiceTTSItems = new LinkedList<>();
private boolean dataPrepared;
DatabaseHelper dbHelper; DatabaseHelper dbHelper;
public DownloadIndexesThread(Context ctx) { public DownloadIndexesThread(Context ctx) {
@ -141,6 +155,107 @@ public class DownloadIndexesThread {
return itemsToUpdate; return itemsToUpdate;
} }
public boolean isDataPrepared() {
return dataPrepared;
}
public Map<WorldRegion, Map<String, IndexItem>> getResourcesByRegions() {
return resourcesByRegions;
}
public List<IndexItem> getVoiceRecItems() {
return voiceRecItems;
}
public List<IndexItem> getVoiceTTSItems() {
return voiceTTSItems;
}
private boolean prepareData(List<IndexItem> resources) {
List<IndexItem> resourcesInRepository;
if (resources != null) {
resourcesInRepository = resources;
} else {
resourcesInRepository = DownloadActivity.downloadListIndexThread.getCachedIndexFiles();
}
if (resourcesInRepository == null) {
return false;
}
resourcesByRegions.clear();
voiceRecItems.clear();
voiceTTSItems.clear();
List<WorldRegion> mergedRegions = app.getWorldRegion().getFlattenedSubregions();
mergedRegions.add(app.getWorldRegion());
boolean voiceFilesProcessed = false;
for (WorldRegion region : mergedRegions) {
String downloadsIdPrefix = region.getDownloadsIdPrefix();
Map<String, IndexItem> regionResources = new HashMap<>();
Set<DownloadActivityType> typesSet = new TreeSet<>(new Comparator<DownloadActivityType>() {
@Override
public int compare(DownloadActivityType dat1, DownloadActivityType dat2) {
return dat1.getTag().compareTo(dat2.getTag());
}
});
for (IndexItem resource : resourcesInRepository) {
if (!voiceFilesProcessed) {
if (resource.getSimplifiedFileName().endsWith(".voice.zip")) {
voiceRecItems.add(resource);
continue;
} else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) {
voiceTTSItems.add(resource);
continue;
}
}
if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) {
continue;
}
typesSet.add(resource.getType());
regionResources.put(resource.getSimplifiedFileName(), resource);
}
voiceFilesProcessed = true;
if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) {
if (region.getSuperregion().getResourceTypes() == null) {
region.getSuperregion().setResourceTypes(typesSet);
} else {
region.getSuperregion().getResourceTypes().addAll(typesSet);
}
}
region.setResourceTypes(typesSet);
resourcesByRegions.put(region, regionResources);
}
final Collator collator = OsmAndCollator.primaryCollator();
final OsmandRegions osmandRegions = app.getRegions();
Collections.sort(voiceRecItems, new Comparator<IndexItem>() {
@Override
public int compare(IndexItem lhs, IndexItem rhs) {
return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions),
rhs.getVisibleName(app.getApplicationContext(), osmandRegions));
}
});
Collections.sort(voiceTTSItems, new Comparator<IndexItem>() {
@Override
public int compare(IndexItem lhs, IndexItem rhs) {
return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions),
rhs.getVisibleName(app.getApplicationContext(), osmandRegions));
}
});
return true;
}
public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning { public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning {
@ -408,13 +523,14 @@ public class DownloadIndexesThread {
currentRunningTask.add(this); currentRunningTask.add(this);
super.onPreExecute(); super.onPreExecute();
this.message = ctx.getString(R.string.downloading_list_indexes); this.message = ctx.getString(R.string.downloading_list_indexes);
dataPrepared = false;
} }
@Override @Override
protected IndexFileList doInBackground(Void... params) { protected IndexFileList doInBackground(Void... params) {
IndexFileList indexFileList = DownloadOsmandIndexesHelper.getIndexesList(ctx); IndexFileList indexFileList = DownloadOsmandIndexesHelper.getIndexesList(ctx);
if (indexFileList != null) { if (indexFileList != null) {
ItemsListBuilder.prepareData(app, indexFileList.getIndexFiles()); prepareData(indexFileList.getIndexFiles());
} }
return indexFileList; return indexFileList;
} }
@ -422,6 +538,7 @@ public class DownloadIndexesThread {
protected void onPostExecute(IndexFileList result) { protected void onPostExecute(IndexFileList result) {
indexFiles = result; indexFiles = result;
if (indexFiles != null && uiActivity != null) { if (indexFiles != null && uiActivity != null) {
dataPrepared = resourcesByRegions.size() > 0;
prepareFilesToUpdate(); prepareFilesToUpdate();
boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap(); boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap();
IndexItem basemap = indexFiles.getBasemap(); IndexItem basemap = indexFiles.getBasemap();

View file

@ -2,15 +2,12 @@ package net.osmand.plus.download.items;
import android.content.Context; import android.content.Context;
import net.osmand.Collator;
import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.map.OsmandRegions; import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.WorldRegion; import net.osmand.plus.WorldRegion;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
@ -18,20 +15,19 @@ import net.osmand.util.Algorithms;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ItemsListBuilder { public class ItemsListBuilder {
public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip";
public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip";
private Map<WorldRegion, Map<String, IndexItem>> resourcesByRegions;
private List<IndexItem> voiceRecItems;
private List<IndexItem> voiceTTSItems;
public class ResourceItem { public class ResourceItem {
private String resourceId; private String resourceId;
@ -101,15 +97,6 @@ public class ItemsListBuilder {
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ItemsListBuilder.class); private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ItemsListBuilder.class);
private static Map<WorldRegion, Map<String, IndexItem>> resourcesByRegions =
new HashMap<>();
private static List<WorldRegion> searchableWorldwideRegionItems = new LinkedList<>();
private static List<IndexItem> voiceRecItems = new LinkedList<>();
private static List<IndexItem> voiceTTSItems = new LinkedList<>();
private static final Lock lock = new ReentrantLock();
private List<ResourceItem> regionMapItems; private List<ResourceItem> regionMapItems;
private List<Object> allResourceItems; private List<Object> allResourceItems;
private List<WorldRegion> allSubregionItems; private List<WorldRegion> allSubregionItems;
@ -138,18 +125,18 @@ public class ItemsListBuilder {
return list; return list;
} }
public static String getVoicePromtName(Context context, VoicePromptsType type) { public String getVoicePromtName(VoicePromptsType type) {
switch (type) { switch (type) {
case RECORDED: case RECORDED:
return context.getResources().getString(R.string.index_name_voice); return app.getResources().getString(R.string.index_name_voice);
case TTS: case TTS:
return context.getResources().getString(R.string.index_name_tts_voice); return app.getResources().getString(R.string.index_name_tts_voice);
default: default:
return null; return null;
} }
} }
public static List<IndexItem> getVoicePromptsItems(VoicePromptsType type) { public List<IndexItem> getVoicePromptsItems(VoicePromptsType type) {
switch (type) { switch (type) {
case RECORDED: case RECORDED:
return voiceRecItems; return voiceRecItems;
@ -160,7 +147,7 @@ public class ItemsListBuilder {
} }
} }
public static boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { public boolean isVoicePromptsItemsEmpty(VoicePromptsType type) {
switch (type) { switch (type) {
case RECORDED: case RECORDED:
return voiceRecItems.isEmpty(); return voiceRecItems.isEmpty();
@ -171,28 +158,22 @@ public class ItemsListBuilder {
} }
} }
public ItemsListBuilder(OsmandApplication app) { public ItemsListBuilder(OsmandApplication app, String regionId, Map<WorldRegion, Map<String, IndexItem>> resourcesByRegions,
List<IndexItem> voiceRecItems, List<IndexItem> voiceTTSItems) {
this.app = app; this.app = app;
regionMapItems = new LinkedList<>(); this.resourcesByRegions = resourcesByRegions;
allResourceItems = new LinkedList<Object>(); this.voiceRecItems = voiceRecItems;
allSubregionItems = new LinkedList<>(); this.voiceTTSItems = voiceTTSItems;
}
public ItemsListBuilder(OsmandApplication app, WorldRegion region) { regionMapItems = new LinkedList<>();
this(app); allResourceItems = new LinkedList<>();
this.region = region; allSubregionItems = new LinkedList<>();
region = app.getWorldRegion().getRegionById(regionId);
} }
public boolean build() { public boolean build() {
if (lock.tryLock()) { return obtainDataAndItems();
try {
return obtainDataAndItems();
} finally {
lock.unlock();
}
} else {
return false;
}
} }
private boolean obtainDataAndItems() { private boolean obtainDataAndItems() {
@ -206,92 +187,6 @@ public class ItemsListBuilder {
return true; return true;
} }
public static boolean prepareData(final OsmandApplication app, List<IndexItem> resources) {
lock.lock();
try {
List<IndexItem> resourcesInRepository;
if (resources != null) {
resourcesInRepository = resources;
} else {
resourcesInRepository = DownloadActivity.downloadListIndexThread.getCachedIndexFiles();
}
if (resourcesInRepository == null) {
return false;
}
resourcesByRegions.clear();
searchableWorldwideRegionItems.clear();
voiceRecItems.clear();
voiceTTSItems.clear();
List<WorldRegion> mergedRegions = app.getWorldRegion().getFlattenedSubregions();
mergedRegions.add(app.getWorldRegion());
boolean voiceFilesProcessed = false;
for (WorldRegion region : mergedRegions) {
searchableWorldwideRegionItems.add(region);
String downloadsIdPrefix = region.getDownloadsIdPrefix();
Map<String, IndexItem> regionResources = new HashMap<>();
Set<DownloadActivityType> typesSet = new TreeSet<>(new Comparator<DownloadActivityType>() {
@Override
public int compare(DownloadActivityType dat1, DownloadActivityType dat2) {
return dat1.getTag().compareTo(dat2.getTag());
}
});
for (IndexItem resource : resourcesInRepository) {
if (!voiceFilesProcessed) {
if (resource.getSimplifiedFileName().endsWith(".voice.zip")) {
voiceRecItems.add(resource);
continue;
} else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) {
voiceTTSItems.add(resource);
continue;
}
}
if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) {
continue;
}
typesSet.add(resource.getType());
regionResources.put(resource.getSimplifiedFileName(), resource);
}
voiceFilesProcessed = true;
if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) {
if (region.getSuperregion().getResourceTypes() == null) {
region.getSuperregion().setResourceTypes(typesSet);
} else {
region.getSuperregion().getResourceTypes().addAll(typesSet);
}
}
region.setResourceTypes(typesSet);
resourcesByRegions.put(region, regionResources);
}
final Collator collator = OsmAndCollator.primaryCollator();
final OsmandRegions osmandRegions = app.getRegions();
Collections.sort(voiceRecItems, new Comparator<IndexItem>() {
@Override
public int compare(IndexItem lhs, IndexItem rhs) {
return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions),
rhs.getVisibleName(app.getApplicationContext(), osmandRegions));
}
});
return true;
} finally {
lock.unlock();
}
}
private void collectSubregionsDataAndItems() { private void collectSubregionsDataAndItems() {
srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null;
hasSrtm = false; hasSrtm = false;

View file

@ -21,8 +21,8 @@ import org.apache.commons.logging.Log;
public class RegionDialogFragment extends DialogFragment { public class RegionDialogFragment extends DialogFragment {
private static final Log LOG = PlatformUtil.getLog(RegionDialogFragment.class); private static final Log LOG = PlatformUtil.getLog(RegionDialogFragment.class);
public static final String TAG = "RegionDialogFragment"; public static final String TAG = "RegionDialogFragment";
private static final String REGION_DLG_KEY = "world_region_dialog_key"; private static final String REGION_ID_DLG_KEY = "world_region_dialog_key";
private WorldRegion region; private String regionId;
private DownloadsUiHelper.MapDownloadListener mProgressListener; private DownloadsUiHelper.MapDownloadListener mProgressListener;
@Override @Override
@ -39,22 +39,13 @@ public class RegionDialogFragment extends DialogFragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false);
WorldRegion region = null;
if (savedInstanceState != null) { if (savedInstanceState != null) {
Object regionObj = savedInstanceState.getSerializable(REGION_DLG_KEY); regionId = savedInstanceState.getString(REGION_ID_DLG_KEY);
if (regionObj != null) {
region = (WorldRegion)regionObj;
}
} }
if (region == null) { if (regionId == null) {
Object regionObj = getArguments().getSerializable(REGION_DLG_KEY); regionId = getArguments().getString(REGION_ID_DLG_KEY);
if (regionObj != null) {
region = (WorldRegion)regionObj;
}
} }
this.region = region;
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
toolbar.setNavigationOnClickListener(new View.OnClickListener() { toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@ -64,13 +55,16 @@ public class RegionDialogFragment extends DialogFragment {
} }
}); });
if (this.region != null) { if (regionId != null) {
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer);
if (fragment == null) { if (fragment == null) {
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer,
RegionItemsFragment.createInstance(region)).commit(); RegionItemsFragment.createInstance(regionId)).commit();
}
WorldRegion region = getMyApplication().getWorldRegion().getRegionById(regionId);
if (region != null) {
toolbar.setTitle(region.getName());
} }
toolbar.setTitle(this.region.getName());
} }
DownloadsUiHelper.initFreeVersionBanner(view, getMyApplication(), DownloadsUiHelper.initFreeVersionBanner(view, getMyApplication(),
getResources()); getResources());
@ -95,7 +89,6 @@ public class RegionDialogFragment extends DialogFragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
LOG.debug(region.getName() + " onResume()");
getMyActivity().setOnProgressUpdateListener(mProgressListener); getMyActivity().setOnProgressUpdateListener(mProgressListener);
} }
@ -107,7 +100,7 @@ public class RegionDialogFragment extends DialogFragment {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putSerializable(REGION_DLG_KEY, region); outState.putString(REGION_ID_DLG_KEY, regionId);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -119,13 +112,13 @@ public class RegionDialogFragment extends DialogFragment {
return (DownloadActivity) getActivity(); return (DownloadActivity) getActivity();
} }
public void onRegionSelected(WorldRegion region) { public void onRegionSelected(String regionId) {
DownloadsUiHelper.showDialog(getActivity(), createInstance(region)); DownloadsUiHelper.showDialog(getActivity(), createInstance(regionId));
} }
public static RegionDialogFragment createInstance(WorldRegion region) { public static RegionDialogFragment createInstance(String regionId) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putSerializable(REGION_DLG_KEY, region); bundle.putString(REGION_ID_DLG_KEY, regionId);
RegionDialogFragment fragment = new RegionDialogFragment(); RegionDialogFragment fragment = new RegionDialogFragment();
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;

View file

@ -7,7 +7,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -21,7 +20,6 @@ import net.osmand.plus.activities.OsmandExpandableListFragment;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -38,13 +36,12 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
private static final Log LOG = PlatformUtil.getLog(RegionItemsFragment.class); private static final Log LOG = PlatformUtil.getLog(RegionItemsFragment.class);
private static final MessageFormat formatGb = new MessageFormat("{0, number,<b>#.##</b>} GB", Locale.US); private static final MessageFormat formatGb = new MessageFormat("{0, number,<b>#.##</b>} GB", Locale.US);
private ItemsListBuilder builder;
private RegionsItemsAdapter listAdapter; private RegionsItemsAdapter listAdapter;
private int regionMapsGroupPos = -1; private int regionMapsGroupPos = -1;
private int additionalMapsGroupPos = -1; private int additionalMapsGroupPos = -1;
private static final String REGION_KEY = "world_region_key"; private static final String REGION_ID_KEY = "world_region_id_key";
private WorldRegion region; private String regionId;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -56,33 +53,25 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_items_fragment, container, false); View view = inflater.inflate(R.layout.download_items_fragment, container, false);
WorldRegion region = null;
if (savedInstanceState != null) { if (savedInstanceState != null) {
Object regionObj = savedInstanceState.getSerializable(REGION_KEY); regionId = savedInstanceState.getString(REGION_ID_KEY);
if (regionObj != null) {
region = (WorldRegion) regionObj;
}
} }
if (region == null) { if (regionId == null) {
Object regionObj = getArguments().getSerializable(REGION_KEY); regionId = getArguments().getString(REGION_ID_KEY);
if (regionObj != null) {
region = (WorldRegion) regionObj;
}
} }
this.region = region;
builder = new ItemsListBuilder(getMyApplication(), this.region);
ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list);
listAdapter = new RegionsItemsAdapter(getActivity()); listAdapter = new RegionsItemsAdapter(getActivity());
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
setListView(listView); setListView(listView);
if (builder.build()) { if (regionId != null) {
fillRegionItemsAdapter(); ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(regionId);
listAdapter.notifyDataSetChanged(); if (builder != null && builder.build()) {
expandAllGroups(); fillRegionItemsAdapter(builder);
listAdapter.notifyDataSetChanged();
expandAllGroups();
}
} }
return view; return view;
@ -90,7 +79,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putSerializable(REGION_KEY, region); outState.putString(REGION_ID_KEY, regionId);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -100,7 +89,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
if (obj instanceof WorldRegion) { if (obj instanceof WorldRegion) {
WorldRegion region = (WorldRegion) obj; WorldRegion region = (WorldRegion) obj;
((RegionDialogFragment) getParentFragment()) ((RegionDialogFragment) getParentFragment())
.onRegionSelected(region); .onRegionSelected(region.getRegionId());
return true; return true;
} else { } else {
return false; return false;
@ -117,17 +106,12 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
private void fillRegionItemsAdapter() { private void fillRegionItemsAdapter(ItemsListBuilder builder) {
if (listAdapter != null) { if (listAdapter != null) {
listAdapter.clear(); listAdapter.clear();
int nextAvailableGroupPos = 0; int nextAvailableGroupPos = 0;
if (builder.getRegionMapItems().size() > 0) { if (builder.getRegionMapItems().size() > 0) {
String sectionTitle; String sectionTitle = "Region maps";
if (builder.getAllResourceItems().size() > 0) {
sectionTitle = "Region maps";
} else {
sectionTitle = "";
}
regionMapsGroupPos = nextAvailableGroupPos++; regionMapsGroupPos = nextAvailableGroupPos++;
listAdapter.add(sectionTitle, builder.getRegionMapItems()); listAdapter.add(sectionTitle, builder.getRegionMapItems());
} }
@ -136,7 +120,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
if (builder.getRegionMapItems().size() > 0) { if (builder.getRegionMapItems().size() > 0) {
sectionTitle = "Additional maps"; sectionTitle = "Additional maps";
} else { } else {
sectionTitle = ""; sectionTitle = "Regions";
} }
additionalMapsGroupPos = nextAvailableGroupPos; additionalMapsGroupPos = nextAvailableGroupPos;
listAdapter.add(sectionTitle, builder.getAllResourceItems()); listAdapter.add(sectionTitle, builder.getAllResourceItems());
@ -148,9 +132,9 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
return (DownloadActivity) getActivity(); return (DownloadActivity) getActivity();
} }
public static RegionItemsFragment createInstance(WorldRegion region) { public static RegionItemsFragment createInstance(String regionId) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putSerializable(REGION_KEY, region); bundle.putString(REGION_ID_KEY, regionId);
RegionItemsFragment fragment = new RegionItemsFragment(); RegionItemsFragment fragment = new RegionItemsFragment();
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
@ -254,14 +238,6 @@ public class RegionItemsFragment extends OsmandExpandableListFragment {
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = convertView; View v = convertView;
String section = getGroup(groupPosition); String section = getGroup(groupPosition);
/*
if (section.length() == 0) {
LinearLayout emptyLL = new LinearLayout(parent.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
emptyLL.setLayoutParams(params);
return emptyLL;
}
*/
if (v == null) { if (v == null) {
LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View file

@ -23,7 +23,6 @@ import net.osmand.plus.WorldRegion;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.OsmandExpandableListFragment;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.download.newimplementation.DownloadsUiHelper; import net.osmand.plus.download.newimplementation.DownloadsUiHelper;
import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
@ -42,7 +41,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
public static final int RELOAD_ID = 0; public static final int RELOAD_ID = 0;
private ItemsListBuilder builder;
private WorldItemsAdapter listAdapter; private WorldItemsAdapter listAdapter;
private int worldRegionsIndex = -1; private int worldRegionsIndex = -1;
@ -62,8 +60,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_index_fragment, container, false); View view = inflater.inflate(R.layout.download_index_fragment, container, false);
builder = new ItemsListBuilder(getMyApplication(), getMyApplication().getWorldRegion());
ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list);
listAdapter = new WorldItemsAdapter(getActivity()); listAdapter = new WorldItemsAdapter(getActivity());
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
@ -97,7 +93,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
private void fillWorldItemsAdapter() { private void fillWorldItemsAdapter(ItemsListBuilder builder) {
if (listAdapter != null) { if (listAdapter != null) {
listAdapter.clear(); listAdapter.clear();
if (builder.getRegionMapItems().size() > 0) { if (builder.getRegionMapItems().size() > 0) {
@ -109,12 +105,12 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
int unusedSubIndex = 0; int unusedSubIndex = 0;
List<String> voicePromptsItems = new LinkedList<>(); List<String> voicePromptsItems = new LinkedList<>();
if (!ItemsListBuilder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.RECORDED)) { if (!builder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.RECORDED)) {
voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getMyApplication(), ItemsListBuilder.VoicePromptsType.RECORDED)); voicePromptsItems.add(builder.getVoicePromtName(ItemsListBuilder.VoicePromptsType.RECORDED));
voicePromptsItemsRecordedSubIndex = unusedSubIndex++; voicePromptsItemsRecordedSubIndex = unusedSubIndex++;
} }
if (!ItemsListBuilder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.TTS)) { if (!builder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.TTS)) {
voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getMyApplication(), ItemsListBuilder.VoicePromptsType.TTS)); voicePromptsItems.add(builder.getVoicePromtName(ItemsListBuilder.VoicePromptsType.TTS));
voicePromptsItemsTTSSubIndex = unusedSubIndex; voicePromptsItemsTTSSubIndex = unusedSubIndex;
} }
if (!voicePromptsItems.isEmpty()) { if (!voicePromptsItems.isEmpty()) {
@ -130,7 +126,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == worldRegionsIndex) { if (groupPosition == worldRegionsIndex) {
WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition); WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition);
DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region)); DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region.getRegionId()));
return true; return true;
} else if (groupPosition == voicePromptsIndex) { } else if (groupPosition == voicePromptsIndex) {
// //
@ -160,8 +156,9 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
} }
public void onCategorizationFinished() { public void onCategorizationFinished() {
if (builder.build()) { ItemsListBuilder builder = getDownloadActivity().getItemsBuilder();
fillWorldItemsAdapter(); if (builder != null && builder.build()) {
fillWorldItemsAdapter(builder);
listAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged();
expandAllGroups(); expandAllGroups();
} }