diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index d940aa78a1..dbb5f3cc24 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -190,7 +190,9 @@ public class AppInitializer implements IProgress { } app.getSettings().SHOW_TRAVEL_UPDATE_CARD.set(true); app.getSettings().SHOW_TRAVEL_NEEDED_MAPS_CARD.set(true); + ApplicationMode.initCustomProfiles(app.getSettings()); initSettings = true; + } public int getNumberOfStarts() { diff --git a/OsmAnd/src/net/osmand/plus/ApplicationMode.java b/OsmAnd/src/net/osmand/plus/ApplicationMode.java index fb41b9056c..02ae475b9f 100644 --- a/OsmAnd/src/net/osmand/plus/ApplicationMode.java +++ b/OsmAnd/src/net/osmand/plus/ApplicationMode.java @@ -2,6 +2,13 @@ package net.osmand.plus; import android.content.Context; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.annotations.Expose; +import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.HashMap; +import net.osmand.PlatformUtil; import net.osmand.StateChangedListener; import java.util.ArrayList; @@ -11,9 +18,14 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import net.osmand.util.Algorithms; +import net.sf.junidecode.App; +import org.apache.commons.logging.Log; public class ApplicationMode { + + private static final Log LOG = PlatformUtil.getLog(ApplicationMode.class); private static Map> widgetsVisibilityMap = new LinkedHashMap<>(); private static Map> widgetsAvailabilityMap = new LinkedHashMap<>(); private static List defaultValues = new ArrayList<>(); @@ -42,6 +54,8 @@ public class ApplicationMode { public static final ApplicationMode AIRCRAFT = create(R.string.app_mode_aircraft, "aircraft").speed(40f, 100).carLocation(). icon(R.drawable.map_action_aircraft, R.drawable.ic_action_aircraft).reg(); + + //--------------------------------------------------------------------------------------------------------------- // public static final ApplicationMode HIKING = create(R.string.app_mode_hiking, "hiking").speed(1.5f, 5).parent(PEDESTRIAN). // icon(R.drawable.map_action_trekking_dark, R.drawable.ic_action_trekking_dark).reg(); @@ -134,6 +148,7 @@ public class ApplicationMode { * 1 - car, 2 - bicicle, 3 - nautical, any other - default */ public ApplicationModeBuilder setLocationAndBearingIcons(int type) { + applicationMode.mapIconsSetId = type; switch (type) { case 1: return this.carLocation(); @@ -209,7 +224,7 @@ public class ApplicationMode { } public ApplicationModeBuilder userProfileTitle(String userProfileTitle) { - applicationMode.userProfileTitle = userProfileTitle; + applicationMode.userProfileName = userProfileTitle; return this; } } @@ -401,11 +416,20 @@ public class ApplicationMode { } public String toHumanString(Context ctx) { - return ctx.getString(key); + if (Algorithms.isEmpty(userProfileName)) { + return ctx.getString(key); + } else { + return userProfileName; + } + } public String toHumanStringCtx(Context ctx) { - return ctx.getString(key); + if (Algorithms.isEmpty(userProfileName)) { + return ctx.getString(key); + } else { + return userProfileName; + } } public static ApplicationMode valueOfStringKey(String key, ApplicationMode def) { @@ -437,28 +461,73 @@ public class ApplicationMode { return this == mode || getParent() == mode; } - public String getUserProfileTitle() { - return userProfileTitle; + public int getMapIconsSetId() { + return mapIconsSetId; } - private final int key; - private final String stringKey; - private String userProfileTitle = ""; - private ApplicationMode parent; - private int mapIconId = R.drawable.map_world_globe_dark; - private int smallIconDark = R.drawable.ic_world_globe_dark; - private float defaultSpeed = 10f; - private int minDistanceForTurn = 50; - private int arrivalDistance = 90; - private int offRouteDistance = 350; - private int bearingIconDay = R.drawable.map_pedestrian_bearing; - private int bearingIconNight = R.drawable.map_pedestrian_bearing_night; - private int headingIconDay = R.drawable.map_pedestrian_location_view_angle; - private int headingIconNight = R.drawable.map_pedestrian_location_view_angle_night; - private int locationIconDay = R.drawable.map_pedestrian_location; - private int locationIconNight = R.drawable.map_pedestrian_location_night; - private int locationIconDayLost = R.drawable.map_pedestrian_location_lost; - private int locationIconNightLost = R.drawable.map_pedestrian_location_lost_night; + public String getUserProfileName() { + return userProfileName; + } + + @Expose private final int key; + @Expose private final String stringKey; + @Expose private String userProfileName = ""; + @Expose private int mapIconsSetId = 0; + @Expose private ApplicationMode parent; + @Expose private int mapIconId = R.drawable.map_world_globe_dark; + @Expose private int smallIconDark = R.drawable.ic_world_globe_dark; + @Expose private float defaultSpeed = 10f; + @Expose private int minDistanceForTurn = 50; + @Expose private int arrivalDistance = 90; + @Expose private int offRouteDistance = 350; + @Expose private int bearingIconDay = R.drawable.map_pedestrian_bearing; + @Expose private int bearingIconNight = R.drawable.map_pedestrian_bearing_night; + @Expose private int headingIconDay = R.drawable.map_pedestrian_location_view_angle; + @Expose private int headingIconNight = R.drawable.map_pedestrian_location_view_angle_night; + @Expose private int locationIconDay = R.drawable.map_pedestrian_location; + @Expose private int locationIconNight = R.drawable.map_pedestrian_location_night; + @Expose private int locationIconDayLost = R.drawable.map_pedestrian_location_lost; + @Expose private int locationIconNightLost = R.drawable.map_pedestrian_location_lost_night; private static StateChangedListener listener; private static OsmAndAppCustomization.OsmAndAppCustomizationListener customizationListener; + + + public void saveCustomProfileToSettings(OsmandSettings settings){ + List customModes = new ArrayList<>(); + for (ApplicationMode mode : values) { + if (mode.parent != null) { + customModes.add(mode); + } + } + Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); + String profiles = gson.toJson(customModes); + settings.CUSTOM_APP_PROFILES.set(profiles); + } + + + + public static boolean initCustomProfiles(OsmandSettings settings){ + Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); + Type t = new TypeToken>() {}.getType(); + List customProfiles = gson.fromJson(settings.CUSTOM_APP_PROFILES.get(), t); + + if (!Algorithms.isEmpty(customProfiles)) { + for (ApplicationMode m : customProfiles) { + if (!values.contains(m)) { + values.add(m); + if (m.getParent() != null) { + LOG.debug("parent: " + m.getParent().getStringKey()); + } else { + LOG.debug("parent: propal!!!!!111 " ); + } + + } + } + return true; + } + return false; + } + + + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 3dcffc67e7..dcde1af648 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2745,6 +2745,9 @@ public class OsmandSettings { RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values()) .makeGlobal(); + public final CommonPreference CUSTOM_APP_PROFILES = new StringPreference( + "custom_profiles", "").makeGlobal().cache(); + public enum DayNightMode { AUTO(R.string.daynight_mode_auto, R.drawable.ic_action_map_sunst), diff --git a/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java b/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java index 3e24fbc362..631d3ea94a 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java +++ b/OsmAnd/src/net/osmand/plus/profiles/EditProfileActivity.java @@ -4,6 +4,9 @@ import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v4.view.MenuItemCompat; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import net.osmand.plus.R; @@ -17,34 +20,34 @@ public class EditProfileActivity extends OsmandActionBarActivity { getMyApplication().applyTheme(this); super.onCreate(savedInstanceState); setContentView(R.layout.single_fragment_layout); -// Intent intent = getIntent(); -// if (intent.getExtras() != null) { -// String title = ""; -// if (intent.getBooleanExtra("isUserProfile", false)) { -// -// } else if (intent.getBooleanExtra("isNew", false)) { -// title = String.format("%s (new)", intent.getStringExtra("stringKey").replace("_", " ")); -// } else { -// title = Algorithms.capitalizeFirstLetterAndLowercase( -// intent.getStringExtra("stringKey").replace("_", " ")); -// } -// -// if (getSupportActionBar() != null) { -// getSupportActionBar().setTitle(title); -// getSupportActionBar().setElevation(5.0f); -// } -// } if (savedInstanceState == null) { EditProfileFragment editProfileFragment = new EditProfileFragment(); editProfileFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction().add(android.R.id.content, - editProfileFragment).commit(); + editProfileFragment, "editProfileFragment").commit(); } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + + MenuItem m = menu.add(0, 0, 0, R.string.action_delete).setIcon(R.drawable.ic_action_delete_dark); + MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS); + super.onCreateOptionsMenu(menu); + return true; + } + + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + + + } @Override public boolean onOptionsItemSelected(MenuItem item) { + + int itemId = item.getItemId(); switch (itemId) { case android.R.id.home: diff --git a/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java index 77b0461840..694042132e 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/EditProfileFragment.java @@ -3,14 +3,17 @@ package net.osmand.plus.profiles; import android.content.Intent; import android.graphics.drawable.GradientDrawable; import android.os.Bundle; -import android.os.Parcel; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBar; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; @@ -23,9 +26,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.osmand.PlatformUtil; -import net.osmand.StateChangedListener; import net.osmand.plus.ApplicationMode; -import net.osmand.plus.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -36,8 +37,6 @@ import net.osmand.plus.profiles.ProfileBottomSheetDialogFragment.ProfileTypeDial import net.osmand.plus.profiles.SelectIconBottomSheetDialogFragment.IconIdListener; import net.osmand.plus.widgets.OsmandTextFieldBoxes; import net.osmand.router.GeneralRouter.GeneralRouterProfile; -import net.osmand.util.Algorithms; -import net.sf.junidecode.App; import org.apache.commons.logging.Log; import studio.carbonylgroup.textfieldboxes.ExtendedEditText; @@ -72,8 +71,10 @@ public class EditProfileFragment extends BaseOsmAndFragment { String modeName = getArguments().getString("stringKey", "car"); isNew = getArguments().getBoolean("isNew", false); isUserProfile = getArguments().getBoolean("isUserProfile", false); + profile = new TempApplicationProfile( - ApplicationMode.valueOfStringKey(modeName, ApplicationMode.DEFAULT), isNew); + ApplicationMode.valueOfStringKey(modeName, ApplicationMode.DEFAULT), isNew, isUserProfile); + LOG.debug("Name: " + modeName + ", "); } routingProfiles = getRoutingProfiles(); @@ -291,9 +292,13 @@ public class EditProfileFragment extends BaseOsmAndFragment { } private boolean saveNewProfile(TempApplicationProfile profile, RoutingProfile selectedRoutingProfile) { - //todo check if profile exists + + if (isUserProfile && !isNew) { + return updateProfile(); + } + List copyAllModes = new ArrayList<>(ApplicationMode.allPossibleValues()); - List copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication())); +// List copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication())); Iterator it = copyAllModes.iterator(); while (it.hasNext()) { ApplicationMode am = it.next(); @@ -304,14 +309,11 @@ public class EditProfileFragment extends BaseOsmAndFragment { it.remove(); } } - - String customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode(); - for (ApplicationMode mode : ApplicationMode.allPossibleValues()) { - if (mode.getStringKey().equals(customStringKey)) { - //todo notify user that there is already profile with such name - return false; - } + String customStringKey = profile.stringKey; + if (isNew && profile.getParent() != null) { + customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode(); } + ApplicationMode.ApplicationModeBuilder builder = ApplicationMode .createCustomMode(profile.userProfileTitle, customStringKey) .parent(profile.parent) @@ -320,17 +322,18 @@ public class EditProfileFragment extends BaseOsmAndFragment { switch (profile.parent.getStringKey()) { case "car": case "aircraft": - builder.carLocation(); + builder.setLocationAndBearingIcons(1); break; - case "bicicle": - builder.bicycleLocation(); + case "bicycle": + builder.setLocationAndBearingIcons(2); break; case "boat": - builder.nauticalLocation(); + builder.setLocationAndBearingIcons(3); break; } - builder.customReg(); + ApplicationMode customMode = builder.customReg(); + customMode.saveCustomProfileToSettings(getSettings()); //todo build profile, save and register: @@ -381,7 +384,7 @@ public class EditProfileFragment extends BaseOsmAndFragment { private class TempApplicationProfile { int key = -1; - String stringKey = ""; + String stringKey; String userProfileTitle = ""; ApplicationMode parent = null; int iconId = R.drawable.map_world_globe_dark; @@ -389,20 +392,21 @@ public class EditProfileFragment extends BaseOsmAndFragment { int minDistanceForTurn = 50; //todo use default or what? RoutingProfile routingProfile = null; - TempApplicationProfile(ApplicationMode mode, boolean isNew) { + TempApplicationProfile(ApplicationMode mode, boolean isNew, boolean isUserProfile) { if (isNew ) { stringKey = "new_" + mode.getStringKey(); parent = mode; } else if (isUserProfile) { stringKey = mode.getStringKey(); - parent = getParent(); + parent = mode.getParent(); iconId = mode.getSmallIconDark(); - userProfileTitle = mode.getUserProfileTitle(); + userProfileTitle = mode.getUserProfileName(); } else { key = mode.getStringResource(); stringKey = mode.getStringKey(); iconId = mode.getSmallIconDark(); } + LOG.debug("Parent: " + getParent()); } public RoutingProfile getRoutingProfile() { diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java index 5b5a17657d..a4122b63c4 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileMenuAdapter.java @@ -1,6 +1,5 @@ package net.osmand.plus.profiles; -import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SwitchCompat; @@ -10,16 +9,13 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; -import java.util.ArrayList; import java.util.List; import java.util.Set; import net.osmand.plus.ApplicationMode; -import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.profiles.ProfileMenuAdapter.ProfileViewHolder; import net.osmand.util.Algorithms; -import net.sf.junidecode.App; public class ProfileMenuAdapter extends RecyclerView.Adapter { @@ -63,7 +59,7 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter public void onBindViewHolder(@NonNull final ProfileViewHolder holder, int position) { final ApplicationMode item = items.get(position); if (item.getParent() != null) { - holder.title.setText(item.getUserProfileTitle()); + holder.title.setText(item.getUserProfileName()); holder.descr.setText(String.format("Type: %s", Algorithms.capitalizeFirstLetterAndLowercase(item.getParent().getStringKey().replace("_", " ")))); } else { holder.title.setText(app.getResources().getString(item.getStringResource())); diff --git a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java index 09df1a9a96..96d353911c 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SettingsProfileFragment.java @@ -59,7 +59,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { listener = new ProfileListener() { @Override public void changeProfileStatus(ApplicationMode item, boolean isSelected) { - LOG.debug(getString(item.getStringResource()) + " - " + isSelected); + //LOG.debug(getString(item.getStringResource()) + " - " + isSelected); StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey()+","); ApplicationMode mode = null; for (ApplicationMode sam : allAppModes) { @@ -87,10 +87,8 @@ public class SettingsProfileFragment extends BaseOsmAndFragment { Intent intent = new Intent(getActivity(), EditProfileActivity.class); intent.putExtra("stringKey", item.getStringKey()); intent.putExtra("isNew", false); - if (!item.getUserProfileTitle().isEmpty()) { + if (!item.getUserProfileName().isEmpty()) { intent.putExtra("isUserProfile", true); - } else { - intent.putExtra("isUserProfile", false); } startActivity(intent); } diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RoutingOptionsHelper.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RoutingOptionsHelper.java index 13bf1f03a9..ef7d3dbe8e 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RoutingOptionsHelper.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RoutingOptionsHelper.java @@ -72,7 +72,7 @@ public class RoutingOptionsHelper { addRouteMenuAppModes(ApplicationMode.PEDESTRIAN, PermanentAppModeOptions.PEDESTRIAN.routingParameters); addRouteMenuAppModes(ApplicationMode.PUBLIC_TRANSPORT, PermanentAppModeOptions.PUBLIC_TRANSPORT.routingParameters); addRouteMenuAppModes(ApplicationMode.BOAT, PermanentAppModeOptions.BOAT.routingParameters); - addRouteMenuAppModes(ApplicationMode.AIRCRAFT, PermanentAppModeOptions.AIRCAFT.routingParameters); + addRouteMenuAppModes(ApplicationMode.AIRCRAFT, PermanentAppModeOptions.AIRCRAFT.routingParameters); // addRouteMenuAppModes(ApplicationMode.HIKING, PermanentAppModeOptions.HIKING.routingParameters); // addRouteMenuAppModes(ApplicationMode.MOTORCYCLE, PermanentAppModeOptions.MOTORCYCLE.routingParameters); // addRouteMenuAppModes(ApplicationMode.TRUCK, PermanentAppModeOptions.TRUCK.routingParameters); @@ -971,11 +971,11 @@ public class RoutingOptionsHelper { PEDESTRIAN(MuteSoundRoutingParameter.KEY, GeneralRouter.USE_HEIGHT_OBSTACLES), PUBLIC_TRANSPORT(MuteSoundRoutingParameter.KEY), BOAT(MuteSoundRoutingParameter.KEY), - AIRCAFT(MuteSoundRoutingParameter.KEY), - HIKING(MuteSoundRoutingParameter.KEY), - MOTORCYCLE(MuteSoundRoutingParameter.KEY), - TRUCK(MuteSoundRoutingParameter.KEY), - TRAIN(MuteSoundRoutingParameter.KEY); + AIRCRAFT(MuteSoundRoutingParameter.KEY); +// HIKING(MuteSoundRoutingParameter.KEY), +// MOTORCYCLE(MuteSoundRoutingParameter.KEY), +// TRUCK(MuteSoundRoutingParameter.KEY), +// TRAIN(MuteSoundRoutingParameter.KEY); List routingParameters;