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);
}
public static ApplicationModeBuilder changeBaseMode(ApplicationMode applicationMode) {
ApplicationModeBuilder builder = new ApplicationModeBuilder();
builder.applicationMode = applicationMode;
return builder;
}
public static List<ApplicationMode> values(OsmandApplication app) {
if (customizationListener == null) {
@ -392,6 +397,16 @@ public class ApplicationMode {
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
public static Set<ApplicationMode> regWidgetVisibility(String widgetId, ApplicationMode... am) {
HashSet<ApplicationMode> set = new HashSet<>();
@ -607,6 +622,7 @@ public class ApplicationMode {
public static void onApplicationStart(OsmandApplication app) {
// load for default profiles to initialize later custom modes
initDefaultModesUpdates(app);
initDefaultSpeed(app);
initCustomModes(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);
private static void saveCustomModeToSettings(OsmandSettings settings){
List<ApplicationModeBean> customModes = new ArrayList<>();
for (ApplicationMode mode : values) {
if (mode.parentAppMode != null) {
ApplicationModeBean mb = new ApplicationModeBean();
mb.userProfileName = mode.userProfileName;
mb.iconColor = mode.iconColor;
mb.iconName = mode.iconResName;
mb.parent = mode.parentAppMode.getStringKey();
mb.stringKey = mode.stringKey;
mb.routeService = mode.routeService;
mb.routingProfile = mode.routingProfile;
customModes.add(mb);
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) {
List<ApplicationModeBean> customModes = createApplicationModeBeans(getCustomValues());
saveModesToSettings(settings, customModes, true);
}
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();
mb.userProfileName = mode.userProfileName;
mb.iconColor = mode.iconColor;
mb.iconName = mode.iconResName;
mb.parent = mode.parentAppMode != null ? mode.parentAppMode.getStringKey() : null;
mb.stringKey = mode.stringKey;
mb.routeService = mode.routeService;
mb.routingProfile = mode.routingProfile;
modeBeans.add(mb);
}
return modeBeans;
}
private static void saveModesToSettings(OsmandSettings settings, List<ApplicationModeBean> modeBeans, boolean customModes) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String profiles = gson.toJson(customModes);
settings.CUSTOM_APP_PROFILES.set(profiles);
String profiles = gson.toJson(modeBeans);
if (customModes) {
settings.CUSTOM_APP_PROFILES.set(profiles);
} else {
settings.DEFAULT_APP_PROFILES.set(profiles);
}
}
public static ApplicationMode saveCustomProfile(ApplicationModeBuilder builder, OsmandApplication app) {

View file

@ -2938,6 +2938,9 @@ public class OsmandSettings {
RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values())
.makeGlobal();
public final CommonPreference<String> DEFAULT_APP_PROFILES =
new StringPreference("default_app_profiles", "").makeGlobal().cache();
public final CommonPreference<String> CUSTOM_APP_PROFILES =
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) {
Intent intent = new Intent(getActivity(), EditProfileActivity.class);
intent.putExtra(PROFILE_STRING_KEY, item.getStringKey());
if (item.isCustomProfile()) {
intent.putExtra(IS_USER_PROFILE, true);
}
intent.putExtra(IS_USER_PROFILE, item.isCustomProfile());
startActivity(intent);
}

View file

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

View file

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