Downloads search in progress

This commit is contained in:
Alexey Kulish 2015-10-20 19:39:48 +03:00
parent 23b98e0ade
commit 51e3709fad
4 changed files with 413 additions and 448 deletions

View file

@ -0,0 +1,71 @@
package net.osmand.plus.download.ui;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;
import net.osmand.plus.IconsCache;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.IndexItem;
public class DownloadGroupViewHolder {
TextView textView;
private DownloadActivity ctx;
public DownloadGroupViewHolder(DownloadActivity ctx, View v) {
this.ctx = ctx;
textView = (TextView) v.findViewById(R.id.title);
}
private boolean isParentWorld(DownloadResourceGroup group) {
return group.getParentGroup() == null
|| group.getParentGroup().getType() == DownloadResourceGroup.DownloadResourceGroupType.WORLD;
}
private Drawable getIconForGroup(DownloadResourceGroup group) {
Drawable iconLeft;
if (group.getType() == DownloadResourceGroup.DownloadResourceGroupType.VOICE_REC
|| group.getType() == DownloadResourceGroup.DownloadResourceGroupType.VOICE_TTS) {
iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up);
} else {
IconsCache cache = ctx.getMyApplication().getIconsCache();
if (isParentWorld(group) || isParentWorld(group.getParentGroup())) {
iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark);
} else {
DownloadResourceGroup ggr = group
.getSubGroupById(DownloadResourceGroup.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, R.color.color_distance);
} else {
iconLeft = cache.getIcon(R.drawable.ic_map, 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(ctx);
textView.setText(name);
}
}

View file

@ -1,31 +1,11 @@
package net.osmand.plus.download.ui; 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.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.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -39,6 +19,22 @@ import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion;
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 java.util.ArrayList;
import java.util.List;
public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, OnChildClickListener { public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, OnChildClickListener {
public static final int RELOAD_ID = 0; public static final int RELOAD_ID = 0;
public static final int SEARCH_ID = 1; public static final int SEARCH_ID = 1;
@ -231,66 +227,6 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow
} }
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 boolean isParentWorld(DownloadResourceGroup group) {
return group.getParentGroup() == null
|| group.getParentGroup().getType() == DownloadResourceGroupType.WORLD;
}
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 (isParentWorld(group) || isParentWorld(group.getParentGroup())) {
iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark);
} else {
DownloadResourceGroup ggr = group
.getSubGroupById(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, R.color.color_distance);
} else {
iconLeft = cache.getIcon(R.drawable.ic_map, 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(ctx);
textView.setText(name);
}
}
public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter { public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter {
private List<DownloadResourceGroup> data = new ArrayList<DownloadResourceGroup>(); private List<DownloadResourceGroup> data = new ArrayList<DownloadResourceGroup>();

View file

@ -1,34 +1,50 @@
package net.osmand.plus.download.ui; package net.osmand.plus.download.ui;
import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams; import android.widget.RelativeLayout.LayoutParams;
import android.widget.SearchView; import android.widget.SearchView;
import net.osmand.Collator;
import net.osmand.OsmAndCollator;
import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.DownloadResourceGroup;
import net.osmand.plus.download.DownloadResources;
import net.osmand.plus.download.IndexItem;
// FIXME import java.util.ArrayList;
public class SearchDialogFragment extends DialogFragment { import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public static DialogFragment createInstance(String tg) { public class SearchDialogFragment extends DialogFragment implements DownloadEvents, OnItemClickListener {
return new SearchDialogFragment();
}
/*extends DialogFragment implements DownloadEvents {
public static final String TAG = "SearchDialogFragment"; public static final String TAG = "SearchDialogFragment";
private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key";
private ListView listView;
private SearchListAdapter listAdapter;
private BannerAndDownloadFreeVersion banner;
private String searchText; private String searchText;
private SearchView search; private SearchView search;
@ -63,6 +79,24 @@ public class SearchDialogFragment extends DialogFragment {
} }
}); });
banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity());
LinearLayout ll = (LinearLayout) view;
ExpandableListView expandablelistView = (ExpandableListView) view.findViewById(android.R.id.list);
ll.removeView(expandablelistView);
listView = new ListView(getActivity());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
layoutParams.weight = 1;
layoutParams.setMargins(0, 0, 0, 0);
listView.setLayoutParams(layoutParams);
ll.addView(listView);
listView.setOnItemClickListener(this);
listAdapter = new SearchListAdapter(getDownloadActivity());
listView.setOnItemClickListener(this);
listView.setAdapter(listAdapter);
search = new SearchView(getActivity()); search = new SearchView(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
@ -95,23 +129,44 @@ public class SearchDialogFragment extends DialogFragment {
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
Fragment f = getChildFragmentManager().findFragmentByTag(SearchItemsFragment.TAG); updateSearchText(newText);
if (f != null) {
((SearchItemsFragment) f).updateSearchText(newText);
return true; return true;
} }
return false;
}
}); });
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); return view;
if (fragment == null) {
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer,
SearchItemsFragment.createInstance(searchText), SearchItemsFragment.TAG).commit();
} }
getDownloadActivity().registerFreeVersionBanner(view); @Override
return view; public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setShowsDialog(true);
listView.setBackgroundColor(getResources().getColor(
getMyApplication().getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark));
}
@Override
public void newDownloadIndexes() {
if(banner != null) {
banner.updateBannerInProgress();
}
updateSearchText(searchText);
}
@Override
public void downloadHasFinished() {
if(banner != null) {
banner.updateBannerInProgress();
}
listAdapter.notifyDataSetChanged();
}
@Override
public void downloadInProgress() {
if(banner != null) {
banner.updateBannerInProgress();
}
listAdapter.notifyDataSetChanged();
} }
@Override @Override
@ -126,6 +181,11 @@ public class SearchDialogFragment extends DialogFragment {
search.setIconified(false); search.setIconified(false);
} }
public void updateSearchText(String searchText) {
this.searchText = searchText;
listAdapter.getFilter().filter(searchText);
}
private OsmandApplication getMyApplication() { private OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
@ -134,13 +194,6 @@ public class SearchDialogFragment extends DialogFragment {
return (DownloadActivity) getActivity(); return (DownloadActivity) getActivity();
} }
public void newDownloadIndexes() {
Fragment f = getChildFragmentManager().findFragmentByTag(SearchItemsFragment.TAG);
if (f != null) {
((SearchItemsFragment) f).onCategorizationFinished();
}
}
public static SearchDialogFragment createInstance(String searchText) { public static SearchDialogFragment createInstance(String searchText) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(SEARCH_TEXT_DLG_KEY, searchText); bundle.putString(SEARCH_TEXT_DLG_KEY, searchText);
@ -148,5 +201,247 @@ public class SearchDialogFragment extends DialogFragment {
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Object obj = listAdapter.getItem(position);
if (obj instanceof DownloadResourceGroup) {
String uniqueId = ((DownloadResourceGroup) obj).getUniqueId();
final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment
.createInstance(uniqueId);
((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment);
} else if (obj instanceof IndexItem) {
IndexItem indexItem = (IndexItem) obj;
ItemViewHolder vh = (ItemViewHolder) v.getTag();
View.OnClickListener ls = vh.getRightButtonAction(indexItem, vh.getClickAction(indexItem), null);
ls.onClick(v);
}
}
private class SearchListAdapter extends BaseAdapter implements Filterable {
private SearchIndexFilter mFilter;
private List<Object> items = new LinkedList<>();
private DownloadActivity ctx;
public SearchListAdapter(DownloadActivity ctx) {
this.ctx = ctx;
TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
ta.recycle();
}
public void clear() {
items.clear();
notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
Object obj = items.get(position);
if (obj instanceof IndexItem) {
return 0;
} else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Object obj = items.get(position);
if (obj instanceof IndexItem) {
IndexItem item = (IndexItem) obj;
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, getDownloadActivity());
viewHolder.setShowRemoteDate(true);
convertView.setTag(viewHolder);
}
viewHolder.setShowTypeInDesc(true);
viewHolder.bindIndexItem(item, null);
} else {
DownloadResourceGroup group = (DownloadResourceGroup) obj;
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(getDownloadActivity(), convertView);
convertView.setTag(viewHolder);
}
viewHolder.bindItem(group);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new SearchIndexFilter();
}
return mFilter;
}
private final class SearchIndexFilter extends Filter {
private OsmandRegions osmandRegions;
public SearchIndexFilter() {
this.osmandRegions = ctx.getMyApplication().getRegions();
}
private void processGroup(DownloadResourceGroup group, List<Object> filter, List<List<String>> conds) {
String indexLC = group.getName(ctx).toLowerCase();
if (isMatch(conds, false, indexLC)) {
filter.add(group);
}
if (group.getGroups() != null) {
for (DownloadResourceGroup g : group.getGroups()) {
processGroup(g, filter, conds);
}
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
DownloadResources root = ctx.getDownloadThread().getIndexes();
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() < 2) {
results.values = new ArrayList<>();
results.count = 0;
} else {
String[] ors = constraint.toString().split(",");
List<List<String>> conds = new ArrayList<>();
for (String or : ors) {
final ArrayList<String> cond = new ArrayList<>();
for (String term : or.split("\\s")) {
final String t = term.trim().toLowerCase();
if (t.length() > 0) {
cond.add(t);
}
}
if (cond.size() > 0) {
conds.add(cond);
}
}
List<Object> filter = new ArrayList<>();
processGroup(root, filter, conds);
/*
List<WorldRegion> regions = new ArrayList<>();
for (WorldRegion region : worldRegions) {
String indexLC = region.getName().toLowerCase();
if (isMatch(conds, false, indexLC)) {
regions.add(region);
}
}
for (WorldRegion region : regions) {
if (region.getSubregions().size() > 0) {
filter.add(region);
}
List<IndexItem> items = getDownloadActivity().getIndexItemsByRegion(region);
if (items.size() > 1) {
if (!filter.contains(region)) {
filter.add(region);
}
} else {
filter.addAll(items);
}
}
*/ */
final Collator collator = OsmAndCollator.primaryCollator();
Collections.sort(filter, new Comparator<Object>() {
@Override
public int compare(Object obj1, Object obj2) {
String str1;
String str2;
if (obj1 instanceof DownloadResourceGroup) {
str1 = ((DownloadResourceGroup) obj1).getName(ctx);
} else {
str1 = ((IndexItem) obj1).getVisibleName(getMyApplication(), osmandRegions, false);
}
if (obj2 instanceof DownloadResourceGroup) {
str2 = ((DownloadResourceGroup) obj2).getName(ctx);
} else {
str2 = ((IndexItem) obj2).getVisibleName(getMyApplication(), osmandRegions, false);
}
return collator.compare(str1, str2);
}
});
results.values = filter;
results.count = filter.size();
}
return results;
}
private boolean isMatch(List<List<String>> conditions, boolean matchByDefault, String text) {
boolean res = matchByDefault;
for (List<String> or : conditions) {
boolean tadd = true;
for (String var : or) {
if (!text.contains(var)) {
tadd = false;
break;
}
}
if (!tadd) {
res = false;
} else {
res = true;
break;
}
}
return res;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items.clear();
List<Object> values = (List<Object>) results.values;
if (values != null && !values.isEmpty()) {
items.addAll(values);
}
notifyDataSetChanged();
}
}
}
} }

