Migrate custom app modes

This commit is contained in:
Vitaliy 2020-01-31 11:19:27 +02:00
parent 826eba624d
commit 2ada619bae
2 changed files with 58 additions and 37 deletions

View file

@ -350,8 +350,10 @@ public class ApplicationMode {
} }
public void setIconResName(String iconResName) { public void setIconResName(String iconResName) {
if (!Algorithms.isEmpty(iconResName)) {
app.getSettings().ICON_RES_NAME.setModeValue(this, iconResName); app.getSettings().ICON_RES_NAME.setModeValue(this, iconResName);
} }
}
private void updateAppModeIcon() { private void updateAppModeIcon() {
String iconResName = app.getSettings().ICON_RES_NAME.getModeValue(this); String iconResName = app.getSettings().ICON_RES_NAME.getModeValue(this);
@ -420,48 +422,60 @@ public class ApplicationMode {
} }
public void setUserProfileName(String userProfileName) { public void setUserProfileName(String userProfileName) {
if (!Algorithms.isEmpty(userProfileName)) {
app.getSettings().USER_PROFILE_NAME.setModeValue(this, userProfileName); app.getSettings().USER_PROFILE_NAME.setModeValue(this, userProfileName);
} }
}
public String getRoutingProfile() { public String getRoutingProfile() {
return app.getSettings().ROUTING_PROFILE.getModeValue(this); return app.getSettings().ROUTING_PROFILE.getModeValue(this);
} }
public void setRoutingProfile(String routingProfile) { public void setRoutingProfile(String routingProfile) {
if (!Algorithms.isEmpty(routingProfile)) {
app.getSettings().ROUTING_PROFILE.setModeValue(this, routingProfile); app.getSettings().ROUTING_PROFILE.setModeValue(this, routingProfile);
} }
}
public RouteService getRouteService() { public RouteService getRouteService() {
return app.getSettings().ROUTE_SERVICE.getModeValue(this); return app.getSettings().ROUTE_SERVICE.getModeValue(this);
} }
public void setRouteService(RouteService routeService) { public void setRouteService(RouteService routeService) {
if (routeService != null) {
app.getSettings().ROUTE_SERVICE.setModeValue(this, routeService); app.getSettings().ROUTE_SERVICE.setModeValue(this, routeService);
} }
}
public NavigationIcon getNavigationIcon() { public NavigationIcon getNavigationIcon() {
return app.getSettings().NAVIGATION_ICON.getModeValue(this); return app.getSettings().NAVIGATION_ICON.getModeValue(this);
} }
public void setNavigationIcon(NavigationIcon navigationIcon) { public void setNavigationIcon(NavigationIcon navigationIcon) {
if (navigationIcon != null) {
app.getSettings().NAVIGATION_ICON.setModeValue(this, navigationIcon); app.getSettings().NAVIGATION_ICON.setModeValue(this, navigationIcon);
} }
}
public LocationIcon getLocationIcon() { public LocationIcon getLocationIcon() {
return app.getSettings().LOCATION_ICON.getModeValue(this); return app.getSettings().LOCATION_ICON.getModeValue(this);
} }
public void setLocationIcon(LocationIcon locationIcon) { public void setLocationIcon(LocationIcon locationIcon) {
if (locationIcon != null) {
app.getSettings().LOCATION_ICON.setModeValue(this, locationIcon); app.getSettings().LOCATION_ICON.setModeValue(this, locationIcon);
} }
}
public ProfileIconColors getIconColorInfo() { public ProfileIconColors getIconColorInfo() {
return app.getSettings().ICON_COLOR.getModeValue(this); return app.getSettings().ICON_COLOR.getModeValue(this);
} }
public void setIconColor(ProfileIconColors iconColor) { public void setIconColor(ProfileIconColors iconColor) {
if (iconColor != null) {
app.getSettings().ICON_COLOR.setModeValue(this, iconColor); app.getSettings().ICON_COLOR.setModeValue(this, iconColor);
} }
}
public int getOrder() { public int getOrder() {
return app.getSettings().APP_MODE_ORDER.getModeValue(this); return app.getSettings().APP_MODE_ORDER.getModeValue(this);
@ -556,6 +570,7 @@ public class ApplicationMode {
mode = builder.customReg(app); mode = builder.customReg(app);
initRegVisibility(); initRegVisibility();
} }
reorderAppModes();
saveCustomAppModesToSettings(app); saveCustomAppModesToSettings(app);
return mode; return mode;
} }
@ -566,7 +581,7 @@ public class ApplicationMode {
} }
public static ApplicationModeBuilder fromModeBean(ApplicationModeBean modeBean) { public static ApplicationModeBuilder fromModeBean(ApplicationModeBean modeBean) {
ApplicationModeBuilder builder = createCustomMode(valueOfStringKey(modeBean.parent, null), modeBean.stringKey); ApplicationModeBuilder builder = createCustomMode(valueOfStringKey(modeBean.parent, CAR), modeBean.stringKey);
builder.setUserProfileName(modeBean.userProfileName); builder.setUserProfileName(modeBean.userProfileName);
builder.setIconResName(modeBean.iconName); builder.setIconResName(modeBean.iconName);
builder.setIconColor(modeBean.iconColor); builder.setIconColor(modeBean.iconColor);
@ -659,9 +674,9 @@ public class ApplicationMode {
private String routingProfile; private String routingProfile;
private String iconResName; private String iconResName;
private ProfileIconColors iconColor; private ProfileIconColors iconColor;
private Integer order;
private LocationIcon locationIcon; private LocationIcon locationIcon;
private NavigationIcon navigationIcon; private NavigationIcon navigationIcon;
private int order = -1;
public ApplicationMode getApplicationMode() { public ApplicationMode getApplicationMode() {
return applicationMode; return applicationMode;
@ -677,34 +692,21 @@ public class ApplicationMode {
values.add(applicationMode); values.add(applicationMode);
applicationMode.app = app; applicationMode.app = app;
applicationMode.minDistanceForTurn = applicationMode.parentAppMode.minDistanceForTurn;
applicationMode.arrivalDistance = applicationMode.parentAppMode.arrivalDistance;
applicationMode.offRouteDistance = applicationMode.parentAppMode.offRouteDistance;
if (userProfileName != null) { ApplicationMode parent = applicationMode.parentAppMode;
applicationMode.minDistanceForTurn = parent.minDistanceForTurn;
applicationMode.arrivalDistance = parent.arrivalDistance;
applicationMode.offRouteDistance = parent.offRouteDistance;
applicationMode.setParentAppMode(parent);
applicationMode.setUserProfileName(userProfileName); applicationMode.setUserProfileName(userProfileName);
}
if (routeService != null) {
applicationMode.setRouteService(routeService); applicationMode.setRouteService(routeService);
}
if (routingProfile != null) {
applicationMode.setRoutingProfile(routingProfile); applicationMode.setRoutingProfile(routingProfile);
}
if (iconResName != null) {
applicationMode.setIconResName(iconResName); applicationMode.setIconResName(iconResName);
}
if (iconColor != null) {
applicationMode.setIconColor(iconColor); applicationMode.setIconColor(iconColor);
}
if (order != null) {
applicationMode.setOrder(order);
}
if (locationIcon != null) {
applicationMode.setLocationIcon(locationIcon); applicationMode.setLocationIcon(locationIcon);
}
if (navigationIcon != null) {
applicationMode.setNavigationIcon(navigationIcon); applicationMode.setNavigationIcon(navigationIcon);
} applicationMode.setOrder(order != -1 ? order : values.size());
return applicationMode; return applicationMode;
} }
@ -801,6 +803,6 @@ public class ApplicationMode {
@Expose @Expose
NavigationIcon navIcon = null; NavigationIcon navIcon = null;
@Expose @Expose
int order; int order = -1;
} }
} }

View file

@ -19,6 +19,10 @@ import android.support.annotation.StringRes;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import android.support.v7.preference.PreferenceDataStore; import android.support.v7.preference.PreferenceDataStore;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.StateChangedListener; import net.osmand.StateChangedListener;
import net.osmand.ValueHolder; import net.osmand.ValueHolder;
@ -30,6 +34,7 @@ import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager; import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.ApplicationMode.ApplicationModeBean;
import net.osmand.plus.access.AccessibilityMode; import net.osmand.plus.access.AccessibilityMode;
import net.osmand.plus.access.RelativeDirectionStyle; import net.osmand.plus.access.RelativeDirectionStyle;
import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.api.SettingsAPI;
@ -54,6 +59,7 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -277,6 +283,19 @@ public class OsmandSettings {
} }
} }
} }
String json = settingsAPI.getString(globalPreferences, "custom_app_profiles", "");
if (!Algorithms.isEmpty(json)) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Type t = new TypeToken<ArrayList<ApplicationModeBean>>() {
}.getType();
List<ApplicationModeBean> customProfiles = gson.fromJson(json, t);
if (!Algorithms.isEmpty(customProfiles)) {
for (ApplicationModeBean modeBean : customProfiles) {
ApplicationMode.saveProfile(ApplicationMode.fromModeBean(modeBean), ctx);
}
}
}
} }
void migrateHomeWorkParkingToFavorites() { void migrateHomeWorkParkingToFavorites() {