diff --git a/OsmAnd/res/layout/download_item_list_section.xml b/OsmAnd/res/layout/download_item_list_section.xml new file mode 100644 index 0000000000..06fef271b9 --- /dev/null +++ b/OsmAnd/res/layout/download_item_list_section.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/local_items_fragment.xml b/OsmAnd/res/layout/local_items_fragment.xml index 39ecab8c13..56593c17da 100644 --- a/OsmAnd/res/layout/local_items_fragment.xml +++ b/OsmAnd/res/layout/local_items_fragment.xml @@ -1,73 +1,10 @@ - - - - - - - - - - - - - - - - - - - - - + android:id="@android:id/list" + android:layout_width="fill_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:focusable="true" + android:groupIndicator="@android:color/transparent"/> diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 6690e10860..4838854220 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -1,6 +1,8 @@ package net.osmand.plus.download; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLConnection; @@ -107,9 +109,27 @@ public class DownloadOsmandIndexesHelper { log.info(strUrl); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); - URLConnection connection = NetworkUtils.getHttpURLConnection(strUrl); - InputStream in = connection.getInputStream(); - GZIPInputStream gzin = new GZIPInputStream(in); + File tmp = new File(ctx.getAppPath(IndexConstants.BACKUP_INDEX_DIR), "map_indexes.tmp"); + if (!tmp.exists()) { + log.info(strUrl); + + log.warn("111 - NO FILE"); + + URLConnection connection = NetworkUtils.getHttpURLConnection(strUrl); + InputStream in = connection.getInputStream(); + + FileOutputStream out = new FileOutputStream(tmp); + int read; + byte[] buffer = new byte[32256]; + while ((read = in.read(buffer)) != -1) { + out.write(buffer, 0, read); + } + out.close(); + } else { + log.warn("111 - READ BACKUP"); + } + + GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(tmp)); parser.setInput(gzin, "UTF-8"); //$NON-NLS-1$ int next; while((next = parser.next()) != XmlPullParser.END_DOCUMENT) { @@ -128,7 +148,7 @@ public class DownloadOsmandIndexesHelper { } result.sort(); gzin.close(); - in.close(); + //in.close(); } catch (IOException e) { log.error("Error while loading indexes from repository", e); //$NON-NLS-1$ return null; diff --git a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java b/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java index bd3cac874d..1d6f516705 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java +++ b/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java @@ -6,12 +6,10 @@ 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.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.util.Algorithms; @@ -35,9 +33,6 @@ public class ItemsListBuilder { private String resourceId; private String title; - private long contentSize; - private long containerSize; - private boolean disabled; private IndexItem indexItem; private WorldRegion worldRegion; @@ -66,30 +61,6 @@ public class ItemsListBuilder { this.title = title; } - public long getContentSize() { - return contentSize; - } - - public void setContentSize(long contentSize) { - this.contentSize = contentSize; - } - - public long getContainerSize() { - return containerSize; - } - - public void setContainerSize(long containerSize) { - this.containerSize = containerSize; - } - - public boolean isDisabled() { - return disabled; - } - - public void setDisabled(boolean disabled) { - this.disabled = disabled; - } - public ResourceItem(IndexItem indexItem, WorldRegion worldRegion) { this.indexItem = indexItem; this.worldRegion = worldRegion; @@ -158,7 +129,7 @@ public class ItemsListBuilder { public ItemsListBuilder(OsmandApplication app) { this.app = app; - regionMapItems = new LinkedList(); + regionMapItems = new LinkedList<>(); allResourceItems = new LinkedList(); allSubregionItems = new LinkedList<>(); } @@ -320,8 +291,6 @@ public class ItemsListBuilder { ResourceItem resItem = new ResourceItem(indexItem, region); resItem.setResourceId(indexItem.getSimplifiedFileName()); resItem.setTitle(name); - resItem.setContentSize(indexItem.getContentSize()); - resItem.setContainerSize(indexItem.getSize()); if (region != this.region && srtmDisabled) { if (hasSrtm && indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java index 1b893cdab6..83ff161119 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java @@ -1,16 +1,12 @@ package net.osmand.plus.download.items; import android.content.Context; -import android.graphics.drawable.Drawable; +import android.content.res.TypedArray; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; -import android.widget.ListView; +import android.widget.ExpandableListView; import android.widget.TextView; import net.osmand.PlatformUtil; @@ -18,30 +14,28 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; 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.srtmplugin.SRTMPlugin; import org.apache.commons.logging.Log; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Locale; +import java.util.Map; -public class RegionItemsFragment extends Fragment { +public class RegionItemsFragment extends OsmandExpandableListFragment { public static final String TAG = "RegionItemsFragment"; 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 RegionsAdapter regionsAdapter; - private MapsAdapter mapsAdapter; - private VoicePromtsAdapter voicePromtsAdapter; - - private TextView mapsTextView; - private ListView mapsListView; - private TextView regionsTextView; - private ListView regionsListView; - private TextView voicePromtsTextView; - private ListView voicePromtsListView; + private RegionsItemsAdapter listAdapter; private static final String REGION_KEY = "world_region_key"; private WorldRegion region; @@ -75,39 +69,15 @@ public class RegionItemsFragment extends Fragment { builder = new ItemsListBuilder(getMyApplication(), this.region); - mapsTextView = (TextView) view.findViewById(R.id.list_world_regions_title); - mapsTextView.setText("Region maps".toUpperCase()); - mapsListView = (ListView) view.findViewById(R.id.list_world_regions); - mapsAdapter = new MapsAdapter(getActivity()); - mapsListView.setAdapter(mapsAdapter); - - regionsTextView = (TextView) view.findViewById(R.id.list_world_maps_title); - regionsTextView.setText("Additional maps".toUpperCase()); - regionsListView = (ListView) view.findViewById(R.id.list_world_maps); - regionsAdapter = new RegionsAdapter(getActivity()); - regionsListView.setAdapter(regionsAdapter); - regionsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - Object obj = regionsAdapter.getItem(position); - if (obj instanceof WorldRegion) { - WorldRegion region = (WorldRegion) obj; - ((RegionDialogFragment) getParentFragment()) - .onRegionSelected(region); - } - } - }); - - voicePromtsTextView = (TextView) view.findViewById(R.id.list_voice_promts_title); - voicePromtsTextView.setText("Voice promts".toUpperCase()); - voicePromtsListView = (ListView) view.findViewById(R.id.list_voice_promts); - voicePromtsAdapter = new VoicePromtsAdapter(getActivity(), getMyApplication()); - voicePromtsListView.setAdapter(voicePromtsAdapter); + ExpandableListView listView = (ExpandableListView)view.findViewById(android.R.id.list); + listAdapter = new RegionsItemsAdapter(getActivity()); + listView.setAdapter(listAdapter); + setListView(listView); if (builder.build()) { - fillMapsAdapter(); - fillRegionsAdapter(); - fillVoicePromtsAdapter(); + fillRegionItemsAdapter(); + listAdapter.notifyDataSetChanged(); + expandAllGroups(); } return view; @@ -119,41 +89,38 @@ public class RegionItemsFragment extends Fragment { super.onSaveInstanceState(outState); } + @Override + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + Object obj = listAdapter.getChild(groupPosition, childPosition); + if (obj instanceof WorldRegion) { + WorldRegion region = (WorldRegion) obj; + ((RegionDialogFragment) getParentFragment()) + .onRegionSelected(region); + return true; + } else { + return false; + } + } + + private void expandAllGroups() { + for (int i = 0; i < listAdapter.getGroupCount(); i++) { + getExpandableListView().expandGroup(i); + } + } + public OsmandApplication getMyApplication() { return (OsmandApplication)getActivity().getApplication(); } - private void fillRegionsAdapter() { - if (regionsAdapter != null) { - regionsAdapter.clear(); - regionsAdapter.addAll(builder.getAllResourceItems()); - updateVisibility(regionsAdapter, regionsTextView, regionsListView); - } - } - - private void fillMapsAdapter() { - if (mapsAdapter != null) { - mapsAdapter.clear(); - mapsAdapter.addAll(builder.getRegionMapItems()); - updateVisibility(mapsAdapter, mapsTextView, mapsListView); - } - } - - private void fillVoicePromtsAdapter() { - if (voicePromtsAdapter != null) { - voicePromtsAdapter.clear(); - //voicePromtsAdapter.addAll(cats); - updateVisibility(voicePromtsAdapter, voicePromtsTextView, voicePromtsListView); - } - } - - private void updateVisibility(ArrayAdapter adapter, TextView textView, ListView listView) { - if (adapter.isEmpty()) { - textView.setVisibility(View.GONE); - listView.setVisibility(View.GONE); - } else { - textView.setVisibility(View.VISIBLE); - listView.setVisibility(View.VISIBLE); + private void fillRegionItemsAdapter() { + if (listAdapter != null) { + listAdapter.clear(); + if (builder.getRegionMapItems().size() > 0) { + listAdapter.add("Region maps".toUpperCase(), builder.getRegionMapItems()); + } + if (builder.getAllResourceItems().size() > 0) { + listAdapter.add("Additional maps".toUpperCase(), builder.getAllResourceItems()); + } } } @@ -169,97 +136,143 @@ public class RegionItemsFragment extends Fragment { return fragment; } - private static class MapsAdapter extends ArrayAdapter { + private class RegionsItemsAdapter extends OsmandBaseExpandableListAdapter { + + private Map data = new LinkedHashMap<>(); + private List sections = new LinkedList<>(); + private Context ctx; private boolean srtmDisabled; - public MapsAdapter(Context context) { - super(context, R.layout.simple_list_menu_item); + public RegionsItemsAdapter(Context ctx) { + this.ctx = ctx; srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; + TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); + ta.recycle(); + } + + public void clear() { + data.clear(); + sections.clear(); + notifyDataSetChanged(); + } + + public void add(String section, List list) { + if (!sections.contains(section)) { + sections.add(section); + } + if (!data.containsKey(section)) { + data.put(section, new ArrayList()); + } + data.get(section).addAll(list); } @Override - public View getView(int position, View convertView, ViewGroup parent) { - ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView); - convertView.setTag(viewHolder); - } else { - viewHolder = (ItemViewHolder) convertView.getTag(); - } - viewHolder.setSrtmDisabled(srtmDisabled); - - ItemsListBuilder.ResourceItem item = getItem(position); - viewHolder.bindIndexItem(item.getIndexItem(), (DownloadActivity) getContext(), true, false); - return convertView; - } - } - - private static class RegionsAdapter extends ArrayAdapter { - private boolean srtmDisabled; - - public RegionsAdapter(Context context) { - super(context, R.layout.two_line_with_images_list_item); - srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; + public Object getChild(int groupPosition, int childPosition) { + String section = sections.get(groupPosition); + return data.get(section).get(childPosition); } @Override - public View getView(int position, View convertView, ViewGroup parent) { - ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView); - convertView.setTag(viewHolder); - } else { - viewHolder = (ItemViewHolder) convertView.getTag(); - } - viewHolder.setSrtmDisabled(srtmDisabled); - - Object item = getItem(position); - if (item instanceof WorldRegion) { - viewHolder.bindRegion((WorldRegion) item, (DownloadActivity) getContext()); - } else if (item instanceof ItemsListBuilder.ResourceItem) { - viewHolder.bindIndexItem(((ItemsListBuilder.ResourceItem) item).getIndexItem(), - (DownloadActivity) getContext(), false, true); - } else { - throw new IllegalArgumentException("Item must be of type WorldRegion or " + - "IndexItem but is of type:" + item.getClass()); - } - return convertView; - } - } - - private static class VoicePromtsAdapter extends ArrayAdapter { - private final OsmandApplication osmandApplication; - - public VoicePromtsAdapter(Context context, OsmandApplication osmandApplication) { - super(context, R.layout.simple_list_menu_item); - this.osmandApplication = osmandApplication; + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 10000 + childPosition; } @Override - public View getView(int position, View convertView, ViewGroup parent) { - RegionItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.simple_list_menu_item, parent, false); - viewHolder = new RegionItemViewHolder(); - viewHolder.textView = (TextView) convertView.findViewById(R.id.title); - convertView.setTag(viewHolder); + public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { + + final Object child = getChild(groupPosition, childPosition); + + if (child instanceof ItemsListBuilder.ResourceItem && groupPosition == 0 && getGroupCount() > 1) { + ItemViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.two_line_with_images_list_item, parent, false); + viewHolder = new ItemViewHolder(convertView); + convertView.setTag(viewHolder); + } else { + viewHolder = (ItemViewHolder) convertView.getTag(); + } + viewHolder.setSrtmDisabled(srtmDisabled); + + ItemsListBuilder.ResourceItem item = (ItemsListBuilder.ResourceItem)child; + viewHolder.bindIndexItem(item.getIndexItem(), getDownloadActivity(), true, false); } else { - viewHolder = (RegionItemViewHolder) convertView.getTag(); + ItemViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.two_line_with_images_list_item, parent, false); + viewHolder = new ItemViewHolder(convertView); + convertView.setTag(viewHolder); + } else { + viewHolder = (ItemViewHolder) convertView.getTag(); + } + viewHolder.setSrtmDisabled(srtmDisabled); + + if (child instanceof WorldRegion) { + viewHolder.bindRegion((WorldRegion) child, getDownloadActivity()); + } else if (child instanceof ItemsListBuilder.ResourceItem) { + viewHolder.bindIndexItem(((ItemsListBuilder.ResourceItem) child).getIndexItem(), + getDownloadActivity(), false, true); + } else { + throw new IllegalArgumentException("Item must be of type WorldRegion or " + + "IndexItem but is of type:" + child.getClass()); + } } - Drawable iconLeft = osmandApplication.getIconsCache() - .getContentIcon(R.drawable.ic_world_globe_dark); - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - viewHolder.textView.setText(getItem(position).toString()); + + convertView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onChildClick(null, v, groupPosition, childPosition, 0); + } + }); + return convertView; } - } - private static class RegionItemViewHolder { - TextView textView; + @Override + public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { + View v = convertView; + String section = getGroup(groupPosition); + if (v == null) { + LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = inflater.inflate(R.layout.download_item_list_section, parent, false); + } + TextView nameView = ((TextView) v.findViewById(R.id.section_name)); + nameView.setText(section); + + v.setOnClickListener(null); + return v; + } + + @Override + public int getChildrenCount(int groupPosition) { + String section = sections.get(groupPosition); + return data.get(section).size(); + } + + @Override + public String getGroup(int groupPosition) { + return sections.get(groupPosition); + } + + @Override + public int getGroupCount() { + return sections.size(); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } } } diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java index 73a9bd28ae..00c1958e3a 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java @@ -1,9 +1,9 @@ package net.osmand.plus.download.items; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; import android.view.LayoutInflater; import android.view.Menu; @@ -11,38 +11,34 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; -import android.widget.ListView; +import android.widget.ExpandableListView; import android.widget.TextView; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; 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.newimplementation.DownloadsUiHelper; import org.apache.commons.logging.Log; -public class WorldItemsFragment extends Fragment { +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class WorldItemsFragment extends OsmandExpandableListFragment { public static final String TAG = "WorldItemsFragment"; private static final Log LOG = PlatformUtil.getLog(WorldItemsFragment.class); public static final int RELOAD_ID = 0; private ItemsListBuilder builder; - private WorldRegionsAdapter worldRegionsAdapter; - private WorldMapsAdapter worldMapsAdapter; - private VoicePromtsAdapter voicePromtsAdapter; - - private TextView worldRegionsTextView; - private ListView worldRegionsListView; - private TextView worldMapsTextView; - private ListView worldMapsListView; - private TextView voicePromtsTextView; - private ListView voicePromtsListView; + private WorldItemsAdapter listAdapter; @Override public void onCreate(Bundle savedInstanceState) { @@ -56,30 +52,11 @@ public class WorldItemsFragment extends Fragment { builder = new ItemsListBuilder(getMyApplication(), getMyApplication().getWorldRegion()); - worldRegionsTextView = (TextView) view.findViewById(R.id.list_world_regions_title); - worldRegionsTextView.setText("World regions".toUpperCase()); - worldRegionsListView = (ListView) view.findViewById(R.id.list_world_regions); - worldRegionsAdapter = new WorldRegionsAdapter(getActivity(), getMyApplication()); - worldRegionsListView.setAdapter(worldRegionsAdapter); - worldRegionsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - WorldRegion region = worldRegionsAdapter.getItem(position); - DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region)); - } - }); - - worldMapsTextView = (TextView) view.findViewById(R.id.list_world_maps_title); - worldMapsTextView.setText("World maps".toUpperCase()); - worldMapsListView = (ListView) view.findViewById(R.id.list_world_maps); - worldMapsAdapter = new WorldMapsAdapter(getActivity()); - worldMapsListView.setAdapter(worldMapsAdapter); - - voicePromtsTextView = (TextView) view.findViewById(R.id.list_voice_promts_title); - voicePromtsTextView.setText("Voice promts".toUpperCase()); - voicePromtsListView = (ListView) view.findViewById(R.id.list_voice_promts); - voicePromtsAdapter = new VoicePromtsAdapter(getActivity(), getMyApplication()); - voicePromtsListView.setAdapter(voicePromtsAdapter); + ExpandableListView listView = (ExpandableListView)view.findViewById(android.R.id.list); + listAdapter = new WorldItemsAdapter(getActivity()); + listView.setAdapter(listAdapter); + expandAllGroups(); + setListView(listView); onCategorizationFinished(); @@ -89,63 +66,33 @@ public class WorldItemsFragment extends Fragment { return view; } - public static void setListViewHeightBasedOnChildren(ListView listView) { - ListAdapter listAdapter = listView.getAdapter(); - if (listAdapter == null) { - // pre-condition - return; + private void expandAllGroups() { + for (int i = 0; i < listAdapter.getGroupCount(); i++) { + getExpandableListView().expandGroup(i); } - - int totalHeight = 0; - for (int i = 0; i < listAdapter.getCount(); i++) { - View listItem = listAdapter.getView(i, null, listView); - listItem.measure(0, 0); - totalHeight += listItem.getMeasuredHeight(); - } - - ViewGroup.LayoutParams params = listView.getLayoutParams(); - params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); - listView.setLayoutParams(params); - listView.requestLayout(); } public OsmandApplication getMyApplication() { return (OsmandApplication)getActivity().getApplication(); } - private void fillWorldRegionsAdapter() { - if (worldRegionsAdapter != null) { - worldRegionsAdapter.clear(); - worldRegionsAdapter.addAll(builder.getRegionsFromAllItems()); - updateVisibility(worldRegionsAdapter, worldRegionsTextView, worldRegionsListView); + private void fillWorldItemsAdapter() { + if (listAdapter != null) { + listAdapter.clear(); + listAdapter.add("World regions".toUpperCase(), builder.getRegionsFromAllItems()); + listAdapter.add("World maps".toUpperCase(), builder.getRegionMapItems()); + //listAdapter.add("Voice promts".toUpperCase(), null); } } - private void fillWorldMapsAdapter() { - if (worldMapsAdapter != null) { - worldMapsAdapter.clear(); - worldMapsAdapter.addAll(builder.getRegionMapItems()); - updateVisibility(worldMapsAdapter, worldMapsTextView, worldMapsListView); - } - } - - private void fillVoicePromtsAdapter() { - if (voicePromtsAdapter != null) { - voicePromtsAdapter.clear(); - //voicePromtsAdapter.addAll(cats); - updateVisibility(voicePromtsAdapter, voicePromtsTextView, voicePromtsListView); - } - } - - private void updateVisibility(ArrayAdapter adapter, TextView textView, ListView listView) { - if (adapter.isEmpty()) { - textView.setVisibility(View.GONE); - listView.setVisibility(View.GONE); - } else { - textView.setVisibility(View.VISIBLE); - listView.setVisibility(View.VISIBLE); - setListViewHeightBasedOnChildren(listView); + @Override + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + if (groupPosition == 0) { + WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition); + DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region)); + return true; } + return false; } @Override @@ -171,97 +118,160 @@ public class WorldItemsFragment extends Fragment { public void onCategorizationFinished() { if (builder.build()) { - fillWorldRegionsAdapter(); - fillWorldMapsAdapter(); - fillVoicePromtsAdapter(); + fillWorldItemsAdapter(); + listAdapter.notifyDataSetChanged(); + expandAllGroups(); } } - private static class WorldRegionsAdapter extends ArrayAdapter { - private final OsmandApplication osmandApplication; + private class WorldItemsAdapter extends OsmandBaseExpandableListAdapter { - public WorldRegionsAdapter(Context context, OsmandApplication osmandApplication) { - super(context, R.layout.simple_list_menu_item); - this.osmandApplication = osmandApplication; - } + Map data = new LinkedHashMap<>(); + List sections = new LinkedList<>(); + Context ctx; - @Override - public View getView(int position, View convertView, ViewGroup parent) { - ViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.simple_list_menu_item, parent, false); - viewHolder = new ViewHolder(); - viewHolder.textView = (TextView) convertView.findViewById(R.id.title); - convertView.setTag(viewHolder); - } else { - viewHolder = (ViewHolder) convertView.getTag(); - } - Drawable iconLeft = osmandApplication.getIconsCache() - .getContentIcon(R.drawable.ic_world_globe_dark); - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - viewHolder.textView.setText(getItem(position).getName()); - return convertView; - } - - private static class ViewHolder { + private class SimpleViewHolder { TextView textView; } - } - private static class WorldMapsAdapter extends ArrayAdapter { + public WorldItemsAdapter(Context ctx) { + this.ctx = ctx; + TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); + ta.recycle(); + } - public WorldMapsAdapter(Context context) { - super(context, R.layout.simple_list_menu_item); + public void clear() { + data.clear(); + sections.clear(); + notifyDataSetChanged(); + } + + public void add(String section, List list) { + if (!sections.contains(section)) { + sections.add(section); + } + if (!data.containsKey(section)) { + data.put(section, new ArrayList()); + } + data.get(section).addAll(list); } @Override - public View getView(int position, View convertView, ViewGroup parent) { - ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView); - convertView.setTag(viewHolder); - } else { - viewHolder = (ItemViewHolder) convertView.getTag(); - } - ItemsListBuilder.ResourceItem item = getItem(position); - viewHolder.bindIndexItem(item.getIndexItem(), (DownloadActivity) getContext(), false, false); - return convertView; - } - } - - private static class VoicePromtsAdapter extends ArrayAdapter { - private final OsmandApplication osmandApplication; - - public VoicePromtsAdapter(Context context, OsmandApplication osmandApplication) { - super(context, R.layout.simple_list_menu_item); - this.osmandApplication = osmandApplication; + public Object getChild(int groupPosition, int childPosition) { + String section = sections.get(groupPosition); + return data.get(section).get(childPosition); } @Override - public View getView(int position, View convertView, ViewGroup parent) { - ViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.simple_list_menu_item, parent, false); - viewHolder = new ViewHolder(); - viewHolder.textView = (TextView) convertView.findViewById(R.id.title); - convertView.setTag(viewHolder); + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 10000 + childPosition; + } + + @Override + public int getChildType(int groupPosition, int childPosition) { + final Object child = getChild(groupPosition, childPosition); + if (child instanceof WorldRegion) { + return 0; } else { - viewHolder = (ViewHolder) convertView.getTag(); + return 1; } - Drawable iconLeft = osmandApplication.getIconsCache() - .getContentIcon(R.drawable.ic_world_globe_dark); - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - viewHolder.textView.setText(getItem(position).toString()); + } + + @Override + public int getChildTypeCount() { + return 2; + } + + @Override + public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { + + final Object child = getChild(groupPosition, childPosition); + + if (child instanceof WorldRegion) { + WorldRegion item = (WorldRegion)child; + SimpleViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.simple_list_menu_item, parent, false); + viewHolder = new SimpleViewHolder(); + viewHolder.textView = (TextView) convertView.findViewById(R.id.title); + convertView.setTag(viewHolder); + } else { + viewHolder = (SimpleViewHolder) convertView.getTag(); + } + Drawable iconLeft = getMyApplication().getIconsCache() + .getContentIcon(R.drawable.ic_world_globe_dark); + viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); + viewHolder.textView.setText(item.getName()); + + } else if (child instanceof ItemsListBuilder.ResourceItem) { + ItemsListBuilder.ResourceItem item = (ItemsListBuilder.ResourceItem) child; + ItemViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.two_line_with_images_list_item, parent, false); + viewHolder = new ItemViewHolder(convertView); + convertView.setTag(viewHolder); + } else { + viewHolder = (ItemViewHolder) convertView.getTag(); + } + viewHolder.bindIndexItem(item.getIndexItem(), getDownloadActivity(), false, false); + } + + convertView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onChildClick(null, v, groupPosition, childPosition, 0); + } + }); + return convertView; } - private static class ViewHolder { - TextView textView; + @Override + public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { + View v = convertView; + String section = getGroup(groupPosition); + if (v == null) { + LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = inflater.inflate(R.layout.download_item_list_section, parent, false); + } + TextView nameView = ((TextView) v.findViewById(R.id.section_name)); + nameView.setText(section); + + v.setOnClickListener(null); + return v; + } + + @Override + public int getChildrenCount(int groupPosition) { + String section = sections.get(groupPosition); + return data.get(section).size(); + } + + @Override + public String getGroup(int groupPosition) { + return sections.get(groupPosition); + } + + @Override + public int getGroupCount() { + return sections.size(); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; } } - }