Add ability to change routing profile in default app modes

This commit is contained in:
Chumva 2019-10-01 17:33:34 +03:00
parent 5143f186d5
commit c470f0113d
5 changed files with 113 additions and 55 deletions

View file

@ -350,6 +350,11 @@ public class ApplicationMode {
return create(parent,-1, stringKey).userProfileTitle(userProfileTitle); return create(parent,-1, stringKey).userProfileTitle(userProfileTitle);
} }
public static ApplicationModeBuilder changeBaseMode(ApplicationMode applicationMode) {
ApplicationModeBuilder builder = new ApplicationModeBuilder();
builder.applicationMode = applicationMode;
return builder;
}
public static List<ApplicationMode> values(OsmandApplication app) { public static List<ApplicationMode> values(OsmandApplication app) {
if (customizationListener == null) { if (customizationListener == null) {
@ -392,6 +397,16 @@ public class ApplicationMode {
return defaultValues; return defaultValues;
} }
public static List<ApplicationMode> getCustomValues() {
List<ApplicationMode> customModes = new ArrayList<>();
for (ApplicationMode mode : values) {
if (mode.isCustomProfile()) {
customModes.add(mode);
}
}
return customModes;
}
// returns modifiable ! Set<ApplicationMode> to exclude non-wanted derived // returns modifiable ! Set<ApplicationMode> to exclude non-wanted derived
public static Set<ApplicationMode> regWidgetVisibility(String widgetId, ApplicationMode... am) { public static Set<ApplicationMode> regWidgetVisibility(String widgetId, ApplicationMode... am) {
HashSet<ApplicationMode> set = new HashSet<>(); HashSet<ApplicationMode> set = new HashSet<>();
@ -607,6 +622,7 @@ public class ApplicationMode {
public static void onApplicationStart(OsmandApplication app) { public static void onApplicationStart(OsmandApplication app) {
// load for default profiles to initialize later custom modes // load for default profiles to initialize later custom modes
initDefaultModesUpdates(app);
initDefaultSpeed(app); initDefaultSpeed(app);
initCustomModes(app); initCustomModes(app);
initDefaultSpeed(app); initDefaultSpeed(app);
@ -640,25 +656,60 @@ public class ApplicationMode {
} }
private static void initDefaultModesUpdates(OsmandApplication app) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Type t = new TypeToken<ArrayList<ApplicationModeBean>>() {}.getType();
List<ApplicationModeBean> defaultAppModeBeans = gson.fromJson(app.getSettings().DEFAULT_APP_PROFILES.get(), t);
if (!Algorithms.isEmpty(defaultAppModeBeans)) {
for (ApplicationModeBean modeBean : defaultAppModeBeans) {
ApplicationMode applicationMode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null);
if (applicationMode != null) {
if (!applicationMode.routingProfile.equals(modeBean.routingProfile)) {
applicationMode.routingProfile = modeBean.routingProfile;
}
if (!applicationMode.routeService.equals(modeBean.routeService)) {
applicationMode.routeService = modeBean.routeService;
}
}
}
}
}
private static void saveCustomModeToSettings(OsmandSettings settings) { private static void saveCustomModeToSettings(OsmandSettings settings) {
List<ApplicationModeBean> customModes = new ArrayList<>(); List<ApplicationModeBean> customModes = createApplicationModeBeans(getCustomValues());
for (ApplicationMode mode : values) { saveModesToSettings(settings, customModes, true);
if (mode.parentAppMode != null) { }
public static void saveDefaultModeToSettings(OsmandSettings settings) {
List<ApplicationModeBean> defaultModes = createApplicationModeBeans(defaultValues);
saveModesToSettings(settings, defaultModes, false);
}
private static List<ApplicationModeBean> createApplicationModeBeans(List<ApplicationMode> applicationModes) {
List<ApplicationModeBean> modeBeans = new ArrayList<>();
for (ApplicationMode mode : applicationModes) {
ApplicationModeBean mb = new ApplicationModeBean(); ApplicationModeBean mb = new ApplicationModeBean();
mb.userProfileName = mode.userProfileName; mb.userProfileName = mode.userProfileName;
mb.iconColor = mode.iconColor; mb.iconColor = mode.iconColor;
mb.iconName = mode.iconResName; mb.iconName = mode.iconResName;
mb.parent = mode.parentAppMode.getStringKey(); mb.parent = mode.parentAppMode != null ? mode.parentAppMode.getStringKey() : null;
mb.stringKey = mode.stringKey; mb.stringKey = mode.stringKey;
mb.routeService = mode.routeService; mb.routeService = mode.routeService;
mb.routingProfile = mode.routingProfile; mb.routingProfile = mode.routingProfile;
customModes.add(mb); modeBeans.add(mb);
} }
return modeBeans;
} }
private static void saveModesToSettings(OsmandSettings settings, List<ApplicationModeBean> modeBeans, boolean customModes) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String profiles = gson.toJson(customModes); String profiles = gson.toJson(modeBeans);
if (customModes) {
settings.CUSTOM_APP_PROFILES.set(profiles); settings.CUSTOM_APP_PROFILES.set(profiles);
} else {
settings.DEFAULT_APP_PROFILES.set(profiles);
}
} }
public static ApplicationMode saveCustomProfile(ApplicationModeBuilder builder, OsmandApplication app) { public static ApplicationMode saveCustomProfile(ApplicationModeBuilder builder, OsmandApplication app) {

View file

@ -2938,6 +2938,9 @@ public class OsmandSettings {
RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values()) RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values())
.makeGlobal(); .makeGlobal();
public final CommonPreference<String> DEFAULT_APP_PROFILES =
new StringPreference("default_app_profiles", "").makeGlobal().cache();
public final CommonPreference<String> CUSTOM_APP_PROFILES = public final CommonPreference<String> CUSTOM_APP_PROFILES =
new StringPreference("custom_app_profiles", "").makeGlobal().cache(); new StringPreference("custom_app_profiles", "").makeGlobal().cache();

View file

@ -81,9 +81,7 @@ public abstract class AppModesBottomSheetDialogFragment<T extends AbstractProfil
public void onProfilePressed(ApplicationMode item) { public void onProfilePressed(ApplicationMode item) {
Intent intent = new Intent(getActivity(), EditProfileActivity.class); Intent intent = new Intent(getActivity(), EditProfileActivity.class);
intent.putExtra(PROFILE_STRING_KEY, item.getStringKey()); intent.putExtra(PROFILE_STRING_KEY, item.getStringKey());
if (item.isCustomProfile()) { intent.putExtra(IS_USER_PROFILE, item.isCustomProfile());
intent.putExtra(IS_USER_PROFILE, true);
}
startActivity(intent); startActivity(intent);
} }

View file

@ -285,20 +285,20 @@ public class EditProfileFragment extends BaseOsmAndFragment {
selectNavTypeBtn.setOnClickListener(new View.OnClickListener() { selectNavTypeBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (isNew || isUserProfile) { FragmentActivity activity = getActivity();
if (activity != null) {
hideKeyboard(); hideKeyboard();
final SelectProfileBottomSheetDialogFragment fragment = new SelectProfileBottomSheetDialogFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
if (profile.routingProfileDataObject != null) { if (profile.routingProfileDataObject != null) {
bundle.putString(SELECTED_KEY, bundle.putString(SELECTED_KEY, profile.routingProfileDataObject.getStringKey());
profile.routingProfileDataObject.getStringKey());
} }
bundle.putString(DIALOG_TYPE, TYPE_NAV_PROFILE); bundle.putString(DIALOG_TYPE, TYPE_NAV_PROFILE);
SelectProfileBottomSheetDialogFragment fragment = new SelectProfileBottomSheetDialogFragment();
fragment.setArguments(bundle); fragment.setArguments(bundle);
if (getActivity() != null) { activity.getSupportFragmentManager().beginTransaction()
getActivity().getSupportFragmentManager().beginTransaction()
.add(fragment, "select_nav_type").commitAllowingStateLoss(); .add(fragment, "select_nav_type").commitAllowingStateLoss();
}
navTypeEt.setCursorVisible(false); navTypeEt.setCursorVisible(false);
navTypeEt.setTextIsSelectable(false); navTypeEt.setTextIsSelectable(false);
navTypeEt.clearFocus(); navTypeEt.clearFocus();
@ -403,16 +403,13 @@ public class EditProfileFragment extends BaseOsmAndFragment {
} }
}); });
if (!isNew && !isUserProfile) {
saveButtonSV.setEnabled(false);
saveButton.setEnabled(false);
} else {
saveButton.setOnClickListener(new OnClickListener() { saveButton.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (saveNewProfile()) { FragmentActivity activity = getActivity();
if (activity != null && saveNewProfile()) {
activateMode(mode); activateMode(mode);
getActivity().onBackPressed(); activity.onBackPressed();
} }
} }
}); });
@ -420,13 +417,13 @@ public class EditProfileFragment extends BaseOsmAndFragment {
saveButtonSV.setOnClickListener(new OnClickListener() { saveButtonSV.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (saveNewProfile()) { FragmentActivity activity = getActivity();
if (activity != null && saveNewProfile()) {
activateMode(mode); activateMode(mode);
getActivity().onBackPressed(); activity.onBackPressed();
} }
} }
}); });
}
final float d = getResources().getDisplayMetrics().density; final float d = getResources().getDisplayMetrics().density;
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@ -519,7 +516,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
if (!isUserProfile && !isNew) { if (!isUserProfile && !isNew) {
profileNameEt.setFocusable(false); profileNameEt.setFocusable(false);
navTypeEt.setFocusable(false);
} }
if (isNew) { if (isNew) {
@ -659,22 +655,34 @@ public class EditProfileFragment extends BaseOsmAndFragment {
profile.parent.getStringKey() + "_" + System.currentTimeMillis(); profile.parent.getStringKey() + "_" + System.currentTimeMillis();
} }
ApplicationMode.ApplicationModeBuilder builder = ApplicationMode boolean customMode = isNew || isUserProfile;
.createCustomMode(profile.parent, profile.userProfileTitle.trim(), customStringKey)
.icon(app, profile.iconStringName); ApplicationMode.ApplicationModeBuilder builder;
if (customMode) {
builder = ApplicationMode.createCustomMode(profile.parent, profile.userProfileTitle.trim(), customStringKey);
} else {
builder = ApplicationMode.changeBaseMode(mode);
}
builder.icon(app, profile.iconStringName);
if(profile.routingProfileDataObject.getStringKey().equals( if(profile.routingProfileDataObject.getStringKey().equals(
RoutingProfilesResources.STRAIGHT_LINE_MODE.name())) { RoutingProfilesResources.STRAIGHT_LINE_MODE.name())) {
builder.setRoutingProfile(RoutingProfilesResources.STRAIGHT_LINE_MODE.name());
builder.setRouteService(RouteService.STRAIGHT); builder.setRouteService(RouteService.STRAIGHT);
} else if(profile.routingProfileDataObject.getStringKey().equals( } else if(profile.routingProfileDataObject.getStringKey().equals(
RoutingProfilesResources.BROUTER_MODE.name())) { RoutingProfilesResources.BROUTER_MODE.name())) {
builder.setRoutingProfile(RoutingProfilesResources.BROUTER_MODE.name());
builder.setRouteService(RouteService.BROUTER); builder.setRouteService(RouteService.BROUTER);
} else if (profile.routingProfileDataObject != null) { } else if (profile.routingProfileDataObject != null) {
builder.setRoutingProfile(profile.routingProfileDataObject.getStringKey()); builder.setRoutingProfile(profile.routingProfileDataObject.getStringKey());
builder.setRouteService(RouteService.OSMAND);
} }
builder.setColor(profile.iconColor); builder.setColor(profile.iconColor);
if (customMode) {
mode = ApplicationMode.saveCustomProfile(builder, getMyApplication()); mode = ApplicationMode.saveCustomProfile(builder, getMyApplication());
} else {
ApplicationMode.saveDefaultModeToSettings(getSettings());
}
if (!ApplicationMode.values(app).contains(mode)) { if (!ApplicationMode.values(app).contains(mode)) {
ApplicationMode.changeProfileAvailability(mode, true, getMyApplication()); ApplicationMode.changeProfileAvailability(mode, true, getMyApplication());
} }

View file

@ -190,9 +190,7 @@ public class SettingsProfileFragment extends BaseOsmAndFragment
if (activity instanceof SettingsProfileActivity) { if (activity instanceof SettingsProfileActivity) {
Intent intent = new Intent(getActivity(), EditProfileActivity.class); Intent intent = new Intent(getActivity(), EditProfileActivity.class);
intent.putExtra(PROFILE_STRING_KEY, item.getStringKey()); intent.putExtra(PROFILE_STRING_KEY, item.getStringKey());
if (item.isCustomProfile()) { intent.putExtra(IS_USER_PROFILE, item.isCustomProfile());
intent.putExtra(IS_USER_PROFILE, true);
}
activity.startActivity(intent); activity.startActivity(intent);
} else { } else {
FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentManager fragmentManager = activity.getSupportFragmentManager();