Update fragments

This commit is contained in:
Victor Shcherb 2015-10-19 09:18:27 +02:00
parent 3cc2d941e5
commit be38b19e88
10 changed files with 289 additions and 434 deletions

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?attr/ctx_menu_info_view_bg"
android:orientation="vertical">
<include layout="@layout/download_items_list"/>
</LinearLayout>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/ctx_menu_info_view_bg">
<include layout="@layout/download_items_list"/>
</LinearLayout>

View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:background="@android:color/transparent"/>

View file

@ -4,8 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.osmand.plus.download.newimplementation.NewLocalIndexesFragment">
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
@ -19,7 +18,8 @@
android:minHeight="@dimen/dashboard_map_toolbar"
android:theme="?attr/toolbar_theme"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp"/>
app:contentInsetStart="72dp"
/>
</android.support.design.widget.AppBarLayout>
<include

View file

@ -16,9 +16,9 @@ import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment;
import net.osmand.plus.download.ui.DownloadResourceGroupFragment;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.plus.download.ui.UpdatesIndexFragment;
import net.osmand.plus.download.ui.WorldItemsFragment;
import net.osmand.plus.views.controls.PagerSlidingTabStrip;
import android.content.ActivityNotFoundException;
import android.content.Intent;
@ -78,7 +78,7 @@ public class DownloadActivity extends BaseDownloadActivity {
mTabs.add(new TabActivity.TabItem(R.string.download_tab_downloads,
getString(R.string.download_tab_downloads), WorldItemsFragment.class));
getString(R.string.download_tab_downloads), DownloadResourceGroupFragment.class));
mTabs.add(new TabActivity.TabItem(R.string.download_tab_local,
getString(R.string.download_tab_local), LocalIndexesFragment.class));
mTabs.add(new TabActivity.TabItem(R.string.download_tab_updates,
@ -136,9 +136,6 @@ public class DownloadActivity extends BaseDownloadActivity {
@UiThread
public void downloadHasFinished() {
visibleBanner.updateBannerInProgress();
// FIXME
//((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView());
for (WeakReference<Fragment> ref : fragSet) {
Fragment f = ref.get();
if (f.isAdded()) {
@ -169,8 +166,6 @@ public class DownloadActivity extends BaseDownloadActivity {
@UiThread
public void newDownloadIndexes() {
visibleBanner.updateBannerInProgress();
// FIXME
//((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView());
for (WeakReference<Fragment> ref : fragSet) {
Fragment f = ref.get();
if (f.isAdded()) {

View file

@ -135,9 +135,26 @@ public class IndexItem implements Comparable<IndexItem> {
FIXME;
}
public String getLocalDate() {
FIXME;
}
public boolean isDownloaded() {
// return listAlreadyDownloaded.containsKey(getTargetFileName());
Map<String,String> indexFileNames = context.getIndexFileNames();
if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) {
boolean outdated = false;
String date;
if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) {
date = indexItem.getDate(dateFormat);
} else {
String sfName = indexItem.getTargetFileName();
Map<String,String> indexActivatedFileNames = context.getIndexActivatedFileNames();
final boolean updatableResource = indexActivatedFileNames.containsKey(sfName);
date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName);
outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem);
}
}
public String getVisibleName(Context ctx, OsmandRegions osmandRegions) {

View file

@ -1,36 +1,56 @@
package net.osmand.plus.download.ui;
import java.util.ArrayList;
import java.util.List;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.WorldRegion;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.IndexItem;
import net.osmand.util.Algorithms;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, OnChildClickListener {
public static final int RELOAD_ID = 0;
public static final int SEARCH_ID = 1;
public static final String TAG = "RegionDialogFragment";
private static final String REGION_ID_DLG_KEY = "world_region_dialog_key";
private String groupId;
private View view;
private BannerAndDownloadFreeVersion banner;
protected ExpandableListView listView;
protected WorldItemsFragment.DownloadResourceGroupAdapter listAdapter;
protected DownloadResourceGroupAdapter listAdapter;
private DownloadResourceGroup group;
private DownloadActivity activity;
private Toolbar toolbar;
@ -44,6 +64,10 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
setHasOptionsMenu(true);
}
public boolean openAsDialog() {
return !Algorithms.isEmpty(groupId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.maps_in_category_fragment, container, false);
@ -66,16 +90,19 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
dismiss();
}
});
if (!openAsDialog()) {
toolbar.setVisibility(View.GONE);
}
setHasOptionsMenu(true);
banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity());
listView = (ExpandableListView) view.findViewById(android.R.id.list);
listView.setOnChildClickListener(this);
listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity);
listAdapter = new DownloadResourceGroupAdapter(activity);
listView.setAdapter(listAdapter);
reloadData();
return view;
}
@ -101,6 +128,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setShowsDialog(openAsDialog());
listView.setBackgroundColor(getResources().getColor(
getMyApplication().getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark));
}
@ -163,6 +191,34 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
return (DownloadActivity) getActivity();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!openAsDialog()) {
MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh);
itemReload.setIcon(R.drawable.ic_action_refresh_dark);
MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search);
itemSearch.setIcon(R.drawable.ic_action_search_dark);
MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case RELOAD_ID:
// re-create the thread
getDownloadActivity().getDownloadThread().runReloadIndexFiles();
return true;
case SEARCH_ID:
getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance(""));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onRegionSelected(String regionId) {
final DownloadResourceGroupFragment regionDialogFragment = createInstance(regionId);
getDownloadActivity().showDialog(getActivity(), regionDialogFragment);
@ -206,4 +262,192 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
return fragment;
}
}
private static class DownloadGroupViewHolder {
TextView textView;
private DownloadActivity ctx;
public DownloadGroupViewHolder(DownloadActivity ctx, View v) {
this.ctx = ctx;
textView = (TextView) v.findViewById(R.id.title);
}
private Drawable getIconForGroup(DownloadResourceGroup group) {
Drawable iconLeft;
if (group.getType() == DownloadResourceGroupType.VOICE_REC
|| group.getType() == DownloadResourceGroupType.VOICE_TTS) {
iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up);
} else {
IconsCache cache = ctx.getMyApplication().getIconsCache();
if (group.getParentGroup() == null
|| group.getParentGroup().getType() == DownloadResourceGroupType.WORLD) {
iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark);
} else {
DownloadResourceGroup ggr = group
.getGroupById(DownloadResourceGroupType.REGION_MAPS.getDefaultId());
iconLeft = cache.getContentIcon(R.drawable.ic_map);
if (ggr != null && ggr.getIndividualResources() != null) {
IndexItem item = null;
for (IndexItem ii : ggr.getIndividualResources()) {
if (ii.getType() == DownloadActivityType.NORMAL_FILE
|| ii.getType() == DownloadActivityType.ROADS_FILE) {
if (ii.isDownloaded() || ii.isOutdated()) {
item = ii;
break;
}
}
}
if (item != null) {
if (item.isOutdated()) {
iconLeft = cache.getIcon(R.drawable.ic_map,
ctx.getResources().getColor(R.color.color_distance));
} else {
iconLeft = cache.getIcon(R.drawable.ic_map,
ctx.getResources().getColor(R.color.color_ok));
}
}
}
}
}
return iconLeft;
}
public void bindItem(DownloadResourceGroup group) {
Drawable iconLeft = getIconForGroup(group);
textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null);
String name = group.getName();
WorldRegion region = group.getRegion();
if (region != null) {
name = region.getName();
}
textView.setText(name);
}
}
public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter {
private List<DownloadResourceGroup> data = new ArrayList<DownloadResourceGroup>();
private DownloadActivity ctx;
public DownloadResourceGroupAdapter(DownloadActivity ctx) {
this.ctx = ctx;
TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary });
ta.recycle();
}
public void clear() {
data.clear();
notifyDataSetChanged();
}
public void update(DownloadResourceGroup mainGroup) {
data = mainGroup.getGroups();
notifyDataSetChanged();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
DownloadResourceGroup drg = data.get(groupPosition);
if (drg.getType().containsIndexItem()) {
return drg.getItemByIndex(childPosition);
}
return drg.getGroupByIndex(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 10000 + childPosition;
}
@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 IndexItem) {
IndexItem item = (IndexItem) child;
ItemViewHolder viewHolder;
if (convertView != null && convertView.getTag() instanceof ItemViewHolder) {
viewHolder = (ItemViewHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.two_line_with_images_list_item, parent, false);
viewHolder = new ItemViewHolder(convertView, ctx);
convertView.setTag(viewHolder);
}
viewHolder.bindIndexItem(item, false, false);
} else {
DownloadResourceGroup group = (DownloadResourceGroup) child;
DownloadGroupViewHolder viewHolder;
if (convertView != null && convertView.getTag() instanceof DownloadGroupViewHolder) {
viewHolder = (DownloadGroupViewHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_menu_item,
parent, false);
viewHolder = new DownloadGroupViewHolder(ctx, convertView);
convertView.setTag(viewHolder);
}
viewHolder.bindItem(group);
}
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, final View convertView, final ViewGroup parent) {
View v = convertView;
String section = getGroup(groupPosition);
if (v == null) {
LayoutInflater inflater = (LayoutInflater) ctx.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);
TypedValue typedValue = new TypedValue();
Resources.Theme theme = ctx.getTheme();
theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true);
v.setBackgroundColor(typedValue.data);
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
DownloadResourceGroup drg = data.get(groupPosition);
int rid = drg.getType().getResourceId();
if (rid != -1) {
return ctx.getString(rid);
}
return "";
}
@Override
public int getGroupCount() {
return data.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;
}
}
}

