Prepare favorite activity

This commit is contained in:
Victor Shcherb 2014-06-04 15:08:08 +02:00
parent 7853e24671
commit aa3bf5f996
8 changed files with 214 additions and 748 deletions

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView
android:id="@android:id/list"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/OsmandListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:groupIndicator="@android:color/transparent" >
</ExpandableListView>

View file

@ -1,22 +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:orientation="vertical"
>
<ExpandableListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:groupIndicator="@android:color/transparent"
style="@style/OsmandListView">
</ExpandableListView>
</LinearLayout>

View file

@ -9,6 +9,7 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="my_favorites">My Favorites</string>
<string name="my_data_Button">My Data</string>
<string name="my_data_activity">My Data</string>
<string name="osmo_user_joined">User %1$s joined group %2$s</string>

View file

@ -1679,6 +1679,9 @@ public class OsmandSettings {
public final CommonPreference<Integer> SEARCH_TAB =
new IntPreference("SEARCH_TAB", 0).makeGlobal().cache();
public final CommonPreference<Integer> FAVORITES_TAB =
new IntPreference("FAVORITES_TAB", 0).makeGlobal().cache();
public final CommonPreference<Integer> OSMAND_THEME =
new IntPreference("osmand_theme", OSMAND_DARK_THEME).makeGlobal().cache();

View file

@ -3,84 +3,34 @@
*/
package net.osmand.plus.activities;
import java.io.File;
import java.text.Collator;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import android.content.Intent;
import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import net.osmand.access.AccessibleToast;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.sherpafy.TourSelectionFragment;
import net.osmand.plus.sherpafy.TourCommonActivity.TabsAdapter;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.util.MapUtils;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabWidget;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Window;
/**
*
*/
public class FavouritesActivity extends OsmandExpandableListActivity {
public class FavouritesActivity extends SherlockFragmentActivity {
public static final int EXPORT_ID = 0;
public static final int IMPORT_ID = 1;
public static final int DELETE_ID = 2;
public static final int DELETE_ACTION_ID = 3;
public static final int SHARE_ID = 4;
private FavouritesAdapter favouritesAdapter;
private FavouritesDbHelper helper;
private boolean selectionMode = false;
private Set<FavouritePoint> favoritesToDelete = new LinkedHashSet<FavouritePoint>();
private Set<String> groupsToDelete = new LinkedHashSet<String>();
private Comparator<FavouritePoint> favoritesComparator;
private ActionMode actionMode;
private static final String FAVOURITES_INFO = "FAVOURITES_INFO";
private TabsAdapter mTabsAdapter;
@ -91,639 +41,136 @@ public class FavouritesActivity extends OsmandExpandableListActivity {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getSherlock().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
super.onCreate(icicle);
final Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
favoritesComparator = new Comparator<FavouritePoint>(){
@Override
public int compare(FavouritePoint object1, FavouritePoint object2) {
return collator.compare(object1.getName(), object2.getName());
}
};
setContentView(R.layout.favourites_list);
setContentView(R.layout.tab_content);
getSupportActionBar().setTitle(R.string.favourites_activity);
setSupportProgressBarIndeterminateVisibility(false);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
OsmandSettings settings = ((OsmandApplication) getApplication()).getSettings();
Integer tab = settings.FAVORITES_TAB.get();
ViewPager mViewPager = (ViewPager)findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, tabHost, mViewPager);
mTabsAdapter.addTab(tabHost.newTabSpec(TOUR_INFO).setIndicator(getString(R.string.tab_current_tour)),
TourInformationFragment.class, null);
mTabsAdapter.addTab(tabHost.newTabSpec(TOUR_STAGE).setIndicator(getString(R.string.tab_stages)),
TourStageFragment.class, null);
mTabsAdapter.addTab(tabHost.newTabSpec(TOUR_SELECTION).setIndicator(getString(R.string.tab_tours)),
TourSelectionFragment.class, null);
if (savedInstanceState != null) {
tabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
mTabsAdapter = new TabsAdapter(this, tabHost, mViewPager, settings);
mTabsAdapter.addTab(tabHost.newTabSpec(FAVOURITES_INFO).setIndicator(getString(R.string.my_favorites)),
FavouritesTreeFragment.class, null);
tabHost.setCurrentTab(tab);
// getSupportActionBar().setIcon(R.drawable.tab_search_favorites_icon);
helper = getMyApplication().getFavorites();
favouritesAdapter = new FavouritesAdapter();
favouritesAdapter.setFavoriteGroups(helper.getFavoriteGroups());
getExpandableListView().setAdapter(favouritesAdapter);
}
private void deleteFavorites() {
new AsyncTask<Void, Object, String>(){
@Override
protected void onPreExecute() {
showProgressBar();
};
@Override
protected void onPostExecute(String result) {
hideProgressBar();
favouritesAdapter.synchronizeGroups();
favouritesAdapter.sort(favoritesComparator);
}
@Override
protected void onProgressUpdate(Object... values) {
for(Object o : values){
if(o instanceof FavouritePoint){
favouritesAdapter.deleteFavoritePoint((FavouritePoint) o);
} else if(o instanceof String){
favouritesAdapter.deleteCategory((String) o);
}
}
}
@Override
protected String doInBackground(Void... params) {
for (FavouritePoint fp : favoritesToDelete) {
helper.deleteFavourite(fp);
publishProgress(fp);
}
favoritesToDelete.clear();
for (String group : groupsToDelete) {
helper.deleteGroup(group);
publishProgress(group);
}
groupsToDelete.clear();
return getString(R.string.favourites_delete_multiple_succesful);
}
}.execute();
}
@Override
protected void onResume() {
super.onResume();
// final LatLon mapLocation = getMyApplication().getSettings().getLastKnownMapLocation();
favouritesAdapter.synchronizeGroups();
// Sort Favs by distance on Search tab, but sort alphabetically here
favouritesAdapter.sort(favoritesComparator);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if(selectionMode){
CheckBox ch = (CheckBox) v.findViewById(R.id.check_item);
FavouritePoint model = favouritesAdapter.getChild(groupPosition, childPosition);
ch.setChecked(!ch.isChecked());
if(ch.isChecked()){
favoritesToDelete.add(model);
} else {
favoritesToDelete.remove(model);
}
} else {
final QuickAction qa = new QuickAction(v);
final OsmandSettings settings = getMyApplication().getSettings();
final FavouritePoint point = (FavouritePoint) favouritesAdapter.getChild(groupPosition, childPosition);
String name = getString(R.string.favorite) + ": " + point.getName();
LatLon location = new LatLon(point.getLatitude(), point.getLongitude());
OnClickListener onshow = new OnClickListener() {
@Override
public void onClick(View v) {
settings.SHOW_FAVORITES.set(true);
}
};
MapActivityActions.createDirectionsActions(qa, location, point, name, settings.getLastKnownMapZoom(), this,
true, onshow, false);
if (point.isStored()) {
ActionItem edit = new ActionItem();
edit.setIcon(getResources().getDrawable(R.drawable.ic_action_edit_light));
edit.setTitle(getString(R.string.favourites_context_menu_edit));
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editPoint(point);
qa.dismiss();
}
});
qa.addActionItem(edit);
ActionItem delete = new ActionItem();
delete.setTitle(getString(R.string.favourites_context_menu_delete));
delete.setIcon(getResources().getDrawable(R.drawable.ic_action_delete_light));
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
deletePoint(point);
qa.dismiss();
}
});
qa.addActionItem(delete);
}
qa.show();
}
return true;
}
private boolean editPoint(final FavouritePoint point) {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.favourites_context_menu_edit);
final View v = getLayoutInflater().inflate(R.layout.favourite_edit_dialog, getExpandableListView(), false);
final AutoCompleteTextView cat = (AutoCompleteTextView) v.findViewById(R.id.Category);
final EditText editText = (EditText) v.findViewById(R.id.Name);
builder.setView(v);
editText.setText(point.getName());
cat.setText(point.getCategory());
cat.setThreshold(1);
cat.setAdapter(new ArrayAdapter<String>(this, R.layout.list_textview, helper.getFavoriteGroups().keySet().toArray(new String[] {})));
builder.setNegativeButton(R.string.default_buttons_cancel, null);
builder.setPositiveButton(R.string.default_buttons_apply, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean editied = helper.editFavouriteName(point, editText.getText().toString().trim(), cat.getText().toString());
if (editied) {
favouritesAdapter.synchronizeGroups();
favouritesAdapter.sort(favoritesComparator);
}
}
});
builder.create().show();
editText.requestFocus();
return true;
}
private boolean deletePoint(final FavouritePoint point) {
final Resources resources = this.getResources();
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.favourites_remove_dialog_msg, point.getName()));
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean deleted = helper.deleteFavourite(point);
if (deleted) {
AccessibleToast.makeText(FavouritesActivity.this,
MessageFormat.format(resources.getString(R.string.favourites_remove_dialog_success), point.getName()),
Toast.LENGTH_SHORT).show();
favouritesAdapter.synchronizeGroups();
favouritesAdapter.sort(favoritesComparator);
}
}
});
builder.create().show();
return true;
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
if (item.getItemId() == EXPORT_ID) {
export();
return true;
} else if (item.getItemId() == IMPORT_ID) {
importFile();
return true;
} else if (item.getItemId() == SHARE_ID) {
shareFavourites();
return true;
} else if (item.getItemId() == DELETE_ID) {
enterDeleteMode();
return true;
} else if (item.getItemId() == DELETE_ACTION_ID) {
deleteFavoritesAction();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
createMenuItem(menu, EXPORT_ID, R.string.export_fav, R.drawable.ic_action_gsave_light, R.drawable.ic_action_gsave_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, SHARE_ID, R.string.share_fav, R.drawable.ic_action_gshare_light, R.drawable.ic_action_gshare_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, IMPORT_ID, R.string.import_fav, R.drawable.ic_action_grefresh_light, R.drawable.ic_action_grefresh_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, DELETE_ID, R.string.default_buttons_delete, R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT) ;
return super.onCreateOptionsMenu(menu);
}
/**
* This is a helper class that implements the management of tabs and all
* details of connecting a ViewPager with associated TabHost. It relies on a
* trick. Normally a tab host has a simple API for supplying a View or
* Intent that each tab will show. This is not sufficient for switching
* between pages. So instead we make the content part of the tab host
* 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
* view to show as the tab content. It listens to changes in tabs, and takes
* care of switch to the correct paged in the ViewPager whenever the selected
* tab changes.
*/
public static class TabsAdapter extends FragmentPagerAdapter
implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private OsmandSettings osmSettings;
public void showProgressBar(){
setSupportProgressBarIndeterminateVisibility(true);
}
static final class TabInfo {
private final String tag;
private Class<?> clss;
private Bundle args;
public void hideProgressBar(){
setSupportProgressBarIndeterminateVisibility(false);
}
TabInfo(String _tag, Class<?> _class, Bundle _args) {
tag = _tag;
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
private void enterDeleteMode() {
actionMode = startActionMode(new Callback() {
public DummyTabFactory(Context context) {
mContext = context;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
selectionMode = true;
createMenuItem(menu, DELETE_ACTION_ID, R.string.default_buttons_delete, R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM);
favoritesToDelete.clear();
groupsToDelete.clear();
favouritesAdapter.notifyDataSetInvalidated();
return true;
}
@Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost,ViewPager pager, OsmandSettings settings) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
osmSettings = settings;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
@Override
public void onDestroyActionMode(ActionMode mode) {
selectionMode = false;
favouritesAdapter.notifyDataSetInvalidated();
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if(item.getItemId() == DELETE_ACTION_ID) {
deleteFavoritesAction();
}
return true;
}
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
});
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
osmSettings.FAVORITES_TAB.set(position);
mViewPager.setCurrentItem(position);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
private void deleteFavoritesAction() {
if (groupsToDelete.size() + favoritesToDelete.size() > 0) {
Builder b = new AlertDialog.Builder(FavouritesActivity.this);
b.setMessage(getString(R.string.favorite_delete_multiple, favoritesToDelete.size(), groupsToDelete.size()));
b.setPositiveButton(R.string.default_buttons_delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(actionMode != null) {
actionMode.finish();
}
deleteFavorites();
}
});
b.setNegativeButton(R.string.default_buttons_cancel, null);
b.show();
}
}
private void importFile() {
final File tosave = getMyApplication().getAppPath(FavouritesDbHelper.FILE_TO_SAVE);
if (!tosave.exists()) {
AccessibleToast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), tosave.getAbsolutePath()),
Toast.LENGTH_LONG).show();
} else {
new AsyncTask<Void, FavouritePoint, String>() {
@Override
protected String doInBackground(Void... params) {
Set<String> existedPoints = new LinkedHashSet<String>();
if (!favouritesAdapter.isEmpty()) {
for (FavouritePoint fp : helper.getFavouritePoints()) {
existedPoints.add(fp.getName() + "_" + fp.getCategory());
}
}
GPXFile res = GPXUtilities.loadGPXFile(getMyApplication(), tosave);
if (res.warning != null) {
return res.warning;
}
for (WptPt p : res.points) {
if (existedPoints.contains(p.name) || existedPoints.contains(p.name + "_" + p.category)) {
continue;
}
int c;
String name = p.name;
String categoryName = p.category != null ? p.category : "";
if (name == null) {
name = "";
}
// old way to store the category, in name.
if ("".equals(categoryName.trim()) && (c = p.name.lastIndexOf('_')) != -1) {
categoryName = p.name.substring(c + 1);
name = p.name.substring(0, c);
}
FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName);
if (helper.addFavourite(fp)) {
publishProgress(fp);
}
}
return null;
}
@Override
protected void onProgressUpdate(FavouritePoint... values) {
for (FavouritePoint p : values) {
favouritesAdapter.addFavoritePoint(p);
}
};
@Override
protected void onPreExecute() {
showProgressBar();
};
@Override
protected void onPostExecute(String warning) {
hideProgressBar();
if (warning == null) {
AccessibleToast.makeText(FavouritesActivity.this, R.string.fav_imported_sucessfully, Toast.LENGTH_SHORT).show();
} else {
AccessibleToast.makeText(FavouritesActivity.this, warning, Toast.LENGTH_LONG).show();
}
favouritesAdapter.synchronizeGroups();
favouritesAdapter.sort(favoritesComparator);
};
}.execute();
}
}
private void shareFavourites() {
if (favouritesAdapter.isEmpty()) {
AccessibleToast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
} else {
final AsyncTask<Void, Void, GPXFile> exportTask = new AsyncTask<Void, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(Void... params) {
return helper.asGpxFile();
}
@Override
protected void onPreExecute() {
showProgressBar();
}
@Override
protected void onPostExecute(GPXFile gpxFile) {
hideProgressBar();
final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, GPXUtilities.asString(gpxFile, getMyApplication()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_fav_subject));
sendIntent.setType("application/gpx+xml");
startActivity(sendIntent);
}
};
exportTask.execute();
}
}
private void export() {
final File tosave = getMyApplication().getAppPath(FavouritesDbHelper.FILE_TO_SAVE);
if (favouritesAdapter.isEmpty()) {
AccessibleToast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
} else if (!tosave.getParentFile().exists()) {
AccessibleToast.makeText(this, R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show();
} else {
final AsyncTask<Void, Void, String> exportTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
return helper.exportFavorites();
}
@Override
protected void onPreExecute() {
showProgressBar();
};
@Override
protected void onPostExecute(String warning) {
hideProgressBar();
if (warning == null) {
AccessibleToast.makeText(FavouritesActivity.this,
MessageFormat.format(getString(R.string.fav_saved_sucessfully), tosave.getAbsolutePath()),
Toast.LENGTH_LONG).show();
} else {
AccessibleToast.makeText(FavouritesActivity.this, warning, Toast.LENGTH_LONG).show();
}
};
};
if (tosave.exists()) {
Builder bld = new AlertDialog.Builder(this);
bld.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
exportTask.execute();
}
});
bld.setNegativeButton(R.string.default_buttons_no, null);
bld.setMessage(R.string.fav_export_confirmation);
bld.show();
} else {
exportTask.execute();
}
}
}
class FavouritesAdapter extends OsmandBaseExpandableListAdapter {
Map<String, List<FavouritePoint>> sourceFavoriteGroups;
Map<String, List<FavouritePoint>> favoriteGroups = new LinkedHashMap<String, List<FavouritePoint>>();
List<String> groups = new ArrayList<String>();
public void setFavoriteGroups(Map<String, List<FavouritePoint>> favoriteGroups) {
this.sourceFavoriteGroups = favoriteGroups;
synchronizeGroups();
}
public void sort(Comparator<FavouritePoint> comparator) {
for(List<FavouritePoint> ps : favoriteGroups.values()) {
Collections.sort(ps, comparator);
}
}
public void addFavoritePoint(FavouritePoint p) {
if(!favoriteGroups.containsKey(p.getCategory())){
favoriteGroups.put(p.getCategory(), new ArrayList<FavouritePoint>());
groups.add(p.getCategory());
}
favoriteGroups.get(p.getCategory()).add(p);
notifyDataSetChanged();
}
public void deleteFavoritePoint(FavouritePoint p) {
if(favoriteGroups.containsKey(p.getCategory())){
favoriteGroups.get(p.getCategory()).remove(p);
}
notifyDataSetChanged();
}
public void deleteCategory(String p) {
favoriteGroups.remove(p);
groups.remove(p);
notifyDataSetChanged();
}
public void synchronizeGroups(){
favoriteGroups.clear();
groups.clear();
for(String key : sourceFavoriteGroups.keySet()){
groups.add(key);
favoriteGroups.put(key, new ArrayList<FavouritePoint>(sourceFavoriteGroups.get(key)));
}
notifyDataSetChanged();
}
@Override
public FavouritePoint getChild(int groupPosition, int childPosition) {
return favoriteGroups.get(groups.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 10000 + childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return favoriteGroups.get(groups.get(groupPosition)).size();
}
@Override
public String getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.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;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.expandable_list_item_category, parent, false);
fixBackgroundRepeat(row);
}
adjustIndicator(groupPosition, isExpanded, row);
TextView label = (TextView) row.findViewById(R.id.category_name);
final String model = getGroup(groupPosition);
label.setText(model);
final CheckBox ch = (CheckBox) row.findViewById(R.id.check_item);
if(selectionMode){
ch.setVisibility(View.VISIBLE);
ch.setChecked(groupsToDelete.contains(model));
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ch.isChecked()) {
groupsToDelete.add(model);
List<FavouritePoint> fvs = helper.getFavoriteGroups().get(model);
if (fvs != null) {
favoritesToDelete.addAll(fvs);
}
favouritesAdapter.notifyDataSetInvalidated();
} else {
groupsToDelete.remove(model);
}
}
});
} else {
ch.setVisibility(View.GONE);
}
return row;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favourites_list_item, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.favourite_label);
ImageView icon = (ImageView) row.findViewById(R.id.favourite_icon);
final FavouritePoint model = (FavouritePoint) getChild(groupPosition, childPosition);
row.setTag(model);
if(model.isStored()){
icon.setImageResource(R.drawable.list_favorite);
} else {
icon.setImageResource(R.drawable.opened_poi);
}
LatLon lastKnownMapLocation = getMyApplication().getSettings().getLastKnownMapLocation();
int dist = (int) (MapUtils.getDistance(model.getLatitude(), model.getLongitude(),
lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude()));
String distance = OsmAndFormatter.getFormattedDistance(dist, getMyApplication()) + " ";
label.setText(distance + model.getName(), TextView.BufferType.SPANNABLE);
((Spannable) label.getText()).setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_distance)), 0, distance.length() - 1, 0);
final CheckBox ch = (CheckBox) row.findViewById(R.id.check_item);
if(selectionMode && model.isStored()){
ch.setVisibility(View.VISIBLE);
ch.setChecked(favoritesToDelete.contains(model));
row.findViewById(R.id.favourite_icon).setVisibility(View.GONE);
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ch.isChecked()){
favoritesToDelete.add(model);
} else {
favoritesToDelete.remove(model);
if(groupsToDelete.contains(model.getCategory())){
groupsToDelete.remove(model.getCategory());
favouritesAdapter.notifyDataSetInvalidated();
}
}
}
});
} else {
row.findViewById(R.id.favourite_icon).setVisibility(View.VISIBLE);
ch.setVisibility(View.GONE);
}
return row;
}
}
@Override
public void onPageSelected(int position) {
// Unfortunately when TabHost changes the current tab, it kindly
// also takes care of putting focus on it when not in touch mode.
// The jerk.
// This hack tries to prevent this from pulling focus out of our
// ViewPager.
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}

