From f03b0ae890ba01ec6e2b74a2cdc32103294e24eb Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 24 Apr 2020 17:42:48 +0300 Subject: [PATCH] Add override profile prefs --- .../net/osmand/plus/CustomOsmandPlugin.java | 7 +- OsmAnd/src/net/osmand/plus/CustomRegion.java | 9 +- .../osmand/plus/OsmAndAppCustomization.java | 11 ++- .../src/net/osmand/plus/SettingsHelper.java | 86 +++++++++++++------ .../osmand/plus/download/CustomIndexItem.java | 1 + .../net/osmand/plus/helpers/ImportHelper.java | 17 +--- 6 files changed, 84 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index 69fc52c809..9b25c4accc 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -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 collectRegionsFromJson(JSONArray jsonArray) throws JSONException { + public static List collectRegionsFromJson(@NonNull Context ctx, JSONArray jsonArray) throws JSONException { List customRegions = new ArrayList<>(); Map 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()) { diff --git a/OsmAnd/src/net/osmand/plus/CustomRegion.java b/OsmAnd/src/net/osmand/plus/CustomRegion.java index 6280c0d866..fc433a7a5e 100644 --- a/OsmAnd/src/net/osmand/plus/CustomRegion.java +++ b/OsmAnd/src/net/osmand/plus/CustomRegion.java @@ -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); diff --git a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java index d91ec51e8f..69015288be 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java @@ -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 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) { diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 1e19886497..532a9abd8d 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -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 appModeBeanPrefsIds; - private Map 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 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 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 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 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); diff --git a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java index 6d154d5cc4..16067b7c04 100644 --- a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java @@ -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; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 1c2c1a4b88..f20b51dfc2 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -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 drawerLogoNames = profileItem.getDrawerLogoParams(); - if (!Algorithms.isEmpty(drawerLogoNames)) { - String pluginResDir = IndexConstants.PLUGINS_DIR + plugin.getId() + "/" + plugin.getPluginResDir().getName(); - for (Map.Entry 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) {