diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 05b9be8057..a017574055 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -154,7 +154,7 @@ public class DownloadActivity extends SherlockFragmentActivity { public void setType(DownloadActivityType type) { this.type = type;} public void changeType(final DownloadActivityType tp) { - invalidateOptionsMenu(); + //invalidateOptionsMenu(); if (downloadListIndexThread != null && type != tp) { type = tp; downloadListIndexThread.runCategorization(tp); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java index 745c0c4534..3a3549edf7 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java @@ -278,6 +278,7 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { s.setIcon(isLightActionBar() ? R.drawable.abs__ic_menu_moreoverflow_holo_light : R.drawable.abs__ic_menu_moreoverflow_holo_dark); + s.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); } } @@ -290,7 +291,7 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { DownloadActivity.downloadListIndexThread.getEntriesToDownload().clear(); listAdapter.notifyDataSetInvalidated(); - getView().findViewById(R.id.DownloadButton).setVisibility(View.GONE); + getDownloadActivity().findViewById(R.id.DownloadButton).setVisibility(View.GONE); } diff --git a/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java index 5c53e76f89..41e92bd3d3 100644 --- a/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java @@ -10,12 +10,15 @@ import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.SubMenu; +import net.osmand.access.AccessibleToast; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.OsmandExpandableListFragment; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -27,13 +30,15 @@ public class UpdatesIndexFragment extends SherlockListFragment { private OsmandRegions osmandRegions; private java.text.DateFormat format; + private UpdateIndexAdapter listAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); format = getMyApplication().getResourceManager().getDateFormat(); osmandRegions = getMyApplication().getResourceManager().getOsmandRegions(); - setListAdapter(new UpdateIndexAdapter(getDownloadActivity(), R.layout.download_index_list_item, DownloadActivity.downloadListIndexThread.getItemsToUpdate())); + listAdapter = new UpdateIndexAdapter(getDownloadActivity(), R.layout.download_index_list_item, DownloadActivity.downloadListIndexThread.getItemsToUpdate()); + setListAdapter(listAdapter); setHasOptionsMenu(true); } @@ -45,13 +50,13 @@ public class UpdatesIndexFragment extends SherlockListFragment { } - public void updateItemsList(List items){ + public void updateItemsList(List items) { UpdateIndexAdapter adapter = (UpdateIndexAdapter) getListAdapter(); - if (adapter == null){ + if (adapter == null) { return; } adapter.clear(); - for (IndexItem item : items){ + for (IndexItem item : items) { adapter.add(item); } } @@ -61,9 +66,93 @@ public class UpdatesIndexFragment extends SherlockListFragment { super.onListItemClick(l, v, position, id); } - public DownloadActivity getDownloadActivity() { return (DownloadActivity)getActivity(); } + public DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } - private class UpdateIndexAdapter extends ArrayAdapter{ + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + ActionBar actionBar = getDownloadActivity().getSupportActionBar(); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + + if (getMyApplication().getAppCustomization().showDownloadExtraActions()) { + SubMenu s = menu.addSubMenu(0, DownloadIndexFragment.MORE_ID, 0, R.string.default_buttons_other_actions); + s.add(0, DownloadIndexFragment.RELOAD_ID, 0, R.string.update_downlod_list); + s.add(0, DownloadIndexFragment.SELECT_ALL_ID, 0, R.string.select_all); + s.add(0, DownloadIndexFragment.DESELECT_ALL_ID, 0, R.string.deselect_all); + + s.setIcon(isLightActionBar() ? R.drawable.abs__ic_menu_moreoverflow_holo_light + : R.drawable.abs__ic_menu_moreoverflow_holo_dark); + s.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); + } + } + + public OsmandApplication getMyApplication() { + return getDownloadActivity().getMyApplication(); + } + + public boolean isLightActionBar() { + return ((OsmandApplication) getActivity().getApplication()).getSettings().isLightActionBar(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == DownloadIndexFragment.RELOAD_ID) { + // re-create the thread + DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); + return true; + } else if (item.getItemId() == DownloadIndexFragment.SELECT_ALL_ID) { + selectAll(); + return true; + } else if (item.getItemId() == DownloadIndexFragment.FILTER_EXISTING_REGIONS) { + filterExisting(); + return true; + } else if (item.getItemId() == DownloadIndexFragment.DESELECT_ALL_ID) { + deselectAll(); + return true; + } + return super.onOptionsItemSelected(item); + } + + private void selectAll() { + int selected = 0; + for (int i = 0; i < listAdapter.getCount(); i++) { + IndexItem es = listAdapter.getItem(i); + if (!getDownloadActivity().getEntriesToDownload().containsKey(es)) { + selected++; + getDownloadActivity().getEntriesToDownload().put(es, es.createDownloadEntry(getMyApplication(), + getDownloadActivity().getType(), new ArrayList(1))); + + } + } + AccessibleToast.makeText(getDownloadActivity(), MessageFormat.format(getString(R.string.items_were_selected), selected), Toast.LENGTH_SHORT).show(); + listAdapter.notifyDataSetInvalidated(); + if (selected > 0) { + getDownloadActivity().updateDownloadButton(true); + } + } + + public void deselectAll() { + DownloadActivity.downloadListIndexThread.getEntriesToDownload().clear(); + listAdapter.notifyDataSetInvalidated(); + + getDownloadActivity().findViewById(R.id.DownloadButton).setVisibility(View.GONE); + } + + private void filterExisting() { + final Map listAlreadyDownloaded = DownloadActivity.downloadListIndexThread.getDownloadedIndexFileNames(); + + final List filtered = new ArrayList(); + for (IndexItem fileItem : listAdapter.getIndexFiles()) { + if(fileItem.isAlreadyDownloaded(listAlreadyDownloaded)){ + filtered.add(fileItem); + } + } + listAdapter.setIndexFiles(filtered); + listAdapter.notifyDataSetChanged(); + } + + private class UpdateIndexAdapter extends ArrayAdapter { List items; public UpdateIndexAdapter(Context context, int resource, List items) { @@ -71,11 +160,15 @@ public class UpdatesIndexFragment extends SherlockListFragment { this.items = items; } + public List getIndexFiles() { + return items; + } + @Override public View getView(final int position, View convertView, ViewGroup parent) { View v = convertView; - if(v == null){ + if (v == null) { LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.download_index_list_item, null); } @@ -95,7 +188,7 @@ public class UpdatesIndexFragment extends SherlockListFragment { public void onClick(View v) { ch.setChecked(!ch.isChecked()); final IndexItem e = (IndexItem) getListAdapter().getItem(position); - if(ch.isChecked()){ + if (ch.isChecked()) { ch.setChecked(!ch.isChecked()); getDownloadActivity().getEntriesToDownload().remove(e); getDownloadActivity().updateDownloadButton(true); @@ -114,27 +207,12 @@ public class UpdatesIndexFragment extends SherlockListFragment { return v; } - } - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - ActionBar actionBar = getDownloadActivity().getSupportActionBar(); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - - if (getMyApplication().getAppCustomization().showDownloadExtraActions()) { - SubMenu s = menu.addSubMenu(0, DownloadIndexFragment.MORE_ID, 0, R.string.default_buttons_other_actions); - s.add(0, DownloadIndexFragment.RELOAD_ID, 0, R.string.update_downlod_list); - s.add(0, DownloadIndexFragment.SELECT_ALL_ID, 0, R.string.select_all); - s.add(0, DownloadIndexFragment.DESELECT_ALL_ID, 0, R.string.deselect_all); - - s.setIcon(isLightActionBar() ? R.drawable.abs__ic_menu_moreoverflow_holo_light - : R.drawable.abs__ic_menu_moreoverflow_holo_dark); + public void setIndexFiles(List filtered) { + this.items.clear(); + this.items.addAll(filtered); + notifyDataSetChanged(); } } - public OsmandApplication getMyApplication() { return getDownloadActivity().getMyApplication(); } - - public boolean isLightActionBar() { - return ((OsmandApplication) getActivity().getApplication()).getSettings().isLightActionBar(); - } }