Fix #10346 additional refactoring

This commit is contained in:
nazar-kutz 2020-12-18 11:17:11 +02:00
parent e3c15e217e
commit 1bb3fb730c
6 changed files with 111 additions and 124 deletions

View file

@ -121,7 +121,6 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_S
import static net.osmand.plus.ContextMenuAdapter.PROFILES_CHOSEN_PROFILE_TAG; import static net.osmand.plus.ContextMenuAdapter.PROFILES_CHOSEN_PROFILE_TAG;
import static net.osmand.plus.ContextMenuAdapter.PROFILES_CONTROL_BUTTON_TAG; import static net.osmand.plus.ContextMenuAdapter.PROFILES_CONTROL_BUTTON_TAG;
import static net.osmand.plus.ContextMenuAdapter.PROFILES_NORMAL_PROFILE_TAG; import static net.osmand.plus.ContextMenuAdapter.PROFILES_NORMAL_PROFILE_TAG;
import static net.osmand.plus.settings.fragments.NavigationFragment.getRoutingProfiles;
public class MapActivityActions implements DialogProvider { public class MapActivityActions implements DialogProvider {
@ -737,7 +736,7 @@ public class MapActivityActions implements DialogProvider {
String modeDescription; String modeDescription;
Map<String, RoutingProfileDataObject> profilesObjects = getRoutingProfiles(app); Map<String, RoutingProfileDataObject> profilesObjects = RoutingProfileDataObject.getRoutingProfiles(app);
for (final ApplicationMode appMode : activeModes) { for (final ApplicationMode appMode : activeModes) {
if (appMode.isCustomProfile()) { if (appMode.isCustomProfile()) {
modeDescription = getProfileDescription(app, appMode, profilesObjects, getString(R.string.profile_type_user_string)); modeDescription = getProfileDescription(app, appMode, profilesObjects, getString(R.string.profile_type_user_string));
@ -1046,7 +1045,7 @@ public class MapActivityActions implements DialogProvider {
//switch profile button //switch profile button
ApplicationMode currentMode = app.getSettings().APPLICATION_MODE.get(); ApplicationMode currentMode = app.getSettings().APPLICATION_MODE.get();
String modeDescription; String modeDescription;
Map<String, RoutingProfileDataObject> profilesObjects = getRoutingProfiles(app); Map<String, RoutingProfileDataObject> profilesObjects = RoutingProfileDataObject.getRoutingProfiles(app);
if (currentMode.isCustomProfile()) { if (currentMode.isCustomProfile()) {
modeDescription = getProfileDescription(app, currentMode, profilesObjects, getString(R.string.profile_type_user_string)); modeDescription = getProfileDescription(app, currentMode, profilesObjects, getString(R.string.profile_type_user_string));
} else { } else {

View file

@ -1,12 +1,22 @@
package net.osmand.plus.profiles; package net.osmand.plus.profiles;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class RoutingProfileDataObject extends ProfileDataObject { public class RoutingProfileDataObject extends ProfileDataObject {
public static final String OSMAND_NAVIGATION = "osmand_navigation";
private String fileName; private String fileName;
@ -59,4 +69,91 @@ public class RoutingProfileDataObject extends ProfileDataObject {
return rpValues.contains(value); return rpValues.contains(value);
} }
} }
public static List<RoutingProfileDataObject> getSortedRoutingProfiles(OsmandApplication app) {
List<RoutingProfileDataObject> result = new ArrayList<>();
Map<String, List<RoutingProfileDataObject>> routingProfilesByFileNames = getRoutingProfilesByFileNames(app);
List<String> fileNames = new ArrayList<>(routingProfilesByFileNames.keySet());
Collections.sort(fileNames, new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return s.equals(OSMAND_NAVIGATION) ? -1 : t1.equals(OSMAND_NAVIGATION) ? 1 : s.compareToIgnoreCase(t1);
}
});
for (String fileName : fileNames) {
List<RoutingProfileDataObject> routingProfilesFromFile = routingProfilesByFileNames.get(fileName);
if (routingProfilesFromFile != null) {
Collections.sort(routingProfilesFromFile);
result.addAll(routingProfilesFromFile);
}
}
return result;
}
public static Map<String, List<RoutingProfileDataObject>> getRoutingProfilesByFileNames(OsmandApplication app) {
Map<String, List<RoutingProfileDataObject>> result = new HashMap<>();
for (final RoutingProfileDataObject profile : getRoutingProfiles(app).values()) {
String fileName = profile.getFileName() != null ? profile.getFileName() : OSMAND_NAVIGATION;
if (result.containsKey(fileName)) {
result.get(fileName).add(profile);
} else {
result.put(fileName, new ArrayList<RoutingProfileDataObject>() {
{ add(profile); }
});
}
}
return result;
}
public static Map<String, RoutingProfileDataObject> getRoutingProfiles(OsmandApplication context) {
Map<String, RoutingProfileDataObject> profilesObjects = new HashMap<>();
profilesObjects.put(RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.STRAIGHT_LINE_MODE.name(),
context.getString(RoutingProfilesResources.STRAIGHT_LINE_MODE.getStringRes()),
context.getString(R.string.special_routing_type),
RoutingProfilesResources.STRAIGHT_LINE_MODE.getIconRes(),
false, null));
profilesObjects.put(RoutingProfilesResources.DIRECT_TO_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.DIRECT_TO_MODE.name(),
context.getString(RoutingProfilesResources.DIRECT_TO_MODE.getStringRes()),
context.getString(R.string.special_routing_type),
RoutingProfilesResources.DIRECT_TO_MODE.getIconRes(),
false, null));
if (context.getBRouterService() != null) {
profilesObjects.put(RoutingProfilesResources.BROUTER_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.BROUTER_MODE.name(),
context.getString(RoutingProfilesResources.BROUTER_MODE.getStringRes()),
context.getString(R.string.third_party_routing_type),
RoutingProfilesResources.BROUTER_MODE.getIconRes(),
false, null));
}
List<String> disabledRouterNames = OsmandPlugin.getDisabledRouterNames();
for (RoutingConfiguration.Builder builder : context.getAllRoutingConfigs()) {
collectRoutingProfilesFromConfig(context, builder, profilesObjects, disabledRouterNames);
}
return profilesObjects;
}
private static void collectRoutingProfilesFromConfig(OsmandApplication app, RoutingConfiguration.Builder builder,
Map<String, RoutingProfileDataObject> profilesObjects, List<String> disabledRouterNames) {
for (Map.Entry<String, GeneralRouter> entry : builder.getAllRouters().entrySet()) {
String routerKey = entry.getKey();
GeneralRouter router = entry.getValue();
if (!routerKey.equals("geocoding") && !disabledRouterNames.contains(router.getFilename())) {
int iconRes = R.drawable.ic_action_gdirections_dark;
String name = router.getProfileName();
String description = app.getString(R.string.osmand_default_routing);
String fileName = router.getFilename();
if (!Algorithms.isEmpty(fileName)) {
description = fileName;
} else if (RoutingProfilesResources.isRpValue(name.toUpperCase())) {
iconRes = RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes();
name = app.getString(RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes());
}
profilesObjects.put(routerKey, new RoutingProfileDataObject(routerKey, name, description,
iconRes, false, fileName));
}
}
}
} }

View file

@ -19,7 +19,7 @@ import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.settings.fragments.NavigationFragment; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet;
import java.util.ArrayList; import java.util.ArrayList;
@ -54,7 +54,8 @@ public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet
private void refreshProfiles(OsmandApplication app) { private void refreshProfiles(OsmandApplication app) {
profiles.clear(); profiles.clear();
profiles.addAll(NavigationFragment.getBaseProfiles(app, true)); List<ApplicationMode> appModes = ApplicationMode.allPossibleValues();
profiles.addAll(ProfileDataObject.getDataObjects(app, appModes));
for (ProfileDataObject profile : profiles) { for (ProfileDataObject profile : profiles) {
String key = profile.getStringKey(); String key = profile.getStringKey();
profile.setSelected(selectedProfiles.contains(key)); profile.setSelected(selectedProfiles.contains(key));

View file

@ -38,7 +38,6 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.helpers.FontCache; import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.fragments.NavigationFragment;
import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet;
import net.osmand.router.RoutingConfiguration; import net.osmand.router.RoutingConfiguration;
@ -332,11 +331,12 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
profiles.clear(); profiles.clear();
switch (dialogMode) { switch (dialogMode) {
case BASE_PROFILE: case BASE_PROFILE:
profiles.addAll(NavigationFragment.getBaseProfiles(app)); List<ApplicationMode> appModes = ApplicationMode.values(app, false, ApplicationMode.DEFAULT);
profiles.addAll(ProfileDataObject.getDataObjects(app, appModes));
break; break;
case NAVIGATION_PROFILE: case NAVIGATION_PROFILE:
profiles.addAll(NavigationFragment.getSortedRoutingProfiles(app)); profiles.addAll(RoutingProfileDataObject.getSortedRoutingProfiles(app));
break; break;
case DEFAULT_PROFILE: case DEFAULT_PROFILE:

View file

@ -117,11 +117,9 @@ public class ApplicationMode {
.description(R.string.base_profile_descr_ski).reg(); .description(R.string.base_profile_descr_ski).reg();
public static List<ApplicationMode> values(OsmandApplication app, public static List<ApplicationMode> values(OsmandApplication app,
boolean onlyActiveValues, boolean onlyActive,
ApplicationMode ... exclude) { ApplicationMode ... exclude) {
List<ApplicationMode> appModes = List<ApplicationMode> appModes = new ArrayList<>(onlyActive ? values(app) : allPossibleValues());
new ArrayList<>(onlyActiveValues ? values(app) : allPossibleValues());
if (exclude != null && exclude.length > 0) { if (exclude != null && exclude.length > 0) {
for (ApplicationMode m : exclude) { for (ApplicationMode m : exclude) {
appModes.remove(m); appModes.remove(m);

View file

@ -13,37 +13,26 @@ import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet; import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet;
import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DialogMode; import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DialogMode;
import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.profiles.ProfileDataObject;
import net.osmand.plus.profiles.RoutingProfileDataObject; import net.osmand.plus.profiles.RoutingProfileDataObject;
import net.osmand.plus.profiles.RoutingProfileDataObject.RoutingProfilesResources; import net.osmand.plus.profiles.RoutingProfileDataObject.RoutingProfilesResources;
import net.osmand.plus.profiles.SelectProfileBottomSheet; import net.osmand.plus.profiles.SelectProfileBottomSheet;
import net.osmand.plus.profiles.SelectProfileBottomSheet.OnSelectProfileCallback;
import net.osmand.plus.routing.RouteProvider; import net.osmand.plus.routing.RouteProvider;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import static net.osmand.plus.profiles.SelectProfileBottomSheet.IS_PROFILE_IMPORTED_ARG; import static net.osmand.plus.profiles.SelectProfileBottomSheet.IS_PROFILE_IMPORTED_ARG;
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG; import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG;
import static net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DIALOG_MODE_KEY; import static net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DIALOG_MODE_KEY;
public class NavigationFragment extends BaseSettingsFragment public class NavigationFragment extends BaseSettingsFragment implements OnSelectProfileCallback {
implements SelectProfileBottomSheet.OnSelectProfileCallback {
public static final String TAG = NavigationFragment.class.getSimpleName(); public static final String TAG = NavigationFragment.class.getSimpleName();
public static final String NAVIGATION_TYPE = "navigation_type"; public static final String NAVIGATION_TYPE = "navigation_type";
public static final String OSMAND_NAVIGATION = "osmand_navigation";
private Map<String, RoutingProfileDataObject> routingProfileDataObjects; private Map<String, RoutingProfileDataObject> routingProfileDataObjects;
private Preference navigationType; private Preference navigationType;
@ -51,7 +40,7 @@ public class NavigationFragment extends BaseSettingsFragment
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
routingProfileDataObjects = getRoutingProfiles(app); routingProfileDataObjects = RoutingProfileDataObject.getRoutingProfiles(app);
setupOnBackPressedCallback(); setupOnBackPressedCallback();
} }
@ -169,103 +158,6 @@ public class NavigationFragment extends BaseSettingsFragment
appMode.setRoutingProfile(profileKey); appMode.setRoutingProfile(profileKey);
} }
public static List<RoutingProfileDataObject> getSortedRoutingProfiles(OsmandApplication app) {
List<RoutingProfileDataObject> result = new ArrayList<>();
Map<String, List<RoutingProfileDataObject>> routingProfilesByFileNames = getRoutingProfilesByFileNames(app);
List<String> fileNames = new ArrayList<>(routingProfilesByFileNames.keySet());
Collections.sort(fileNames, new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return s.equals(OSMAND_NAVIGATION) ? -1 : t1.equals(OSMAND_NAVIGATION) ? 1 : s.compareToIgnoreCase(t1);
}
});
for (String fileName : fileNames) {
List<RoutingProfileDataObject> routingProfilesFromFile = routingProfilesByFileNames.get(fileName);
if (routingProfilesFromFile != null) {
Collections.sort(routingProfilesFromFile);
result.addAll(routingProfilesFromFile);
}
}
return result;
}
public static Map<String, List<RoutingProfileDataObject>> getRoutingProfilesByFileNames(OsmandApplication app) {
Map<String, List<RoutingProfileDataObject>> result = new HashMap<>();
for (final RoutingProfileDataObject profile : getRoutingProfiles(app).values()) {
String fileName = profile.getFileName() != null ? profile.getFileName() : OSMAND_NAVIGATION;
if (result.containsKey(fileName)) {
result.get(fileName).add(profile);
} else {
result.put(fileName, new ArrayList<RoutingProfileDataObject>() {
{ add(profile); }
});
}
}
return result;
}
public static Map<String, RoutingProfileDataObject> getRoutingProfiles(OsmandApplication context) {
Map<String, RoutingProfileDataObject> profilesObjects = new HashMap<>();
profilesObjects.put(RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.STRAIGHT_LINE_MODE.name(),
context.getString(RoutingProfilesResources.STRAIGHT_LINE_MODE.getStringRes()),
context.getString(R.string.special_routing_type),
RoutingProfilesResources.STRAIGHT_LINE_MODE.getIconRes(),
false, null));
profilesObjects.put(RoutingProfilesResources.DIRECT_TO_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.DIRECT_TO_MODE.name(),
context.getString(RoutingProfilesResources.DIRECT_TO_MODE.getStringRes()),
context.getString(R.string.special_routing_type),
RoutingProfilesResources.DIRECT_TO_MODE.getIconRes(),
false, null));
if (context.getBRouterService() != null) {
profilesObjects.put(RoutingProfilesResources.BROUTER_MODE.name(), new RoutingProfileDataObject(
RoutingProfilesResources.BROUTER_MODE.name(),
context.getString(RoutingProfilesResources.BROUTER_MODE.getStringRes()),
context.getString(R.string.third_party_routing_type),
RoutingProfilesResources.BROUTER_MODE.getIconRes(),
false, null));
}
List<String> disabledRouterNames = OsmandPlugin.getDisabledRouterNames();
for (RoutingConfiguration.Builder builder : context.getAllRoutingConfigs()) {
collectRoutingProfilesFromConfig(context, builder, profilesObjects, disabledRouterNames);
}
return profilesObjects;
}
private static void collectRoutingProfilesFromConfig(OsmandApplication app, RoutingConfiguration.Builder builder,
Map<String, RoutingProfileDataObject> profilesObjects, List<String> disabledRouterNames) {
for (Map.Entry<String, GeneralRouter> entry : builder.getAllRouters().entrySet()) {
String routerKey = entry.getKey();
GeneralRouter router = entry.getValue();
if (!routerKey.equals("geocoding") && !disabledRouterNames.contains(router.getFilename())) {
int iconRes = R.drawable.ic_action_gdirections_dark;
String name = router.getProfileName();
String description = app.getString(R.string.osmand_default_routing);
String fileName = router.getFilename();
if (!Algorithms.isEmpty(fileName)) {
description = fileName;
} else if (RoutingProfilesResources.isRpValue(name.toUpperCase())) {
iconRes = RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes();
name = app.getString(RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes());
}
profilesObjects.put(routerKey, new RoutingProfileDataObject(routerKey, name, description,
iconRes, false, fileName));
}
}
}
public static List<ProfileDataObject> getBaseProfiles(OsmandApplication app) {
return getBaseProfiles(app, false);
}
public static List<ProfileDataObject> getBaseProfiles(OsmandApplication app, boolean includeBrowseMap) {
List<ApplicationMode> appModes =
ApplicationMode.values(app, false, includeBrowseMap ? null : ApplicationMode.DEFAULT);
return ProfileDataObject.getDataObjects(app, appModes);
}
private void setupVehicleParametersPref() { private void setupVehicleParametersPref() {
Preference vehicleParameters = findPreference("vehicle_parameters"); Preference vehicleParameters = findPreference("vehicle_parameters");
int iconRes = getSelectedAppMode().getIconRes(); int iconRes = getSelectedAppMode().getIconRes();
@ -282,7 +174,7 @@ public class NavigationFragment extends BaseSettingsFragment
@Override @Override
public void onProfileSelected(Bundle args) { public void onProfileSelected(Bundle args) {
if (args.getBoolean(IS_PROFILE_IMPORTED_ARG)) { if (args.getBoolean(IS_PROFILE_IMPORTED_ARG)) {
routingProfileDataObjects = getRoutingProfiles(app); routingProfileDataObjects = RoutingProfileDataObject.getRoutingProfiles(app);
} }
updateRoutingProfile(args.getString(PROFILE_KEY_ARG)); updateRoutingProfile(args.getString(PROFILE_KEY_ARG));
} }