From afb7f865d3746bdfeb7c907da146c1bd576a4f12 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 15 Oct 2014 13:24:11 +0300 Subject: [PATCH] Firs implementation of refactor to map settings --- OsmAnd/AndroidManifest.xml | 1 + .../plus/activities/MapActivityActions.java | 5 +- .../plus/activities/MapActivityLayers.java | 2 +- .../plus/activities/SettingsBaseActivity.java | 27 +- .../ConfigureScreenSettingsActivity.java | 390 ++++++++++++++++++ 5 files changed, 416 insertions(+), 9 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/configuremap/ConfigureScreenSettingsActivity.java diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index 8020a1ae85..235b08f9eb 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -130,6 +130,7 @@ + diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 0d43e7fbd0..f3d5ab0c55 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -47,6 +47,7 @@ import net.osmand.plus.activities.actions.ShareLocation; import net.osmand.plus.activities.actions.StartGPSStatus; import net.osmand.plus.activities.search.SearchActivity; import net.osmand.plus.base.FavoriteImageDrawable; +import net.osmand.plus.configuremap.ConfigureScreenSettingsActivity; import net.osmand.plus.development.OsmandDevelopmentPlugin; import net.osmand.plus.helpers.WaypointDialogHelper; import net.osmand.plus.osmo.OsMoPositionLayer; @@ -900,7 +901,9 @@ public class MapActivityActions implements DialogProvider { .listen(new OnContextMenuClick() { @Override public void onContextMenuClick(int itemId, int pos, boolean isChecked, DialogInterface dialog) { - mapActivity.getMapLayers().openLayerSelectionDialog(mapView); + mapActivity.startActivity(new Intent(mapActivity, ConfigureScreenSettingsActivity.class)); + + //mapActivity.getMapLayers().openLayerSelectionDialog(mapView); } }).reg(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index cf4f953713..7f26d1ff90 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -349,7 +349,7 @@ public class MapActivityLayers { adapter.item(R.string.layer_transport).selected( settings.SHOW_TRANSPORT_OVER_MAP.get() ? 1 : 0) .icons(R.drawable.ic_action_bus_dark, R.drawable.ic_action_bus_light).reg(); if(TransportRouteHelper.getInstance().routeIsCalculated()){ - adapter.item(R.string.layer_transport_route).selected(1 ) + adapter.item(R.string.layer_transport_route).selected(1) .icons(R.drawable.ic_action_bus_dark, R.drawable.ic_action_bus_light).reg(); } diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java index 17f9c5292c..f901eeb20a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java @@ -9,6 +9,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import android.os.Build; +import android.preference.*; import net.osmand.access.AccessibleToast; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; @@ -25,14 +27,8 @@ import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; -import android.preference.CheckBoxPreference; -import android.preference.EditTextPreference; -import android.preference.ListPreference; -import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; -import android.preference.PreferenceGroup; -import android.preference.PreferenceScreen; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Toast; @@ -46,7 +42,7 @@ public abstract class SettingsBaseActivity extends SherlockPreferenceActivity im protected OsmandSettings settings; protected final boolean profileSettings ; - private List modes = new ArrayList(); + protected List modes = new ArrayList(); private ApplicationMode previousAppMode; private Map screenPreferences = new LinkedHashMap(); @@ -74,6 +70,23 @@ public abstract class SettingsBaseActivity extends SherlockPreferenceActivity im return p; } + public CheckBoxPreference createCheckBoxPreference(OsmandPreference b, int title, int darkIcon, int lightIcon){ + CheckBoxPreference p = new CheckBoxPreference(this); + p.setTitle(title); + p.setKey(b.getId()); + p.setOnPreferenceChangeListener(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + if (getMyApplication().getSettings().isLightContent()) { + p.setIcon(lightIcon); + } else { + p.setIcon(darkIcon); + } + } + screenPreferences.put(b.getId(), p); + booleanPreferences.put(b.getId(), b); + return p; + } + public CheckBoxPreference createCheckBoxPreference(OsmandPreference b, int title, int summary) { CheckBoxPreference p = new CheckBoxPreference(this); p.setTitle(title); diff --git a/OsmAnd/src/net/osmand/plus/configuremap/ConfigureScreenSettingsActivity.java b/OsmAnd/src/net/osmand/plus/configuremap/ConfigureScreenSettingsActivity.java new file mode 100644 index 0000000000..08d6fe6683 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configuremap/ConfigureScreenSettingsActivity.java @@ -0,0 +1,390 @@ +package net.osmand.plus.configuremap; + +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.preference.*; +import android.view.KeyEvent; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.Toast; +import net.osmand.access.AccessibleToast; +import net.osmand.data.AmenityType; +import net.osmand.plus.*; +import net.osmand.plus.activities.*; +import net.osmand.plus.render.RenderingIcons; +import net.osmand.plus.views.MapInfoLayer; +import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.mapwidgets.AppearanceWidgetsFactory; +import net.osmand.plus.views.mapwidgets.MapWidgetRegistry; +import net.osmand.render.RenderingRuleProperty; +import net.osmand.render.RenderingRulesStorage; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * Created by Denis on 14.10.2014. + */ +public class ConfigureScreenSettingsActivity extends SettingsBaseActivity { + + private String ADDITIONAL_VECTOR_RENDERING_CATEGORY; + private ApplicationMode previousMode; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + for (ApplicationMode a : ApplicationMode.values(settings)) { + modes.add(a); + } + getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + List s = new ArrayList(); + for (ApplicationMode a : modes) { + s.add(a.toHumanString(getMyApplication())); + } + + ArrayAdapter spinnerAdapter = new ArrayAdapter(getSupportActionBar().getThemedContext(), + R.layout.sherlock_spinner_item, s); + spinnerAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item); + getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, new com.actionbarsherlock.app.ActionBar.OnNavigationListener() { + + @Override + public boolean onNavigationItemSelected(int itemPosition, long itemId) { + settings.APPLICATION_MODE.set(modes.get(itemPosition)); + createAllCategories(); + return true; + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + previousMode = settings.getApplicationMode(); + boolean found = setSelectedAppMode(previousMode); + if (!found) { + getSupportActionBar().setSelectedNavigationItem(0); + } + createAllCategories(); + } + + + @Override + protected void onPause() { + super.onPause(); + settings.APPLICATION_MODE.set(previousMode); + } + + private void createAllCategories() { + getPreferenceScreen().removeAll(); + createLayersPreferences(); + createRenderingAttributePreferences(); + + } + + private void createRenderingAttributePreferences() { + PreferenceCategory renderingCategory = new PreferenceCategory(this); + renderingCategory.setTitle(R.string.map_widget_map_rendering); + PreferenceScreen grp = getPreferenceScreen(); + grp.addPreference(renderingCategory); + Preference p = new Preference(this); + p.setTitle(R.string.map_widget_renderer); + p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); + bld.setTitle(R.string.renderers); + Collection rendererNames = getMyApplication().getRendererRegistry().getRendererNames(); + final String[] items = rendererNames.toArray(new String[rendererNames.size()]); + final String[] visibleNames = new String[items.length]; + int selected = -1; + final String selectedName = getMyApplication().getRendererRegistry().getCurrentSelectedRenderer().getName(); + for (int j = 0; j < items.length; j++) { + if (items[j].equals(selectedName)) { + selected = j; + } + visibleNames[j] = items[j].replace('_', ' ').replace( + '-', ' '); + } + bld.setSingleChoiceItems(visibleNames, selected, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + String renderer = items[which]; + RenderingRulesStorage loaded = getMyApplication().getRendererRegistry().getRenderer(renderer); + if (loaded != null) { + getMyApplication().getSettings().RENDERER.set(renderer); + getMyApplication().getRendererRegistry().setCurrentSelectedRender(loaded); + getMyApplication().getResourceManager().getRenderer().clearCache(); + } else { + AccessibleToast.makeText(getMyApplication(), R.string.renderer_load_exception, Toast.LENGTH_SHORT).show(); + } + dialog.dismiss(); + createAllCategories(); + } + }); + bld.show(); + return true; + } + }); + grp.addPreference(p); + + p = new Preference(this); + p.setTitle(R.string.map_widget_day_night); + p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); + bld.setTitle(R.string.daynight); + final String[] items = new String[OsmandSettings.DayNightMode.values().length]; + for (int i = 0; i < items.length; i++) { + items[i] = OsmandSettings.DayNightMode.values()[i].toHumanString(getMyApplication()); + } + int i = getMyApplication().getSettings().DAYNIGHT_MODE.get().ordinal(); + bld.setSingleChoiceItems(items, i, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getMyApplication().getSettings().DAYNIGHT_MODE.set(OsmandSettings.DayNightMode.values()[which]); + getMyApplication().getResourceManager().getRenderer().clearCache(); + dialog.dismiss(); + } + }); + bld.show(); + return true; + } + }); + grp.addPreference(p); + + RenderingRulesStorage renderer = getMyApplication().getRendererRegistry().getCurrentSelectedRenderer(); + if(renderer != null && AppearanceWidgetsFactory.EXTRA_SETTINGS) { + createMapRenderingPreferences(); + } + } + + + private void createMapRenderingPreferences() { + PreferenceCategory renderingCategory = new PreferenceCategory(this); + renderingCategory.setTitle(R.string.map_widget_vector_attributes); + PreferenceScreen grp = getPreferenceScreen(); + grp.addPreference(renderingCategory); + + RenderingRulesStorage renderer = getMyApplication().getRendererRegistry().getCurrentSelectedRenderer(); + final OsmandApplication app = getMyApplication(); + List customRules = renderer.PROPS.getCustomRules(); + for (final RenderingRuleProperty p : customRules) { + String propertyName = SettingsActivity.getStringPropertyName(getActivity(), p.getAttrName(), p.getName()); + //test old descr as title + final String propertyDescr = SettingsActivity.getStringPropertyDescription(getActivity(), p.getAttrName(), p.getName()); + if(p.isBoolean()) { + final OsmandSettings.CommonPreference pref = getMyApplication().getSettings().getCustomRenderBooleanProperty(p.getAttrName()); + CheckBoxPreference preference = createCheckBoxPreference(pref); + preference.setTitle(propertyName); + renderingCategory.addPreference(preference); + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + pref.set(!pref.get()); + app.getResourceManager().getRenderer().clearCache(); + return true; + } + }); + + } else { + final OsmandSettings.CommonPreference pref = getMyApplication().getSettings().getCustomRenderProperty(p.getAttrName()); + Preference preference = new Preference(getActivity()); + preference.setTitle(propertyName); + renderingCategory.addPreference(preference); + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); + //test old descr as title + b.setTitle(propertyDescr); + + int i = Arrays.asList(p.getPossibleValues()).indexOf(pref.get()); + + String[] possibleValuesString = new String[p.getPossibleValues().length]; + + for (int j = 0; j < p.getPossibleValues().length; j++) { + possibleValuesString[j] = SettingsActivity.getStringPropertyValue(getApplication(), p.getPossibleValues()[j]); + } + + b.setSingleChoiceItems(possibleValuesString, i, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + pref.set(p.getPossibleValues()[which]); + app.getResourceManager().getRenderer().clearCache(); + dialog.dismiss(); + } + }); + b.show(); + return true; + } + }); + + } + } + } + + + private void createLayersPreferences() { + PreferenceCategory mapLayersCategory = new PreferenceCategory(this); + mapLayersCategory.setTitle(R.string.menu_layers); + PreferenceScreen grp = getPreferenceScreen(); + grp.addPreference(mapLayersCategory); + final CheckBoxPreference poi = new CheckBoxPreference(this); + poi.setTitle(R.string.layer_poi); + poi.setChecked(settings.SHOW_POI_OVER_MAP.get()); + addIcon(poi, R.drawable.ic_action_info_light, R.drawable.ic_action_info_dark); + poi.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + settings.SHOW_POI_OVER_MAP.set(!settings.SHOW_POI_OVER_MAP.get()); + if (settings.SHOW_POI_OVER_MAP.get()) { + selectPOIFilterLayer(null); + } else { + poi.setSummary(""); + } + return true; + } + }); + if (settings.SHOW_POI_OVER_MAP.get()) { + poi.setSummary(settings.getPoiFilterForMap()); + } + mapLayersCategory.addPreference(poi); + CheckBoxPreference p = createCheckBoxPreference(settings.SHOW_POI_LABEL, R.string.layer_amenity_label, + R.drawable.ic_action_text_dark, R.drawable.ic_action_text_light); + mapLayersCategory.addPreference(p); + + p = createCheckBoxPreference(settings.SHOW_FAVORITES, R.string.layer_favorites, + R.drawable.ic_action_fav_dark, R.drawable.ic_action_fav_light); + mapLayersCategory.addPreference(p); + p = createCheckBoxPreference(settings.SHOW_POI_LABEL, R.string.layer_gpx_layer, + R.drawable.ic_action_polygom_dark, R.drawable.ic_action_polygom_light); + mapLayersCategory.addPreference(p); + p = createCheckBoxPreference(settings.SHOW_TRANSPORT_OVER_MAP, R.string.layer_transport, + R.drawable.ic_action_bus_dark, R.drawable.ic_action_bus_light); + mapLayersCategory.addPreference(p); + if (TransportRouteHelper.getInstance().routeIsCalculated()) { + p = new CheckBoxPreference(this); + p.setChecked(true); + addIcon(p, R.drawable.ic_action_bus_light, R.drawable.ic_action_bus_dark); + mapLayersCategory.addPreference(p); + } + } + + public void addIcon(CheckBoxPreference p, int lightIcon, int darkIcon) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + if (getMyApplication().getSettings().isLightContent()) { + p.setIcon(lightIcon); + } else { + p.setIcon(darkIcon); + } + } + } + + public AlertDialog selectPOIFilterLayer(final PoiFilter[] selected) { + final List userDefined = new ArrayList(); + OsmandApplication app = (OsmandApplication) getApplication(); + final PoiFiltersHelper poiFilters = app.getPoiFilters(); + final ContextMenuAdapter adapter = new ContextMenuAdapter(this); + + ContextMenuAdapter.Item is = adapter.item(getString(R.string.any_poi)); + if (RenderingIcons.containsBigIcon("null")) { + is.icon(RenderingIcons.getBigIconResourceId("null")); + } + is.reg(); + // 2nd custom + adapter.item(getString(R.string.poi_filter_custom_filter)).icon(RenderingIcons.getBigIconResourceId("user_defined")).reg(); + + for (PoiFilter f : poiFilters.getUserDefinedPoiFilters()) { + ContextMenuAdapter.Item it = adapter.item(f.getName()); + if (RenderingIcons.containsBigIcon(f.getSimplifiedId())) { + it.icon(RenderingIcons.getBigIconResourceId(f.getSimplifiedId())); + } else { + it.icon(RenderingIcons.getBigIconResourceId("user_defined")); + } + it.reg(); + userDefined.add(f); + } + final AmenityType[] categories = AmenityType.getCategories(); + for (AmenityType t : categories) { + ContextMenuAdapter.Item it = adapter.item(OsmAndFormatter.toPublicString(t, getMyApplication())); + if (RenderingIcons.containsBigIcon(t.toString().toLowerCase())) { + it.icon(RenderingIcons.getBigIconResourceId(t.toString().toLowerCase())); + } + it.reg(); + } + final AlertDialog.Builder builder = new AlertDialog.Builder(this); + ListAdapter listAdapter; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + listAdapter = + adapter.createListAdapter(this, R.layout.list_menu_item, app.getSettings().isLightContentMenu()); + } else { + listAdapter = + adapter.createListAdapter(this, R.layout.list_menu_item_native, app.getSettings().isLightContentMenu()); + } + builder.setAdapter(listAdapter, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == 1) { + String filterId = PoiFilter.CUSTOM_FILTER_ID; + getMyApplication().getSettings().setPoiFilterForMap(filterId); + Intent newIntent = new Intent(getApplication(), EditPOIFilterActivity.class); + newIntent.putExtra(EditPOIFilterActivity.AMENITY_FILTER, filterId); + //newIntent.putExtra(EditPOIFilterActivity.SEARCH_LAT, getMyApplication().getMapActivity().getMapView().getLatitude()); + //newIntent.putExtra(EditPOIFilterActivity.SEARCH_LON, getMyApplication().getMapActivity().getMapView().getLongitude()); + getActivity().startActivity(newIntent); + } else { + String filterId; + if (which == 0) { + filterId = PoiFiltersHelper.getOsmDefinedFilterId(null); + } else if (which <= userDefined.size() + 1) { + filterId = userDefined.get(which - 2).getFilterId(); + } else { + filterId = PoiFiltersHelper.getOsmDefinedFilterId(categories[which - userDefined.size() - 2]); + } + getMyApplication().getSettings().setPoiFilterForMap(filterId); + PoiFilter f = poiFilters.getFilterById(filterId); + if (f != null) { + f.clearNameFilter(); + } + if (selected != null && selected.length > 0) { + selected[0] = f; + } + createAllCategories(); + } + } + + }); + builder.setOnKeyListener(new DialogInterface.OnKeyListener() { + @Override + public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent event) { + if (i == KeyEvent.KEYCODE_BACK && + event.getAction() == KeyEvent.ACTION_UP && + !event.isCanceled()) { + dialogInterface.cancel(); + settings.SHOW_POI_OVER_MAP.set(false); + createAllCategories(); + return true; + } + return false; + } + }); + return builder.show(); + } + + private Activity getActivity() { + return this; + } + +}