View file

@ -1,337 +0,0 @@
package net.osmand.plus.download.ui;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import net.osmand.Collator;
import net.osmand.OsmAndCollator;
import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.WorldRegion;
import net.osmand.plus.download.BaseDownloadActivity;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.IndexItem;
import android.content.Context;
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.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
//FIXME merge into search dialog fragment
public class SearchItemsFragment { /*extends Fragment implements DownloadEvents {
public static final String TAG = "SearchItemsFragment";
private SearchItemsAdapter listAdapter;
private static final String SEARCH_TEXT_KEY = "world_region_id_key";
private String searchText;
@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_search_items_fragment, container, false);
if (savedInstanceState != null) {
searchText = savedInstanceState.getString(SEARCH_TEXT_KEY);
}
if (searchText == null) {
searchText = getArguments().getString(SEARCH_TEXT_KEY);
}
if (searchText == null)
searchText = "";
ListView listView = (ListView) view.findViewById(android.R.id.list);
listAdapter = new SearchItemsAdapter(getActivity());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
doItemClick(view, position);
}
});
fillSearchItemsAdapter();
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(SEARCH_TEXT_KEY, searchText);
super.onSaveInstanceState(outState);
}
public boolean doItemClick(View view, int position) {
Object obj = listAdapter.getItem(position);
if (obj instanceof WorldRegion) {
WorldRegion region = (WorldRegion) obj;
getDownloadActivity().showDialog(getActivity(), RegionDialogFragment.createInstance(region.getRegionId()));
return true;
} else if (obj instanceof ItemsListBuilder.ResourceItem) {
if (((ItemViewHolder) view.getTag()).isItemAvailable()) {
IndexItem indexItem = ((ItemsListBuilder.ResourceItem) obj).getIndexItem();
((BaseDownloadActivity) getActivity()).startDownload(indexItem);
return true;
}
} else if (obj instanceof IndexItem) {
if (((ItemViewHolder) view.getTag()).isItemAvailable()) {
IndexItem indexItem = (IndexItem) obj;
((BaseDownloadActivity) getActivity()).startDownload(indexItem);
return true;
}
}
return false;
}
public OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
public DownloadActivity getMyActivity() {
return (DownloadActivity) getActivity();
}
private void fillSearchItemsAdapter() {
if (listAdapter != null) {
listAdapter.clear();
List<WorldRegion> flattenedList = getMyApplication().getWorldRegion().getFlattenedSubregions();
List<IndexItem> indexItems = getDownloadActivity().getIndexFiles();
if (flattenedList != null && flattenedList.size() > 0 &&
indexItems != null && indexItems.size() > 0) {
listAdapter.addWorldRegions(flattenedList);
listAdapter.addIndexItems(indexItems);
listAdapter.notifyDataSetChanged();
}
}
}
public void onCategorizationFinished() {
fillSearchItemsAdapter();
}
public void updateSearchText(String searchText) {
this.searchText = searchText;
if(listAdapter != null){
listAdapter.getFilter().filter(searchText);
}
}
private DownloadActivity getDownloadActivity() {
return (DownloadActivity) getActivity();
}
public static SearchItemsFragment createInstance(String regionId) {
Bundle bundle = new Bundle();
bundle.putString(SEARCH_TEXT_KEY, regionId);
SearchItemsFragment fragment = new SearchItemsFragment();
fragment.setArguments(bundle);
return fragment;
}
private class SearchItemsAdapter extends BaseAdapter implements Filterable {
private SearchIndexFilter mFilter;
private List<WorldRegion> worldRegions = new LinkedList<>();
private List<IndexItem> indexItems = new LinkedList<>();
private List<Object> items = new LinkedList<>();
private OsmandRegions osmandRegions;
public SearchItemsAdapter(Context ctx) {
osmandRegions = getMyApplication().getResourceManager().getOsmandRegions();
TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
ta.recycle();
}
public void clear() {
worldRegions.clear();
indexItems.clear();
items.clear();
notifyDataSetChanged();
}
public void addWorldRegions(List<WorldRegion> worldRegions) {
this.worldRegions.addAll(worldRegions);
}
public void addIndexItems(List<IndexItem> indexItems) {
this.indexItems.addAll(indexItems);
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Object item = getItem(position);
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,
getMyActivity(),
getMyApplication().getResourceManager().getDateFormat());
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
if (item instanceof WorldRegion) {
viewHolder.bindRegion((WorldRegion) item);
} else if (item instanceof IndexItem) {
viewHolder.bindIndexItem((IndexItem) item, false, true);
} else {
throw new IllegalArgumentException("Item must be of type WorldRegion or " +
"IndexItem but is of type:" + item.getClass());
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new SearchIndexFilter();
}
return mFilter;
}
private final class SearchIndexFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() < 2) {
results.values = new ArrayList<>();
results.count = 0;
} else {
String[] ors = constraint.toString().split(",");
List<List<String>> conds = new ArrayList<>();
for (String or : ors) {
final ArrayList<String> cond = new ArrayList<>();
for (String term : or.split("\\s")) {
final String t = term.trim().toLowerCase();
if (t.length() > 0) {
cond.add(t);
}
}
if (cond.size() > 0) {
conds.add(cond);
}
}
List<Object> filter = new ArrayList<>();
List<WorldRegion> regions = new ArrayList<>();
for (WorldRegion region : worldRegions) {
String indexLC = region.getName().toLowerCase();
if (isMatch(conds, false, indexLC)) {
regions.add(region);
}
}
for (WorldRegion region : regions) {
if (region.getSubregions().size() > 0) {
filter.add(region);
}
List<IndexItem> items = getDownloadActivity().getIndexItemsByRegion(region);
if (items.size() > 1) {
if (!filter.contains(region)) {
filter.add(region);
}
} else {
filter.addAll(items);
}
}
final Collator collator = OsmAndCollator.primaryCollator();
Collections.sort(filter, new Comparator<Object>() {
@Override
public int compare(Object obj1, Object obj2) {
String str1;
String str2;
if (obj1 instanceof WorldRegion) {
str1 = ((WorldRegion) obj1).getName();
} else {
str1 = ((IndexItem) obj1).getVisibleName(getMyApplication(), osmandRegions, false);
}
if (obj2 instanceof WorldRegion) {
str2 = ((WorldRegion) obj2).getName();
} else {
str2 = ((IndexItem) obj2).getVisibleName(getMyApplication(), osmandRegions, false);
}
return collator.compare(str1, str2);
}
});
results.values = filter;
results.count = filter.size();
}
return results;
}
private boolean isMatch(List<List<String>> conditions, boolean matchByDefault, String text) {
boolean res = matchByDefault;
for (List<String> or : conditions) {
boolean tadd = true;
for (String var : or) {
if (!text.contains(var)) {
tadd = false;
break;
}
}
if (!tadd) {
res = false;
} else {
res = true;
break;
}
}
return res;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items.clear();
List<Object> values = (List<Object>) results.values;
if (values != null && !values.isEmpty()) {
items.addAll(values);
}
notifyDataSetChanged();
}
}
}
*/
}