add save and read profiles from settings, deleting profiles - w.i.p.

This commit is contained in:
madwasp79 2019-04-22 17:36:16 +03:00
parent 3c81d0a147
commit 7b60c15a49
8 changed files with 155 additions and 80 deletions

View file

@ -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() {

View file

@ -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<String, Set<ApplicationMode>> widgetsVisibilityMap = new LinkedHashMap<>();
private static Map<String, Set<ApplicationMode>> widgetsAvailabilityMap = new LinkedHashMap<>();
private static List<ApplicationMode> 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) {
if (Algorithms.isEmpty(userProfileName)) {
return ctx.getString(key);
} else {
return userProfileName;
}
}
public String toHumanStringCtx(Context ctx) {
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<String> listener;
private static OsmAndAppCustomization.OsmAndAppCustomizationListener customizationListener;
public void saveCustomProfileToSettings(OsmandSettings settings){
List<ApplicationMode> 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<ArrayList<ApplicationMode>>() {}.getType();
List<ApplicationMode> 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;
}
}

View file

@ -2745,6 +2745,9 @@ public class OsmandSettings {
RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values())
.makeGlobal();
public final CommonPreference<String> CUSTOM_APP_PROFILES = new StringPreference(
"custom_profiles", "").makeGlobal().cache();
public enum DayNightMode {
AUTO(R.string.daynight_mode_auto, R.drawable.ic_action_map_sunst),

View file

@ -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:

View file

@ -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<ApplicationMode> copyAllModes = new ArrayList<>(ApplicationMode.allPossibleValues());
List<ApplicationMode> copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication()));
// List<ApplicationMode> copyAllAvailableModes = new ArrayList<>(ApplicationMode.values(getMyApplication()));
Iterator<ApplicationMode> it = copyAllModes.iterator();
while (it.hasNext()) {
ApplicationMode am = it.next();
@ -304,14 +309,11 @@ public class EditProfileFragment extends BaseOsmAndFragment {
it.remove();
}
}
String customStringKey = profile.stringKey;
if (isNew && profile.getParent() != null) {
customStringKey = profile.getParent().getStringKey() + "_" + profile.userProfileTitle.hashCode();
}
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;
}
}
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() {

View file

@ -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<ProfileViewHolder> {
@ -63,7 +59,7 @@ public class ProfileMenuAdapter extends RecyclerView.Adapter<ProfileViewHolder>
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()));

View file

@ -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);
}

View file

@ -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<String> routingParameters;