From d8fa958c3707df49a76794435f7f7b014c00739f Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 5 Oct 2015 20:15:22 +0300 Subject: [PATCH] Downloads refactoring --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 13 ++ .../plus/download/BaseDownloadActivity.java | 19 +++ .../plus/download/DownloadIndexesThread.java | 119 +++++++++++++- .../plus/download/items/ItemsListBuilder.java | 145 +++--------------- .../download/items/RegionDialogFragment.java | 39 ++--- .../download/items/RegionItemsFragment.java | 62 +++----- .../download/items/WorldItemsFragment.java | 21 ++- 7 files changed, 214 insertions(+), 204 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index 6bea8a0bcd..fc78b63f54 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -282,4 +282,17 @@ public class WorldRegion implements Serializable { 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; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index d7418160df..16c30103ec 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -19,6 +19,7 @@ import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.items.ItemsListBuilder; import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; 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) { if (downloadListIndexThread.getCurrentRunningTask() != null && getEntriesToDownload().get(item) == null) { downloadQueue.add(item); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 8233685c83..80de30d777 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -15,13 +15,17 @@ import android.os.StatFs; import android.view.View; import android.widget.Toast; +import net.osmand.Collator; import net.osmand.IndexConstants; +import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.access.AccessibleToast; +import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; import net.osmand.plus.Version; +import net.osmand.plus.WorldRegion; import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; @@ -39,13 +43,17 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @SuppressLint("NewApi") @@ -64,6 +72,12 @@ public class DownloadIndexesThread { private java.text.DateFormat dateFormat; private List itemsToUpdate = new ArrayList(); + private Map> resourcesByRegions = new HashMap<>(); + private List voiceRecItems = new LinkedList<>(); + private List voiceTTSItems = new LinkedList<>(); + + private boolean dataPrepared; + DatabaseHelper dbHelper; public DownloadIndexesThread(Context ctx) { @@ -141,6 +155,107 @@ public class DownloadIndexesThread { return itemsToUpdate; } + public boolean isDataPrepared() { + return dataPrepared; + } + + public Map> getResourcesByRegions() { + return resourcesByRegions; + } + + public List getVoiceRecItems() { + return voiceRecItems; + } + + public List getVoiceTTSItems() { + return voiceTTSItems; + } + + private boolean prepareData(List resources) { + List resourcesInRepository; + if (resources != null) { + resourcesInRepository = resources; + } else { + resourcesInRepository = DownloadActivity.downloadListIndexThread.getCachedIndexFiles(); + } + if (resourcesInRepository == null) { + return false; + } + + resourcesByRegions.clear(); + voiceRecItems.clear(); + voiceTTSItems.clear(); + + List mergedRegions = app.getWorldRegion().getFlattenedSubregions(); + mergedRegions.add(app.getWorldRegion()); + boolean voiceFilesProcessed = false; + for (WorldRegion region : mergedRegions) { + String downloadsIdPrefix = region.getDownloadsIdPrefix(); + + Map regionResources = new HashMap<>(); + + Set typesSet = new TreeSet<>(new Comparator() { + @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() { + @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() { + @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 implements DownloadFileShowWarning { @@ -408,13 +523,14 @@ public class DownloadIndexesThread { currentRunningTask.add(this); super.onPreExecute(); this.message = ctx.getString(R.string.downloading_list_indexes); + dataPrepared = false; } @Override protected IndexFileList doInBackground(Void... params) { IndexFileList indexFileList = DownloadOsmandIndexesHelper.getIndexesList(ctx); if (indexFileList != null) { - ItemsListBuilder.prepareData(app, indexFileList.getIndexFiles()); + prepareData(indexFileList.getIndexFiles()); } return indexFileList; } @@ -422,6 +538,7 @@ public class DownloadIndexesThread { protected void onPostExecute(IndexFileList result) { indexFiles = result; if (indexFiles != null && uiActivity != null) { + dataPrepared = resourcesByRegions.size() > 0; prepareFilesToUpdate(); boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap(); IndexItem basemap = indexFiles.getBasemap(); diff --git a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java b/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java index 8ec8a5650c..b99478d549 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java +++ b/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java @@ -2,15 +2,12 @@ package net.osmand.plus.download.items; import android.content.Context; -import net.osmand.Collator; -import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; -import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.IndexItem; import net.osmand.plus.srtmplugin.SRTMPlugin; @@ -18,20 +15,19 @@ import net.osmand.util.Algorithms; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; 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 static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; + private Map> resourcesByRegions; + private List voiceRecItems; + private List voiceTTSItems; + public class ResourceItem { 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 Map> resourcesByRegions = - new HashMap<>(); - private static List searchableWorldwideRegionItems = new LinkedList<>(); - - private static List voiceRecItems = new LinkedList<>(); - private static List voiceTTSItems = new LinkedList<>(); - - private static final Lock lock = new ReentrantLock(); - private List regionMapItems; private List allResourceItems; private List allSubregionItems; @@ -138,18 +125,18 @@ public class ItemsListBuilder { return list; } - public static String getVoicePromtName(Context context, VoicePromptsType type) { + public String getVoicePromtName(VoicePromptsType type) { switch (type) { case RECORDED: - return context.getResources().getString(R.string.index_name_voice); + return app.getResources().getString(R.string.index_name_voice); case TTS: - return context.getResources().getString(R.string.index_name_tts_voice); + return app.getResources().getString(R.string.index_name_tts_voice); default: return null; } } - public static List getVoicePromptsItems(VoicePromptsType type) { + public List getVoicePromptsItems(VoicePromptsType type) { switch (type) { case RECORDED: return voiceRecItems; @@ -160,7 +147,7 @@ public class ItemsListBuilder { } } - public static boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { + public boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { switch (type) { case RECORDED: return voiceRecItems.isEmpty(); @@ -171,28 +158,22 @@ public class ItemsListBuilder { } } - public ItemsListBuilder(OsmandApplication app) { + public ItemsListBuilder(OsmandApplication app, String regionId, Map> resourcesByRegions, + List voiceRecItems, List voiceTTSItems) { this.app = app; - regionMapItems = new LinkedList<>(); - allResourceItems = new LinkedList(); - allSubregionItems = new LinkedList<>(); - } + this.resourcesByRegions = resourcesByRegions; + this.voiceRecItems = voiceRecItems; + this.voiceTTSItems = voiceTTSItems; - public ItemsListBuilder(OsmandApplication app, WorldRegion region) { - this(app); - this.region = region; + regionMapItems = new LinkedList<>(); + allResourceItems = new LinkedList<>(); + allSubregionItems = new LinkedList<>(); + + region = app.getWorldRegion().getRegionById(regionId); } public boolean build() { - if (lock.tryLock()) { - try { - return obtainDataAndItems(); - } finally { - lock.unlock(); - } - } else { - return false; - } + return obtainDataAndItems(); } private boolean obtainDataAndItems() { @@ -206,92 +187,6 @@ public class ItemsListBuilder { return true; } - public static boolean prepareData(final OsmandApplication app, List resources) { - lock.lock(); - try { - List 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 mergedRegions = app.getWorldRegion().getFlattenedSubregions(); - mergedRegions.add(app.getWorldRegion()); - boolean voiceFilesProcessed = false; - for (WorldRegion region : mergedRegions) { - searchableWorldwideRegionItems.add(region); - - String downloadsIdPrefix = region.getDownloadsIdPrefix(); - - Map regionResources = new HashMap<>(); - - Set typesSet = new TreeSet<>(new Comparator() { - @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() { - @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() { srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; hasSrtm = false; diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java index 99808eaba8..60d9e43c5b 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java @@ -21,8 +21,8 @@ import org.apache.commons.logging.Log; public class RegionDialogFragment extends DialogFragment { private static final Log LOG = PlatformUtil.getLog(RegionDialogFragment.class); public static final String TAG = "RegionDialogFragment"; - private static final String REGION_DLG_KEY = "world_region_dialog_key"; - private WorldRegion region; + private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; + private String regionId; private DownloadsUiHelper.MapDownloadListener mProgressListener; @Override @@ -39,22 +39,13 @@ public class RegionDialogFragment extends DialogFragment { Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); - WorldRegion region = null; if (savedInstanceState != null) { - Object regionObj = savedInstanceState.getSerializable(REGION_DLG_KEY); - if (regionObj != null) { - region = (WorldRegion)regionObj; - } + regionId = savedInstanceState.getString(REGION_ID_DLG_KEY); } - if (region == null) { - Object regionObj = getArguments().getSerializable(REGION_DLG_KEY); - if (regionObj != null) { - region = (WorldRegion)regionObj; - } + if (regionId == null) { + regionId = getArguments().getString(REGION_ID_DLG_KEY); } - this.region = region; - Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); 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); if (fragment == null) { 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(), getResources()); @@ -95,7 +89,6 @@ public class RegionDialogFragment extends DialogFragment { @Override public void onResume() { super.onResume(); - LOG.debug(region.getName() + " onResume()"); getMyActivity().setOnProgressUpdateListener(mProgressListener); } @@ -107,7 +100,7 @@ public class RegionDialogFragment extends DialogFragment { @Override public void onSaveInstanceState(Bundle outState) { - outState.putSerializable(REGION_DLG_KEY, region); + outState.putString(REGION_ID_DLG_KEY, regionId); super.onSaveInstanceState(outState); } @@ -119,13 +112,13 @@ public class RegionDialogFragment extends DialogFragment { return (DownloadActivity) getActivity(); } - public void onRegionSelected(WorldRegion region) { - DownloadsUiHelper.showDialog(getActivity(), createInstance(region)); + public void onRegionSelected(String regionId) { + DownloadsUiHelper.showDialog(getActivity(), createInstance(regionId)); } - public static RegionDialogFragment createInstance(WorldRegion region) { + public static RegionDialogFragment createInstance(String regionId) { Bundle bundle = new Bundle(); - bundle.putSerializable(REGION_DLG_KEY, region); + bundle.putString(REGION_ID_DLG_KEY, regionId); RegionDialogFragment fragment = new RegionDialogFragment(); fragment.setArguments(bundle); return fragment; diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java index e71cbbaa8f..b6ac20a9c0 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java @@ -7,7 +7,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; -import android.widget.LinearLayout; import android.widget.TextView; import net.osmand.PlatformUtil; @@ -21,7 +20,6 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; -import net.osmand.util.Algorithms; 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 MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); - private ItemsListBuilder builder; private RegionsItemsAdapter listAdapter; private int regionMapsGroupPos = -1; private int additionalMapsGroupPos = -1; - private static final String REGION_KEY = "world_region_key"; - private WorldRegion region; + private static final String REGION_ID_KEY = "world_region_id_key"; + private String regionId; @Override public void onCreate(Bundle savedInstanceState) { @@ -56,33 +53,25 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.download_items_fragment, container, false); - WorldRegion region = null; if (savedInstanceState != null) { - Object regionObj = savedInstanceState.getSerializable(REGION_KEY); - if (regionObj != null) { - region = (WorldRegion) regionObj; - } + regionId = savedInstanceState.getString(REGION_ID_KEY); } - if (region == null) { - Object regionObj = getArguments().getSerializable(REGION_KEY); - if (regionObj != null) { - region = (WorldRegion) regionObj; - } + if (regionId == null) { + regionId = getArguments().getString(REGION_ID_KEY); } - this.region = region; - - builder = new ItemsListBuilder(getMyApplication(), this.region); - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); listAdapter = new RegionsItemsAdapter(getActivity()); listView.setAdapter(listAdapter); setListView(listView); - if (builder.build()) { - fillRegionItemsAdapter(); - listAdapter.notifyDataSetChanged(); - expandAllGroups(); + if (regionId != null) { + ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(regionId); + if (builder != null && builder.build()) { + fillRegionItemsAdapter(builder); + listAdapter.notifyDataSetChanged(); + expandAllGroups(); + } } return view; @@ -90,7 +79,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { @Override public void onSaveInstanceState(Bundle outState) { - outState.putSerializable(REGION_KEY, region); + outState.putString(REGION_ID_KEY, regionId); super.onSaveInstanceState(outState); } @@ -100,7 +89,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { if (obj instanceof WorldRegion) { WorldRegion region = (WorldRegion) obj; ((RegionDialogFragment) getParentFragment()) - .onRegionSelected(region); + .onRegionSelected(region.getRegionId()); return true; } else { return false; @@ -117,17 +106,12 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { return (OsmandApplication) getActivity().getApplication(); } - private void fillRegionItemsAdapter() { + private void fillRegionItemsAdapter(ItemsListBuilder builder) { if (listAdapter != null) { listAdapter.clear(); int nextAvailableGroupPos = 0; if (builder.getRegionMapItems().size() > 0) { - String sectionTitle; - if (builder.getAllResourceItems().size() > 0) { - sectionTitle = "Region maps"; - } else { - sectionTitle = ""; - } + String sectionTitle = "Region maps"; regionMapsGroupPos = nextAvailableGroupPos++; listAdapter.add(sectionTitle, builder.getRegionMapItems()); } @@ -136,7 +120,7 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { if (builder.getRegionMapItems().size() > 0) { sectionTitle = "Additional maps"; } else { - sectionTitle = ""; + sectionTitle = "Regions"; } additionalMapsGroupPos = nextAvailableGroupPos; listAdapter.add(sectionTitle, builder.getAllResourceItems()); @@ -148,9 +132,9 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { return (DownloadActivity) getActivity(); } - public static RegionItemsFragment createInstance(WorldRegion region) { + public static RegionItemsFragment createInstance(String regionId) { Bundle bundle = new Bundle(); - bundle.putSerializable(REGION_KEY, region); + bundle.putString(REGION_ID_KEY, regionId); RegionItemsFragment fragment = new RegionItemsFragment(); fragment.setArguments(bundle); return fragment; @@ -254,14 +238,6 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View v = convertView; 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) { LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java index f59b93f7be..a9f20a4fd3 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java @@ -23,7 +23,6 @@ import net.osmand.plus.WorldRegion; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.newimplementation.DownloadsUiHelper; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; @@ -42,7 +41,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { public static final int RELOAD_ID = 0; - private ItemsListBuilder builder; private WorldItemsAdapter listAdapter; private int worldRegionsIndex = -1; @@ -62,8 +60,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); listAdapter = new WorldItemsAdapter(getActivity()); listView.setAdapter(listAdapter); @@ -97,7 +93,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return (OsmandApplication) getActivity().getApplication(); } - private void fillWorldItemsAdapter() { + private void fillWorldItemsAdapter(ItemsListBuilder builder) { if (listAdapter != null) { listAdapter.clear(); if (builder.getRegionMapItems().size() > 0) { @@ -109,12 +105,12 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { int unusedSubIndex = 0; List voicePromptsItems = new LinkedList<>(); - if (!ItemsListBuilder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.RECORDED)) { - voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getMyApplication(), ItemsListBuilder.VoicePromptsType.RECORDED)); + if (!builder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.RECORDED)) { + voicePromptsItems.add(builder.getVoicePromtName(ItemsListBuilder.VoicePromptsType.RECORDED)); voicePromptsItemsRecordedSubIndex = unusedSubIndex++; } - if (!ItemsListBuilder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.TTS)) { - voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getMyApplication(), ItemsListBuilder.VoicePromptsType.TTS)); + if (!builder.isVoicePromptsItemsEmpty(ItemsListBuilder.VoicePromptsType.TTS)) { + voicePromptsItems.add(builder.getVoicePromtName(ItemsListBuilder.VoicePromptsType.TTS)); voicePromptsItemsTTSSubIndex = unusedSubIndex; } 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) { if (groupPosition == worldRegionsIndex) { WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition); - DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region)); + DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region.getRegionId())); return true; } else if (groupPosition == voicePromptsIndex) { // @@ -160,8 +156,9 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { } public void onCategorizationFinished() { - if (builder.build()) { - fillWorldItemsAdapter(); + ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(); + if (builder != null && builder.build()) { + fillWorldItemsAdapter(builder); listAdapter.notifyDataSetChanged(); expandAllGroups(); }