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) {
if (!Algorithms.isEmpty(iconResName)) {
app.getSettings().ICON_RES_NAME.setModeValue(this, iconResName);
}
}
private void updateAppModeIcon() {
String iconResName = app.getSettings().ICON_RES_NAME.getModeValue(this);
@ -420,48 +422,60 @@ public class ApplicationMode {
}
public void setUserProfileName(String userProfileName) {
if (!Algorithms.isEmpty(userProfileName)) {
app.getSettings().USER_PROFILE_NAME.setModeValue(this, userProfileName);
}
}
public String getRoutingProfile() {
return app.getSettings().ROUTING_PROFILE.getModeValue(this);
}
public void setRoutingProfile(String routingProfile) {
if (!Algorithms.isEmpty(routingProfile)) {
app.getSettings().ROUTING_PROFILE.setModeValue(this, routingProfile);
}
}
public RouteService getRouteService() {
return app.getSettings().ROUTE_SERVICE.getModeValue(this);
}
public void setRouteService(RouteService routeService) {
if (routeService != null) {
app.getSettings().ROUTE_SERVICE.setModeValue(this, routeService);
}
}
public NavigationIcon getNavigationIcon() {
return app.getSettings().NAVIGATION_ICON.getModeValue(this);
}
public void setNavigationIcon(NavigationIcon navigationIcon) {
if (navigationIcon != null) {
app.getSettings().NAVIGATION_ICON.setModeValue(this, navigationIcon);
}
}
public LocationIcon getLocationIcon() {
return app.getSettings().LOCATION_ICON.getModeValue(this);
}
public void setLocationIcon(LocationIcon locationIcon) {
if (locationIcon != null) {
app.getSettings().LOCATION_ICON.setModeValue(this, locationIcon);
}
}
public ProfileIconColors getIconColorInfo() {
return app.getSettings().ICON_COLOR.getModeValue(this);
}
public void setIconColor(ProfileIconColors iconColor) {
if (iconColor != null) {
app.getSettings().ICON_COLOR.setModeValue(this, iconColor);
}
}
public int getOrder() {
return app.getSettings().APP_MODE_ORDER.getModeValue(this);
@ -556,6 +570,7 @@ public class ApplicationMode {
mode = builder.customReg(app);
initRegVisibility();
}
reorderAppModes();
saveCustomAppModesToSettings(app);
return mode;
}
@ -566,7 +581,7 @@ public class ApplicationMode {
}
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.setIconResName(modeBean.iconName);
builder.setIconColor(modeBean.iconColor);
@ -659,9 +674,9 @@ public class ApplicationMode {
private String routingProfile;
private String iconResName;
private ProfileIconColors iconColor;
private Integer order;
private LocationIcon locationIcon;
private NavigationIcon navigationIcon;
private int order = -1;
public ApplicationMode getApplicationMode() {
return applicationMode;
@ -677,34 +692,21 @@ public class ApplicationMode {
values.add(applicationMode);
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);
}
if (routeService != null) {
applicationMode.setRouteService(routeService);
}
if (routingProfile != null) {
applicationMode.setRoutingProfile(routingProfile);
}
if (iconResName != null) {
applicationMode.setIconResName(iconResName);
}
if (iconColor != null) {
applicationMode.setIconColor(iconColor);
}
if (order != null) {
applicationMode.setOrder(order);
}
if (locationIcon != null) {
applicationMode.setLocationIcon(locationIcon);
}
if (navigationIcon != null) {
applicationMode.setNavigationIcon(navigationIcon);
}
applicationMode.setOrder(order != -1 ? order : values.size());
return applicationMode;
}
@ -801,6 +803,6 @@ public class ApplicationMode {
@Expose
NavigationIcon navIcon = null;
@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.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.StateChangedListener;
import net.osmand.ValueHolder;
@ -30,6 +34,7 @@ import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.ApplicationMode.ApplicationModeBean;
import net.osmand.plus.access.AccessibilityMode;
import net.osmand.plus.access.RelativeDirectionStyle;
import net.osmand.plus.api.SettingsAPI;
@ -54,6 +59,7 @@ import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
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() {