View file

@ -19,48 +19,41 @@ import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.activities.FavouritesActivity.FavouritesAdapter;
import net.osmand.plus.sherpafy.TourSelectionFragment;
import net.osmand.plus.sherpafy.TourCommonActivity.TabsAdapter;
import net.osmand.util.MapUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class FavouritesTreeFragment extends Fragment{
public class FavouritesTreeFragment extends OsmandExpandableListFragment {
public static final int EXPORT_ID = 0;
public static final int IMPORT_ID = 1;
@ -77,13 +70,11 @@ public class FavouritesTreeFragment extends Fragment{
private Set<String> groupsToDelete = new LinkedHashSet<String>();
private Comparator<FavouritePoint> favoritesComparator;
private ActionMode actionMode;
private TabsAdapter mTabsAdapter;
@Override
public void onCreate(Bundle icicle) {
//This has to be called before setContentView and you must use the
//class in com.actionbarsherlock.view and NOT android.view
public void onAttach(Activity activity) {
super.onAttach(activity);
final Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
favoritesComparator = new Comparator<FavouritePoint>(){
@ -95,15 +86,13 @@ public class FavouritesTreeFragment extends Fragment{
};
setContentView(R.layout.favourites_list);
helper = getMyApplication().getFavorites();
favouritesAdapter = new FavouritesAdapter();
favouritesAdapter.setFavoriteGroups(helper.getFavoriteGroups());
getExpandableListView().setAdapter(favouritesAdapter);
setAdapter(favouritesAdapter);
}
private void deleteFavorites() {
new AsyncTask<Void, Object, String>(){
@ -149,7 +138,7 @@ public class FavouritesTreeFragment extends Fragment{
}
@Override
protected void onResume() {
public void onResume() {
super.onResume();
// final LatLon mapLocation = getMyApplication().getSettings().getLastKnownMapLocation();
favouritesAdapter.synchronizeGroups();
@ -182,7 +171,7 @@ public class FavouritesTreeFragment extends Fragment{
settings.SHOW_FAVORITES.set(true);
}
};
MapActivityActions.createDirectionsActions(qa, location, point, name, settings.getLastKnownMapZoom(), this,
MapActivityActions.createDirectionsActions(qa, location, point, name, settings.getLastKnownMapZoom(), getActivity(),
true, onshow, false);
if (point.isStored()) {
ActionItem edit = new ActionItem();
@ -217,16 +206,16 @@ public class FavouritesTreeFragment extends Fragment{
private boolean editPoint(final FavouritePoint point) {
Builder builder = new AlertDialog.Builder(this);
Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.favourites_context_menu_edit);
final View v = getLayoutInflater().inflate(R.layout.favourite_edit_dialog, getExpandableListView(), false);
final View v = getActivity().getLayoutInflater().inflate(R.layout.favourite_edit_dialog, getExpandableListView(), false);
final AutoCompleteTextView cat = (AutoCompleteTextView) v.findViewById(R.id.Category);
final EditText editText = (EditText) v.findViewById(R.id.Name);
builder.setView(v);
editText.setText(point.getName());
cat.setText(point.getCategory());
cat.setThreshold(1);
cat.setAdapter(new ArrayAdapter<String>(this, R.layout.list_textview, helper.getFavoriteGroups().keySet().toArray(new String[] {})));
cat.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_textview, helper.getFavoriteGroups().keySet().toArray(new String[] {})));
builder.setNegativeButton(R.string.default_buttons_cancel, null);
builder.setPositiveButton(R.string.default_buttons_apply, new DialogInterface.OnClickListener() {
@Override
@ -244,9 +233,10 @@ public class FavouritesTreeFragment extends Fragment{
return true;
}
private boolean deletePoint(final FavouritePoint point) {
final Resources resources = this.getResources();
Builder builder = new AlertDialog.Builder(this);
Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.favourites_remove_dialog_msg, point.getName()));
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@ -254,7 +244,7 @@ public class FavouritesTreeFragment extends Fragment{
public void onClick(DialogInterface dialog, int which) {
boolean deleted = helper.deleteFavourite(point);
if (deleted) {
AccessibleToast.makeText(FavouritesActivity.this,
AccessibleToast.makeText(getActivity(),
MessageFormat.format(resources.getString(R.string.favourites_remove_dialog_success), point.getName()),
Toast.LENGTH_SHORT).show();
favouritesAdapter.synchronizeGroups();
@ -291,7 +281,7 @@ public class FavouritesTreeFragment extends Fragment{
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
createMenuItem(menu, EXPORT_ID, R.string.export_fav, R.drawable.ic_action_gsave_light, R.drawable.ic_action_gsave_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, SHARE_ID, R.string.share_fav, R.drawable.ic_action_gshare_light, R.drawable.ic_action_gshare_dark,
@ -300,20 +290,19 @@ public class FavouritesTreeFragment extends Fragment{
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
createMenuItem(menu, DELETE_ID, R.string.default_buttons_delete, R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark,
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT) ;
return super.onCreateOptionsMenu(menu);
}
public void showProgressBar(){
setSupportProgressBarIndeterminateVisibility(true);
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(true);
}
public void hideProgressBar(){
setSupportProgressBarIndeterminateVisibility(false);
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
}
private void enterDeleteMode() {
actionMode = startActionMode(new Callback() {
actionMode = getSherlockActivity().startActionMode(new Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
@ -353,7 +342,7 @@ public class FavouritesTreeFragment extends Fragment{
private void deleteFavoritesAction() {
if (groupsToDelete.size() + favoritesToDelete.size() > 0) {
Builder b = new AlertDialog.Builder(FavouritesActivity.this);
Builder b = new AlertDialog.Builder(getActivity());
b.setMessage(getString(R.string.favorite_delete_multiple, favoritesToDelete.size(), groupsToDelete.size()));
b.setPositiveButton(R.string.default_buttons_delete, new DialogInterface.OnClickListener() {
@Override
@ -372,7 +361,7 @@ public class FavouritesTreeFragment extends Fragment{
private void importFile() {
final File tosave = getMyApplication().getAppPath(FavouritesDbHelper.FILE_TO_SAVE);
if (!tosave.exists()) {
AccessibleToast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), tosave.getAbsolutePath()),
AccessibleToast.makeText(getActivity(), MessageFormat.format(getString(R.string.fav_file_to_load_not_found), tosave.getAbsolutePath()),
Toast.LENGTH_LONG).show();
} else {
new AsyncTask<Void, FavouritePoint, String>() {
@ -427,9 +416,9 @@ public class FavouritesTreeFragment extends Fragment{
protected void onPostExecute(String warning) {
hideProgressBar();
if (warning == null) {
AccessibleToast.makeText(FavouritesActivity.this, R.string.fav_imported_sucessfully, Toast.LENGTH_SHORT).show();
AccessibleToast.makeText(getActivity(), R.string.fav_imported_sucessfully, Toast.LENGTH_SHORT).show();
} else {
AccessibleToast.makeText(FavouritesActivity.this, warning, Toast.LENGTH_LONG).show();
AccessibleToast.makeText(getActivity(), warning, Toast.LENGTH_LONG).show();
}
favouritesAdapter.synchronizeGroups();
favouritesAdapter.sort(favoritesComparator);
@ -441,7 +430,7 @@ public class FavouritesTreeFragment extends Fragment{
private void shareFavourites() {
if (favouritesAdapter.isEmpty()) {
AccessibleToast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
AccessibleToast.makeText(getActivity(), R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
} else {
final AsyncTask<Void, Void, GPXFile> exportTask = new AsyncTask<Void, Void, GPXFile>() {
@Override
@ -473,9 +462,9 @@ public class FavouritesTreeFragment extends Fragment{
private void export() {
final File tosave = getMyApplication().getAppPath(FavouritesDbHelper.FILE_TO_SAVE);
if (favouritesAdapter.isEmpty()) {
AccessibleToast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
AccessibleToast.makeText(getActivity(), R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
} else if (!tosave.getParentFile().exists()) {
AccessibleToast.makeText(this, R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show();
AccessibleToast.makeText(getActivity(), R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show();
} else {
final AsyncTask<Void, Void, String> exportTask = new AsyncTask<Void, Void, String>() {
@Override
@ -492,17 +481,17 @@ public class FavouritesTreeFragment extends Fragment{
protected void onPostExecute(String warning) {
hideProgressBar();
if (warning == null) {
AccessibleToast.makeText(FavouritesActivity.this,
AccessibleToast.makeText(getActivity(),
MessageFormat.format(getString(R.string.fav_saved_sucessfully), tosave.getAbsolutePath()),
Toast.LENGTH_LONG).show();
} else {
AccessibleToast.makeText(FavouritesActivity.this, warning, Toast.LENGTH_LONG).show();
AccessibleToast.makeText(getActivity(), warning, Toast.LENGTH_LONG).show();
}
};
};
if (tosave.exists()) {
Builder bld = new AlertDialog.Builder(this);
Builder bld = new AlertDialog.Builder(getActivity());
bld.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
@ -613,7 +602,7 @@ public class FavouritesTreeFragment extends Fragment{
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.expandable_list_item_category, parent, false);
fixBackgroundRepeat(row);
}
@ -652,7 +641,7 @@ public class FavouritesTreeFragment extends Fragment{
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.favourites_list_item, parent, false);
}

View file

@ -2,31 +2,69 @@ package net.osmand.plus.activities;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import android.app.ActionBar;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import com.actionbarsherlock.app.SherlockExpandableListActivity;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
public abstract class OsmandExpandableListFragment extends SherlockFragment {
public abstract class OsmandExpandableListFragment extends SherlockFragment implements OnChildClickListener {
private ExpandableListView listView;
private ExpandableListAdapter adapter;
public OsmandApplication getMyApplication() {
return (OsmandApplication)getActivity().getApplication();
}
public
public View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, Bundle savedInstanceState) {
com.android.internal.R.layout.list_content
listView = new ExpandableListView(getActivity());
View v = createView(inflater, container);
listView = (ExpandableListView) v.findViewById(android.R.id.list);
listView.setOnChildClickListener(this);
if(this.adapter != null) {
listView.setAdapter(this.adapter);
}
return v;
}
public View createView(android.view.LayoutInflater inflater, android.view.ViewGroup container) {
return inflater.inflate(R.layout.expandable_list, container, false);
}
public void setAdapter(ExpandableListAdapter a) {
this.adapter = a;
if(listView != null) {
listView.setAdapter(a);
}
}
public void fixBackgroundRepeat(View view) {
Drawable bg = view.getBackground();
if (bg != null) {
if (bg instanceof BitmapDrawable) {
BitmapDrawable bmp = (BitmapDrawable) bg;
// bmp.mutate(); // make sure that we aren't sharing state anymore
bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
}
}
}
public ExpandableListView getListView() {
return listView;
}
public ExpandableListView getExpandableListView() {
return listView;
}
@ -47,6 +85,6 @@ public abstract class OsmandExpandableListFragment extends SherlockFragment {
public boolean isLightActionBar() {
return ((OsmandApplication) getApplication()).getSettings().isLightActionBar();
return ((OsmandApplication) getActivity().getApplication()).getSettings().isLightActionBar();
}
}

View file

@ -1,23 +1,19 @@
package net.osmand.plus.sherpafy;
import java.text.MessageFormat;
import java.util.List;
import net.osmand.IProgress;
import net.osmand.access.AccessibleToast;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.PoiFilter;
import net.osmand.plus.R;
import net.osmand.plus.activities.DownloadIndexActivity;
import net.osmand.plus.activities.EditPOIFilterActivity;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.sherpafy.TourInformation.StageInformation;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@ -31,13 +27,13 @@ import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.LinearLayout.LayoutParams;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;