Add override profile prefs

This commit is contained in:
Vitaliy 2020-04-24 17:42:48 +03:00
parent f8aca54aff
commit f03b0ae890
6 changed files with 84 additions and 47 deletions

View file

@ -1,6 +1,7 @@
package net.osmand.plus;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
@ -318,7 +319,7 @@ public class CustomOsmandPlugin extends OsmandPlugin {
JSONArray regionsJson = json.optJSONArray("regionsJson");
if (regionsJson != null) {
customRegions.addAll(collectRegionsFromJson(regionsJson));
customRegions.addAll(collectRegionsFromJson(app, regionsJson));
}
}
@ -365,12 +366,12 @@ public class CustomOsmandPlugin extends OsmandPlugin {
json.put("pluginResDir", resourceDirName);
}
public static List<CustomRegion> collectRegionsFromJson(JSONArray jsonArray) throws JSONException {
public static List<CustomRegion> collectRegionsFromJson(@NonNull Context ctx, JSONArray jsonArray) throws JSONException {
List<CustomRegion> customRegions = new ArrayList<>();
Map<String, CustomRegion> flatRegions = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject regionJson = jsonArray.getJSONObject(i);
CustomRegion region = CustomRegion.fromJson(regionJson);
CustomRegion region = CustomRegion.fromJson(ctx, regionJson);
flatRegions.put(region.getPath(), region);
}
for (CustomRegion region : flatRegions.values()) {

View file

@ -1,6 +1,9 @@
package net.osmand.plus;
import android.content.Context;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.JsonUtils;
@ -74,7 +77,7 @@ public class CustomRegion extends WorldRegion {
return descriptionInfo;
}
public static CustomRegion fromJson(JSONObject object) throws JSONException {
public static CustomRegion fromJson(@NonNull Context ctx, JSONObject object) throws JSONException {
String scopeId = object.optString("scope-id", null);
String path = object.optString("path", null);
String type = object.optString("type", null);
@ -90,9 +93,9 @@ public class CustomRegion extends WorldRegion {
region.names = JsonUtils.getLocalizedMapFromJson("name", object);
if (!Algorithms.isEmpty(region.names)) {
region.regionName = region.names.get("");
region.regionNameEn = region.names.get("");
region.regionNameEn = region.names.get("en");
region.regionFullName = region.names.get("");
region.regionNameLocale = region.names.get("");
region.regionNameLocale = JsonUtils.getLocalizedResFromMap(ctx, region.names, region.regionName);
}
region.icons = JsonUtils.getLocalizedMapFromJson("icon", object);

View file

@ -17,6 +17,7 @@ import androidx.annotation.Nullable;
import net.osmand.AndroidUtils;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.JsonUtils;
import net.osmand.PlatformUtil;
import net.osmand.aidl.ConnectedApp;
import net.osmand.data.LocationPoint;
@ -253,7 +254,15 @@ public class OsmAndAppCustomization {
@Nullable
public String getNavDrawerLogoUrl() {
return app.getSettings().NAV_DRAWER_URL.get();
String url = app.getSettings().NAV_DRAWER_URL.get();
try {
JSONObject json = new JSONObject(url);
Map<String, String> localizedMap = JsonUtils.getLocalizedMapFromJson(json);
url = JsonUtils.getLocalizedResFromMap(app, localizedMap, url);
} catch (JSONException e) {
LOG.error(e);
}
return url;
}
public boolean setNavDrawerLogo(String uri, @Nullable String packageName, @Nullable String intent) {

View file

@ -603,7 +603,7 @@ public class SettingsHelper {
return;
}
JSONArray jsonArray = json.getJSONArray("items");
items.addAll(CustomOsmandPlugin.collectRegionsFromJson(jsonArray));
items.addAll(CustomOsmandPlugin.collectRegionsFromJson(app, jsonArray));
} catch (JSONException e) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
throw new IllegalArgumentException("Json parse error", e);
@ -784,6 +784,10 @@ public class SettingsHelper {
} catch (JSONException e) {
throw new IllegalArgumentException("Json parse error", e);
}
readPreferencesFromJson(json);
}
void readPreferencesFromJson(final JSONObject json) {
settings.getContext().runInUIThread(new Runnable() {
@Override
public void run() {
@ -900,13 +904,13 @@ public class SettingsHelper {
private ApplicationMode appMode;
private ApplicationModeBuilder builder;
private ApplicationModeBean modeBean;
private JSONObject additionalPrefsJson;
private Set<String> appModeBeanPrefsIds;
private Map<String, String> drawerLogoParams;
public ProfileSettingsItem(@NonNull OsmandApplication app, @NonNull ApplicationMode appMode) {
super(app.getSettings());
this.appMode = appMode;
appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds));
}
public ProfileSettingsItem(@NonNull OsmandApplication app, @NonNull ApplicationModeBean modeBean) {
@ -914,18 +918,16 @@ public class SettingsHelper {
this.modeBean = modeBean;
builder = ApplicationMode.fromModeBean(app, modeBean);
appMode = builder.getApplicationMode();
appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds));
}
public ProfileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.PROFILE, app.getSettings(), json);
appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds));
}
@Override
protected void init() {
super.init();
drawerLogoParams = new HashMap<>();
appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds));
}
@NonNull
@ -981,24 +983,7 @@ public class SettingsHelper {
@Override
void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
JSONObject prefsJson = json.optJSONObject("prefs");
try {
JSONObject drawerLogoJson = prefsJson.optJSONObject("drawer_logo");
if (drawerLogoJson != null) {
for (Iterator<String> it = drawerLogoJson.keys(); it.hasNext(); ) {
String localeKey = it.next();
String name = drawerLogoJson.getString(localeKey);
drawerLogoParams.put(localeKey, name);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@NonNull
public Map<String, String> getDrawerLogoParams() {
return drawerLogoParams;
additionalPrefsJson = json.optJSONObject("prefs");
}
@Override
@ -1063,6 +1048,59 @@ public class SettingsHelper {
ApplicationMode.changeProfileAvailability(appMode, true, app);
}
public void applyAdditionalPrefs() {
if (additionalPrefsJson != null) {
updatePluginResPrefs();
SettingsItemReader reader = getReader();
if (reader instanceof OsmandSettingsItemReader) {
((OsmandSettingsItemReader) reader).readPreferencesFromJson(additionalPrefsJson);
}
}
}
private void updatePluginResPrefs() {
String pluginId = getPluginId();
if (Algorithms.isEmpty(pluginId)) {
return;
}
OsmandPlugin plugin = OsmandPlugin.getPlugin(pluginId);
if (plugin instanceof CustomOsmandPlugin) {
CustomOsmandPlugin customPlugin = (CustomOsmandPlugin) plugin;
String resDirPath = IndexConstants.PLUGINS_DIR + pluginId + "/" + customPlugin.getResourceDirName();
for (Iterator<String> it = additionalPrefsJson.keys(); it.hasNext(); ) {
try {
String prefId = it.next();
Object value = additionalPrefsJson.get(prefId);
if (value instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) value;
for (Iterator<String> iterator = jsonObject.keys(); iterator.hasNext(); ) {
String key = iterator.next();
Object val = jsonObject.get(key);
if (val instanceof String) {
val = checkPluginResPath((String) val, resDirPath);
}
jsonObject.put(key, val);
}
} else if (value instanceof String) {
value = checkPluginResPath((String) value, resDirPath);
additionalPrefsJson.put(prefId, value);
}
} catch (JSONException e) {
LOG.error(e);
}
}
}
}
private String checkPluginResPath(String path, String resDirPath) {
if (path.startsWith("@")) {
return resDirPath + "/" + path.substring(1);
}
return path;
}
@Override
void writeToJson(@NonNull JSONObject json) throws JSONException {
super.writeToJson(json);

View file

@ -50,6 +50,7 @@ public class CustomIndexItem extends IndexItem {
DownloadEntry entry = super.createDownloadEntry(ctx);
if (entry != null) {
entry.urlToDownload = downloadUrl;
entry.zipStream = fileName.endsWith(".zip");
}
return entry;
}

View file

@ -64,7 +64,6 @@ import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -80,7 +79,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipInputStream;
import static android.app.Activity.RESULT_OK;
@ -850,20 +848,7 @@ public class ImportHelper {
for (SettingsItem item : items) {
if (item instanceof ProfileSettingsItem) {
ProfileSettingsItem profileItem = (ProfileSettingsItem) item;
Map<String, String> drawerLogoNames = profileItem.getDrawerLogoParams();
if (!Algorithms.isEmpty(drawerLogoNames)) {
String pluginResDir = IndexConstants.PLUGINS_DIR + plugin.getId() + "/" + plugin.getPluginResDir().getName();
for (Map.Entry<String, String> entry : drawerLogoNames.entrySet()) {
String value = entry.getValue();
if (value.startsWith("@") || value.startsWith("/")) {
value = value.substring(1);
}
entry.setValue(pluginResDir + "/" + value);
}
String json = new JSONObject(drawerLogoNames).toString();
app.getSettings().NAV_DRAWER_LOGO.setModeValue(profileItem.getAppMode(), json);
}
((ProfileSettingsItem) item).applyAdditionalPrefs();
}
}
if (activity != null) {