View file

@ -25,6 +25,7 @@ import net.osmand.plus.srtmplugin.SRTMPlugin;
import java.text.DateFormat;
import java.util.Map;
// FIXME
public class ItemViewHolder {
private final java.text.DateFormat dateFormat;
@ -36,11 +37,13 @@ public class ItemViewHolder {
protected final Button rightButton;
protected final ProgressBar progressBar;
protected final TextView mapDateTextView;
protected final DownloadActivity context;
private boolean srtmDisabled;
private boolean nauticalPluginDisabled;
private boolean freeVersion;
protected final DownloadActivity context;
private int textColorPrimary;
private int textColorSecondary;
private RightButtonAction rightButtonAction;
@ -63,12 +66,6 @@ public class ItemViewHolder {
descrTextView = (TextView) view.findViewById(R.id.description);
rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton);
nameTextView = (TextView) view.findViewById(R.id.name);
}
public ItemViewHolder(View convertView,
DownloadActivity context) {
this(convertView, context);
this.dateFormat = context.getMyApplication().getResourceManager().getDateFormat();
@ -80,17 +77,25 @@ public class ItemViewHolder {
textColorSecondary = typedValue.data;
}
public void initAppStatusVariables() {
// FIXME don't initialize on every row
private void initAppStatusVariables() {
srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null;
nauticalPluginDisabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null;
freeVersion = Version.isFreeVersion(context.getMyApplication());
}
// FIXME
public void bindIndexItem(final IndexItem indexItem,
boolean showTypeInTitle, boolean showTypeInDesc, int progress) {
boolean showTypeInTitle, boolean showTypeInDesc) {
initAppStatusVariables();
boolean disabled = false;
rightButtonAction = RightButtonAction.UNKNOWN;
boolean downloading = context.getDownloadThread().isDownloading(indexItem);
int progress = -1;
if (context.getDownloadThread().getCurrentDownloadingItem() == indexItem) {
progress = context.getDownloadThread().getCurrentDownloadingItemProgress();
}
rightImageButton.setClickable(false);
if (progress != -1) {
rightImageButton.setClickable(true);
@ -145,18 +150,9 @@ public class ItemViewHolder {
progressBar.setVisibility(View.GONE);
Map<String,String> indexFileNames = context.getIndexFileNames();
if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) {
boolean outdated = false;
String date;
if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) {
date = indexItem.getDate(dateFormat);
} else {
String sfName = indexItem.getTargetFileName();
Map<String,String> indexActivatedFileNames = context.getIndexActivatedFileNames();
final boolean updatableResource = indexActivatedFileNames.containsKey(sfName);
date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName);
outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem);
}
if (indexItem.isDownloaded()) {
String date = indexItem.getLocalDate();
boolean outdated = indexItem.isOutdated();
String updateDescr = context.getResources().getString(R.string.local_index_installed) + ": "
+ date;
mapDateTextView.setText(updateDescr);
@ -185,9 +181,7 @@ public class ItemViewHolder {
if (rightButtonAction != RightButtonAction.UNKNOWN) {
rightButton.setText(R.string.get_plugin);
rightButton.setVisibility(View.VISIBLE);
rightImageButton.setVisibility(View.GONE);
final RightButtonAction action = rightButtonAction;
rightButton.setOnClickListener(new View.OnClickListener() {
@ -228,46 +222,6 @@ public class ItemViewHolder {
}
}
public void bindIndexItem(final IndexItem indexItem,
boolean showTypeInTitle, boolean showTypeInDesc) {
bindIndexItem(indexItem, showTypeInTitle, showTypeInDesc, -1);
}
public void bindRegion(WorldRegion region) {
nameTextView.setText(region.getName());
nameTextView.setTextColor(textColorPrimary);
if (region.getResourceTypes().size() > 0) {
StringBuilder stringBuilder = new StringBuilder();
for (DownloadActivityType activityType : region.getResourceTypes()) {
if (stringBuilder.length() > 0) {
stringBuilder.append(", ");
}
stringBuilder.append(activityType.getString(context));
}
}
descrTextView.setVisibility(View.GONE);
mapDateTextView.setVisibility(View.GONE);
Drawable leftImageDrawable = null;
switch (region.getMapState()) {
case NOT_DOWNLOADED:
leftImageDrawable = getContentIcon(context, R.drawable.ic_map);
break;
case DOWNLOADED:
leftImageDrawable = getContentIcon(context, R.drawable.ic_map,
context.getResources().getColor(R.color.color_ok));
break;
case OUTDATED:
leftImageDrawable = getContentIcon(context, R.drawable.ic_map,
context.getResources().getColor(R.color.color_distance));
break;
}
leftImageView.setImageDrawable(leftImageDrawable);
rightButton.setVisibility(View.GONE);
rightImageButton.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
private Drawable getContentIcon(DownloadActivity context, int resourceId) {
return context.getMyApplication().getIconsCache().getContentIcon(resourceId);
}

View file

@ -459,6 +459,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
@Override
public void downloadHasFinished() {
((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView());
reloadData();
}

View file

@ -1,329 +0,0 @@
package net.osmand.plus.download.ui;
import java.util.ArrayList;
import java.util.List;
import net.osmand.plus.IconsCache;
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.DownloadActivityType;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.IndexItem;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class WorldItemsFragment extends OsmandExpandableListFragment implements DownloadEvents {
public static final String TAG = "WorldItemsFragment";
public static final int RELOAD_ID = 0;
public static final int SEARCH_ID = 1;
private DownloadResourceGroupAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_index_fragment, container, false);
ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list);
listAdapter = new DownloadResourceGroupAdapter((DownloadActivity) getActivity());
listView.setAdapter(listAdapter);
expandAllGroups();
setListView(listView);
View usedSpaceCard = inflater.inflate(R.layout.used_space_card, listView, false);
getMyActivity().updateDescriptionTextWithSize(usedSpaceCard);
listView.addHeaderView(usedSpaceCard);
newDownloadIndexes();
return view;
}
@Override
public void onResume() {
super.onResume();
if (!listAdapter.isEmpty()) {
expandAllGroups();
}
listAdapter.notifyDataSetChanged();
}
@Override
public void downloadHasFinished() {
listAdapter.notifyDataSetChanged();
}
@Override
public void downloadInProgress() {
listAdapter.notifyDataSetChanged();
}
@Override
public void newDownloadIndexes() {
DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes();
listAdapter.update(indexes);
expandAllGroups();
}
private void expandAllGroups() {
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
getExpandableListView().expandGroup(i);
}
}
public OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
public DownloadActivity getMyActivity() {
return (DownloadActivity) getActivity();
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Object child = listAdapter.getChild(groupPosition, childPosition);
if (child instanceof DownloadResourceGroup) {
final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(((DownloadResourceGroup) child).getUniqueId());
((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment);
return true;
} else if (child instanceof IndexItem) {
IndexItem indexItem = (IndexItem) child;
((DownloadActivity) getActivity()).startDownload(indexItem);
return true;
}
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh);
itemReload.setIcon(R.drawable.ic_action_refresh_dark);
MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search);
itemSearch.setIcon(R.drawable.ic_action_search_dark);
MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case RELOAD_ID:
// re-create the thread
getDownloadActivity().getDownloadThread().runReloadIndexFiles();
return true;
case SEARCH_ID:
getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance(""));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private DownloadActivity getDownloadActivity() {
return (DownloadActivity) getActivity();
}
public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter {
private List<DownloadResourceGroup> data = new ArrayList<DownloadResourceGroup>();
private DownloadActivity ctx;
private class SimpleViewHolder {
TextView textView;
}
public DownloadResourceGroupAdapter(DownloadActivity ctx) {
this.ctx = ctx;
TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
ta.recycle();
}
public void clear() {
data.clear();
notifyDataSetChanged();
}
public void update(DownloadResourceGroup mainGroup) {
data = mainGroup.getGroups();
notifyDataSetChanged();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
DownloadResourceGroup drg = data.get(groupPosition);
if(drg.getType().containsIndexItem()) {
return drg.getItemByIndex(childPosition);
}
return drg.getGroupByIndex(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 10000 + childPosition;
}
@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 IndexItem) {
IndexItem item = (IndexItem) child;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.two_line_with_images_list_item, parent, false);
}
ItemViewHolder viewHolder;
if(convertView.getTag() instanceof ItemViewHolder) {
viewHolder = (ItemViewHolder) convertView.getTag();
} else {
viewHolder = new ItemViewHolder(convertView,ctx);
convertView.setTag(viewHolder);
}
viewHolder.bindIndexItem(item, false, false);
} else {
DownloadResourceGroup group = (DownloadResourceGroup) child;
SimpleViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.simple_list_menu_item, parent, false);
}
if(convertView.getTag() instanceof SimpleViewHolder) {
viewHolder = (SimpleViewHolder) convertView.getTag();
} else {
viewHolder = new SimpleViewHolder();
convertView.setTag(viewHolder);
}
Drawable iconLeft;
if(group.getType() == DownloadResourceGroupType.VOICE_REC ||
group.getType() == DownloadResourceGroupType.VOICE_TTS) {
iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up);
} else {
IconsCache cache = ctx.getMyApplication().getIconsCache();
if (group.getParentGroup() == null
|| group.getParentGroup().getType() == DownloadResourceGroupType.WORLD) {
iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark);
} else {
DownloadResourceGroup ggr = group.getGroupById(DownloadResourceGroupType.REGION_MAPS
.getDefaultId());
iconLeft = cache.getContentIcon(R.drawable.ic_map);
if (ggr != null && ggr.getIndividualResources() != null) {
IndexItem item = null;
for (IndexItem ii : ggr.getIndividualResources()) {
if (ii.getType() == DownloadActivityType.NORMAL_FILE
|| ii.getType() == DownloadActivityType.ROADS_FILE) {
if (ii.isDownloaded() || ii.isOutdated()) {
item = ii;
break;
}
}
}
if (item != null) {
if (item.isOutdated()) {
iconLeft = cache.getIcon(R.drawable.ic_map,
ctx.getResources().getColor(R.color.color_distance));
} else {
iconLeft = cache.getIcon(R.drawable.ic_map,
ctx.getResources().getColor(R.color.color_ok));
}
}
}
}
}
viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null);
String name = group.getName();
WorldRegion region = group.getRegion();
if(region != null) {
name = region.getName();
}
viewHolder.textView.setText(name);
}
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, final View convertView,
final ViewGroup parent) {
View v = convertView;
String section = getGroup(groupPosition);
if (v == null) {
LayoutInflater inflater = (LayoutInflater) ctx.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);
TypedValue typedValue = new TypedValue();
Resources.Theme theme = ctx.getTheme();
theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true);
v.setBackgroundColor(typedValue.data);
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
DownloadResourceGroup drg = data.get(groupPosition);
int rid = drg.getType().getResourceId();
if(rid != -1) {
return ctx.getString(rid);
}
return "";
}
@Override
public int getGroupCount() {
return data.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;
}
}
}