diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 9ebaf7a32a..08caab5c50 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -111,6 +111,7 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -243,6 +244,7 @@ public class OsmandAidlApi { public void onDestroyMapActivity(MapActivity mapActivity) { app.getAppCustomization().removeListener(mapActivity); + aMapPointUpdateListener = null; mapActivityActive = false; for (BroadcastReceiver b : receivers.values()) { if(b == null) { @@ -1819,9 +1821,10 @@ public class OsmandAidlApi { return res; } - public boolean switchEnabled(@NonNull ConnectedApp app) { - app.enabled = !app.enabled; - return saveConnectedApps(); + public boolean switchEnabled(@NonNull ConnectedApp connectedApp) { + connectedApp.enabled = !connectedApp.enabled; + ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get(); + return saveConnectedApps(selectedAppMode, connectedApps); } boolean isAppEnabled(@NonNull String pack) { @@ -1829,12 +1832,22 @@ public class OsmandAidlApi { if (app == null) { app = new ConnectedApp(pack, true); connectedApps.put(pack, app); - saveConnectedApps(); + saveNewConnectedApp(app); } return app.enabled; } - private boolean saveConnectedApps() { + private void saveNewConnectedApp(ConnectedApp connectedApp) { + for (ApplicationMode mode : ApplicationMode.allPossibleValues()) { + Map connectedApps = loadConnectedAppsForMode(mode); + if (!connectedApps.containsKey(connectedApp.pack)) { + connectedApps.put(connectedApp.pack, connectedApp); + saveConnectedApps(mode, connectedApps); + } + } + } + + private boolean saveConnectedApps(ApplicationMode mode, Map connectedApps) { try { JSONArray array = new JSONArray(); for (ConnectedApp app : connectedApps.values()) { @@ -1843,7 +1856,7 @@ public class OsmandAidlApi { obj.put(ConnectedApp.PACK_KEY, app.pack); array.put(obj); } - return app.getSettings().API_CONNECTED_APPS_JSON.set(array.toString()); + return app.getSettings().API_CONNECTED_APPS_JSON.setModeValue(mode, array.toString()); } catch (JSONException e) { e.printStackTrace(); } @@ -1851,9 +1864,16 @@ public class OsmandAidlApi { } public void loadConnectedApps() { + ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get(); + Map appsForMode = loadConnectedAppsForMode(selectedAppMode); + connectedApps.clear(); + connectedApps.putAll(appsForMode); + } + + private Map loadConnectedAppsForMode(ApplicationMode mode) { + Map connectedApps = new HashMap<>(); try { - connectedApps.clear(); - JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get()); + JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.getModeValue(mode)); for (int i = 0; i < array.length(); i++) { JSONObject obj = array.getJSONObject(i); String pack = obj.optString(ConnectedApp.PACK_KEY, ""); @@ -1863,6 +1883,7 @@ public class OsmandAidlApi { } catch (JSONException e) { e.printStackTrace(); } + return connectedApps; } boolean setNavDrawerLogo(@Nullable String uri) { diff --git a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java index 8c8a26f542..824de9449c 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java @@ -15,6 +15,7 @@ import android.widget.ArrayAdapter; import net.osmand.IProgress; import net.osmand.IndexConstants; +import net.osmand.aidl.OsmandAidlApi; import net.osmand.aidl.customization.CustomizationInfoParams; import net.osmand.aidl.customization.OsmandSettingsParams; import net.osmand.aidl.customization.SetWidgetsParams; @@ -524,18 +525,21 @@ public class OsmAndAppCustomization { JSONObject allItems = new JSONObject(app.getSettings().API_NAV_DRAWER_ITEMS_JSON.get()); for (Iterator it = allItems.keys(); it.hasNext(); ) { String appPackage = (String) it.next(); - JSONArray jArray = allItems.getJSONArray(appPackage); - List list = new ArrayList<>(); - for (int i = 0; i < jArray.length(); i++) { - JSONObject obj = jArray.getJSONObject(i); - list.add(new NavDrawerItem( - obj.optString(NavDrawerItem.NAME_KEY), - obj.optString(NavDrawerItem.URI_KEY), - obj.optString(NavDrawerItem.ICON_NAME_KEY), - obj.optInt(NavDrawerItem.FLAGS_KEY, -1) - )); + OsmandAidlApi.ConnectedApp connectedApp = app.getAidlApi().getConnectedApp(appPackage); + if (connectedApp != null && connectedApp.isEnabled()) { + JSONArray jArray = allItems.getJSONArray(appPackage); + List list = new ArrayList<>(); + for (int i = 0; i < jArray.length(); i++) { + JSONObject obj = jArray.getJSONObject(i); + list.add(new NavDrawerItem( + obj.optString(NavDrawerItem.NAME_KEY), + obj.optString(NavDrawerItem.URI_KEY), + obj.optString(NavDrawerItem.ICON_NAME_KEY), + obj.optInt(NavDrawerItem.FLAGS_KEY, -1) + )); + } + res.put(appPackage, list); } - res.put(appPackage, list); } } catch (JSONException e) { e.printStackTrace(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 870a77280d..4019e16c60 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -922,7 +922,12 @@ public class OsmandSettings { public final CommonPreference SHOW_OSMAND_WELCOME_SCREEN = new BooleanPreference("show_osmand_welcome_screen", true).makeGlobal(); public final CommonPreference API_NAV_DRAWER_ITEMS_JSON = new StringPreference("api_nav_drawer_items_json", "{}").makeGlobal(); - public final CommonPreference API_CONNECTED_APPS_JSON = new StringPreference("api_connected_apps_json", "[]").makeProfile(); + public final CommonPreference API_CONNECTED_APPS_JSON = new StringPreference("api_connected_apps_json", "[]") { + @Override + public String getModeValue(ApplicationMode mode) { + return getValue(getProfilePreferences(mode), "[]"); + } + }.makeProfile(); public final CommonPreference NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal();