add edit filter activity

git-svn-id: https://osmand.googlecode.com/svn/trunk@196 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
frolovmo 2010-06-22 12:10:16 +00:00
parent c7b8711743
commit 77f68ca6ee
9 changed files with 193 additions and 29 deletions

View file

@ -10,6 +10,6 @@
<TextView android:id="@+id/filter_poi_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="25px"></TextView>
</LinearLayout>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:menuCategory="container" android:id="@+id/edit_filter_context_menu">
<item android:title="@string/edit_filter_save_as_menu_item" android:id="@+id/edit_filter_save_as" android:icon="@android:drawable/ic_menu_save"></item>
<item android:title="@string/edit_filter_delete_menu_item" android:id="@+id/edit_filter_delete" android:icon="@android:drawable/ic_menu_delete"></item>
</group>
</menu>

View file

@ -10,4 +10,5 @@
<item android:id="@+id/map_show_settings" android:title="@string/settings_Button" android:icon="@android:drawable/ic_menu_preferences"></item>
</group>
</menu>

View file

@ -216,4 +216,10 @@
<string name="default_buttons_commit">Сохранить</string>
<string name="default_buttons_reset">Сброс</string>
<string name="filter_current_poiButton">Фильтровать</string>
<string name="edit_filter_delete_menu_item">Удалить</string>
<string name="edit_filter_save_as_menu_item">Сохранить как</string>
<string name="edit_filter_delete_dialog_title">Вы действительно хотите удалить текущий фильтр?</string>
<string name="edit_filter_delete_message">Фильтр {0} удален</string>
<string name="edit_filter_create_message">Фильтр {0} создан</string>
<string name="default_buttons_selectall">Выделить все</string>
</resources>

View file

@ -218,4 +218,10 @@
<string name="default_buttons_commit">Commit</string>
<string name="default_buttons_reset">Reset</string>
<string name="filter_current_poiButton">Filter</string>
<string name="edit_filter_delete_menu_item">Delete</string>
<string name="edit_filter_save_as_menu_item">Save As</string>
<string name="edit_filter_delete_dialog_title">Are you sure about deleting selected filter?</string>
<string name="edit_filter_delete_message">Filter {0} has been deleted</string>
<string name="edit_filter_create_message">Filter {0} has been created</string>
<string name="default_buttons_selectall">Select All</string>
</resources>

View file

@ -1,5 +1,6 @@
package com.osmand;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@ -107,7 +108,7 @@ public class PoiFilter {
* @return null if all subtypes are accepted/ empty list if type is not accepted at all
*/
public List<String> getAcceptedSubtypes(AmenityType type){
if(acceptedTypes.containsKey(type)){
if(!acceptedTypes.containsKey(type)){
return Collections.emptyList();
}
return acceptedTypes.get(type);
@ -136,7 +137,7 @@ public class PoiFilter {
public void setTypeToAccept(AmenityType type, boolean accept){
if(accept){
acceptedTypes.put(type, null);
acceptedTypes.put(type, new ArrayList<String>());
} else {
acceptedTypes.remove(type);
}
@ -177,7 +178,6 @@ public class PoiFilter {
}
b.append(")"); //$NON-NLS-1$
}
b.append(")"); //$NON-NLS-1$
return b.toString();
}

View file

@ -128,10 +128,13 @@ public class PoiFiltersHelper {
}
public static boolean removePoiFilter(Context ctx, PoiFilter filter){
if(filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID)){
return false;
}
PoiFilterDbHelper helper = new PoiFilterDbHelper(ctx);
boolean res = helper.deleteFilter(filter);
if(res){
getUserDefinedPoiFilters(ctx).remove(filter);
cacheUserDefinedFilters.remove(filter);
}
helper.close();
return res;
@ -141,13 +144,16 @@ public class PoiFiltersHelper {
PoiFilterDbHelper helper = new PoiFilterDbHelper(ctx);
boolean res = helper.addFilter(filter, helper.getWritableDatabase(), false);
if(res){
getUserDefinedPoiFilters(ctx).add(filter);
cacheUserDefinedFilters.add(filter);
}
helper.close();
return res;
}
public static boolean editPoiFilter(Context ctx, PoiFilter filter){
if(filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID)){
return false;
}
PoiFilterDbHelper helper = new PoiFilterDbHelper(ctx);
boolean res = helper.editFilter(filter);
helper.close();

View file

@ -3,23 +3,34 @@
*/
package com.osmand.activities;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
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.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.osmand.OsmandSettings;
import com.osmand.PoiFilter;
import com.osmand.PoiFiltersHelper;
import com.osmand.R;
@ -44,8 +55,10 @@ public class EditPOIFilterActivity extends ListActivity {
filterLevel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO
OsmandSettings.setPoiFilterForMap(EditPOIFilterActivity.this, filter.getFilterId());
OsmandSettings.setShowPoiOverMap(EditPOIFilterActivity.this, true);
Intent newIntent = new Intent(EditPOIFilterActivity.this, MapActivity.class);
startActivity(newIntent);
}
});
@ -56,20 +69,130 @@ public class EditPOIFilterActivity extends ListActivity {
setListAdapter(new AmenityAdapter(AmenityType.getCategories()));
}
private void showDialog(AmenityType amenity) {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.edit_filter_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.edit_filter_delete) {
EditPOIFilterActivity.this.finish();
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.edit_filter_delete_dialog_title);
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) {
if (PoiFiltersHelper.removePoiFilter(EditPOIFilterActivity.this, filter)) {
Toast.makeText(
EditPOIFilterActivity.this,
MessageFormat.format(EditPOIFilterActivity.this.getText(R.string.edit_filter_delete_message).toString(),
filter.getName()), Toast.LENGTH_SHORT).show();
}
}
});
builder.create().show();
return true;
} else if (item.getItemId() == R.id.edit_filter_save_as) {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.edit_filter_save_as_menu_item);
final EditText editText = new EditText(this);
builder.setView(editText);
builder.setNegativeButton(R.string.default_buttons_cancel, null);
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PoiFilter nFilter = new PoiFilter(editText.getText().toString(), null, filter.getAcceptedTypes());
if (PoiFiltersHelper.createPoiFilter(EditPOIFilterActivity.this, nFilter)) {
Toast.makeText(
EditPOIFilterActivity.this,
MessageFormat.format(EditPOIFilterActivity.this.getText(R.string.edit_filter_create_message).toString(),
editText.getText().toString()), Toast.LENGTH_SHORT).show();
}
EditPOIFilterActivity.this.finish();
}
});
builder.create().show();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog(final AmenityType amenity) {
Builder builder = new AlertDialog.Builder(this);
ScrollView scroll = new ScrollView(this);
ListView listView = new ListView(this);
final List<String> accepted = new ArrayList<String>();
final LinkedHashSet<String> subCategories = new LinkedHashSet<String>(AmenityType.getSubCategories(amenity));
List<String> subtypes = filter.getAcceptedSubtypes(amenity);
boolean allSubTypesAccepted = subtypes == null;
LinkedHashSet<String> acceptedCategories = subtypes == null ? null : new LinkedHashSet<String>(subtypes);
if (subtypes != null) {
for (String s : acceptedCategories) {
if (!subCategories.contains(s)) {
subCategories.add(s);
}
}
}
final String[] array = subCategories.toArray(new String[0]);
boolean[] selected = new boolean[array.length];
for (int i = 0; i < selected.length; i++) {
if (allSubTypesAccepted) {
selected[i] = true;
accepted.add(array[i]);
} else {
selected[i] = acceptedCategories.contains(array[i]);
if (selected[i]) {
accepted.add(array[i]);
}
}
}
scroll.addView(listView);
builder.setView(scroll);
builder.setNegativeButton("Close", null);
builder.setNeutralButton("Select all", null);
builder.setMultiChoiceItems(AmenityType.getSubCategories(amenity).toArray(new String[0]), null, null);
builder.setNegativeButton(EditPOIFilterActivity.this.getText(R.string.default_buttons_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (subCategories.size() == accepted.size()) {
filter.selectSubTypesToAccept(amenity, null);
} else {
filter.selectSubTypesToAccept(amenity, accepted);
}
PoiFiltersHelper.editPoiFilter(EditPOIFilterActivity.this, filter);
((AmenityAdapter) EditPOIFilterActivity.this.getListAdapter()).notifyDataSetInvalidated();
}
});
builder.setNeutralButton(EditPOIFilterActivity.this.getText(R.string.default_buttons_selectall), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
filter.selectSubTypesToAccept(amenity, null);
PoiFiltersHelper.editPoiFilter(EditPOIFilterActivity.this, filter);
((AmenityAdapter) EditPOIFilterActivity.this.getListAdapter()).notifyDataSetInvalidated();
}
});
builder.setMultiChoiceItems(array, selected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
if (isChecked && !accepted.contains(array[item])) {
accepted.add(array[item]);
} else if (!isChecked && accepted.contains(array[item])) {
accepted.remove(array[item]);
}
}
});
builder.show();
}
class AmenityAdapter extends ArrayAdapter<AmenityType> {
AmenityAdapter(AmenityType[] amenityTypes) {
super(EditPOIFilterActivity.this, R.layout.editing_poi_filter_list, amenityTypes);
@ -106,9 +229,11 @@ public class EditPOIFilterActivity extends ListActivity {
@Override
public void onClick(View v) {
if (check.isChecked()) {
filter.setTypeToAccept(model, true);
showDialog(model);
} else {
filter.setTypeToAccept(model, false);
PoiFiltersHelper.editPoiFilter(EditPOIFilterActivity.this, filter);
}
}
});

View file

@ -39,9 +39,7 @@ public class SearchPoiFilterActivity extends ListActivity {
super.onCreate(icicle);
setContentView(R.layout.searchpoilist);
List<PoiFilter> filters = new ArrayList<PoiFilter>(PoiFiltersHelper.getUserDefinedPoiFilters(this)) ;
filters.addAll(PoiFiltersHelper.getOsmDefinedPoiFilters(this));
setListAdapter(new AmenityAdapter(filters));
typeFace = Typeface.create((String)null, Typeface.ITALIC);
// ListActivity has a ListView, which you can get with:
@ -52,6 +50,20 @@ public class SearchPoiFilterActivity extends ListActivity {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
PoiFilter poi = ((AmenityAdapter) getListAdapter()).getItem(pos);
showEditActivity(poi);
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
List<PoiFilter> filters = new ArrayList<PoiFilter>(PoiFiltersHelper.getUserDefinedPoiFilters(this)) ;
filters.addAll(PoiFiltersHelper.getOsmDefinedPoiFilters(this));
setListAdapter(new AmenityAdapter(filters));
}
private void showEditActivity(PoiFilter poi) {
if(!poi.isStandardFilter()) {
Bundle bundle = new Bundle();
Intent newIntent = new Intent(SearchPoiFilterActivity.this, EditPOIFilterActivity.class);
@ -60,14 +72,13 @@ public class SearchPoiFilterActivity extends ListActivity {
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
return true;
}
});
}
public void onListItemClick(ListView parent, View v, int position, long id) {
PoiFilter filter = ((AmenityAdapter) getListAdapter()).getItem(position);
if(filter.getFilterId().equals(PoiFilter.CUSTOM_FILTER_ID)){
showEditActivity(filter);
return;
}
Bundle bundle = new Bundle();
Intent newIntent = new Intent(SearchPoiFilterActivity.this, SearchPOIActivity.class);
bundle.putString(SearchPOIActivity.AMENITY_FILTER, filter.getFilterId());
@ -76,6 +87,7 @@ public class SearchPoiFilterActivity extends ListActivity {
}
class AmenityAdapter extends ArrayAdapter<PoiFilter> {
AmenityAdapter(List<PoiFilter> list) {
super(SearchPoiFilterActivity.this, R.layout.searchpoi_list, list);